"""Offline Ionic 1 ion-header-bar & ion-footer-bar directive verifier."""
import json
from lxml import html
def resolve_title_align(attr_value, platform: str) -> str:
if attr_value in {"left", "center", "right"}:
return attr_value
return "center" if platform == "ios" else "left"
def simulate_header_tap(scroll_y: int, no_tap_scroll: bool) -> int:
return scroll_y if no_tap_scroll else 0
def main():
cases = []
# Case 1: Parse ion-header-bar + bar-subheader + ion-content
hdr_markup = """
<div>
<ion-header-bar align-title="left" class="bar-positive">
<div class="buttons"><button class="button" ng-click="doSomething()">Left Button</button></div>
<h1 class="title">Title!</h1>
<div class="buttons"><button class="button">Right Button</button></div>
</ion-header-bar>
<ion-header-bar class="bar-subheader bar-light">
<h2 class="title">Subheader</h2>
</ion-header-bar>
<ion-content>Some content!</ion-content>
</div>
"""
doc = html.fromstring(hdr_markup)
bars = doc.xpath("//ion-header-bar")
actual_hdr = {
"bar_count": len(bars),
"primary_align": bars[0].get("align-title"),
"primary_class": bars[0].get("class"),
"primary_title": bars[0].xpath("./h1[@class='title']/text()")[0],
"button_groups": len(bars[0].xpath("./div[@class='buttons']")),
"has_subheader": "bar-subheader" in (bars[1].get("class") or ""),
}
expected_hdr = {
"bar_count": 2,
"primary_align": "left",
"primary_class": "bar-positive",
"primary_title": "Title!",
"button_groups": 2,
"has_subheader": True,
}
cases.append({
"name": "parse_ion_header_bar_and_subheader",
"summary": "解析 ion-header-bar 的 align-title、buttons 分组与 bar-subheader 副标题栏",
"expected": expected_hdr,
"actual": actual_hdr,
"passed": actual_hdr == expected_hdr,
})
# Case 2: Platform default title alignment & no-tap-scroll behavior
actual_api = {
"ios_default": resolve_title_align(None, "ios"),
"android_default": resolve_title_align(None, "android"),
"explicit_right": resolve_title_align("right", "android"),
"tap_scroll_enabled": simulate_header_tap(420, no_tap_scroll=False),
"tap_scroll_disabled": simulate_header_tap(420, no_tap_scroll=True),
}
expected_api = {
"ios_default": "center",
"android_default": "left",
"explicit_right": "right",
"tap_scroll_enabled": 0,
"tap_scroll_disabled": 420,
}
cases.append({
"name": "header_align_and_no_tap_scroll",
"summary": "验证 iOS/Android 默认标题对齐规则与 no-tap-scroll 禁止点顶回滚",
"expected": expected_api,
"actual": actual_api,
"passed": actual_api == expected_api,
})
# Case 3: Parse ion-footer-bar structure and single align-title API
ftr_markup = """
<div>
<ion-content>Some content!</ion-content>
<ion-footer-bar align-title="left" class="bar-assertive">
<div class="buttons"><button class="button">Left Button</button></div>
<h1 class="title">Title!</h1>
<div class="buttons" ng-click="doSomething()"><button class="button">Right Button</button></div>
</ion-footer-bar>
</div>
"""
fdoc = html.fromstring(ftr_markup)
ftr = fdoc.xpath("//ion-footer-bar")[0]
actual_ftr = {
"align_title": ftr.get("align-title"),
"class": ftr.get("class"),
"right_ng_click": ftr.xpath("./div[@class='buttons']")[1].get("ng-click"),
"title": ftr.xpath("./h1[@class='title']/text()")[0],
}
expected_ftr = {
"align_title": "left",
"class": "bar-assertive",
"right_ng_click": "doSomething()",
"title": "Title!",
}
cases.append({
"name": "parse_ion_footer_bar",
"summary": "解析 ion-footer-bar 的 align-title、bar-assertive 样式与左右按钮容器",
"expected": expected_ftr,
"actual": actual_ftr,
"passed": actual_ftr == expected_ftr,
})
# Case 4: Modern Ionic 7/8 <ion-header>/<ion-footer> + <ion-toolbar>
modern = html.fromstring(
'<div>'
'<ion-header><ion-toolbar color="primary">'
'<ion-buttons slot="start"><ion-button>Left</ion-button></ion-buttons>'
'<ion-title>Title!</ion-title>'
'</ion-toolbar></ion-header>'
'<ion-footer><ion-toolbar color="danger"><ion-title>Footer</ion-title></ion-toolbar></ion-footer>'
'</div>'
)
actual_modern = {
"header_toolbar_color": modern.xpath("//ion-header/ion-toolbar/@color")[0],
"buttons_slot": modern.xpath("//ion-buttons/@slot")[0],
"footer_toolbar_color": modern.xpath("//ion-footer/ion-toolbar/@color")[0],
}
expected_modern = {
"header_toolbar_color": "primary",
"buttons_slot": "start",
"footer_toolbar_color": "danger",
}
cases.append({
"name": "modern_header_footer_toolbar",
"summary": "核对现代 Ionic 7/8 <ion-header>/<ion-footer> 与 <ion-toolbar> 槽位映射",
"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-ion-header-bar-lab/explore-ion-header-bar.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-ion-header-bar.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a2013.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2026.zip