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

运行单页区域、多页锚点、外部链接与对话框模式离线核验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-pages.py
explore-pages.py143 行
"""Offline jQuery Mobile DOM & data-role verifier for Chapter 04: pages."""
import json
from lxml import html

VERSION = "CPython 3.12.14 / lxml offline jQuery Mobile DOM & data-role verifier"


def run_cases():
    cases = []

    # Case 1: Single-page structure with header, main.ui-content, footer
    single_doc = html.fromstring(
        """
        <body>
          <div data-role="page">
            <div data-role="header"><h1>欢迎来到我的主页</h1></div>
            <div data-role="main" class="ui-content"><p>移动端页面正文</p></div>
            <div data-role="footer"><h1>底部文本</h1></div>
          </div>
        </body>
        """
    )
    page = single_doc.xpath("//div[@data-role='page']")[0]
    main = page.xpath("./div[@data-role='main']")[0]
    actual_1 = {
        "roles": [el.get("data-role") for el in page.xpath("./div")],
        "main_classes": main.get("class", "").split(),
        "data_attr_count": len(single_doc.xpath("//@*[starts-with(name(), 'data-')]")),
    }
    expected_1 = {
        "roles": ["header", "main", "footer"],
        "main_classes": ["ui-content"],
        "data_attr_count": 4,
    }
    cases.append({
        "name": "single_page_regions",
        "summary": "Verify data-role page, header, main (with ui-content class), and footer container structure.",
        "expected": expected_1,
        "actual": actual_1,
        "passed": actual_1 == expected_1,
    })

    # Case 2: Multi-page document with internal #id links
    multi_doc = html.fromstring(
        """
        <body>
          <div data-role="page" id="pageone">
            <div data-role="main" class="ui-content"><a href="#pagetwo">跳转到第二个页面</a></div>
          </div>
          <div data-role="page" id="pagetwo">
            <div data-role="main" class="ui-content"><a href="#pageone">跳转到第一个页面</a></div>
          </div>
        </body>
        """
    )
    page_ids = multi_doc.xpath("//div[@data-role='page']/@id")
    links = multi_doc.xpath("//div[@data-role='page']//a/@href")
    resolved = [href[1:] in page_ids for href in links if href.startswith("#")]
    actual_2 = {
        "page_ids": page_ids,
        "unique_ids": len(page_ids) == len(set(page_ids)),
        "all_internal_links_resolve": all(resolved) and len(resolved) == 2,
    }
    expected_2 = {
        "page_ids": ["pageone", "pagetwo"],
        "unique_ids": True,
        "all_internal_links_resolve": True,
    }
    cases.append({
        "name": "multipage_internal_anchors",
        "summary": "Verify multi-page HTML document with unique page ids and bidirectional internal #id links.",
        "expected": expected_2,
        "actual": actual_2,
        "passed": actual_2 == expected_2,
    })

    # Case 3: External page link for heavy content separation
    ext_link = html.fragment_fromstring('<a href="externalfile.html">访问外部文件</a>')
    href = ext_link.get("href")
    actual_3 = {
        "href": href,
        "is_internal_hash": href.startswith("#"),
        "is_external_html_file": href.endswith(".html") and not href.startswith("#"),
    }
    expected_3 = {
        "href": "externalfile.html",
        "is_internal_hash": False,
        "is_external_html_file": True,
    }
    cases.append({
        "name": "external_file_navigation",
        "summary": "Verify external HTML file navigation link used when splitting large page assets.",
        "expected": expected_3,
        "actual": actual_3,
        "passed": actual_3 == expected_3,
    })

    # Case 4: Page as dialog (jQM 1.4 data-dialog="true" vs 1.3 data-rel="dialog")
    dialog_doc = html.fromstring(
        """
        <body>
          <div data-role="page" id="pageone">
            <div data-role="main" class="ui-content">
              <a href="#pagetwo" data-rel="dialog">旧版触发方式</a>
              <a href="#pagetwo">1.4 目标页声明方式</a>
            </div>
          </div>
          <div data-role="page" data-dialog="true" id="pagetwo">
            <div data-role="main" class="ui-content"><a href="#pageone">返回第一页</a></div>
          </div>
        </body>
        """
    )
    dialog_pages = dialog_doc.xpath("//div[@data-role='page' and @data-dialog='true']/@id")
    rel_dialog_links = dialog_doc.xpath("//a[@data-rel='dialog']/@href")
    actual_4 = {
        "jqm14_dialog_pages": dialog_pages,
        "jqm13_rel_dialog_links": rel_dialog_links,
        "modern_native_element": "<dialog>",
    }
    expected_4 = {
        "jqm14_dialog_pages": ["pagetwo"],
        "jqm13_rel_dialog_links": ["#pagetwo"],
        "modern_native_element": "<dialog>",
    }
    cases.append({
        "name": "page_as_dialog_modes",
        "summary": "Compare jQuery Mobile 1.4 data-dialog='true' on target page vs legacy data-rel='dialog' on trigger link.",
        "expected": expected_4,
        "actual": actual_4,
        "passed": actual_4 == expected_4,
    })

    return cases


def main():
    print(json.dumps({"version": VERSION, "cases": run_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/jquerymobile-pages-lab/explore-pages.py

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

下载课程关联工程包:jQuery Mobile

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

下载课程关联工程包:jQuery Mobile

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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