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

运行 ion-toggle 模板解析、批量开关切换、ng-change 与自定义状态离线实测

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-ion-toggle.py
explore-ion-toggle.py125 行
"""Offline Ionic 1 ion-toggle directive, toggle-class & model binding verifier."""
import json
from lxml import html


def main():
    cases = []

    # Case 1: Parse ion-toggle template with ng-repeat, ng-change, toggle-class, true/false values
    markup = """
    <ion-content>
      <div class="list">
        <div class="item item-divider">Settings</div>
        <ion-toggle ng-repeat="item in settingsList" ng-model="item.checked" ng-checked="item.checked">
          {{ item.text }}
        </ion-toggle>
        <div class="item"><div ng-bind="settingsList | json"></div></div>
        <div class="item item-divider">Notifications</div>
        <ion-toggle ng-model="pushNotification.checked" ng-change="pushNotificationChange()">
          Push Notifications
        </ion-toggle>
        <ion-toggle toggle-class="toggle-assertive" ng-model="emailNotification"
                    ng-true-value="'Subscribed'" ng-false-value="'Unsubscribed'">
          Newsletter
        </ion-toggle>
      </div>
    </ion-content>
    """
    doc = html.fromstring(markup)
    toggles = doc.xpath("//ion-toggle")
    actual_dom = {
        "toggle_count": len(toggles),
        "repeat_expr": toggles[0].get("ng-repeat"),
        "change_expr": toggles[1].get("ng-change"),
        "toggle_class": toggles[2].get("toggle-class"),
        "true_val": toggles[2].get("ng-true-value"),
        "false_val": toggles[2].get("ng-false-value"),
    }
    expected_dom = {
        "toggle_count": 3,
        "repeat_expr": "item in settingsList",
        "change_expr": "pushNotificationChange()",
        "toggle_class": "toggle-assertive",
        "true_val": "'Subscribed'",
        "false_val": "'Unsubscribed'",
    }
    cases.append({
        "name": "parse_ion_toggle_template",
        "summary": "解析 ion-toggle 的 ng-repeat、ng-change、toggle-class 与自定义真假值",
        "expected": expected_dom,
        "actual": actual_dom,
        "passed": actual_dom == expected_dom,
    })

    # Case 2: Settings list switching & JSON serialization
    settings = [
        {"text": "Wireless", "checked": True},
        {"text": "GPS", "checked": False},
        {"text": "Bluetooth", "checked": False},
    ]
    settings[1]["checked"] = True
    settings[2]["checked"] = True
    settings[0]["checked"] = False
    serialized = json.dumps(settings, ensure_ascii=False)
    actual_set = {
        "enabled": [s["text"] for s in settings if s["checked"]],
        "json_roundtrip_len": len(json.loads(serialized)),
    }
    expected_set = {"enabled": ["GPS", "Bluetooth"], "json_roundtrip_len": 3}
    cases.append({
        "name": "toggle_settings_model_sync",
        "summary": "验证 Wireless/GPS/Bluetooth 开关切换与 JSON 序列化回显",
        "expected": expected_set,
        "actual": actual_set,
        "passed": actual_set == expected_set,
    })

    # Case 3: Push notification ng-change & Newsletter custom string values
    log = []
    push = {"checked": True}
    push["checked"] = False
    log.append(push["checked"])
    email_off = "Unsubscribed" if not False else "Subscribed"
    email_on = "Subscribed" if True else "Unsubscribed"
    actual_cb = {"push_log": log, "email_transition": [email_on, email_off]}
    expected_cb = {"push_log": [False], "email_transition": ["Subscribed", "Unsubscribed"]}
    cases.append({
        "name": "toggle_change_and_custom_state",
        "summary": "验证通知开关 ng-change 与 toggle-assertive 自定义字符串状态映射",
        "expected": expected_cb,
        "actual": actual_cb,
        "passed": actual_cb == expected_cb,
    })

    # Case 4: Modern Ionic 7/8 <ion-toggle color="danger">
    modern = html.fromstring('<ion-toggle color="danger" checked="true">Newsletter</ion-toggle>')
    actual_modern = {
        "tag": modern.tag,
        "color": modern.get("color"),
        "checked": modern.get("checked") == "true",
        "label": modern.text_content().strip(),
    }
    expected_modern = {
        "tag": "ion-toggle",
        "color": "danger",
        "checked": True,
        "label": "Newsletter",
    }
    cases.append({
        "name": "modern_ion_toggle_wc",
        "summary": "核对现代 Ionic 7/8 <ion-toggle color='danger'> Web Component 写法",
        "expected": expected_modern,
        "actual": actual_modern,
        "passed": actual_modern == expected_modern,
    })

    print(json.dumps({
        "version": "CPython 3.12.14 / lxml offline Ionic JS directive & service verifier",
        "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-ion-toggle-lab/explore-ion-toggle.py

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

下载课程关联工程包:Ionic

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

下载课程关联工程包:Ionic

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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