"""Offline jQuery Mobile Form Inputs DOM & controlgroup verifier (Chapter 19)."""
from __future__ import annotations
import json
from lxml import html
def main() -> None:
cases = []
# Case 1: text_date_email_placeholder
markup1 = """
<form method="post" action="demo_form.php">
<div class="ui-field-contain">
<label for="fullname">全名:</label>
<input type="text" name="fullname" id="fullname">
<label for="bday">生日:</label>
<input type="date" name="bday" id="bday">
<label for="email">E-mail:</label>
<input type="email" name="email" id="email" placeholder="你的电子邮箱..">
</div>
</form>
"""
doc1 = html.fragment_fromstring(markup1, create_parent=False)
actual1 = {
"input_types": doc1.xpath(".//input/@type"),
"placeholder_email": doc1.xpath("string(.//input[@id='email']/@placeholder)"),
"labels_matched": doc1.xpath(".//label/@for") == doc1.xpath(".//input/@id"),
}
expected1 = {
"input_types": ["text", "date", "email"],
"placeholder_email": "你的电子邮箱..",
"labels_matched": True,
}
cases.append({
"name": "text_date_email_placeholder",
"summary": "验证 HTML5 text/date/email 输入类型、label 对应关系及 placeholder 属性。",
"expected": expected1,
"actual": actual1,
"passed": actual1 == expected1,
})
# Case 2: textarea_and_search_inputs
markup2 = """
<form method="post" action="demo_form.php">
<div class="ui-field-contain">
<label for="info">附加信息:</label>
<textarea name="addinfo" id="info"></textarea>
<label for="search">搜索:</label>
<input type="search" name="search" id="search">
</div>
</form>
"""
doc2 = html.fragment_fromstring(markup2, create_parent=False)
actual2 = {
"has_textarea": bool(doc2.xpath(".//textarea[@id='info'][@name='addinfo']")),
"search_type": doc2.xpath("string(.//input[@id='search']/@type)"),
"in_field_contain": len(doc2.xpath(".//div[@class='ui-field-contain']/*[self::textarea or self::input]")),
}
expected2 = {
"has_textarea": True,
"search_type": "search",
"in_field_contain": 2,
}
cases.append({
"name": "textarea_and_search_inputs",
"summary": "验证多行文本域 textarea 与 HTML5 type='search' 搜索框在字段容器内的结构。",
"expected": expected2,
"actual": actual2,
"passed": actual2 == expected2,
})
# Case 3: radio_and_checkbox_controlgroups
markup3 = """
<form method="post" action="demo_form.php">
<fieldset data-role="controlgroup" id="cg_radio">
<legend>Choose your gender:</legend>
<label for="male">Male</label>
<input type="radio" name="gender" id="male" value="male">
<label for="female">Female</label>
<input type="radio" name="gender" id="female" value="female">
</fieldset>
<fieldset data-role="controlgroup" id="cg_check">
<legend>Choose as many favorite colors as you'd like:</legend>
<label for="red">Red</label>
<input type="checkbox" name="favcolor" id="red" value="red">
<label for="green">Green</label>
<input type="checkbox" name="favcolor" id="green" value="green">
<label for="blue">Blue</label>
<input type="checkbox" name="favcolor" id="blue" value="blue">
</fieldset>
</form>
"""
doc3 = html.fragment_fromstring(markup3, create_parent=False)
radio_names = set(doc3.xpath(".//fieldset[@id='cg_radio']/input/@name"))
actual3 = {
"radio_shared_name": radio_names == {"gender"},
"radio_values": doc3.xpath(".//fieldset[@id='cg_radio']/input/@value"),
"checkbox_values": doc3.xpath(".//fieldset[@id='cg_check']/input/@value"),
"both_controlgroup": doc3.xpath(".//fieldset/@data-role") == ["controlgroup", "controlgroup"],
}
expected3 = {
"radio_shared_name": True,
"radio_values": ["male", "female"],
"checkbox_values": ["red", "green", "blue"],
"both_controlgroup": True,
}
cases.append({
"name": "radio_and_checkbox_controlgroups",
"summary": "验证 fieldset[data-role='controlgroup'] 下单选按钮同名互斥与复选框多选结构。",
"expected": expected3,
"actual": actual3,
"passed": actual3 == expected3,
})
# Case 4: horizontal_fieldcontain_checked
markup4 = """
<div class="ui-field-contain">
<fieldset data-role="controlgroup" data-type="horizontal">
<legend>请选择您的性别:</legend>
<input type="radio" name="g" id="r1" value="m" checked>
<input type="checkbox" name="c" id="c1" value="red" checked>
</fieldset>
</div>
"""
doc4 = html.fragment_fromstring(markup4, create_parent=False)
actual4 = {
"wrapped_in_field_contain": bool(doc4.xpath("self::div[@class='ui-field-contain']/fieldset[@data-role='controlgroup']")),
"orientation": doc4.xpath("string(.//fieldset/@data-type)"),
"preselected_ids": [el.get("id") for el in doc4.xpath(".//input") if "checked" in el.attrib],
}
expected4 = {
"wrapped_in_field_contain": True,
"orientation": "horizontal",
"preselected_ids": ["r1", "c1"],
}
cases.append({
"name": "horizontal_fieldcontain_checked",
"summary": "验证 data-type='horizontal' 水平组合、ui-field-contain 包裹与 checked 预选中状态。",
"expected": expected4,
"actual": actual4,
"passed": actual4 == expected4,
})
# Case 5: popup_login_form_markup
markup5 = """
<div>
<a href="#myPopup" data-rel="popup" class="ui-btn ui-btn-inline">Show Popup Form</a>
<div data-role="popup" id="myPopup" class="ui-content">
<form method="post" action="demoform.php">
<div>
<h3>登录信息</h3>
<label for="usrnm" class="ui-hidden-accessible">用户名:</label>
<input type="text" name="user" id="usrnm" placeholder="用户名">
<label for="pswd" class="ui-hidden-accessible">密码:</label>
<input type="password" name="passw" id="pswd" placeholder="密码">
</div>
</form>
</div>
</div>
"""
doc5 = html.fragment_fromstring(markup5, create_parent=False)
href = doc5.xpath("string(.//a/@href)")
popup_id = doc5.xpath("string(.//div[@data-role='popup']/@id)")
actual5 = {
"trigger_rel": doc5.xpath("string(.//a/@data-rel)"),
"target_matches_popup": href == f"#{popup_id}",
"hidden_label_count": len(doc5.xpath(".//label[@class='ui-hidden-accessible']")),
"input_types": doc5.xpath(".//input/@type"),
}
expected5 = {
"trigger_rel": "popup",
"target_matches_popup": True,
"hidden_label_count": 2,
"input_types": ["text", "password"],
}
cases.append({
"name": "popup_login_form_markup",
"summary": "验证弹窗表单触发链接 data-rel='popup' 与弹窗内无障碍隐藏标签及密码输入框结构。",
"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-form-inputs-lab/explore-form-inputs.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-form-inputs.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2061.zip
课程关联工程包(登记关联32章) · 含当前章节或实例 · mbskycom202612a2050.zip