"""Offline Ionic 1 Checkbox list DOM, color themes & multi-select state verifier for Ch10 (checkbox)."""
from __future__ import annotations
import json
from lxml import html
VERSION = "CPython 3.12.14 / lxml offline Ionic UI & component verifier"
THEMES = ["light", "stable", "positive", "calm", "balanced", "energized", "assertive", "royal", "dark"]
def make_case(name: str, summary: str, expected, actual) -> dict:
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_checkbox_list_dom() -> dict:
markup = """
<ul class="list">
<li class="item item-checkbox">
<label class="checkbox">
<input type="checkbox" checked="checked">
</label>
Flux Capacitor
</li>
<li class="item item-checkbox">
<label class="checkbox checkbox-assertive">
<input type="checkbox">
</label>
1.21 Gigawatts
</li>
</ul>
"""
root = html.fragment_fromstring(markup)
rows = []
for li in root.xpath("./li"):
lbl = li.xpath("./label")[0]
inp = lbl.xpath("./input")[0]
text = "".join(t.strip() for t in li.xpath("./text()") if t.strip())
rows.append({
"has_item_checkbox": "item-checkbox" in li.get("class", "").split(),
"label_classes": lbl.get("class", "").split(),
"input_type": inp.get("type"),
"checked": inp.get("checked") is not None,
"text": text,
})
actual = rows
expected = [
{
"has_item_checkbox": True,
"label_classes": ["checkbox"],
"input_type": "checkbox",
"checked": True,
"text": "Flux Capacitor",
},
{
"has_item_checkbox": True,
"label_classes": ["checkbox", "checkbox-assertive"],
"input_type": "checkbox",
"checked": False,
"text": "1.21 Gigawatts",
},
]
return make_case(
"checkbox_item_list_markup",
"Verify ul.list > li.item.item-checkbox > label.checkbox > input[type=checkbox] markup",
expected,
actual,
)
def case_checkbox_theme_and_multiselect() -> dict:
options = [
{"id": "flux", "label": "Flux Capacitor", "theme": "checkbox-positive", "checked": True},
{"id": "power", "label": "1.21 Gigawatts", "theme": "checkbox-assertive", "checked": False},
{"id": "speed", "label": "88 MPH", "theme": "checkbox-balanced", "checked": True},
]
# Toggle second item on and first item off
options[1]["checked"] = True
options[0]["checked"] = False
selected_ids = [o["id"] for o in options if o["checked"]]
valid_themes = all(o["theme"] in {f"checkbox-{t}" for t in THEMES} for o in options)
actual = {
"selected_ids": selected_ids,
"selected_count": len(selected_ids),
"all_themes_valid": valid_themes,
"theme_palette_size": len(THEMES),
}
expected = {
"selected_ids": ["power", "speed"],
"selected_count": 2,
"all_themes_valid": True,
"theme_palette_size": 9,
}
return make_case(
"checkbox_theme_classes_and_multiselect_state",
"Verify multi-select independence and checkbox-* theme class palette",
expected,
actual,
)
def case_modern_ion_checkbox_mapping() -> dict:
actual = {
"v1_wrapper": "li.item.item-checkbox > label.checkbox > input[type=checkbox]",
"v7_v8_component": "<ion-item><ion-checkbox color=\"danger\">...</ion-checkbox></ion-item>",
"supports_indeterminate_in_modern": True,
}
expected = {
"v1_wrapper": "li.item.item-checkbox > label.checkbox > input[type=checkbox]",
"v7_v8_component": "<ion-item><ion-checkbox color=\"danger\">...</ion-checkbox></ion-item>",
"supports_indeterminate_in_modern": True,
}
return make_case(
"checkbox_v1_to_modern_mapping",
"Verify Ionic 1 CSS checkbox to Ionic 7/8 <ion-checkbox> Web Component equivalence",
expected,
actual,
)
def main() -> None:
cases = [
case_checkbox_list_dom(),
case_checkbox_theme_and_multiselect(),
case_modern_ion_checkbox_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-checkbox-lab/explore-checkbox.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-checkbox.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a2013.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2016.zip