蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 Ionic 1 单选框分组 DOM、互斥选中状态机与 label 热区离线核验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-radio.py
explore-radio.py162 行
"""Offline Ionic 1 Radio group DOM, shared name grouping & exclusive selection verifier for Ch11 (radio)."""
from __future__ import annotations

import json
from lxml import html

VERSION = "CPython 3.12.14 / lxml offline Ionic UI & component verifier"

RADIO_HTML = """
<div class="list">
  <label class="item item-radio">
    <input type="radio" name="group" value="go" checked="checked">
    <div class="radio-content">
      <div class="item-content">Go</div>
      <i class="radio-icon ion-checkmark"></i>
    </div>
  </label>
  <label class="item item-radio">
    <input type="radio" name="group" value="python">
    <div class="radio-content">
      <div class="item-content">Python</div>
      <i class="radio-icon ion-checkmark"></i>
    </div>
  </label>
  <label class="item item-radio">
    <input type="radio" name="group" value="ruby">
    <div class="radio-content">
      <div class="item-content">Ruby</div>
      <i class="radio-icon ion-checkmark"></i>
    </div>
  </label>
  <label class="item item-radio">
    <input type="radio" name="group" value=".net">
    <div class="radio-content">
      <div class="item-content">.Net</div>
      <i class="radio-icon ion-checkmark"></i>
    </div>
  </label>
  <label class="item item-radio">
    <input type="radio" name="group" value="java">
    <div class="radio-content">
      <div class="item-content">Java</div>
      <i class="radio-icon ion-checkmark"></i>
    </div>
  </label>
  <label class="item item-radio">
    <input type="radio" name="group" value="php">
    <div class="radio-content">
      <div class="item-content">PHP</div>
      <i class="radio-icon ion-checkmark"></i>
    </div>
  </label>
</div>
"""


def make_case(name: str, summary: str, expected, actual) -> dict:
    return {
        "name": name,
        "summary": summary,
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_radio_group_dom() -> dict:
    root = html.fragment_fromstring(RADIO_HTML)
    labels = root.xpath("./label[contains(@class,'item-radio')]")
    names = {lbl.xpath("./input/@name")[0] for lbl in labels}
    values = [lbl.xpath("./input/@value")[0] for lbl in labels]
    texts = [lbl.xpath("./div[@class='radio-content']/div[@class='item-content']/text()")[0].strip() for lbl in labels]
    icons = {lbl.xpath(".//i/@class")[0] for lbl in labels}
    initial_checked = [lbl.xpath("./input/@value")[0] for lbl in labels if lbl.xpath("./input/@checked")]
    actual = {
        "option_count": len(labels),
        "shared_name_set": sorted(names),
        "values": values,
        "labels": texts,
        "icon_classes": sorted(icons),
        "initial_checked": initial_checked,
    }
    expected = {
        "option_count": 6,
        "shared_name_set": ["group"],
        "values": ["go", "python", "ruby", ".net", "java", "php"],
        "labels": ["Go", "Python", "Ruby", ".Net", "Java", "PHP"],
        "icon_classes": ["radio-icon ion-checkmark"],
        "initial_checked": ["go"],
    }
    return make_case(
        "radio_group_markup_and_shared_name",
        "Verify 6 label.item.item-radio rows share name='group', .radio-content/.item-content, and .radio-icon",
        expected,
        actual,
    )


def case_radio_mutual_exclusion_machine() -> dict:
    root = html.fragment_fromstring(RADIO_HTML)
    values = [lbl.xpath("./input/@value")[0] for lbl in root.xpath("./label")]
    selected = "go"

    def select(val: str) -> dict:
        nonlocal selected
        assert val in values
        selected = val
        return {v: (v == selected) for v in values}

    state_after_python = select("python")
    state_after_java = select("java")
    actual = {
        "checked_count_after_python": sum(state_after_python.values()),
        "python_visible_checkmark": state_after_python["python"] and not state_after_python["go"],
        "final_selected": selected,
        "checked_count_after_java": sum(state_after_java.values()),
    }
    expected = {
        "checked_count_after_python": 1,
        "python_visible_checkmark": True,
        "final_selected": "java",
        "checked_count_after_java": 1,
    }
    return make_case(
        "radio_mutual_exclusion_and_icon_visibility",
        "Verify mutual exclusion invariant (exactly one checked option and visible checkmark) on selection changes",
        expected,
        actual,
    )


def case_radio_label_ergonomics_and_modern_mapping() -> dict:
    root = html.fragment_fromstring(RADIO_HTML)
    all_wrapped_in_label = all(el.tag == "label" for el in root.xpath("./*"))
    actual = {
        "all_options_use_label_wrapper": all_wrapped_in_label,
        "modern_component": "<ion-radio-group value=\"go\"><ion-radio value=\"go\">Go</ion-radio></ion-radio-group>",
    }
    expected = {
        "all_options_use_label_wrapper": True,
        "modern_component": "<ion-radio-group value=\"go\"><ion-radio value=\"go\">Go</ion-radio></ion-radio-group>",
    }
    return make_case(
        "radio_label_touch_target_and_modern_mapping",
        "Verify <label> full-row touch target wrapper and Ionic 7/8 <ion-radio-group> equivalence",
        expected,
        actual,
    )


def main() -> None:
    cases = [
        case_radio_group_dom(),
        case_radio_mutual_exclusion_machine(),
        case_radio_label_ergonomics_and_modern_mapping(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

入口文件:projects/ionic-radio-lab/explore-radio.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-radio.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Ionic

课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a2013.zip

下载课程关联工程包:Ionic

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2043.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习