"""Offline Ionic 1 $ionicBackdrop directive & ref-counting service verifier."""
import json
from lxml import html
class BackdropService:
"""Models Ionic 1 $ionicBackdrop reference-counted overlay visibility."""
def __init__(self):
self.holds = 0
self.events = []
@property
def visible(self) -> bool:
return self.holds > 0
def retain(self) -> int:
was_visible = self.visible
self.holds += 1
if not was_visible:
self.events.append("backdrop.shown")
return self.holds
def release(self) -> int:
if self.holds > 0:
self.holds -= 1
if self.holds == 0:
self.events.append("backdrop.hidden")
return self.holds
def main():
cases = []
# Case 1: Parse HTML template with ion-pane / ion-content / ng-click
markup = (
'<body ng-app="starter" ng-controller="backdropCtl">'
'<ion-pane><ion-content>'
'<h2 ng-click="action()">$ionicBackdrop</h2>'
'</ion-content></ion-pane></body>'
)
doc = html.document_fromstring(markup)
body = doc.xpath("//body")[0]
h2 = doc.xpath("//ion-pane/ion-content/h2")[0]
actual_dom = {
"ng_app": body.get("ng-app"),
"ng_controller": body.get("ng-controller"),
"has_ion_pane": len(doc.xpath("//ion-pane")) == 1,
"has_ion_content": len(doc.xpath("//ion-content")) == 1,
"trigger_expr": h2.get("ng-click"),
"trigger_text": h2.text_content().strip(),
}
expected_dom = {
"ng_app": "starter",
"ng_controller": "backdropCtl",
"has_ion_pane": True,
"has_ion_content": True,
"trigger_expr": "action()",
"trigger_text": "$ionicBackdrop",
}
cases.append({
"name": "parse_backdrop_template",
"summary": "解析 ion-pane / ion-content 布局及 ng-click 触发器表达式",
"expected": expected_dom,
"actual": actual_dom,
"passed": actual_dom == expected_dom,
})
# Case 2: Reference counting retain/release lifecycle
svc = BackdropService()
trace = []
svc.retain()
trace.append((svc.holds, svc.visible))
svc.retain()
trace.append((svc.holds, svc.visible))
svc.release()
trace.append((svc.holds, svc.visible))
svc.release()
trace.append((svc.holds, svc.visible))
# Extra release should clamp at 0 without underflow
svc.release()
trace.append((svc.holds, svc.visible))
actual_ref = {"trace": trace, "events": svc.events}
expected_ref = {
"trace": [(1, True), (2, True), (1, True), (0, False), (0, False)],
"events": ["backdrop.shown", "backdrop.hidden"],
}
cases.append({
"name": "backdrop_ref_counting",
"summary": "验证 $ionicBackdrop.retain() / release() 引用计数与显示隐藏事件",
"expected": expected_ref,
"actual": actual_ref,
"passed": actual_ref == expected_ref,
})
# Case 3: Simulated $timeout auto-release workflow (1000ms)
svc2 = BackdropService()
scheduled = []
def timeout_schedule(delay_ms, fn):
scheduled.append((delay_ms, fn))
svc2.retain()
timeout_schedule(1000, svc2.release)
before_tick = svc2.visible
delay_ms, cb = scheduled[0]
cb()
after_tick = svc2.visible
actual_timer = {"delay_ms": delay_ms, "before_tick": before_tick, "after_tick": after_tick, "holds": svc2.holds}
expected_timer = {"delay_ms": 1000, "before_tick": True, "after_tick": False, "holds": 0}
cases.append({
"name": "timeout_auto_release",
"summary": "验证 retain 后经 $timeout(1000ms) 自动调用 release 关闭背景层",
"expected": expected_timer,
"actual": actual_timer,
"passed": actual_timer == expected_timer,
})
# Case 4: Modern Ionic 7/8 <ion-backdrop> Web Component equivalence
modern_markup = '<ion-backdrop visible="true" tappable="true" stop-propagation="true"></ion-backdrop>'
m_el = html.fromstring(modern_markup)
actual_modern = {
"tag": m_el.tag,
"visible": m_el.get("visible") == "true",
"tappable": m_el.get("tappable") == "true",
"stop_propagation": m_el.get("stop-propagation") == "true",
}
expected_modern = {
"tag": "ion-backdrop",
"visible": True,
"tappable": True,
"stop_propagation": True,
}
cases.append({
"name": "modern_ion_backdrop_wc",
"summary": "核对现代 Ionic 7/8 <ion-backdrop> Web Component 属性映射",
"expected": expected_modern,
"actual": actual_modern,
"passed": actual_modern == expected_modern,
})
print(json.dumps({
"version": "CPython 3.12.14 / lxml offline Ionic JS directive & service 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/ionic-ionicbackdrop-lab/explore-ionicbackdrop.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-ionicbackdrop.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a2013.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2037.zip