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

运行 jQuery Mobile tap/taphold/swipe 手势阈值与方向判定离线验证

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-events-touch.py
explore-events-touch.py121 行
"""Offline jQuery Mobile Touch Events threshold & gesture verifier (Chapter 24)."""
from __future__ import annotations

import json
from lxml import html


def classify_gesture(dx: int, dy: int, duration_ms: int, v_limit: int = 75) -> list[str]:
    events: list[str] = []
    if abs(dx) < 10 and abs(dy) < 10:
        if duration_ms >= 750:
            events.append("taphold")
        else:
            events.append("tap")
    if duration_ms <= 1000 and abs(dx) > 30 and abs(dy) < v_limit:
        events.append("swipe")
        if dx < -30:
            events.append("swipeleft")
        elif dx > 30:
            events.append("swiperight")
    return events


def main() -> None:
    cases = []

    # Case 1: tap_and_desktop_pointer_mapping
    doc = html.fragment_fromstring('<div><p id="p1">点我隐藏</p><span id="out"></span></div>', create_parent=False)
    ev_touch = classify_gesture(dx=2, dy=1, duration_ms=120)
    ev_mouse = classify_gesture(dx=0, dy=0, duration_ms=90)
    hidden = "tap" in ev_touch and bool(doc.xpath(".//p[@id='p1']"))
    actual1 = {"touch_tap": ev_touch, "desktop_mouse_tap": ev_mouse, "target_hidden": hidden}
    expected1 = {"touch_tap": ["tap"], "desktop_mouse_tap": ["tap"], "target_hidden": True}
    cases.append({
        "name": "tap_and_desktop_pointer_mapping",
        "summary": "验证短按抬起在触屏与桌面鼠标点击下均触发 tap 事件并隐藏目标段落。",
        "expected": expected1,
        "actual": actual1,
        "passed": actual1 == expected1,
    })

    # Case 2: taphold_duration_threshold
    actual2 = {
        "short_300ms": classify_gesture(dx=1, dy=2, duration_ms=300),
        "hold_1000ms": classify_gesture(dx=1, dy=2, duration_ms=1000),
        "drifted_hold": classify_gesture(dx=25, dy=15, duration_ms=1000),
    }
    expected2 = {"short_300ms": ["tap"], "hold_1000ms": ["taphold"], "drifted_hold": []}
    cases.append({
        "name": "taphold_duration_threshold",
        "summary": "验证长按(约 750ms–1000ms 且位移在容差内)触发 taphold,位移过大则取消。",
        "expected": expected2,
        "actual": actual2,
        "passed": actual2 == expected2,
    })

    # Case 3: swipe_gesture_thresholds
    actual3 = {
        "valid_swipe": classify_gesture(dx=60, dy=10, duration_ms=400, v_limit=20),
        "too_short_dx": classify_gesture(dx=25, dy=5, duration_ms=400, v_limit=20),
        "too_slow": classify_gesture(dx=60, dy=5, duration_ms=1200, v_limit=20),
        "too_vertical": classify_gesture(dx=60, dy=25, duration_ms=400, v_limit=20),
    }
    expected3 = {
        "valid_swipe": ["swipe", "swiperight"],
        "too_short_dx": [],
        "too_slow": [],
        "too_vertical": [],
    }
    cases.append({
        "name": "swipe_gesture_thresholds",
        "summary": "验证 1 秒内水平位移大于 30px 且纵向偏移受限时触发 swipe(同时满足而非逻辑或)。",
        "expected": expected3,
        "actual": actual3,
        "passed": actual3 == expected3,
    })

    # Case 4: swipeleft_and_swiperight_direction
    actual4 = {
        "left_80": classify_gesture(dx=-80, dy=8, duration_ms=300),
        "right_80": classify_gesture(dx=80, dy=8, duration_ms=300),
        "vertical_scroll": classify_gesture(dx=40, dy=90, duration_ms=300),
    }
    expected4 = {
        "left_80": ["swipe", "swipeleft"],
        "right_80": ["swipe", "swiperight"],
        "vertical_scroll": [],
    }
    cases.append({
        "name": "swipeleft_and_swiperight_direction",
        "summary": "验证 swipeleft(dx < -30)与 swiperight(dx > 30)方向区分及纵向滚动排除。",
        "expected": expected4,
        "actual": actual4,
        "passed": actual4 == expected4,
    })

    # Case 5: pointer_events_modern_equivalence
    pointer_map = {
        "tap": ["pointerdown", "pointerup"],
        "taphold": ["pointerdown", "setTimeout", "pointerup/pointercancel"],
        "swipe": ["pointerdown", "pointermove", "pointerup"],
    }
    actual5 = {"unified_pointer_types": ["mouse", "touch", "pen"], "gesture_count": len(pointer_map)}
    expected5 = {"unified_pointer_types": ["mouse", "touch", "pen"], "gesture_count": 3}
    cases.append({
        "name": "pointer_events_modern_equivalence",
        "summary": "核对现代 W3C Pointer Events 统一处理 mouse/touch/pen 与旧触摸手势的对应关系。",
        "expected": expected5,
        "actual": actual5,
        "passed": actual5 == expected5,
    })

    print(json.dumps({
        "version": "CPython 3.12.14 / lxml offline jQuery Mobile DOM & reference verifier",
        "cases": 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-events-touch-lab/explore-events-touch.py

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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