"""Offline Ionic 1 ion-tabs, ion-tab & $ionicTabsDelegate verifier."""
import json
from lxml import html
class TabItem:
def __init__(self, title, icon=None, icon_on=None, icon_off=None, href=None,
badge=None, badge_style=None, has_ng_click=False):
self.title = title
self.icon_on = icon_on or icon
self.icon_off = icon_off or icon
self.href = href
self.badge = badge
self.badge_style = badge_style
self.has_ng_click = has_ng_click
self.history_stack = [href or f"#/tab/{title}"]
class IonicTabsController:
"""Models ion-tabs container & $ionicTabsDelegate."""
def __init__(self, handle, tabs):
self.handle = handle
self.tabs = list(tabs)
self._selected = -1
self.events = []
def selected_index(self) -> int:
return self._selected
def select(self, index: int, should_change_history: bool = False):
if not (0 <= index < len(self.tabs)):
return -1
if self._selected == index:
return self._selected
if self._selected >= 0:
self.events.append(f"deselect:{self.tabs[self._selected].title}")
self._selected = index
tab = self.tabs[index]
self.events.append(f"select:{tab.title}:hist={should_change_history}")
return self._selected
def click_tab(self, index: int):
tab = self.tabs[index]
if tab.has_ng_click:
# Ionic 1 rule: when ng-click is set on ion-tab, auto-select is suppressed
self.events.append(f"ng-click-intercept:{tab.title}")
return self._selected
return self.select(index, should_change_history=True)
def active_icon(self, index: int):
tab = self.tabs[index]
return tab.icon_on if index == self._selected else tab.icon_off
class IonicTabsDelegate:
def __init__(self, controllers):
self._map = {c.handle: c for c in controllers}
def get_by_handle(self, handle):
return self._map[handle]
def main():
cases = []
# Case 1: Parse ion-tabs + ion-tab directives & attributes
markup = """
<ion-tabs delegate-handle="my-handle" class="tabs-positive tabs-icon-only">
<ion-tab title="首页" icon-on="ion-ios-filing" icon-off="ion-ios-filing-outline"
href="#/tab/home" badge="3" badge-style="badge-assertive"
on-select="onTabSelected()" on-deselect="onTabDeselected()"></ion-tab>
<ion-tab title="关于" icon="ion-ios-clock" href="#/tab/about"></ion-tab>
<ion-tab title="设置" icon-on="ion-ios-gear" icon-off="ion-ios-gear-outline"
ng-click="customSettingsAction()"></ion-tab>
</ion-tabs>
"""
doc = html.fromstring(markup)
tabs_el = doc.xpath("//ion-tab")
actual_dom = {
"handle": doc.get("delegate-handle"),
"classes": doc.get("class"),
"tab_titles": [t.get("title") for t in tabs_el],
"first_badge": tabs_el[0].get("badge"),
"first_on_select": tabs_el[0].get("on-select"),
"first_on_deselect": tabs_el[0].get("on-deselect"),
"second_icon": tabs_el[1].get("icon"),
"third_ng_click": tabs_el[2].get("ng-click"),
}
expected_dom = {
"handle": "my-handle",
"classes": "tabs-positive tabs-icon-only",
"tab_titles": ["首页", "关于", "设置"],
"first_badge": "3",
"first_on_select": "onTabSelected()",
"first_on_deselect": "onTabDeselected()",
"second_icon": "ion-ios-clock",
"third_ng_click": "customSettingsAction()",
}
cases.append({
"name": "parse_ion_tabs_directives",
"summary": "解析 ion-tabs 的 delegate-handle 与 ion-tab 全部核心属性",
"expected": expected_dom,
"actual": actual_dom,
"passed": actual_dom == expected_dom,
})
# Case 2: $ionicTabsDelegate select / selectedIndex / $getByHandle & icon-on/off fallback
ctrl = IonicTabsController("my-handle", [
TabItem("首页", icon_on="ion-ios-filing", icon_off="ion-ios-filing-outline", href="#/tab/home"),
TabItem("关于", icon="ion-ios-clock", href="#/tab/about"),
TabItem("设置", icon_on="ion-ios-gear", icon_off="ion-ios-gear-outline", has_ng_click=True),
])
delg = IonicTabsDelegate([ctrl])
h = delg.get_by_handle("my-handle")
initial_idx = h.selected_index()
h.select(0, should_change_history=False)
icons_at_0 = [h.active_icon(0), h.active_icon(1), h.active_icon(2)]
h.select(1, should_change_history=True)
icons_at_1 = [h.active_icon(0), h.active_icon(1), h.active_icon(2)]
actual_del = {
"initial_index": initial_idx,
"selected_now": h.selected_index(),
"icons_at_0": icons_at_0,
"icons_at_1": icons_at_1,
"events": list(h.events),
}
expected_del = {
"initial_index": -1,
"selected_now": 1,
"icons_at_0": ["ion-ios-filing", "ion-ios-clock", "ion-ios-gear-outline"],
"icons_at_1": ["ion-ios-filing-outline", "ion-ios-clock", "ion-ios-gear-outline"],
"events": ["select:首页:hist=False", "deselect:首页", "select:关于:hist=True"],
}
cases.append({
"name": "tabs_delegate_select_and_icons",
"summary": "验证 $ionicTabsDelegate.select/selectedIndex、icon 默认回退与 on-select/on-deselect",
"expected": expected_del,
"actual": actual_del,
"passed": actual_del == expected_del,
})
# Case 3: ng-click suppresses default tab selection until manual delegate.select()
idx_after_ng_click = h.click_tab(2)
idx_after_manual = h.select(2, should_change_history=False)
actual_ng = {
"after_ng_click_still_1": idx_after_ng_click,
"after_manual_select_2": idx_after_manual,
"last_events": h.events[-3:],
}
expected_ng = {
"after_ng_click_still_1": 1,
"after_manual_select_2": 2,
"last_events": ["ng-click-intercept:设置", "deselect:关于", "select:设置:hist=False"],
}
cases.append({
"name": "tab_ng_click_override_behavior",
"summary": "验证设置 ng-click 后点击不自动切换,须显式调用 $ionicTabsDelegate.select()",
"expected": expected_ng,
"actual": actual_ng,
"passed": actual_ng == expected_ng,
})
# Case 4: Platform default tab bar placement (iOS bottom vs Android top) & modern Ionic 7/8 <ion-tab-bar>
placement = {"ios": "bottom", "android": "top"}
modern = html.fromstring(
'<ion-tabs><ion-tab-bar slot="bottom">'
'<ion-tab-button tab="home"><ion-badge color="danger">3</ion-badge></ion-tab-button>'
'</ion-tab-bar></ion-tabs>'
)
actual_modern = {
"ionic1_default_placement": placement,
"modern_slot": modern.xpath("//ion-tab-bar/@slot")[0],
"modern_tab": modern.xpath("//ion-tab-button/@tab")[0],
"modern_badge": modern.xpath("//ion-badge/text()")[0],
}
expected_modern = {
"ionic1_default_placement": {"ios": "bottom", "android": "top"},
"modern_slot": "bottom",
"modern_tab": "home",
"modern_badge": "3",
}
cases.append({
"name": "platform_placement_and_modern_tabs",
"summary": "核对 Ionic 1 iOS/Android 默认选项卡位置差异与现代 Ionic 7/8 ion-tab-bar/ion-badge",
"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-tabs-lab/explore-ion-tabs.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-ion-tabs.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a2013.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2035.zip