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

运行 1–5 列网格映射、块样式定制、ui-block-a 换行与 ui-responsive 离线核验

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

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

GRID_SPECS = [
    ("ui-grid-solo", 1, ["ui-block-a"]),
    ("ui-grid-a", 2, ["ui-block-a", "ui-block-b"]),
    ("ui-grid-b", 3, ["ui-block-a", "ui-block-b", "ui-block-c"]),
    ("ui-grid-c", 4, ["ui-block-a", "ui-block-b", "ui-block-c", "ui-block-d"]),
    ("ui-grid-d", 5, ["ui-block-a", "ui-block-b", "ui-block-c", "ui-block-d", "ui-block-e"]),
]


def run_cases():
    cases = []

    # Case 1: All five grid container classes (ui-grid-solo, a, b, c, d) and equal column widths
    grid_summary = []
    for cls, cols, blocks in GRID_SPECS:
        children = "".join(f'<div class="{b}"><span>{b}</span></div>' for b in blocks)
        node = html.fragment_fromstring(f'<div class="{cls}">{children}</div>')
        parsed_blocks = [d.get("class") for d in node.xpath("./div")]
        grid_summary.append({
            "grid": node.get("class"),
            "cols": len(parsed_blocks),
            "col_width_pct": round(100.0 / cols, 2),
            "blocks_match": parsed_blocks == blocks,
        })
    actual_1 = {
        "grid_count": len(grid_summary),
        "all_blocks_match": all(item["blocks_match"] for item in grid_summary),
        "column_counts": [item["cols"] for item in grid_summary],
    }
    expected_1 = {
        "grid_count": 5,
        "all_blocks_match": True,
        "column_counts": [1, 2, 3, 4, 5],
    }
    cases.append({
        "name": "grid_classes_and_block_mapping",
        "summary": "Verify ui-grid-solo through ui-grid-d column counts (1..5) and matching ui-block-a..e children.",
        "expected": expected_1,
        "actual": actual_1,
        "passed": actual_1 == expected_1,
    })

    # Case 2: Custom CSS styling and inline block styling
    doc2 = html.fragment_fromstring(
        """
        <div>
          <style>
            .ui-block-a, .ui-block-b, .ui-block-c {
              background-color: lightgray;
              border: 1px solid black;
              height: 100px;
              font-weight: bold;
              text-align: center;
              padding: 30px;
            }
          </style>
          <div class="ui-block-a" style="border: 1px solid black;"><span>Text..</span></div>
        </div>
        """
    )
    css_text = doc2.xpath("string(./style)")
    actual_2 = {
        "styles_three_blocks": all(sel in css_text for sel in (".ui-block-a", ".ui-block-b", ".ui-block-c")),
        "has_height_100px": bool(re.search(r"height\s*:\s*100px", css_text)),
        "inline_border": "border: 1px solid black" in doc2.xpath("./div/@style")[0],
    }
    expected_2 = {
        "styles_three_blocks": True,
        "has_height_100px": True,
        "inline_border": True,
    }
    cases.append({
        "name": "grid_custom_css_and_inline",
        "summary": "Verify stylesheet and inline style customization on ui-block-a/b/c grid cells.",
        "expected": expected_2,
        "actual": actual_2,
        "passed": actual_2 == expected_2,
    })

    # Case 3: Multi-row grid segmentation (every ui-block-a starts a new row) + ui-responsive modifier
    doc3 = html.fragment_fromstring(
        """
        <div class="ui-grid-b ui-responsive">
          <div class="ui-block-a"><span>R1C1</span></div>
          <div class="ui-block-b"><span>R1C2</span></div>
          <div class="ui-block-c"><span>R1C3</span></div>
          <div class="ui-block-a"><span>R2C1</span></div>
          <div class="ui-block-b"><span>R2C2</span></div>
          <div class="ui-block-a"><span>R3C1</span></div>
        </div>
        """
    )
    rows = []
    for block in doc3.xpath("./div"):
        cls = block.get("class")
        if cls == "ui-block-a" or not rows:
            rows.append([])
        rows[-1].append(cls)
    actual_3 = {
        "is_responsive": "ui-responsive" in doc3.get("class", "").split(),
        "row_count": len(rows),
        "row_lengths": [len(r) for r in rows],
    }
    expected_3 = {
        "is_responsive": True,
        "row_count": 3,
        "row_lengths": [3, 2, 1],
    }
    cases.append({
        "name": "grid_multirow_and_responsive",
        "summary": "Verify ui-block-a new-row breaking semantics across 3 rows and ui-responsive stacking modifier.",
        "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-grids-lab/explore-grids.py

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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