"""Offline Ionic 1 Header, Sub Header & Footer bar CSS/DOM verifier for Ch04 (header-footer)."""
from __future__ import annotations
import json
from lxml import html
VERSION = "CPython 3.12.14 / lxml offline Ionic UI & component verifier"
THEMES = ["light", "stable", "positive", "calm", "balanced", "energized", "assertive", "royal", "dark"]
def make_case(name: str, summary: str, expected, actual) -> dict:
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_nine_header_themes() -> dict:
blocks = "\n".join(
f'<div class="bar bar-header bar-{t}"><h1 class="title">bar-{t}</h1></div>'
for t in THEMES
)
root = html.fragment_fromstring(f"<section>{blocks}</section>")
headers = root.xpath("./div")
extracted = []
for div in headers:
cls = set(div.get("class", "").split())
title = div.xpath("./h1[@class='title']/text()")[0].strip()
assert {"bar", "bar-header"} <= cls
extracted.append(title)
actual = {
"header_count": len(headers),
"theme_classes": extracted,
"all_have_bar_header": all("bar-header" in d.get("class", "").split() for d in headers),
}
expected = {
"header_count": 9,
"theme_classes": [f"bar-{t}" for t in THEMES],
"all_have_bar_header": True,
}
return make_case(
"header_nine_theme_bars",
"Verify all 9 Ionic bar-header theme classes (bar-light through bar-dark) and .title structure",
expected,
actual,
)
def case_subheader_offset_rules() -> dict:
markup = """
<section>
<div class="bar bar-header"><h1 class="title">Header</h1></div>
<div class="bar bar-subheader"><h2 class="title">Sub Header</h2></div>
</section>
"""
root = html.fragment_fromstring(markup)
# Ionic 1 CSS geometry constants: header height = 44px, bar-subheader top = 44px
header_height_px = 44
subheader_top_px = 44
standalone_subheader_top_px = 44 # even without bar-header present
bars = [d.get("class") for d in root.xpath("./div")]
titles = [t.strip() for t in root.xpath("./div/*[@class='title']/text()")]
actual = {
"bar_classes": bars,
"titles": titles,
"subheader_top_with_header_px": subheader_top_px,
"subheader_top_without_header_px": standalone_subheader_top_px,
"content_offset_class": "has-header has-subheader",
"combined_top_offset_px": header_height_px * 2,
}
expected = {
"bar_classes": ["bar bar-header", "bar bar-subheader"],
"titles": ["Header", "Sub Header"],
"subheader_top_with_header_px": 44,
"subheader_top_without_header_px": 44,
"content_offset_class": "has-header has-subheader",
"combined_top_offset_px": 88,
}
return make_case(
"subheader_stacking_and_offset",
"Verify bar-header + bar-subheader DOM structure and 44px fixed top offset rule",
expected,
actual,
)
def case_footer_variants_and_pull_right() -> dict:
markup = """
<section>
<div class="bar bar-footer bar-balanced">
<div class="title">Footer</div>
</div>
<div class="bar bar-footer">
<button class="button button-clear">Left</button>
<div class="title">Title</div>
<button class="button button-clear">Right</button>
</div>
<div class="bar bar-footer">
<button class="button button-clear pull-right">Right</button>
</div>
</section>
"""
root = html.fragment_fromstring(markup)
footers = root.xpath("./div")
f1_cls = footers[0].get("class", "").split()
f2_btns = [b.text_content().strip() for b in footers[1].xpath("./button")]
f3_has_title = bool(footers[2].xpath("./*[@class='title']"))
f3_btn_cls = footers[2].xpath("./button/@class")[0].split()
actual = {
"footer_count": len(footers),
"first_theme": [c for c in f1_cls if c.startswith("bar-") and c != "bar-footer"][0],
"second_buttons": f2_btns,
"third_has_title": f3_has_title,
"third_uses_pull_right": "pull-right" in f3_btn_cls,
}
expected = {
"footer_count": 3,
"first_theme": "bar-balanced",
"second_buttons": ["Left", "Right"],
"third_has_title": False,
"third_uses_pull_right": True,
}
return make_case(
"footer_title_buttons_and_pull_right",
"Verify bar-footer balanced theme, dual button-clear layout, and titleless pull-right alignment",
expected,
actual,
)
def case_modern_toolbar_slot_mapping() -> dict:
mapping = {
"bar-header": "<ion-header><ion-toolbar>",
"bar-subheader": "second <ion-toolbar> inside <ion-header>",
"bar-footer": "<ion-footer><ion-toolbar>",
"pull-right": '<ion-buttons slot="end">',
}
actual = {
"mapped_count": len(mapping),
"pull_right_replacement": mapping["pull-right"],
}
expected = {
"mapped_count": 4,
"pull_right_replacement": '<ion-buttons slot="end">',
}
return make_case(
"modern_ionic_header_footer_mapping",
"Verify Ionic 1 bar-header/bar-footer/pull-right mapping to Ionic 7/8 ion-toolbar slots",
expected,
actual,
)
def main() -> None:
cases = [
case_nine_header_themes(),
case_subheader_offset_rules(),
case_footer_variants_and_pull_right(),
case_modern_toolbar_slot_mapping(),
]
print(json.dumps({"version": VERSION, "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-header-footer-lab/explore-header-footer.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-header-footer.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a2013.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2022.zip