蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 jQuery Mobile 表单结构、清除按钮、按钮属性与字段容器离线验证

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-form-basic.py
explore-form-basic.py188 行
"""Offline jQuery Mobile Form Basics DOM & attribute verifier (Chapter 18)."""
from __future__ import annotations

import json
from lxml import html


def layout_mode(viewport_width: int, has_field_contain: bool) -> str:
    if not has_field_contain:
        return "stacked"
    return "inline" if viewport_width > 480 else "stacked"


def clear_btn_effective(tag: str, input_type: str, data_clear_btn: str | None) -> bool:
    if tag.lower() == "textarea":
        return False
    if input_type == "search":
        return data_clear_btn != "false"
    return data_clear_btn == "true"


def main() -> None:
    cases = []

    # Case 1: form_structure_and_unique_ids
    markup1 = """
    <div data-role="page" id="p1">
      <form method="post" action="demoform.html">
        <label for="fname">姓名:</label>
        <input type="text" name="fname" id="fname">
      </form>
      <form method="post" action="demoform.html">
        <label for="fname_hidden" class="ui-hidden-accessible">姓名:</label>
        <input type="text" name="fname" id="fname_hidden" placeholder="姓名...">
      </form>
    </div>
    """
    doc1 = html.fragment_fromstring(markup1, create_parent=False)
    forms = doc1.xpath(".//form")
    ids = doc1.xpath(".//input/@id")
    label_fors = doc1.xpath(".//label/@for")
    actual1 = {
        "all_have_method_and_action": all(f.get("method") and f.get("action") for f in forms),
        "ids_unique": len(ids) == len(set(ids)),
        "labels_match_ids": label_fors == ids,
        "hidden_label_class": doc1.xpath("string(.//label[@for='fname_hidden']/@class)"),
        "placeholder": doc1.xpath("string(.//input[@id='fname_hidden']/@placeholder)"),
    }
    expected1 = {
        "all_have_method_and_action": True,
        "ids_unique": True,
        "labels_match_ids": True,
        "hidden_label_class": "ui-hidden-accessible",
        "placeholder": "姓名...",
    }
    cases.append({
        "name": "form_structure_and_unique_ids",
        "summary": "验证表单 method/action、全站唯一 id、label[for] 绑定及 ui-hidden-accessible 隐藏标签。",
        "expected": expected1,
        "actual": actual1,
        "passed": actual1 == expected1,
    })

    # Case 2: clear_button_behavior
    markup2 = """
    <form method="post" action="demoform.html">
      <input type="text" id="t1" data-clear-btn="true">
      <input type="search" id="s1">
      <input type="search" id="s2" data-clear-btn="false">
      <textarea id="ta1" data-clear-btn="true"></textarea>
    </form>
    """
    doc2 = html.fragment_fromstring(markup2, create_parent=False)
    actual2 = {}
    for el in doc2.xpath("./*"):
        actual2[el.get("id")] = clear_btn_effective(el.tag, el.get("type", ""), el.get("data-clear-btn"))
    expected2 = {"t1": True, "s1": True, "s2": False, "ta1": False}
    cases.append({
        "name": "clear_button_behavior",
        "summary": "验证 data-clear-btn 在 text/search/textarea 上的默认与显式开关语义。",
        "expected": expected2,
        "actual": actual2,
        "passed": actual2 == expected2,
    })

    # Case 3: input_button_data_attrs
    markup3 = """
    <form method="post" action="demoform.html">
      <input type="button" value="按钮" data-corners="true" data-icon="search" data-iconpos="left" data-inline="true" data-mini="false" data-shadow="true">
      <input type="reset" value="重置按钮" data-icon="refresh" data-iconpos="right">
      <input type="submit" value="提交按钮" data-icon="check" data-iconpos="top">
    </form>
    """
    doc3 = html.fragment_fromstring(markup3, create_parent=False)
    btn_types = doc3.xpath("./input/@type")
    first_btn = doc3.xpath("./input[1]")[0]
    attrs6 = ["data-corners", "data-icon", "data-iconpos", "data-inline", "data-mini", "data-shadow"]
    actual3 = {
        "button_types": btn_types,
        "supported_data_attrs_present": [a for a in attrs6 if first_btn.get(a) is not None],
        "iconpos_valid": all(v in {"left", "right", "top", "bottom", "notext"} for v in doc3.xpath("./input/@data-iconpos")),
    }
    expected3 = {
        "button_types": ["button", "reset", "submit"],
        "supported_data_attrs_present": attrs6,
        "iconpos_valid": True,
    }
    cases.append({
        "name": "input_button_data_attrs",
        "summary": "核对 button/reset/submit 三类 input 按钮及 6 项 data-* 样式与图标属性。",
        "expected": expected3,
        "actual": actual3,
        "passed": actual3 == expected3,
    })

    # Case 4: field_contain_responsive_layout
    markup4 = """
    <form method="post" action="demoform.php">
      <div class="ui-field-contain">
        <label for="fname">姓:</label>
        <input type="text" name="fname" id="fname">
        <label for="lname">名:</label>
        <input type="text" name="lname" id="lname">
      </div>
    </form>
    """
    doc4 = html.fragment_fromstring(markup4, create_parent=False)
    has_fc = bool(doc4.xpath(".//div[contains(@class,'ui-field-contain')]"))
    actual4 = {
        "has_field_contain": has_fc,
        "pair_count": len(doc4.xpath(".//div[@class='ui-field-contain']/input")),
        "width_375": layout_mode(375, has_fc),
        "width_480": layout_mode(480, has_fc),
        "width_481": layout_mode(481, has_fc),
    }
    expected4 = {
        "has_field_contain": True,
        "pair_count": 2,
        "width_375": "stacked",
        "width_480": "stacked",
        "width_481": "inline",
    }
    cases.append({
        "name": "field_contain_responsive_layout",
        "summary": "验证 ui-field-contain 字段容器结构及 480px 宽度断点下的标签/控件排布模式。",
        "expected": expected4,
        "actual": actual4,
        "passed": actual4 == expected4,
    })

    # Case 5: disable_enhancement_and_ajax_submit
    markup5 = """
    <div>
      <form id="f_default" method="post" action="demoform.php">
        <label for="plain">姓名:</label>
        <input type="text" name="plain" id="plain" data-role="none">
      </form>
      <form id="f_noajax" method="post" action="demoform.php" data-ajax="false"></form>
    </div>
    """
    doc5 = html.fragment_fromstring(markup5, create_parent=False)
    actual5 = {
        "input_enhanced": doc5.xpath("string(.//input[@id='plain']/@data-role)") != "none",
        "default_form_uses_ajax": doc5.xpath("string(.//form[@id='f_default']/@data-ajax)") != "false",
        "optout_form_uses_ajax": doc5.xpath("string(.//form[@id='f_noajax']/@data-ajax)") != "false",
    }
    expected5 = {
        "input_enhanced": False,
        "default_form_uses_ajax": True,
        "optout_form_uses_ajax": False,
    }
    cases.append({
        "name": "disable_enhancement_and_ajax_submit",
        "summary": "验证 data-role='none' 阻止自动样式增强,以及默认表单 Ajax 提交与 data-ajax='false' 回退。",
        "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-basic-lab/explore-form-basic.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-form-basic.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:jQuery Mobile

课程关联工程包(登记关联32章) · 含当前章节或实例 · mbskycom202612a2050.zip

下载课程关联工程包:jQuery Mobile

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2060.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习