"""Offline jQuery Mobile DOM & data-role verifier for Chapter 12: collapsibles."""
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: Basic collapsible (default collapsed) vs data-collapsed="false" (expanded)
doc1 = html.fragment_fromstring(
"""
<div>
<div data-role="collapsible" id="c-default">
<h1>点击展开默认折叠块</h1>
<p>我是可折叠的内容。</p>
</div>
<div data-role="collapsible" id="c-open" data-collapsed="false">
<h1>默认展开的可折叠块</h1>
<p>页面加载时已默认展开。</p>
</div>
</div>
"""
)
c_def, c_open = doc1.xpath("./div[@data-role='collapsible']")
actual_1 = {
"default_collapsed": c_def.get("data-collapsed", "true") == "true",
"default_has_heading": bool(c_def.xpath("./h1|./h2|./h3|./h4|./h5|./h6")),
"open_collapsed": c_open.get("data-collapsed") == "false",
}
expected_1 = {
"default_collapsed": True,
"default_has_heading": True,
"open_collapsed": True,
}
cases.append({
"name": "collapsible_basic_and_expanded",
"summary": "Verify data-role='collapsible' heading child requirement, default collapsed state, and data-collapsed='false'.",
"expected": expected_1,
"actual": actual_1,
"passed": actual_1 == expected_1,
})
# Case 2: Nested collapsibles depth check
doc2 = html.fragment_fromstring(
"""
<div data-role="collapsible" id="outer">
<h1>外层折叠块</h1>
<p>外层说明段落。</p>
<div data-role="collapsible" id="inner">
<h2>内层嵌套折叠块</h2>
<p>内层展开内容。</p>
</div>
</div>
"""
)
inner = doc2.xpath(".//div[@data-role='collapsible']")[0]
actual_2 = {
"outer_id": doc2.get("id"),
"inner_id": inner.get("id"),
"nested_collapsible_count": len(doc2.xpath(".//div[@data-role='collapsible']")),
}
expected_2 = {
"outer_id": "outer",
"inner_id": "inner",
"nested_collapsible_count": 1,
}
cases.append({
"name": "collapsible_nesting",
"summary": "Verify nested data-role='collapsible' containers inside a parent collapsible block.",
"expected": expected_2,
"actual": actual_2,
"passed": actual_2 == expected_2,
})
# Case 3: Accordion collapsible-set + style/icon customization + native HTML5 <details> equivalent
doc3 = html.fragment_fromstring(
"""
<div>
<div data-role="collapsible-set" data-inset="false" data-mini="true"
data-collapsed-icon="arrow-d" data-expanded-icon="arrow-u" data-iconpos="right">
<div data-role="collapsible"><h1>手风琴第一项</h1><p>内容一</p></div>
<div data-role="collapsible"><h1>手风琴第二项</h1><p>内容二</p></div>
</div>
<details name="acc" open><summary>原生 HTML5 折叠项</summary><p>原生免脚本展开</p></details>
</div>
"""
)
cset = doc3.xpath("./div[@data-role='collapsible-set']")[0]
actual_3 = {
"child_collapsibles": len(cset.xpath("./div[@data-role='collapsible']")),
"inset": cset.get("data-inset"),
"mini": cset.get("data-mini"),
"icons": [cset.get("data-collapsed-icon"), cset.get("data-expanded-icon")],
"iconpos": cset.get("data-iconpos"),
"native_details_open": doc3.xpath("./details")[0].get("open") is not None,
}
expected_3 = {
"child_collapsibles": 2,
"inset": "false",
"mini": "true",
"icons": ["arrow-d", "arrow-u"],
"iconpos": "right",
"native_details_open": True,
}
cases.append({
"name": "collapsible_set_and_options",
"summary": "Verify data-role='collapsible-set' accordion grouping, inset/mini/icon customization, and HTML5 <details> counterpart.",
"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-collapsibles-lab/explore-collapsibles.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-collapsibles.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联32章) · 含当前章节或实例 · mbskycom202612a2050.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2052.zip