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

运行 jQuery Mobile 滑动条边界步进、高亮提示与拨动开关离线验证

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-form-sliders.py
explore-form-sliders.py173 行
"""Offline jQuery Mobile Form Sliders & Flipswitch verifier (Chapter 21)."""
from __future__ import annotations

import json
from lxml import html


def clamp_step(val: float, lo: float, hi: float, step: float = 1.0) -> float:
    clamped = max(lo, min(hi, val))
    steps = round((clamped - lo) / step)
    return round(lo + steps * step, 6)


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

    # Case 1: range_slider_bounds_and_step
    markup1 = """
    <form method="post" action="demoform.php">
      <label for="points">进度:</label>
      <input type="range" name="points" id="points" value="50" min="0" max="100" step="5">
    </form>
    """
    doc1 = html.fragment_fromstring(markup1, create_parent=False)
    inp = doc1.xpath(".//input")[0]
    lo, hi, step, val = float(inp.get("min")), float(inp.get("max")), float(inp.get("step")), float(inp.get("value"))
    actual1 = {
        "type": inp.get("type"),
        "default_value": val,
        "clamped_high": clamp_step(130, lo, hi, step),
        "clamped_low": clamp_step(-10, lo, hi, step),
        "quantized_43": clamp_step(43, lo, hi, step),
    }
    expected1 = {
        "type": "range",
        "default_value": 50.0,
        "clamped_high": 100.0,
        "clamped_low": 0.0,
        "quantized_43": 45.0,
    }
    cases.append({
        "name": "range_slider_bounds_and_step",
        "summary": "解析 input[type='range'] 的 min/max/step/value 限制并验证越界截断与步进量化。",
        "expected": expected1,
        "actual": actual1,
        "passed": actual1 == expected1,
    })

    # Case 2: slider_value_popup_highlight
    markup2 = """
    <div>
      <input id="s1" type="range" data-show-value="true">
      <input id="s2" type="range" data-popup-enabled="true">
      <input id="s3" type="range" data-highlight="true">
    </div>
    """
    doc2 = html.fragment_fromstring(markup2, create_parent=False)
    actual2 = {
        "show_value": doc2.xpath("string(.//input[@id='s1']/@data-show-value)"),
        "popup_enabled": doc2.xpath("string(.//input[@id='s2']/@data-popup-enabled)"),
        "highlight": doc2.xpath("string(.//input[@id='s3']/@data-highlight)"),
    }
    expected2 = {"show_value": "true", "popup_enabled": "true", "highlight": "true"}
    cases.append({
        "name": "slider_value_popup_highlight",
        "summary": "验证滑块 data-show-value、data-popup-enabled 与 data-highlight 三个增强属性。",
        "expected": expected2,
        "actual": actual2,
        "passed": actual2 == expected2,
    })

    # Case 3: flipswitch_checkbox_basic
    markup3 = """
    <form method="post" action="demoform.php">
      <label for="switch">切换开关:</label>
      <input type="checkbox" data-role="flipswitch" name="switch" id="switch">
    </form>
    """
    doc3 = html.fragment_fromstring(markup3, create_parent=False)
    sw = doc3.xpath(".//input")[0]
    on_text = sw.get("data-on-text", "On")
    off_text = sw.get("data-off-text", "Off")
    checked = "checked" in sw.attrib
    actual3 = {
        "data_role": sw.get("data-role"),
        "default_checked": checked,
        "default_labels": [on_text, off_text],
        "rendered_label": on_text if checked else off_text,
    }
    expected3 = {
        "data_role": "flipswitch",
        "default_checked": False,
        "default_labels": ["On", "Off"],
        "rendered_label": "Off",
    }
    cases.append({
        "name": "flipswitch_checkbox_basic",
        "summary": "验证 checkbox + data-role='flipswitch' 拨动开关的默认 On/Off 文本与未选中初态。",
        "expected": expected3,
        "actual": actual3,
        "passed": actual3 == expected3,
    })

    # Case 4: flipswitch_custom_text_and_checked
    markup4 = """
    <input type="checkbox" data-role="flipswitch" name="switch" id="switch"
      data-on-text="True" data-off-text="False" checked>
    """
    sw4 = html.fragment_fromstring(markup4, create_parent=False)
    checked4 = "checked" in sw4.attrib
    actual4 = {
        "on_text": sw4.get("data-on-text"),
        "off_text": sw4.get("data-off-text"),
        "checked": checked4,
        "active_label": sw4.get("data-on-text") if checked4 else sw4.get("data-off-text"),
    }
    expected4 = {
        "on_text": "True",
        "off_text": "False",
        "checked": True,
        "active_label": "True",
    }
    cases.append({
        "name": "flipswitch_custom_text_and_checked",
        "summary": "验证 data-on-text/data-off-text 自定义文本与 checked 预激活状态。",
        "expected": expected4,
        "actual": actual4,
        "passed": actual4 == expected4,
    })

    # Case 5: rangeslider_dual_and_legacy_select
    markup5 = """
    <div>
      <div data-role="rangeslider">
        <input type="range" id="range-low" min="0" max="100" value="20">
        <input type="range" id="range-high" min="0" max="100" value="80">
      </div>
      <select id="legacy_flip" data-role="slider">
        <option value="off">Off</option>
        <option value="on">On</option>
      </select>
    </div>
    """
    doc5 = html.fragment_fromstring(markup5, create_parent=False)
    low = int(doc5.xpath("string(.//input[@id='range-low']/@value)"))
    high = int(doc5.xpath("string(.//input[@id='range-high']/@value)"))
    actual5 = {
        "dual_range_valid": 0 <= low <= high <= 100,
        "span": high - low,
        "legacy_slider_options": doc5.xpath(".//select[@data-role='slider']/option/@value"),
    }
    expected5 = {
        "dual_range_valid": True,
        "span": 60,
        "legacy_slider_options": ["off", "on"],
    }
    cases.append({
        "name": "rangeslider_dual_and_legacy_select",
        "summary": "验证双滑块区间控件 rangeslider 及早期 select[data-role='slider'] 二选一开关对照。",
        "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-sliders-lab/explore-form-sliders.py

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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