"""Offline jQuery Mobile Scroll Events state-machine verifier (Chapter 25)."""
from __future__ import annotations
import json
from lxml import html
def simulate_scroll_stream(ticks_ms: list[int], stop_delay_ms: int = 150) -> list[tuple[str, int]]:
emitted: list[tuple[str, int]] = []
scrolling = False
last_t = None
for t in ticks_ms:
if scrolling and last_t is not None and (t - last_t) >= stop_delay_ms:
emitted.append(("scrollstop", last_t + stop_delay_ms))
scrolling = False
if not scrolling:
emitted.append(("scrollstart", t))
scrolling = True
last_t = t
if scrolling and last_t is not None:
emitted.append(("scrollstop", last_t + stop_delay_ms))
return emitted
def main() -> None:
cases = []
# Case 1: scrollstart_trigger_sequence
doc = html.fragment_fromstring(
'<div data-role="page"><div class="ui-content"><p>长页面滚动测试</p></div></div>',
create_parent=False,
)
events1 = simulate_scroll_stream([0, 30, 60, 90], stop_delay_ms=150)
actual1 = {
"has_page_container": doc.get("data-role") == "page",
"scrollstart_count": sum(1 for name, _ in events1 if name == "scrollstart"),
"first_event": events1[0],
}
expected1 = {"has_page_container": True, "scrollstart_count": 1, "first_event": ("scrollstart", 0)}
cases.append({
"name": "scrollstart_trigger_sequence",
"summary": "验证连续滚动事件流仅在滚动开始瞬间触发一次 scrollstart,而非每帧重复触发。",
"expected": expected1,
"actual": actual1,
"passed": actual1 == expected1,
})
# Case 2: scrollstop_debounce_sequence
events2 = simulate_scroll_stream([100, 140, 180, 500, 540], stop_delay_ms=150)
actual2 = {"emitted": events2}
expected2 = {
"emitted": [
("scrollstart", 100),
("scrollstop", 330),
("scrollstart", 500),
("scrollstop", 690),
],
}
cases.append({
"name": "scrollstop_debounce_sequence",
"summary": "验证两段独立滚动在静默超时后准确成对触发 scrollstart 与 scrollstop。",
"expected": expected2,
"actual": actual2,
"passed": actual2 == expected2,
})
# Case 3: ios_dom_lock_and_modern_scrollend
legacy_ios_caveat = {
"dom_updates_frozen_during_legacy_ios_scroll": True,
"modern_replacements": ["scroll (passive: true)", "scrollend", "IntersectionObserver"],
}
actual3 = legacy_ios_caveat
expected3 = {
"dom_updates_frozen_during_legacy_ios_scroll": True,
"modern_replacements": ["scroll (passive: true)", "scrollend", "IntersectionObserver"],
}
cases.append({
"name": "ios_dom_lock_and_modern_scrollend",
"summary": "核对早期 iOS 滚动期间冻结 DOM 绘制的兼容提示,以及现代被动 scroll、scrollend 与 IntersectionObserver 方案。",
"expected": expected3,
"actual": actual3,
"passed": actual3 == expected3,
})
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-scroll-lab/explore-events-scroll.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-events-scroll.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联32章) · 含当前章节或实例 · mbskycom202612a2050.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2056.zip