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

运行 Ionic 1 混合架构清单、前置技能 DOM、Sass 主题变量与 Ionic 7/8 迁移离线核验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-tutorial.py
explore-tutorial.py204 行
"""Offline Ionic 1 architecture, DOM template & Ionic 7/8 migration verifier for Ch01 (tutorial)."""
from __future__ import annotations

import json
import re
from lxml import html

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


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


def case_hybrid_architecture() -> dict:
    stack = {
        "web_core": ["HTML5", "CSS3/Sass", "JavaScript"],
        "mvc_framework": "AngularJS 1.x",
        "native_bridge": "Apache Cordova",
        "min_ios": 6,
        "min_android": "4.1",
        "highlights": [
            "angular_syntax",
            "lightweight_ui",
            "mvc_architecture",
            "sass_components",
            "native_like_ux",
            "cli_tooling",
            "high_performance",
        ],
    }
    actual = {
        "layer_count": len(stack["web_core"]),
        "mvc": stack["mvc_framework"],
        "bridge": stack["native_bridge"],
        "dropped_below": {"ios": stack["min_ios"], "android": stack["min_android"]},
        "highlight_count": len(stack["highlights"]),
    }
    expected = {
        "layer_count": 3,
        "mvc": "AngularJS 1.x",
        "bridge": "Apache Cordova",
        "dropped_below": {"ios": 6, "android": "4.1"},
        "highlight_count": 7,
    }
    return make_case(
        "ionic_v1_hybrid_architecture_manifest",
        "Verify Ionic 1 hybrid stack layers, platform floor (iOS 6+ / Android 4.1+), and 7 core traits",
        expected,
        actual,
    )


def case_v1_to_modern_migration() -> dict:
    v1_markup = """
    <div ng-app="demoApp" ng-controller="HomeCtrl">
      <ion-header-bar class="bar-positive">
        <h1 class="title">{{pageTitle}}</h1>
      </ion-header-bar>
      <ion-content class="padding">
        <div class="card">
          <div class="item item-text-wrap">Hybrid UI</div>
        </div>
        <button class="button button-positive">Start</button>
      </ion-content>
    </div>
    """
    frag = html.fragment_fromstring(v1_markup, create_parent="section")
    v1_tags = [el.tag for el in frag.xpath(".//*[starts-with(name(), 'ion-')]")]
    bindings = re.findall(r"\{\{\s*([a-zA-Z0-9_.$]+)\s*\}\}", frag.text_content())

    migration_map = {
        "ion-header-bar": "ion-header > ion-toolbar",
        "ion-content": "ion-content",
        "button.button-positive": "ion-button[color='primary']",
        "div.card": "ion-card",
        "cordova": "Capacitor",
        "angularjs": "Angular / React / Vue + Web Components",
    }
    has_card = bool(frag.xpath(".//div[contains(concat(' ', @class, ' '), ' card ')]"))
    has_pos_btn = bool(frag.xpath(".//button[contains(@class, 'button-positive')]"))
    actual = {
        "v1_ion_tags": v1_tags,
        "angular_bindings": bindings,
        "has_card": has_card,
        "has_positive_button": has_pos_btn,
        "modern_targets": [
            migration_map["ion-header-bar"],
            migration_map["button.button-positive"],
            migration_map["div.card"],
            migration_map["cordova"],
        ],
    }
    expected = {
        "v1_ion_tags": ["ion-header-bar", "ion-content"],
        "angular_bindings": ["pageTitle"],
        "has_card": True,
        "has_positive_button": True,
        "modern_targets": [
            "ion-header > ion-toolbar",
            "ion-button[color='primary']",
            "ion-card",
            "Capacitor",
        ],
    }
    return make_case(
        "ionic_v1_to_v8_component_migration",
        "Parse Ionic 1 directives and map to modern Ionic 7/8 Web Components + Capacitor",
        expected,
        actual,
    )


def case_prerequisite_web_stack() -> dict:
    sample_doc = """
    <!DOCTYPE html>
    <html lang="zh-CN">
      <head><meta charset="utf-8"><title>Ionic Prerequisites</title></head>
      <body ng-app="starter" ng-controller="MainCtrl">
        <ul class="list">
          <li class="item" data-prereq="HTML">HTML</li>
          <li class="item" data-prereq="CSS">CSS</li>
          <li class="item" data-prereq="JavaScript">JavaScript</li>
          <li class="item" data-prereq="Angular">Angular</li>
        </ul>
      </body>
    </html>
    """
    doc = html.document_fromstring(sample_doc)
    body = doc.xpath("//body")[0]
    prereqs = [li.get("data-prereq") for li in doc.xpath("//ul[@class='list']/li[@class='item']")]
    actual = {
        "ng_app": body.get("ng-app"),
        "ng_controller": body.get("ng-controller"),
        "prerequisites": prereqs,
    }
    expected = {
        "ng_app": "starter",
        "ng_controller": "MainCtrl",
        "prerequisites": ["HTML", "CSS", "JavaScript", "Angular"],
    }
    return make_case(
        "ionic_v1_prerequisite_web_stack",
        "Verify AngularJS bootstrap attributes and 4 prerequisite web skills in DOM",
        expected,
        actual,
    )


def case_sass_theme_tokens() -> dict:
    sass_vars = {
        "$light": "#ffffff",
        "$stable": "#f8f8f8",
        "$positive": "#387ef5",
        "$calm": "#11c1f3",
        "$balanced": "#33cd5f",
        "$energized": "#ffc900",
        "$assertive": "#ef473a",
        "$royal": "#886aea",
        "$dark": "#444444",
    }
    valid_hex = all(re.fullmatch(r"#[0-9a-f]{6}", val) for val in sass_vars.values())
    css_custom_props = {
        f"--ion-color-{k.lstrip('$')}": v for k, v in sass_vars.items()
    }
    actual = {
        "token_count": len(sass_vars),
        "all_hex_valid": valid_hex,
        "positive_hex": sass_vars["$positive"],
        "modern_primary_prop": "--ion-color-positive" in css_custom_props,
    }
    expected = {
        "token_count": 9,
        "all_hex_valid": True,
        "positive_hex": "#387ef5",
        "modern_primary_prop": True,
    }
    return make_case(
        "ionic_v1_sass_theme_tokens",
        "Verify Ionic 1 Sass 9-color palette variables and CSS custom property correspondence",
        expected,
        actual,
    )


def main() -> None:
    cases = [
        case_hybrid_architecture(),
        case_v1_to_modern_migration(),
        case_prerequisite_web_stack(),
        case_sass_theme_tokens(),
    ]
    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-tutorial-lab/explore-tutorial.py

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

下载课程关联工程包:Ionic

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

下载课程关联工程包:Ionic

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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