"""Offline jQuery Mobile DOM & data-role verifier for Chapter 11: panels."""
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: Panel placement inside page (sibling before/after header/main/footer) & trigger link
doc1 = html.fragment_fromstring(
"""
<div data-role="page" id="pageone">
<div data-role="panel" id="myPanel">
<h2>面板头部</h2>
<p>面板内容</p>
<a href="#pageone" data-rel="close" class="ui-btn ui-btn-inline">关闭面板</a>
</div>
<div data-role="header"><h1>标准页面头部</h1></div>
<div data-role="main" class="ui-content">
<p>点击下面按钮打开面板。</p>
<a href="#myPanel" class="ui-btn ui-btn-inline">打开面板</a>
</div>
<div data-role="footer"><h1>底部文本</h1></div>
</div>
"""
)
child_roles = [el.get("data-role") for el in doc1.xpath("./div[@data-role]")]
open_href = doc1.xpath("./div[@data-role='main']//a/@href")[0]
close_btn = doc1.xpath("./div[@data-role='panel']//a[@data-rel='close']")[0]
actual_1 = {
"child_roles": child_roles,
"panel_before_header": child_roles.index("panel") == 0,
"open_matches_panel_id": open_href == "#" + doc1.xpath("./div[@data-role='panel']/@id")[0],
"close_href_points_to_page": close_btn.get("href") == "#" + doc1.get("id"),
}
expected_1 = {
"child_roles": ["panel", "header", "main", "footer"],
"panel_before_header": True,
"open_matches_panel_id": True,
"close_href_points_to_page": True,
}
cases.append({
"name": "panel_page_structure_and_close_btn",
"summary": "Verify data-role='panel' sibling placement inside page, open anchor #id match, and data-rel='close' return href.",
"expected": expected_1,
"actual": actual_1,
"passed": actual_1 == expected_1,
})
# Case 2: Dismiss controls (data-dismissible & data-swipe-close)
doc2 = html.fragment_fromstring(
"""
<div>
<div data-role="panel" id="p-default"></div>
<div data-role="panel" id="p-nodismiss" data-dismissible="false"></div>
<div data-role="panel" id="p-noswipe" data-swipe-close="false"></div>
</div>
"""
)
p_def, p_nodis, p_noswp = doc2.xpath("./div[@data-role='panel']")
actual_2 = {
"default_dismissible": p_def.get("data-dismissible", "true"),
"default_swipe_close": p_def.get("data-swipe-close", "true"),
"disabled_dismissible": p_nodis.get("data-dismissible"),
"disabled_swipe_close": p_noswp.get("data-swipe-close"),
}
expected_2 = {
"default_dismissible": "true",
"default_swipe_close": "true",
"disabled_dismissible": "false",
"disabled_swipe_close": "false",
}
cases.append({
"name": "panel_dismiss_and_swipe_controls",
"summary": "Verify data-dismissible and data-swipe-close defaults ('true') and explicit 'false' overrides.",
"expected": expected_2,
"actual": actual_2,
"passed": actual_2 == expected_2,
})
# Case 3: Display modes (overlay / reveal / push) and positioning (right & position-fixed)
doc3 = html.fragment_fromstring(
"""
<div>
<div data-role="panel" id="overlayPanel" data-display="overlay"></div>
<div data-role="panel" id="revealPanel" data-display="reveal"></div>
<div data-role="panel" id="pushPanel" data-display="push"></div>
<div data-role="panel" id="rightPanel" data-position="right"></div>
<div data-role="panel" id="fixedPanel" data-position-fixed="true"></div>
</div>
"""
)
actual_3 = {
"display_modes": doc3.xpath("./div[@data-display]/@data-display"),
"right_position": doc3.xpath("./div[@id='rightPanel']/@data-position")[0],
"position_fixed": doc3.xpath("./div[@id='fixedPanel']/@data-position-fixed")[0] == "true",
"default_display": "reveal",
"default_position": "left",
}
expected_3 = {
"display_modes": ["overlay", "reveal", "push"],
"right_position": "right",
"position_fixed": True,
"default_display": "reveal",
"default_position": "left",
}
cases.append({
"name": "panel_display_and_position_options",
"summary": "Verify data-display (overlay/reveal/push), data-position='right', and data-position-fixed='true'.",
"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-panels-lab/explore-panels.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-panels.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联32章) · 含当前章节或实例 · mbskycom202612a2050.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2072.zip