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

运行三类按钮、内联/后退属性、controlgroup 组合与 ui-btn 修饰类离线核验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-buttons.py
explore-buttons.py144 行
"""Offline jQuery Mobile DOM & data-role verifier for Chapter 06: buttons."""
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: Three ways to create buttons (<button>, <input>, <a data-role="button">)
    doc1 = html.fragment_fromstring(
        """
        <div data-role="main" class="ui-content">
          <button>按钮</button>
          <input type="button" value="按钮">
          <a href="#" data-role="button">按钮</a>
        </div>
        """
    )
    actual_1 = {
        "button_tags": [el.tag for el in doc1.xpath("./*")],
        "input_type": doc1.xpath("./input/@type")[0],
        "anchor_role": doc1.xpath("./a/@data-role")[0],
    }
    expected_1 = {
        "button_tags": ["button", "input", "a"],
        "input_type": "button",
        "anchor_role": "button",
    }
    cases.append({
        "name": "three_button_creation_patterns",
        "summary": "Verify <button>, <input type='button'>, and <a data-role='button'> creation patterns.",
        "expected": expected_1,
        "actual": actual_1,
        "passed": actual_1 == expected_1,
    })

    # Case 2: Navigation button, inline button, and back button
    doc2 = html.fragment_fromstring(
        """
        <div>
          <a href="#pagetwo" data-role="button">访问第二个页面</a>
          <a href="#pagetwo" data-role="button" data-inline="true">内联导航按钮</a>
          <a href="#" data-role="button" data-rel="back">返回</a>
        </div>
        """
    )
    nav_btn, inline_btn, back_btn = doc2.xpath("./a")
    actual_2 = {
        "nav_href": nav_btn.get("href"),
        "inline_flag": inline_btn.get("data-inline") == "true",
        "back_rel": back_btn.get("data-rel"),
    }
    expected_2 = {
        "nav_href": "#pagetwo",
        "inline_flag": True,
        "back_rel": "back",
    }
    cases.append({
        "name": "nav_inline_and_back_buttons",
        "summary": "Verify page navigation href, data-inline='true' width shrink, and data-rel='back' history return.",
        "expected": expected_2,
        "actual": actual_2,
        "passed": actual_2 == expected_2,
    })

    # Case 3: Controlgroup horizontal vs default vertical grouping
    doc3 = html.fragment_fromstring(
        """
        <div>
          <div data-role="controlgroup" data-type="horizontal">
            <a href="#anylink" data-role="button">按钮 1</a>
            <a href="#anylink" data-role="button">按钮 2</a>
            <a href="#anylink" data-role="button">按钮 3</a>
          </div>
          <div data-role="controlgroup">
            <a href="#anylink" data-role="button">默认垂直 1</a>
            <a href="#anylink" data-role="button">默认垂直 2</a>
          </div>
        </div>
        """
    )
    h_grp, v_grp = doc3.xpath("./div[@data-role='controlgroup']")
    actual_3 = {
        "horizontal_type": h_grp.get("data-type"),
        "horizontal_count": len(h_grp.xpath("./a[@data-role='button']")),
        "default_vertical_type": v_grp.get("data-type", "vertical"),
        "vertical_count": len(v_grp.xpath("./a[@data-role='button']")),
    }
    expected_3 = {
        "horizontal_type": "horizontal",
        "horizontal_count": 3,
        "default_vertical_type": "vertical",
        "vertical_count": 2,
    }
    cases.append({
        "name": "controlgroup_button_grouping",
        "summary": "Verify data-role='controlgroup' horizontal layout and default vertical fallback.",
        "expected": expected_3,
        "actual": actual_3,
        "passed": actual_3 == expected_3,
    })

    # Case 4: jQM 1.4 ui-btn CSS modifier classes
    doc4 = html.fragment_fromstring(
        """
        <div>
          <a href="#" class="ui-btn ui-btn-b">深色主题</a>
          <a href="#" class="ui-btn ui-corner-all">圆角按钮</a>
          <a href="#" class="ui-btn ui-mini">迷你按钮</a>
          <a href="#" class="ui-btn ui-shadow">阴影按钮</a>
          <a href="#" class="ui-btn ui-btn-inline ui-corner-all ui-shadow">组合类按钮</a>
        </div>
        """
    )
    modifier_classes = [a.get("class").split()[1] for a in doc4.xpath("./a")[:4]]
    combo_classes = doc4.xpath("./a")[4].get("class").split()
    actual_4 = {
        "modifier_classes": modifier_classes,
        "combo_has_all_four": set(combo_classes) == {"ui-btn", "ui-btn-inline", "ui-corner-all", "ui-shadow"},
    }
    expected_4 = {
        "modifier_classes": ["ui-btn-b", "ui-corner-all", "ui-mini", "ui-shadow"],
        "combo_has_all_four": True,
    }
    cases.append({
        "name": "ui_btn_modifier_classes",
        "summary": "Verify ui-btn-b, ui-corner-all, ui-mini, ui-shadow, and multi-class composition.",
        "expected": expected_4,
        "actual": actual_4,
        "passed": actual_4 == expected_4,
    })

    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-buttons-lab/explore-buttons.py

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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