"""Offline Ionic 1 Button variants, sizing, icons, bar buttons & button-bar verifier for Ch05 (button)."""
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_color_and_width_buttons() -> dict:
markup = "<section><button class='button'>Default</button>" + "".join(
f"<button class='button button-{t}'>button-{t}</button>" for t in THEMES
) + """
<button class="button button-block button-positive">Block Button</button>
<button class="button button-full button-positive">Full Width Block Button</button>
</section>
"""
root = html.fragment_fromstring(markup)
btns = root.xpath("./button")
theme_btns = [b for b in btns[:10]]
block_cls = btns[10].get("class", "").split()
full_cls = btns[11].get("class", "").split()
def button_layout_model(classes: list[str]) -> dict:
if "button-full" in classes:
return {"display": "block", "width": "100%", "side_margin": False, "border_radius": 0}
if "button-block" in classes:
return {"display": "block", "width": "100%", "side_margin": True, "border_radius": 2}
return {"display": "inline-block", "width": "auto", "side_margin": False, "border_radius": 2}
actual = {
"default_plus_theme_count": len(theme_btns),
"default_display": button_layout_model(theme_btns[0].get("class", "").split())["display"],
"block_model": button_layout_model(block_cls),
"full_model": button_layout_model(full_cls),
}
expected = {
"default_plus_theme_count": 10,
"default_display": "inline-block",
"block_model": {"display": "block", "width": "100%", "side_margin": True, "border_radius": 2},
"full_model": {"display": "block", "width": "100%", "side_margin": False, "border_radius": 0},
}
return make_case(
"button_themes_block_and_full",
"Verify default inline-block + 9 theme buttons and display/margin differences of button-block vs button-full",
expected,
actual,
)
def case_size_outline_clear() -> dict:
markup = """
<section>
<button class="button button-small button-assertive">Small Button</button>
<button class="button button-large button-positive">Large Button</button>
<button class="button button-outline button-positive">Outlined Button</button>
<button class="button button-clear button-positive">Clear Button</button>
</section>
"""
root = html.fragment_fromstring(markup)
styles = []
for btn in root.xpath("./button"):
cls = set(btn.get("class", "").split())
styles.append({
"size": "small" if "button-small" in cls else ("large" if "button-large" in cls else "default"),
"transparent_bg": bool({"button-outline", "button-clear"} & cls),
"has_border": "button-clear" not in cls,
})
actual = styles
expected = [
{"size": "small", "transparent_bg": False, "has_border": True},
{"size": "large", "transparent_bg": False, "has_border": True},
{"size": "default", "transparent_bg": True, "has_border": True},
{"size": "default", "transparent_bg": True, "has_border": False},
]
return make_case(
"button_sizes_outline_and_clear",
"Verify button-small, button-large, button-outline (transparent + border), and button-clear (transparent + no border)",
expected,
actual,
)
def case_icon_buttons() -> dict:
markup = """
<section>
<button class="button icon-left ion-home">Home</button>
<button class="button icon-left ion-star button-positive">Favorites</button>
<a class="button icon-right ion-chevron-right button-calm">Learn More</a>
<a class="button icon-left ion-chevron-left button-clear button-dark">Back</a>
<button class="button icon ion-gear-a"></button>
<a class="button button-icon icon ion-settings"></a>
<a class="button button-outline icon-right ion-navicon button-balanced">Reorder</a>
</section>
"""
root = html.fragment_fromstring(markup)
els = root.xpath("./*")
positions = []
icons = []
for el in els:
cls = el.get("class", "").split()
pos = "left" if "icon-left" in cls else ("right" if "icon-right" in cls else "only")
positions.append(pos)
icons.append([c for c in cls if c.startswith("ion-")][0])
actual = {
"count": len(els),
"positions": positions,
"icons": icons,
"button_icon_count": sum("button-icon" in e.get("class", "").split() for e in els),
}
expected = {
"count": 7,
"positions": ["left", "left", "right", "left", "only", "only", "right"],
"icons": [
"ion-home",
"ion-star",
"ion-chevron-right",
"ion-chevron-left",
"ion-gear-a",
"ion-settings",
"ion-navicon",
],
"button_icon_count": 1,
}
return make_case(
"icon_button_positions_and_classes",
"Parse 7 icon button patterns (icon-left, icon-right, icon-only, button-icon)",
expected,
actual,
)
def case_header_buttons_and_button_bar() -> dict:
markup = """
<section>
<div class="bar bar-header">
<button class="button icon ion-navicon"></button>
<h1 class="title">Header Buttons</h1>
<button class="button">Edit</button>
</div>
<div class="bar bar-header">
<button class="button button-icon icon ion-navicon"></button>
<div class="h1 title">Header Buttons</div>
<button class="button button-clear button-positive">Edit</button>
</div>
<div class="button-bar">
<a class="button">First</a>
<a class="button">Second</a>
<a class="button">Third</a>
</div>
</section>
"""
root = html.fragment_fromstring(markup)
hdr1_btns = len(root.xpath("./div[1]/button"))
hdr2_clear = "button-clear" in root.xpath("./div[2]/button[2]/@class")[0].split()
bar_labels = [a.text_content().strip() for a in root.xpath("./div[@class='button-bar']/a[@class='button']")]
actual = {
"header1_button_count": hdr1_btns,
"header2_right_is_clear": hdr2_clear,
"button_bar_items": bar_labels,
"button_bar_equal_flex": len(bar_labels) == 3,
}
expected = {
"header1_button_count": 2,
"header2_right_is_clear": True,
"button_bar_items": ["First", "Second", "Third"],
"button_bar_equal_flex": True,
}
return make_case(
"header_buttons_and_button_bar",
"Verify bar-header embedded buttons, button-clear variant, and 3-item equal-width button-bar",
expected,
actual,
)
def main() -> None:
cases = [
case_color_and_width_buttons(),
case_size_outline_clear(),
case_icon_buttons(),
case_header_buttons_and_button_bar(),
]
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-button-lab/explore-button.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-button.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a2013.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2014.zip