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

运行列表图标覆盖、16px/80px 图文区分、分割按钮与气泡数字离线核验

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

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


def run_cases():
    cases = []

    # Case 1: List item data-icon overrides (default carat-r, plus/minus/delete/location, false)
    doc1 = html.fragment_fromstring(
        """
        <ul data-role="listview">
          <li><a href="#">Default is right arrow</a></li>
          <li data-icon="plus"><a href="#">plus</a></li>
          <li data-icon="minus"><a href="#">minus</a></li>
          <li data-icon="delete"><a href="#">delete</a></li>
          <li data-icon="location"><a href="#">location</a></li>
          <li data-icon="false"><a href="#">no icon</a></li>
        </ul>
        """
    )
    resolved_icons = [li.get("data-icon", "carat-r") for li in doc1.xpath("./li")]
    actual_1 = {
        "resolved_icons": resolved_icons,
        "icon_removed_count": resolved_icons.count("false"),
    }
    expected_1 = {
        "resolved_icons": ["carat-r", "plus", "minus", "delete", "location", "false"],
        "icon_removed_count": 1,
    }
    cases.append({
        "name": "list_item_data_icons",
        "summary": "Verify default carat-r list icon, custom data-icon values, and data-icon='false' removal.",
        "expected": expected_1,
        "actual": actual_1,
        "passed": actual_1 == expected_1,
    })

    # Case 2: 16x16 ui-li-icon vs 80x80 auto-scaled thumbnail + rich h2/p content
    doc2 = html.fragment_fromstring(
        """
        <ul data-role="listview">
          <li id="icon-item"><a href="#"><img src="us.png" alt="USA" class="ui-li-icon">USA</a></li>
          <li id="thumb-only"><a href="#"><img src="chrome.png" alt="Chrome"></a></li>
          <li id="thumb-rich">
            <a href="#">
              <img src="chrome.png" alt="Chrome">
              <h2>Google Chrome</h2>
              <p>Google Chrome 免费的开源 web 浏览器。发布于 2008 年。</p>
            </a>
          </li>
        </ul>
        """
    )
    icon_img = doc2.xpath("./li[@id='icon-item']//img")[0]
    thumb_img = doc2.xpath("./li[@id='thumb-only']//img")[0]
    rich_li = doc2.xpath("./li[@id='thumb-rich']")[0]
    actual_2 = {
        "icon_has_ui_li_icon": "ui-li-icon" in icon_img.get("class", "").split(),
        "thumb_auto_80x80_mode": "ui-li-icon" not in thumb_img.get("class", "").split(),
        "rich_heading": rich_li.xpath("string(.//h2)"),
        "rich_has_paragraph": bool(rich_li.xpath(".//p")),
    }
    expected_2 = {
        "icon_has_ui_li_icon": True,
        "thumb_auto_80x80_mode": True,
        "rich_heading": "Google Chrome",
        "rich_has_paragraph": True,
    }
    cases.append({
        "name": "list_icons_and_thumbnails",
        "summary": "Distinguish 16x16 img.ui-li-icon from default 80x80 thumbnail images and rich h2/p list markup.",
        "expected": expected_2,
        "actual": actual_2,
        "passed": actual_2 == expected_2,
    })

    # Case 3: Split buttons (two <a> elements in one <li>, second with data-rel="dialog")
    doc3 = html.fragment_fromstring(
        """
        <ul data-role="listview">
          <li id="split-basic">
            <a href="#"><img src="chrome.png" alt="Chrome"></a>
            <a href="#">Some Text</a>
          </li>
          <li id="split-dialog">
            <a href="#"><img src="chrome.png" alt="Chrome"></a>
            <a href="#download" data-rel="dialog">下载浏览器</a>
          </li>
        </ul>
        """
    )
    actual_3 = {
        "basic_link_count": len(doc3.xpath("./li[@id='split-basic']/a")),
        "dialog_link_count": len(doc3.xpath("./li[@id='split-dialog']/a")),
        "secondary_href": doc3.xpath("./li[@id='split-dialog']/a[2]/@href")[0],
        "secondary_rel": doc3.xpath("./li[@id='split-dialog']/a[2]/@data-rel")[0],
    }
    expected_3 = {
        "basic_link_count": 2,
        "dialog_link_count": 2,
        "secondary_href": "#download",
        "secondary_rel": "dialog",
    }
    cases.append({
        "name": "list_split_buttons",
        "summary": "Verify two-anchor split button list items and secondary action data-rel='dialog' binding.",
        "expected": expected_3,
        "actual": actual_3,
        "passed": actual_3 == expected_3,
    })

    # Case 4: Count bubbles (<span class="ui-li-count">)
    doc4 = html.fragment_fromstring(
        """
        <ul data-role="listview">
          <li><a href="#">收件箱<span class="ui-li-count">25</span></a></li>
          <li><a href="#">发件箱<span class="ui-li-count">432</span></a></li>
          <li><a href="#">垃圾箱<span class="ui-li-count">7</span></a></li>
        </ul>
        """
    )
    counts = [int(el.text.strip()) for el in doc4.xpath(".//span[@class='ui-li-count']")]
    actual_4 = {
        "bubble_count": len(counts),
        "values": counts,
        "total_messages": sum(counts),
    }
    expected_4 = {
        "bubble_count": 3,
        "values": [25, 432, 7],
        "total_messages": 464,
    }
    cases.append({
        "name": "list_count_bubbles",
        "summary": "Verify span.ui-li-count numeric count bubbles inside listview anchors.",
        "expected": expected_4,
        "actual": actual_4,
        "passed": actual_4 == expected_4,
    })

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

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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