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

运行 Ionic 1 Toggle 独立结构、item-toggle 列表配色与布尔开关状态机离线核验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-toggle.py
explore-toggle.py142 行
"""Offline Ionic 1 Toggle switch DOM & checked state-machine verifier for Ch09 (toggle)."""
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_standalone_toggle_markup() -> dict:
    markup = """
    <label class="toggle">
      <input type="checkbox">
      <div class="track">
        <div class="handle"></div>
      </div>
    </label>
    """
    root = html.fragment_fromstring(markup)
    actual = {
        "wrapper_tag": root.tag,
        "wrapper_class": root.get("class"),
        "input_type": root.xpath("./input/@type")[0],
        "has_track_and_handle": bool(root.xpath("./div[@class='track']/div[@class='handle']")),
    }
    expected = {
        "wrapper_tag": "label",
        "wrapper_class": "toggle",
        "input_type": "checkbox",
        "has_track_and_handle": True,
    }
    return make_case(
        "toggle_standalone_dom_structure",
        "Verify standalone label.toggle > input[type=checkbox] + .track > .handle structure",
        expected,
        actual,
    )


def case_list_item_toggle_and_themes() -> dict:
    markup = """
    <ul class="list">
      <li class="item item-toggle">
        HTML5
        <label class="toggle toggle-assertive">
          <input type="checkbox" checked="checked">
          <div class="track"><div class="handle"></div></div>
        </label>
      </li>
      <li class="item item-toggle">
        CSS3
        <label class="toggle toggle-balanced">
          <input type="checkbox">
          <div class="track"><div class="handle"></div></div>
        </label>
      </li>
    </ul>
    """
    root = html.fragment_fromstring(markup)
    items = root.xpath("./li")
    parsed = []
    for li in items:
        lbl = li.xpath("./label")[0]
        inp = lbl.xpath("./input")[0]
        theme = [c for c in lbl.get("class", "").split() if c.startswith("toggle-")][0]
        parsed.append({
            "label": (li.text or "").strip(),
            "has_item_toggle": "item-toggle" in li.get("class", "").split(),
            "theme": theme,
            "checked": inp.get("checked") is not None,
        })
    actual = {
        "items": parsed,
        "supported_theme_classes": [f"toggle-{t}" for t in THEMES],
    }
    expected = {
        "items": [
            {"label": "HTML5", "has_item_toggle": True, "theme": "toggle-assertive", "checked": True},
            {"label": "CSS3", "has_item_toggle": True, "theme": "toggle-balanced", "checked": False},
        ],
        "supported_theme_classes": [f"toggle-{t}" for t in THEMES],
    }
    return make_case(
        "toggle_in_list_and_theme_classes",
        "Verify ul.list > li.item.item-toggle structure, checked state, and 9 toggle-* theme classes",
        expected,
        actual,
    )


def case_toggle_state_machine() -> dict:
    state = {"HTML5": True, "CSS3": False, "JavaScript": False}
    events = []

    def tap(key: str) -> None:
        state[key] = not state[key]
        events.append((key, state[key]))

    tap("CSS3")
    tap("HTML5")
    tap("JavaScript")
    actual = {
        "final_state": state,
        "transition_log": events,
        "enabled_count": sum(1 for v in state.values() if v),
    }
    expected = {
        "final_state": {"HTML5": False, "CSS3": True, "JavaScript": True},
        "transition_log": [("CSS3", True), ("HTML5", False), ("JavaScript", True)],
        "enabled_count": 2,
    }
    return make_case(
        "toggle_boolean_state_transitions",
        "Verify independent boolean toggle transitions across multiple switches",
        expected,
        actual,
    )


def main() -> None:
    cases = [
        case_standalone_toggle_markup(),
        case_list_item_toggle_and_themes(),
        case_toggle_state_machine(),
    ]
    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-toggle-lab/explore-toggle.py

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

下载课程关联工程包:Ionic

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

下载课程关联工程包:Ionic

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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