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

运行 Ionic 1 Select 四组选项 DOM、默认选中值与三端弹出机制离线核验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-select.py
explore-select.py170 行
"""Offline Ionic 1 Select dropdown DOM, option selection & platform picker verifier for Ch13 (select)."""
from __future__ import annotations

import json
from lxml import html

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

SELECT_HTML = """
<div class="list">
  <div class="item item-input item-select">
    <div class="input-label">Lightsaber</div>
    <select>
      <option>Blue</option>
      <option selected="">Green</option>
      <option>Red</option>
    </select>
  </div>
  <div class="item item-input item-select">
    <div class="input-label">Fighter</div>
    <select>
      <option>ARC-170</option>
      <option>A-wing</option>
      <option>Delta-7</option>
      <option>Naboo N-1</option>
      <option>TIE</option>
      <option selected="">X-wing</option>
      <option>Y-wing</option>
    </select>
  </div>
  <div class="item item-input item-select">
    <div class="input-label">Droid</div>
    <select>
      <option>2-1B</option>
      <option>B1</option>
      <option>C-3PO</option>
      <option>IG-88</option>
      <option>IT-O</option>
      <option selected="">R2-D2</option>
    </select>
  </div>
  <div class="item item-input item-select">
    <div class="input-label">Planet</div>
    <select>
      <option>Alderaan</option>
      <option selected="">Dagobah</option>
      <option>Felucia</option>
      <option>Geonosis </option>
      <option>Hoth</option>
      <option>Kamino</option>
      <option>Mygeeto</option>
      <option>Naboo</option>
      <option>Tataouine</option>
      <option>Utapau</option>
      <option>Yavin</option>
    </select>
  </div>
</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_item_select_dom() -> dict:
    root = html.fragment_fromstring(SELECT_HTML)
    groups = []
    for item in root.xpath("./div[contains(@class,'item-select')]"):
        cls = set(item.get("class", "").split())
        label = item.xpath("./div[@class='input-label']/text()")[0].strip()
        options = [o.text_content().strip() for o in item.xpath("./select/option")]
        selected = [o.text_content().strip() for o in item.xpath("./select/option[@selected]")]
        groups.append({
            "has_required_classes": {"item", "item-input", "item-select"} <= cls,
            "label": label,
            "option_count": len(options),
            "default_selected": selected[0],
        })
    actual = groups
    expected = [
        {"has_required_classes": True, "label": "Lightsaber", "option_count": 3, "default_selected": "Green"},
        {"has_required_classes": True, "label": "Fighter", "option_count": 7, "default_selected": "X-wing"},
        {"has_required_classes": True, "label": "Droid", "option_count": 6, "default_selected": "R2-D2"},
        {"has_required_classes": True, "label": "Planet", "option_count": 11, "default_selected": "Dagobah"},
    ]
    return make_case(
        "select_list_dom_and_defaults",
        "Verify 4 item.item-input.item-select controls, option counts, and default selected values",
        expected,
        actual,
    )


def case_select_state_and_platform_pickers() -> dict:
    selected_state = {
        "Lightsaber": "Green",
        "Fighter": "X-wing",
        "Droid": "R2-D2",
        "Planet": "Dagobah",
    }
    selected_state["Lightsaber"] = "Blue"
    selected_state["Planet"] = "Naboo"
    platform_ui = {
        "desktop_pc": "native_dropdown_list",
        "android": "native_radio_dialog",
        "ios": "native_picker_wheel",
    }
    actual = {
        "updated_selection": selected_state,
        "platform_popup_delegation": platform_ui,
    }
    expected = {
        "updated_selection": {
            "Lightsaber": "Blue",
            "Fighter": "X-wing",
            "Droid": "R2-D2",
            "Planet": "Naboo",
        },
        "platform_popup_delegation": {
            "desktop_pc": "native_dropdown_list",
            "android": "native_radio_dialog",
            "ios": "native_picker_wheel",
        },
    }
    return make_case(
        "select_value_change_and_platform_picker_behavior",
        "Verify select value updates and native browser/OS popup delegation across PC, Android, and iOS",
        expected,
        actual,
    )


def case_modern_ion_select_mapping() -> dict:
    actual = {
        "v1_pattern": "div.item.item-input.item-select > div.input-label + select > option",
        "v7_v8_component": '<ion-select label="Lightsaber" interface="action-sheet"><ion-select-option value="green">Green</ion-select-option></ion-select>',
        "modern_interfaces": ["alert", "action-sheet", "popover", "modal"],
    }
    expected = {
        "v1_pattern": "div.item.item-input.item-select > div.input-label + select > option",
        "v7_v8_component": '<ion-select label="Lightsaber" interface="action-sheet"><ion-select-option value="green">Green</ion-select-option></ion-select>',
        "modern_interfaces": ["alert", "action-sheet", "popover", "modal"],
    }
    return make_case(
        "select_v1_to_modern_mapping",
        "Verify Ionic 1 native <select> styling vs Ionic 7/8 <ion-select> custom interfaces",
        expected,
        actual,
    )


def main() -> None:
    cases = [
        case_item_select_dom(),
        case_select_state_and_platform_pickers(),
        case_modern_ion_select_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-select-lab/explore-select.py

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

下载课程关联工程包:Ionic

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

下载课程关联工程包:Ionic

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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