"""Offline jQuery Mobile Themes swatch & CSS inheritance verifier (Chapter 22)."""
from __future__ import annotations
import json
import re
from lxml import html
def resolve_effective_theme(el, default: str = "a") -> str:
cur = el
while cur is not None:
theme = cur.get("data-theme")
if theme:
return theme
cur = cur.getparent()
return default
def main() -> None:
cases = []
# Case 1: theme_swatches_a_and_b
swatches = {
"a": {"page_bg": "gray", "text": "black", "btn_bg": "gray", "active_bg": "blue", "active_text": "white"},
"b": {"page_bg": "black", "text": "white", "btn_bg": "charcoal", "active_bg": "blue", "active_text": "white"},
}
btns = html.fragment_fromstring(
'<div><a href="#" class="ui-btn ui-btn-a">浅色</a><a href="#" class="ui-btn ui-btn-b">深色</a></div>',
create_parent=False,
)
actual1 = {
"builtin_swatches": sorted(swatches.keys()),
"btn_classes": btns.xpath("./a/@class"),
"active_consistent": swatches["a"]["active_bg"] == swatches["b"]["active_bg"] == "blue",
}
expected1 = {
"builtin_swatches": ["a", "b"],
"btn_classes": ["ui-btn ui-btn-a", "ui-btn ui-btn-b"],
"active_consistent": True,
}
cases.append({
"name": "theme_swatches_a_and_b",
"summary": "核对内置主题样本 a(灰底黑字)与 b(黑底白字)及 ui-btn-a/ui-btn-b 按钮类配置。",
"expected": expected1,
"actual": actual1,
"passed": actual1 == expected1,
})
# Case 2: header_footer_dialog_popup_panel_themes
markup2 = """
<div data-role="page" data-dialog="true" id="pagetwo">
<div data-role="header" data-theme="b">
<a href="#" class="ui-btn ui-btn-b">主页</a>
<a href="#" class="ui-btn ui-btn-b ui-icon-search ui-btn-icon-notext">Search</a>
</div>
<div data-role="panel" id="myPanel" data-theme="b"></div>
<div data-role="popup" id="myPopup" data-theme="b"></div>
<div data-role="footer" data-theme="b"></div>
</div>
"""
doc2 = html.fragment_fromstring(markup2, create_parent=False)
actual2 = {
"dialog_flag": doc2.get("data-dialog"),
"themed_b_roles": doc2.xpath(".//*[@data-theme='b']/@data-role"),
"icon_only_btn": "ui-btn-icon-notext" in doc2.xpath("string(.//a[2]/@class)"),
}
expected2 = {
"dialog_flag": "true",
"themed_b_roles": ["header", "panel", "popup", "footer"],
"icon_only_btn": True,
}
cases.append({
"name": "header_footer_dialog_popup_panel_themes",
"summary": "验证头部栏、尾部栏、对话框、面板、弹窗与图标按钮的主题 b 标记解析。",
"expected": expected2,
"actual": actual2,
"passed": actual2 == expected2,
})
# Case 3: navbar_and_listview_theme_inheritance
markup3 = """
<div data-role="page">
<div data-role="footer" data-theme="b">
<div data-role="navbar">
<ul>
<li><a id="nb1" href="#" data-icon="home" data-theme="a">Button 1</a></li>
<li><a id="nb2" href="#" data-icon="arrow-r">Button 2</a></li>
</ul>
</div>
</div>
<div data-role="collapsible" data-theme="b" data-content-theme="b">
<ul data-role="listview" data-theme="b" data-split-theme="b">
<li id="li1"><a href="#">Item 1</a></li>
<li id="li2" data-theme="a"><a href="#">Item 2</a></li>
</ul>
</div>
</div>
"""
doc3 = html.fragment_fromstring(markup3, create_parent=False)
actual3 = {
"nb1_override": resolve_effective_theme(doc3.xpath(".//a[@id='nb1']")[0]),
"nb2_inherited": resolve_effective_theme(doc3.xpath(".//a[@id='nb2']")[0]),
"li1_inherited": resolve_effective_theme(doc3.xpath(".//li[@id='li1']")[0]),
"li2_override": resolve_effective_theme(doc3.xpath(".//li[@id='li2']")[0]),
"split_theme": doc3.xpath("string(.//ul[@data-role='listview']/@data-split-theme)"),
"content_theme": doc3.xpath("string(.//div[@data-role='collapsible']/@data-content-theme)"),
}
expected3 = {
"nb1_override": "a",
"nb2_inherited": "b",
"li1_inherited": "b",
"li2_override": "a",
"split_theme": "b",
"content_theme": "b",
}
cases.append({
"name": "navbar_and_listview_theme_inheritance",
"summary": "验证导航栏、可折叠块、列表视图与分割按钮的父级主题继承及子项局部覆盖规则。",
"expected": expected3,
"actual": actual3,
"passed": actual3 == expected3,
})
# Case 4: themed_forms_and_collapsible_fieldset
markup4 = """
<form method="post" action="demo.php">
<fieldset data-role="collapsible" data-theme="b" data-content-theme="b">
<legend>Click me - I'm collapsible!</legend>
<input type="text" id="name" data-theme="b">
<select id="colors" data-theme="b"><option value="red">Red</option></select>
</fieldset>
</form>
"""
doc4 = html.fragment_fromstring(markup4, create_parent=False)
actual4 = {
"fieldset_role": doc4.xpath("string(.//fieldset/@data-role)"),
"input_theme": doc4.xpath("string(.//input/@data-theme)"),
"select_theme": doc4.xpath("string(.//select/@data-theme)"),
"fieldset_content_theme": doc4.xpath("string(.//fieldset/@data-content-theme)"),
}
expected4 = {
"fieldset_role": "collapsible",
"input_theme": "b",
"select_theme": "b",
"fieldset_content_theme": "b",
}
cases.append({
"name": "themed_forms_and_collapsible_fieldset",
"summary": "验证表单输入框、选择菜单与可折叠表单 fieldset 的主题属性配置。",
"expected": expected4,
"actual": actual4,
"passed": actual4 == expected4,
})
# Case 5: custom_swatch_css_rules
css_snippet = """
.ui-bar-f { color: red; background-color: yellow; }
.ui-body-f { font-weight: bold; color: white; background-color: purple; }
.ui-page-theme-f { font-weight: bold; background-color: green; }
"""
selectors = re.findall(r"\.(ui-(?:bar|body|page-theme)-[a-z])\b", css_snippet)
actual5 = {
"custom_swatch_selectors": selectors,
"valid_custom_letter_range": all(s[-1] in "cdefghijklmnopqrstuvwxyz" for s in selectors),
"v14_inheritance_mechanism": "pure_css",
}
expected5 = {
"custom_swatch_selectors": ["ui-bar-f", "ui-body-f", "ui-page-theme-f"],
"valid_custom_letter_range": True,
"v14_inheritance_mechanism": "pure_css",
}
cases.append({
"name": "custom_swatch_css_rules",
"summary": "验证自定义主题 f 的 ui-bar-f/ui-body-f/ui-page-theme-f 选择器及 1.4 纯 CSS 继承机制。",
"expected": expected5,
"actual": actual5,
"passed": actual5 == expected5,
})
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-themes-lab/explore-themes.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-themes.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联32章) · 含当前章节或实例 · mbskycom202612a2050.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2079.zip