"""Offline jQuery Mobile DOM & data-role verifier for Chapter 09: toolbars."""
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: Header toolbar button placement (two buttons, left-only, right-only via ui-btn-right)
doc1 = html.fragment_fromstring(
"""
<div>
<div data-role="header" id="h-both">
<a href="#" class="ui-btn ui-icon-home ui-btn-icon-left">主页</a>
<h1>标题</h1>
<a href="#" class="ui-btn ui-icon-search ui-btn-icon-left">搜索</a>
</div>
<div data-role="header" id="h-left">
<a href="#" class="ui-btn ui-btn-left ui-icon-home ui-btn-icon-left">主页</a>
<h1>标题</h1>
</div>
<div data-role="header" id="h-right">
<h1>标题</h1>
<a href="#" class="ui-btn ui-btn-right ui-icon-search ui-btn-icon-left">搜索</a>
</div>
</div>
"""
)
actual_1 = {
"both_button_count": len(doc1.xpath("./div[@id='h-both']/a")),
"left_explicit_class": "ui-btn-left" in doc1.xpath("./div[@id='h-left']/a/@class")[0].split(),
"right_explicit_class": "ui-btn-right" in doc1.xpath("./div[@id='h-right']/a/@class")[0].split(),
"header_max_buttons": 2,
}
expected_1 = {
"both_button_count": 2,
"left_explicit_class": True,
"right_explicit_class": True,
"header_max_buttons": 2,
}
cases.append({
"name": "header_toolbar_buttons",
"summary": "Verify header toolbar left/right button limits and ui-btn-left / ui-btn-right placement classes.",
"expected": expected_1,
"actual": actual_1,
"passed": actual_1 == expected_1,
})
# Case 2: Footer toolbar multiple buttons, text-align center, and horizontal controlgroup
doc2 = html.fragment_fromstring(
"""
<div>
<div data-role="footer" id="f-plain">
<a href="#" class="ui-btn ui-icon-plus ui-btn-icon-left">关注渠道一</a>
<a href="#" class="ui-btn ui-icon-plus ui-btn-icon-left">关注渠道二</a>
<a href="#" class="ui-btn ui-icon-plus ui-btn-icon-left">关注渠道三</a>
</div>
<div data-role="footer" id="f-grouped" style="text-align:center;">
<div data-role="controlgroup" data-type="horizontal">
<a href="#" class="ui-btn ui-icon-plus ui-btn-icon-left">操作一</a>
<a href="#" class="ui-btn ui-icon-plus ui-btn-icon-left">操作二</a>
<a href="#" class="ui-btn ui-icon-plus ui-btn-icon-left">操作三</a>
</div>
</div>
</div>
"""
)
actual_2 = {
"plain_footer_buttons": len(doc2.xpath("./div[@id='f-plain']/a")),
"grouped_centered": "text-align:center" in doc2.xpath("./div[@id='f-grouped']/@style")[0],
"controlgroup_type": doc2.xpath("./div[@id='f-grouped']/div[@data-role='controlgroup']/@data-type")[0],
"grouped_buttons": len(doc2.xpath("./div[@id='f-grouped']//a")),
}
expected_2 = {
"plain_footer_buttons": 3,
"grouped_centered": True,
"controlgroup_type": "horizontal",
"grouped_buttons": 3,
}
cases.append({
"name": "footer_toolbar_and_controlgroup",
"summary": "Verify footer unlimited buttons, centered styling, and horizontal controlgroup layout.",
"expected": expected_2,
"actual": actual_2,
"passed": actual_2 == expected_2,
})
# Case 3: Toolbar positioning modes (inline, fixed, fixed + fullscreen)
doc3 = html.fragment_fromstring(
"""
<div>
<div data-role="header" data-position="inline"></div>
<div data-role="footer" data-position="inline"></div>
<div data-role="header" data-position="fixed"></div>
<div data-role="footer" data-position="fixed"></div>
<div data-role="header" data-position="fixed" data-fullscreen="true"></div>
<div data-role="footer" data-position="fixed" data-fullscreen="true"></div>
</div>
"""
)
bars = doc3.xpath("./div")
modes = [
(b.get("data-role"), b.get("data-position"), b.get("data-fullscreen") == "true")
for b in bars
]
actual_3 = {
"modes": modes,
"fullscreen_requires_fixed": all(
b.get("data-position") == "fixed" for b in bars if b.get("data-fullscreen") == "true"
),
}
expected_3 = {
"modes": [
("header", "inline", False),
("footer", "inline", False),
("header", "fixed", False),
("footer", "fixed", False),
("header", "fixed", True),
("footer", "fixed", True),
],
"fullscreen_requires_fixed": True,
}
cases.append({
"name": "toolbar_position_modes",
"summary": "Verify inline, fixed, and fixed+fullscreen data-position and data-fullscreen attributes on toolbars.",
"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-toolbars-lab/explore-toolbars.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-toolbars.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联32章) · 含当前章节或实例 · mbskycom202612a2050.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2080.zip