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

运行导航栏等宽/折行规则、data-icon/data-iconpos 与持久激活态离线核验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-navbars.py
explore-navbars.py124 行
"""Offline jQuery Mobile DOM & data-role verifier for Chapter 10: navbars."""
import json
from lxml import html

VERSION = "CPython 3.12.14 / lxml offline jQuery Mobile DOM & data-role verifier"


def navbar_column_plan(item_count: int) -> dict:
    if item_count <= 5:
        return {"columns_per_row": item_count, "wraps_to_two_col_grid": False, "width_pct": round(100.0 / item_count, 1)}
    return {"columns_per_row": 2, "wraps_to_two_col_grid": True, "width_pct": 50.0}


def run_cases():
    cases = []

    # Case 1: Navbar structure inside header & equal-width / >5 wrapping rule
    doc1 = html.fragment_fromstring(
        """
        <div data-role="header">
          <div data-role="navbar">
            <ul>
              <li><a href="#anylink">首页</a></li>
              <li><a href="#anylink">页面二</a></li>
              <li><a href="#anylink">搜索</a></li>
            </ul>
          </div>
        </div>
        """
    )
    items = doc1.xpath(".//div[@data-role='navbar']/ul/li/a")
    actual_1 = {
        "navbar_item_count": len(items),
        "plan_3_items": navbar_column_plan(len(items)),
        "plan_6_items": navbar_column_plan(6),
    }
    expected_1 = {
        "navbar_item_count": 3,
        "plan_3_items": {"columns_per_row": 3, "wraps_to_two_col_grid": False, "width_pct": 33.3},
        "plan_6_items": {"columns_per_row": 2, "wraps_to_two_col_grid": True, "width_pct": 50.0},
    }
    cases.append({
        "name": "navbar_structure_and_width_rules",
        "summary": "Verify data-role='navbar' ul/li/a layout, equal width division up to 5 items, and multi-row wrap beyond 5.",
        "expected": expected_1,
        "actual": actual_1,
        "passed": actual_1 == expected_1,
    })

    # Case 2: Navbar data-icon values and data-iconpos placement on navbar container
    doc2 = html.fragment_fromstring(
        """
        <div>
          <div data-role="navbar" data-iconpos="top">
            <ul>
              <li><a href="#p1" data-icon="home">首页</a></li>
              <li><a href="#p2" data-icon="arrow-r">下一页</a></li>
              <li><a href="#p3" data-icon="search">搜索</a></li>
            </ul>
          </div>
          <div data-role="navbar" data-iconpos="right"></div>
          <div data-role="navbar" data-iconpos="bottom"></div>
          <div data-role="navbar" data-iconpos="left"></div>
        </div>
        """
    )
    actual_2 = {
        "icons": doc2.xpath(".//a/@data-icon"),
        "iconpos_modes": doc2.xpath("./div[@data-role='navbar']/@data-iconpos"),
        "default_iconpos": "top",
    }
    expected_2 = {
        "icons": ["home", "arrow-r", "search"],
        "iconpos_modes": ["top", "right", "bottom", "left"],
        "default_iconpos": "top",
    }
    cases.append({
        "name": "navbar_icons_and_positions",
        "summary": "Verify data-icon attributes on navbar anchors and data-iconpos (top/right/bottom/left) on navbar containers.",
        "expected": expected_2,
        "actual": actual_2,
        "passed": actual_2 == expected_2,
    })

    # Case 3: Active state (ui-btn-active) and multi-page persistence (ui-state-persist)
    doc3 = html.fragment_fromstring(
        """
        <div data-role="navbar">
          <ul>
            <li><a href="#pageone" class="ui-btn-active">当前激活</a></li>
            <li><a href="#pagetwo" class="ui-btn-active ui-state-persist">跨页持久激活</a></li>
          </ul>
        </div>
        """
    )
    a1, a2 = doc3.xpath(".//a")
    actual_3 = {
        "first_classes": a1.get("class").split(),
        "second_classes": a2.get("class").split(),
        "persists_across_pages": {"ui-btn-active", "ui-state-persist"}.issubset(set(a2.get("class").split())),
    }
    expected_3 = {
        "first_classes": ["ui-btn-active"],
        "second_classes": ["ui-btn-active", "ui-state-persist"],
        "persists_across_pages": True,
    }
    cases.append({
        "name": "navbar_active_and_persist_states",
        "summary": "Verify ui-btn-active and ui-state-persist class combinations for single- and multi-page navbars.",
        "expected": expected_3,
        "actual": actual_3,
        "passed": actual_3 == expected_3,
    })

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

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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