"""Offline jQuery Mobile Page Events lifecycle state-machine verifier (Chapter 31)."""
from __future__ import annotations
import json
from lxml import html
def run_init_lifecycle(page_id: str) -> list[tuple[str, str]]:
return [
("pagebeforecreate", page_id),
("pagecreate", page_id),
("pageinit", page_id),
]
def run_external_load(url: str, status_code: int) -> list[tuple[str, str]]:
seq = [("pagebeforeload", url)]
if status_code == 200:
seq.append(("pageload", url))
else:
seq.append(("pageloadfailed", url))
return seq
def run_transition(from_page: str, to_page: str) -> list[tuple[str, str]]:
return [
("pagebeforehide", from_page),
("pagebeforeshow", to_page),
("pagehide", from_page),
("pageshow", to_page),
]
def main() -> None:
cases = []
# Case 1: page_initialization_lifecycle
doc = html.fragment_fromstring(
'<div><div data-role="page" id="pageone"></div><div data-role="page" id="pagetwo"></div></div>',
create_parent=False,
)
actual1 = {
"page_ids": doc.xpath("./div[@data-role='page']/@id"),
"init_order": [e for e, _ in run_init_lifecycle("pageone")],
}
expected1 = {
"page_ids": ["pageone", "pagetwo"],
"init_order": ["pagebeforecreate", "pagecreate", "pageinit"],
}
cases.append({
"name": "page_initialization_lifecycle",
"summary": "验证页面初始化三阶段事件顺序:pagebeforecreate -> pagecreate -> pageinit。",
"expected": expected1,
"actual": actual1,
"passed": actual1 == expected1,
})
# Case 2: external_page_load_lifecycle
actual2 = {
"ok_load": run_external_load("/pages/detail.html", 200),
"failed_load": run_external_load("/pages/missing.html", 404),
}
expected2 = {
"ok_load": [("pagebeforeload", "/pages/detail.html"), ("pageload", "/pages/detail.html")],
"failed_load": [("pagebeforeload", "/pages/missing.html"), ("pageloadfailed", "/pages/missing.html")],
}
cases.append({
"name": "external_page_load_lifecycle",
"summary": "验证外部页面加载成功与失败两条分支:pagebeforeload -> pageload / pageloadfailed。",
"expected": expected2,
"actual": actual2,
"passed": actual2 == expected2,
})
# Case 3: page_transition_lifecycle
actual3 = {"transition_events": run_transition("#pageone", "#pagetwo")}
expected3 = {
"transition_events": [
("pagebeforehide", "#pageone"),
("pagebeforeshow", "#pagetwo"),
("pagehide", "#pageone"),
("pageshow", "#pagetwo"),
],
}
cases.append({
"name": "page_transition_lifecycle",
"summary": "验证从 #pageone 过渡到 #pagetwo 时离开页 hide 与到达页 show 的先后顺序。",
"expected": expected3,
"actual": actual3,
"passed": actual3 == expected3,
})
# Case 4: pagecontainer_14_and_spa_migration
migration = {
"pageinit": "pagecreate",
"pageload": "pagecontainerload",
"pageloadfailed": "pagecontainerloadfailed",
"pagebeforeshow": "pagecontainerbeforeshow",
"pageshow": "pagecontainershow",
"pagebeforehide": "pagecontainerbeforehide",
"pagehide": "pagecontainerhide",
}
actual4 = {"migrated_count": len(migration), "four_categories": ["Initialization", "Load/Unload", "Transition", "Change"]}
expected4 = {"migrated_count": 7, "four_categories": ["Initialization", "Load/Unload", "Transition", "Change"]}
cases.append({
"name": "pagecontainer_14_and_spa_migration",
"summary": "核对页面事件四大分类及 jQuery Mobile 1.4 pagecontainer* 事件迁移对照。",
"expected": expected4,
"actual": actual4,
"passed": actual4 == expected4,
})
print(json.dumps({
"version": "CPython 3.12.14 / lxml offline jQuery Mobile DOM & reference verifier",
"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/jquerymobile-events-page-lab/explore-events-page.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-events-page.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2055.zip
课程关联工程包(登记关联32章) · 含当前章节或实例 · mbskycom202612a2050.zip