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

运行弹窗同页绑定、定位/箭头、对话框结构与图片遮罩离线核验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-popups.py
explore-popups.py151 行
"""Offline jQuery Mobile DOM & data-role verifier for Chapter 08: popups."""
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: Basic popup linkage in same page + ui-content padding + close modes
    doc1 = html.fragment_fromstring(
        """
        <div data-role="page" id="p1">
          <div data-role="main" class="ui-content">
            <a href="#myPopup" data-rel="popup" class="ui-btn ui-btn-inline ui-corner-all">显示弹窗</a>
            <div data-role="popup" id="myPopup" class="ui-content" data-dismissible="false">
              <a href="#" data-rel="back" class="ui-btn ui-corner-all ui-shadow ui-btn-a ui-icon-delete ui-btn-icon-notext ui-btn-right">关闭</a>
              <p>这是一个带内边距与关闭按钮的弹窗</p>
            </div>
          </div>
        </div>
        """
    )
    trigger = doc1.xpath(".//a[@data-rel='popup']")[0]
    target_id = trigger.get("href").lstrip("#")
    popup = doc1.xpath(f".//div[@data-role='popup' and @id='{target_id}']")[0]
    close_btn = popup.xpath("./a[@data-rel='back']")[0]
    actual_1 = {
        "same_page_target_exists": popup is not None,
        "has_ui_content": "ui-content" in popup.get("class", "").split(),
        "dismissible": popup.get("data-dismissible"),
        "close_btn_side": "right" if "ui-btn-right" in close_btn.get("class", "").split() else "left",
    }
    expected_1 = {
        "same_page_target_exists": True,
        "has_ui_content": True,
        "dismissible": "false",
        "close_btn_side": "right",
    }
    cases.append({
        "name": "popup_basic_content_and_close",
        "summary": "Verify same-page data-rel='popup' to data-role='popup' id binding, ui-content class, data-dismissible, and data-rel='back' close button.",
        "expected": expected_1,
        "actual": actual_1,
        "passed": actual_1 == expected_1,
    })

    # Case 2: Positioning (window / #demo / origin), transition, and arrow direction
    doc2 = html.fragment_fromstring(
        """
        <div>
          <a href="#myPopup1" data-rel="popup" class="ui-btn" data-position-to="window" data-transition="fade">Window</a>
          <a href="#myPopup2" data-rel="popup" class="ui-btn" data-position-to="#demo">Selector</a>
          <a href="#myPopup3" data-rel="popup" class="ui-btn" data-position-to="origin">Origin</a>
          <div data-role="popup" id="myPopup1" class="ui-content" data-arrow="l"><p>左向箭头</p></div>
        </div>
        """
    )
    actual_2 = {
        "position_modes": doc2.xpath("./a/@data-position-to"),
        "transition": doc2.xpath("./a[1]/@data-transition")[0],
        "arrow_dir": doc2.xpath("./div[@data-role='popup']/@data-arrow")[0],
        "valid_arrow_dirs": ["l", "t", "r", "b"],
    }
    expected_2 = {
        "position_modes": ["window", "#demo", "origin"],
        "transition": "fade",
        "arrow_dir": "l",
        "valid_arrow_dirs": ["l", "t", "r", "b"],
    }
    cases.append({
        "name": "popup_position_transition_and_arrow",
        "summary": "Verify data-position-to (window/#demo/origin), data-transition, and data-arrow directional pointers.",
        "expected": expected_2,
        "actual": actual_2,
        "passed": actual_2 == expected_2,
    })

    # Case 3: Popup dialog structure (well-formed header/main/footer)
    doc3 = html.fragment_fromstring(
        """
        <div data-role="popup" id="myPopupDialog">
          <div data-role="header"><h1>头部文本</h1></div>
          <div data-role="main" class="ui-content">
            <p>弹窗正文内容</p>
            <a href="#" data-rel="back" class="ui-btn">返回</a>
          </div>
          <div data-role="footer"><h1>底部文本</h1></div>
        </div>
        """
    )
    actual_3 = {
        "popup_id": doc3.get("id"),
        "dialog_child_roles": [el.get("data-role") for el in doc3.xpath("./div[@data-role]")],
    }
    expected_3 = {
        "popup_id": "myPopupDialog",
        "dialog_child_roles": ["header", "main", "footer"],
    }
    cases.append({
        "name": "popup_dialog_structure",
        "summary": "Verify well-formed header/main/footer sibling structure inside a data-role='popup' dialog.",
        "expected": expected_3,
        "actual": actual_3,
        "passed": actual_3 == expected_3,
    })

    # Case 4: Image lightbox popup + data-overlay-theme="b"
    doc4 = html.fragment_fromstring(
        """
        <div>
          <a href="#photoPopup" data-rel="popup" data-position-to="window">
            <img src="sample-photo.jpg" alt="示例风景缩略图" style="width:200px;">
          </a>
          <div data-role="popup" id="photoPopup" data-overlay-theme="b">
            <img src="sample-photo.jpg" alt="示例风景大图" style="width:800px;height:400px;">
          </div>
        </div>
        """
    )
    actual_4 = {
        "trigger_position": doc4.xpath("./a/@data-position-to")[0],
        "overlay_theme": doc4.xpath("./div[@data-role='popup']/@data-overlay-theme")[0],
        "thumb_src": doc4.xpath("./a/img/@src")[0],
        "large_src": doc4.xpath("./div[@data-role='popup']/img/@src")[0],
    }
    expected_4 = {
        "trigger_position": "window",
        "overlay_theme": "b",
        "thumb_src": "sample-photo.jpg",
        "large_src": "sample-photo.jpg",
    }
    cases.append({
        "name": "popup_image_and_overlay_theme",
        "summary": "Verify image lightbox popup centered to window with dark data-overlay-theme='b' backdrop.",
        "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-popups-lab/explore-popups.py

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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