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

运行回流表格约束、列切换 1–6 优先级排序与表格 CSS 规则离线核验

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

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


def run_cases():
    cases = []

    # Case 1: Reflow table structural prerequisites (thead + tbody, no rowspan/colspan)
    doc1 = html.fragment_fromstring(
        """
        <table data-role="table" class="ui-responsive" id="reflowTable">
          <thead>
            <tr><th>CustomerID</th><th>CustomerName</th><th>City</th></tr>
          </thead>
          <tbody>
            <tr><td>1</td><td>Alfreds Futterkiste</td><td>Berlin</td></tr>
          </tbody>
        </table>
        """
    )
    actual_1 = {
        "role": doc1.get("data-role"),
        "default_mode": doc1.get("data-mode", "reflow"),
        "has_responsive_class": "ui-responsive" in doc1.get("class", "").split(),
        "has_thead_and_tbody": bool(doc1.xpath("./thead")) and bool(doc1.xpath("./tbody")),
        "has_span_attrs": bool(doc1.xpath(".//*[@rowspan or @colspan]")),
    }
    expected_1 = {
        "role": "table",
        "default_mode": "reflow",
        "has_responsive_class": True,
        "has_thead_and_tbody": True,
        "has_span_attrs": False,
    }
    cases.append({
        "name": "reflow_table_structure",
        "summary": "Verify reflow table data-role='table', ui-responsive class, thead/tbody requirement, and absence of rowspan/colspan.",
        "expected": expected_1,
        "actual": actual_1,
        "passed": actual_1 == expected_1,
    })

    # Case 2: Columntoggle table with data-priority (1..6 and persistent column without priority)
    doc2 = html.fragment_fromstring(
        """
        <table data-role="table" data-mode="columntoggle" class="ui-responsive"
               data-column-btn-text="点我显示或隐藏列!" id="myTable">
          <thead>
            <tr>
              <th data-priority="6">CustomerID</th>
              <th>CustomerName</th>
              <th data-priority="1">ContactName</th>
              <th data-priority="2">Address</th>
              <th data-priority="3">City</th>
              <th data-priority="4">PostalCode</th>
              <th data-priority="5">Country</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>1</td><td>Alfreds Futterkiste</td><td>Maria Anders</td>
              <td>Obere Str. 57</td><td>Berlin</td><td>12209</td><td>Germany</td>
            </tr>
          </tbody>
        </table>
        """
    )
    ths = doc2.xpath("./thead/tr/th")
    persistent = [th.text.strip() for th in ths if th.get("data-priority") is None]
    priorities = {th.text.strip(): int(th.get("data-priority")) for th in ths if th.get("data-priority") is not None}
    hide_order = [name for name, _ in sorted(priorities.items(), key=lambda kv: -kv[1])]
    actual_2 = {
        "mode": doc2.get("data-mode"),
        "column_btn_text": doc2.get("data-column-btn-text"),
        "persistent_columns": persistent,
        "hide_order_highest_number_first": hide_order,
    }
    expected_2 = {
        "mode": "columntoggle",
        "column_btn_text": "点我显示或隐藏列!",
        "persistent_columns": ["CustomerName"],
        "hide_order_highest_number_first": ["CustomerID", "Country", "PostalCode", "City", "Address", "ContactName"],
    }
    cases.append({
        "name": "columntoggle_priority_and_button_text",
        "summary": "Verify data-mode='columntoggle', persistent column without data-priority, priority 6-to-1 hide order, and data-column-btn-text.",
        "expected": expected_2,
        "actual": actual_2,
        "passed": actual_2 == expected_2,
    })

    # Case 3: Table visual styles (ui-shadow, row border-bottom, th border + zebra striping)
    doc3 = html.fragment_fromstring(
        """
        <div>
          <table data-role="table" data-mode="columntoggle" class="ui-responsive ui-shadow" id="styledTable">
            <thead><tr><th>Col1</th><th>Col2</th></tr></thead>
            <tbody><tr><td>A</td><td>B</td></tr><tr><td>C</td><td>D</td></tr></tbody>
          </table>
          <style>
            tr { border-bottom: 1px solid #d6d6d6; }
            th { border-bottom: 1px solid #d6d6d6; }
            tr:nth-child(even) { background: #e9e9e9; }
          </style>
        </div>
        """
    )
    css_text = doc3.xpath("string(./style)")
    actual_3 = {
        "has_ui_shadow": "ui-shadow" in doc3.xpath("./table/@class")[0].split(),
        "has_tr_border_bottom": bool(re.search(r"tr\s*\{[^}]*border-bottom\s*:\s*1px solid #d6d6d6", css_text)),
        "has_th_border_bottom": bool(re.search(r"th\s*\{[^}]*border-bottom\s*:\s*1px solid #d6d6d6", css_text)),
        "has_even_zebra_bg": bool(re.search(r"tr:nth-child\(even\)\s*\{[^}]*background\s*:\s*#e9e9e9", css_text)),
    }
    expected_3 = {
        "has_ui_shadow": True,
        "has_tr_border_bottom": True,
        "has_th_border_bottom": True,
        "has_even_zebra_bg": True,
    }
    cases.append({
        "name": "table_shadow_and_css_styling",
        "summary": "Verify ui-shadow class and custom CSS rules for row borders, header borders, and even-row striping.",
        "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-tables-lab/explore-tables.py

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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