"""Offline jQuery Mobile DOM & data-role verifier for Chapter 03: install."""
import json
from lxml import html
VERSION = "CPython 3.12.14 / lxml offline jQuery Mobile DOM & data-role verifier"
def inspect_head(head_markup: str) -> dict:
doc = html.fromstring(f"<!DOCTYPE html><html>{head_markup}<body></body></html>")
vp = doc.xpath("//head/meta[@name='viewport']/@content")
css = doc.xpath("//head/link[@rel='stylesheet']/@href")
scripts = doc.xpath("//head/script")
srcs = [s.get("src", "") for s in scripts]
types = [s.get("type") for s in scripts]
jq_idx = next((i for i, u in enumerate(srcs) if "jquery" in u and "mobile" not in u), -1)
jqm_idx = next((i for i, u in enumerate(srcs) if "jquery.mobile" in u or "jquerymobile" in u), -1)
return {
"has_viewport": bool(vp and "width=device-width" in vp[0] and "initial-scale=1" in vp[0]),
"css_count": len(css),
"script_count": len(srcs),
"jquery_before_jqm": jq_idx != -1 and jqm_idx != -1 and jq_idx < jqm_idx,
"omits_legacy_script_type": all(t is None for t in types),
}
def run_cases():
cases = []
# Case 1: Official CDN head setup (jQuery 1.11.3 + jQuery Mobile 1.4.5)
official_head = """
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
"""
actual_1 = inspect_head(official_head)
expected_1 = {
"has_viewport": True,
"css_count": 1,
"script_count": 2,
"jquery_before_jqm": True,
"omits_legacy_script_type": True,
}
cases.append({
"name": "official_cdn_load_order",
"summary": "Verify viewport meta, stylesheet link, jQuery-before-jQuery-Mobile script order, and HTML5 script tag without type attribute.",
"expected": expected_1,
"actual": actual_1,
"passed": actual_1 == expected_1,
})
# Case 2: Domestic mirror CDN head setup (historical Baidu CDN pattern)
mirror_head = """
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
"""
mirror_info = inspect_head(mirror_head)
mirror_doc = html.fromstring(f"<!DOCTYPE html><html>{mirror_head}<body></body></html>")
http_urls = [u for u in mirror_doc.xpath("//link/@href | //script/@src") if u.startswith("http://")]
actual_2 = {
**mirror_info,
"legacy_http_url_count": len(http_urls),
}
expected_2 = {
"has_viewport": True,
"css_count": 1,
"script_count": 2,
"jquery_before_jqm": True,
"omits_legacy_script_type": True,
"legacy_http_url_count": 3,
}
cases.append({
"name": "mirror_cdn_and_http_audit",
"summary": "Verify mirror CDN dependency order and detect legacy cleartext http:// URLs requiring HTTPS migration.",
"expected": expected_2,
"actual": actual_2,
"passed": actual_2 == expected_2,
})
# Case 3: Local self-hosted relative asset setup
local_head = """
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="jquery.mobile-1.4.5.css">
<script src="jquery.js"></script>
<script src="jquery.mobile-1.4.5.js"></script>
</head>
"""
local_info = inspect_head(local_head)
local_doc = html.fromstring(f"<!DOCTYPE html><html>{local_head}<body></body></html>")
rel_paths = local_doc.xpath("//link/@href | //script/@src")
actual_3 = {
**local_info,
"all_relative_same_dir": all("://" not in p and "/" not in p for p in rel_paths),
"files": rel_paths,
}
expected_3 = {
"has_viewport": True,
"css_count": 1,
"script_count": 2,
"jquery_before_jqm": True,
"omits_legacy_script_type": True,
"all_relative_same_dir": True,
"files": ["jquery.mobile-1.4.5.css", "jquery.js", "jquery.mobile-1.4.5.js"],
}
cases.append({
"name": "local_self_hosted_assets",
"summary": "Verify local same-directory CSS and JS references with jQuery loaded before jQuery Mobile.",
"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-install-lab/explore-install.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-install.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联32章) · 含当前章节或实例 · mbskycom202612a2050.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2066.zip