"""Offline jQuery Mobile Orientationchange event & style verifier (Chapter 26)."""
from __future__ import annotations
import json
from lxml import html
def angle_to_orientation(angle: int) -> str:
return "portrait" if angle in (0, 180) else "landscape"
def style_for_orientation(angle: int) -> dict[str, str]:
if angle == 0:
return {"background-color": "yellow", "font-size": "300%"}
return {"background-color": "pink", "font-size": "200%"}
def main() -> None:
cases = []
# Case 1: window_orientationchange_binding
transitions = [0, 90, 0, -90]
fired = []
prev = None
for ang in transitions:
ori = angle_to_orientation(ang)
if prev is not None and ori != prev:
fired.append((prev, ori))
prev = ori
actual1 = {"binding_target": "window", "change_count": len(fired), "transitions": fired}
expected1 = {
"binding_target": "window",
"change_count": 3,
"transitions": [("portrait", "landscape"), ("landscape", "portrait"), ("portrait", "landscape")],
}
cases.append({
"name": "window_orientationchange_binding",
"summary": "验证绑定在 window 上的 orientationchange 在纵横屏交替旋转时准确触发。",
"expected": expected1,
"actual": actual1,
"passed": actual1 == expected1,
})
# Case 2: event_orientation_values
actual2 = {str(a): angle_to_orientation(a) for a in (0, 90, -90, 180)}
expected2 = {"0": "portrait", "90": "landscape", "-90": "landscape", "180": "portrait"}
cases.append({
"name": "event_orientation_values",
"summary": "验证 event.orientation 将 0/180 度映射为 portrait、90/-90 度映射为 landscape。",
"expected": expected2,
"actual": actual2,
"passed": actual2 == expected2,
})
# Case 3: orientation_style_branching
doc = html.fragment_fromstring('<div><p id="target">旋转设备查看样式</p></div>', create_parent=False)
actual3 = {
"has_target_p": bool(doc.xpath(".//p[@id='target']")),
"angle_0": style_for_orientation(0),
"angle_90": style_for_orientation(90),
"angle_neg90": style_for_orientation(-90),
}
expected3 = {
"has_target_p": True,
"angle_0": {"background-color": "yellow", "font-size": "300%"},
"angle_90": {"background-color": "pink", "font-size": "200%"},
"angle_neg90": {"background-color": "pink", "font-size": "200%"},
}
cases.append({
"name": "orientation_style_branching",
"summary": "验证按 window.orientation == 0 与 90/-90 为段落分别切换纵向黄底与横向粉底样式。",
"expected": expected3,
"actual": actual3,
"passed": actual3 == expected3,
})
# Case 4: modern_screen_orientation_api
modern_map = {
0: "portrait-primary",
90: "landscape-primary",
-90: "landscape-secondary",
180: "portrait-secondary",
}
actual4 = {
"window_orientation_deprecated": True,
"screen_orientation_types": modern_map,
"css_media_queries": ["@media (orientation: portrait)", "@media (orientation: landscape)"],
}
expected4 = {
"window_orientation_deprecated": True,
"screen_orientation_types": {
0: "portrait-primary",
90: "landscape-primary",
-90: "landscape-secondary",
180: "portrait-secondary",
},
"css_media_queries": ["@media (orientation: portrait)", "@media (orientation: landscape)"],
}
cases.append({
"name": "modern_screen_orientation_api",
"summary": "核对已废弃 window.orientation 到 W3C Screen Orientation API 与 CSS orientation 媒体查询的迁移对照。",
"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-orientation-lab/explore-events-orientation.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-events-orientation.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2054.zip
课程关联工程包(登记关联32章) · 含当前章节或实例 · mbskycom202612a2050.zip