"""Offline jQuery Mobile DOM & data-role verifier for Chapter 01: tutorial."""
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: Basic single-page data-role skeleton (1.3 content vs 1.4 main)
doc_v13 = html.fragment_fromstring(
"""
<div data-role="page" id="pageone">
<div data-role="header"><h1>页面标题</h1></div>
<div data-role="content"><p>页面内容</p></div>
<div data-role="footer"><h1>页面底部内容</h1></div>
</div>
"""
)
doc_v14 = html.fragment_fromstring(
"""
<div data-role="page" id="pageone">
<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>
"""
)
actual_1 = {
"page_id": doc_v13.get("id"),
"v13_child_roles": [el.get("data-role") for el in doc_v13.xpath("./div[@data-role]")],
"v14_child_roles": [el.get("data-role") for el in doc_v14.xpath("./div[@data-role]")],
"v14_main_has_ui_content": "ui-content" in doc_v14.xpath("./div[@data-role='main']")[0].get("class", "").split(),
}
expected_1 = {
"page_id": "pageone",
"v13_child_roles": ["header", "content", "footer"],
"v14_child_roles": ["header", "main", "footer"],
"v14_main_has_ui_content": True,
}
cases.append({
"name": "basic_page_data_roles",
"summary": "Verify jQuery Mobile page container id and header/content(main)/footer data-role hierarchy.",
"expected": expected_1,
"actual": actual_1,
"passed": actual_1 == expected_1,
})
# Case 2: Semantic HTML5 modern equivalent mapping check
modern_doc = html.fromstring(
"""
<!DOCTYPE html>
<html lang="zh-CN">
<head><meta name="viewport" content="width=device-width, initial-scale=1"></head>
<body>
<header><h1>页面标题</h1></header>
<main><p>页面内容</p></main>
<footer><p>页面底部内容</p></footer>
</body>
</html>
"""
)
actual_2 = {
"has_viewport": bool(modern_doc.xpath("//meta[@name='viewport' and contains(@content, 'width=device-width')]")),
"landmarks": [el.tag for el in modern_doc.xpath("//body/*")],
"h1_text": modern_doc.xpath("string(//header/h1)"),
}
expected_2 = {
"has_viewport": True,
"landmarks": ["header", "main", "footer"],
"h1_text": "页面标题",
}
cases.append({
"name": "modern_html5_landmark_mapping",
"summary": "Verify modern native HTML5 header/main/footer landmarks and viewport meta equivalent.",
"expected": expected_2,
"actual": actual_2,
"passed": actual_2 == expected_2,
})
# Case 3: Data-role attribute inventory check across core widget families
sample_app = html.fragment_fromstring(
"""
<div data-role="page" id="home">
<div data-role="header"><h1>移动端概览</h1></div>
<div data-role="main" class="ui-content">
<ul data-role="listview"><li><a href="#detail">示例条目</a></li></ul>
</div>
<div data-role="footer"><h4>页脚导航</h4></div>
</div>
"""
)
roles = sample_app.xpath(".//@data-role")
actual_3 = {
"root_role": sample_app.get("data-role"),
"nested_roles": roles,
"link_targets": sample_app.xpath(".//a/@href"),
}
expected_3 = {
"root_role": "page",
"nested_roles": ["page", "header", "main", "listview", "footer"],
"link_targets": ["#detail"],
}
cases.append({
"name": "data_role_widget_inventory",
"summary": "Inspect nested data-role declarations and internal anchor links inside a jQM page.",
"expected": expected_3,
"actual": actual_3,
"passed": actual_3 == expected_3,
})
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-tutorial-lab/explore-tutorial.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联32章) · 含当前章节或实例 · mbskycom202612a2050.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2082.zip