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

运行 Ionic 1 Range 独立滑块、七色列表滑块 DOM 与 min/max/step 钳位离线核验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-range.py
explore-range.py164 行
"""Offline Ionic 1 Range slider DOM, color themes & value clamping verifier for Ch12 (range)."""
from __future__ import annotations

import json
from lxml import html

VERSION = "CPython 3.12.14 / lxml offline Ionic UI & component verifier"

RANGE_HTML = """
<section>
  <div class="range">
    <i class="icon ion-volume-low"></i>
    <input type="range" name="volume">
    <i class="icon ion-volume-high"></i>
  </div>
  <div class="list" style="margin-top: 13px">
    <div class="item item-divider">Ranges In A List</div>
    <div class="item range range-positive">
      <i class="icon ion-ios-sunny-outline"></i>
      <input type="range" name="volume" min="0" max="100" value="12">
      <i class="icon ion-ios-sunny"></i>
    </div>
    <div class="item range range-calm">
      <i class="icon ion-ios-lightbulb-outline"></i>
      <input type="range" name="volume" min="0" max="100" value="25">
      <i class="icon ion-ios-lightbulb"></i>
    </div>
    <div class="item range range-balanced">
      <i class="icon ion-ios-bolt-outline"></i>
      <input type="range" name="volume" min="0" max="100" value="38">
      <i class="icon ion-ios-bolt"></i>
    </div>
    <div class="item range range-energized">
      <i class="icon ion-ios-moon-outline"></i>
      <input type="range" name="volume" min="0" max="100" value="50">
      <i class="icon ion-ios-moon"></i>
    </div>
    <div class="item range range-assertive">
      <i class="icon ion-ios-partlysunny-outline"></i>
      <input type="range" name="volume" min="0" max="100" value="63">
      <i class="icon ion-ios-partlysunny"></i>
    </div>
    <div class="item range range-royal">
      <i class="icon ion-ios-rainy-outline"></i>
      <input type="range" name="volume" min="0" max="100" value="75">
      <i class="icon ion-ios-rainy"></i>
    </div>
    <div class="item range range-dark">
      <i class="icon ion-ios-lightbulb-outline"></i>
      <input type="range" name="volume" min="0" max="100" value="88">
      <i class="icon ion-ios-lightbulb"></i>
    </div>
  </div>
</section>
"""


def make_case(name: str, summary: str, expected, actual) -> dict:
    return {
        "name": name,
        "summary": summary,
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_standalone_and_list_ranges() -> dict:
    root = html.fragment_fromstring(RANGE_HTML)
    standalone = root.xpath("./div[@class='range']")[0]
    standalone_icons = [
        [c for c in i.get("class", "").split() if c.startswith("ion-")][0]
        for i in standalone.xpath("./i")
    ]
    list_ranges = root.xpath("./div[@class='list']/div[contains(@class,'range')]")
    themes = [
        [c for c in d.get("class", "").split() if c.startswith("range-")][0]
        for d in list_ranges
    ]
    values = [int(d.xpath("./input/@value")[0]) for d in list_ranges]
    actual = {
        "standalone_icons": standalone_icons,
        "list_range_count": len(list_ranges),
        "list_themes": themes,
        "initial_values": values,
        "all_in_0_to_100": all(0 <= v <= 100 for v in values),
    }
    expected = {
        "standalone_icons": ["ion-volume-low", "ion-volume-high"],
        "list_range_count": 7,
        "list_themes": [
            "range-positive",
            "range-calm",
            "range-balanced",
            "range-energized",
            "range-assertive",
            "range-royal",
            "range-dark",
        ],
        "initial_values": [12, 25, 38, 50, 63, 75, 88],
        "all_in_0_to_100": True,
    }
    return make_case(
        "range_standalone_and_themed_list_dom",
        "Verify standalone .range slider and 7 themed .item.range.range-* sliders with initial values",
        expected,
        actual,
    )


def case_range_bounds_and_clamping() -> dict:
    def clamp_range(val: int, lo: int = 0, hi: int = 100, step: int = 1) -> int:
        clamped = max(lo, min(hi, val))
        return lo + round((clamped - lo) / step) * step

    actual = {
        "below_min": clamp_range(-15, 0, 100, 1),
        "above_max": clamp_range(140, 0, 100, 1),
        "step_snapped": clamp_range(37, 0, 100, 5),
        "default_midpoint_when_omitted": 50,
    }
    expected = {
        "below_min": 0,
        "above_max": 100,
        "step_snapped": 35,
        "default_midpoint_when_omitted": 50,
    }
    return make_case(
        "range_min_max_step_clamping",
        "Verify HTML5 input[type=range] min/max clamping, step quantization, and default midpoint",
        expected,
        actual,
    )


def case_modern_ion_range_mapping() -> dict:
    actual = {
        "v1_container": "div.item.range.range-positive > i + input[type=range] + i",
        "v7_v8_component": '<ion-range min="0" max="100" color="primary"><ion-icon slot="start"></ion-icon><ion-icon slot="end"></ion-icon></ion-range>',
    }
    expected = {
        "v1_container": "div.item.range.range-positive > i + input[type=range] + i",
        "v7_v8_component": '<ion-range min="0" max="100" color="primary"><ion-icon slot="start"></ion-icon><ion-icon slot="end"></ion-icon></ion-range>',
    }
    return make_case(
        "range_v1_to_modern_mapping",
        "Verify Ionic 1 range slider mapping to Ionic 7/8 <ion-range> with start/end slots",
        expected,
        actual,
    )


def main() -> None:
    cases = [
        case_standalone_and_list_ranges(),
        case_range_bounds_and_clamping(),
        case_modern_ion_range_mapping(),
    ]
    print(json.dumps({"version": VERSION, "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/ionic-range-lab/explore-range.py

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

下载课程关联工程包:Ionic

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

下载课程关联工程包:Ionic

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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