"""Offline Playwright Chromium lab for Foundation Ch 07 (icons)."""
import json
from playwright.sync_api import sync_playwright
FOUNDATION_ICON_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 0; padding: 16px; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; color: #222222; }
.wrap { width: 800px; }
[class^="fi-"], [class*=" fi-"] { display: inline-block; font-style: normal; font-weight: normal; line-height: 1; vertical-align: middle; }
.fi-refresh::before { content: "\\21BB"; }
.fi-check::before { content: "\\2713"; }
.fi-home::before { content: "\\2302"; }
.fi-cloud::before { content: "\\2601"; }
.fi-bookmark::before { content: "\\2605"; }
.fi-info::before { content: "\\24D8"; }
.fi-mail::before { content: "\\2709"; }
.fi-like::before { content: "\\2665"; }
.fi-share::before { content: "\\21AA"; }
.fi-magnifying-glass::before { content: "\\2315"; }
.button { display: inline-block; padding: 1rem 2rem; font-size: 1rem; background: #008CBA; color: #ffffff; border: 0; text-decoration: none; cursor: pointer; }
.button.success { background: #43AC6A; }
.button.info { background: #a0d3e8; color: #333333; }
.button.large { padding: 1.125rem 2.25rem; font-size: 1.25rem; }
.icon-bar { width: 100%; font-size: 0; display: inline-block; background: #333333; margin-bottom: 16px; }
.icon-bar > .item { text-align: center; font-size: 1rem; width: 25%; margin: 0 auto; display: block; padding: 1.25rem; float: left; text-decoration: none; }
.icon-bar.five-up > .item { width: 20%; }
.icon-bar .item i { display: block; font-size: 1.875rem; color: #ffffff; margin: 0 auto 0.25rem; }
.icon-bar .item label { display: block; font-size: 1rem; color: #ffffff; margin: 0; cursor: pointer; }
.icon-bar .item.disabled { opacity: 0.7; pointer-events: none; cursor: default; }
.icon-bar.vertical { width: 7rem; }
.icon-bar.vertical > .item { width: 100%; float: none; }
"""
HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{FOUNDATION_ICON_CSS}</style></head>
<body>
<div class="wrap">
<p>刷新: <i id="ic-ref-1" class="fi-refresh"></i> <i id="ic-ref-20" class="fi-refresh" style="font-size:20px;"></i> <i id="ic-ref-30" class="fi-refresh" style="font-size:30px;color:red"></i></p>
<p>检测: <i id="ic-chk" class="fi-check"></i> 主页: <i id="ic-home" class="fi-home"></i></p>
<p>Cloud: <i id="ic-cloud-def" class="fi-cloud"></i> <a id="ic-cloud-link" href="#"><i class="fi-cloud"></i></a> <i id="ic-cloud-styled" class="fi-cloud" style="font-size:35px;color:red;"></i></p>
<p>
<button id="btn-home" type="button" class="button"><i class="fi-home"></i> Home</button>
<button id="btn-home-ok" type="button" class="button success"><i class="fi-home"></i> Home</button>
<a id="btn-home-info" href="#" class="button info large"><i class="fi-home"></i> Home</a>
</p>
<div id="bar-five" class="icon-bar five-up">
<a href="#" class="item"><i class="fi-home"></i></a>
<a href="#" class="item"><i class="fi-bookmark"></i></a>
<a href="#" class="item"><i class="fi-info"></i></a>
<a href="#" class="item"><i class="fi-mail"></i></a>
<a href="#" class="item"><i class="fi-like"></i></a>
</div>
<div id="bar-labeled" class="icon-bar five-up">
<a href="#" class="item"><i class="fi-home"></i><label>Home</label></a>
<a href="#" class="item disabled" id="item-dis-share"><i class="fi-share"></i><label>Share</label></a>
<a href="#" class="item"><i class="fi-info"></i><label>Info</label></a>
<a href="#" class="item disabled" id="item-dis-mail"><i class="fi-mail"></i><label>Mail</label></a>
<a href="#" class="item"><i class="fi-magnifying-glass"></i><label>Search</label></a>
</div>
<div id="bar-vertical" class="icon-bar vertical five-up">
<a href="#" class="item"><i class="fi-home"></i><label>Home</label></a>
<a href="#" class="item"><i class="fi-bookmark"></i><label>Bookmark</label></a>
<a href="#" class="item"><i class="fi-info"></i><label>Info</label></a>
<a href="#" class="item"><i class="fi-mail"></i><label>Mail</label></a>
<a href="#" class="item"><i class="fi-like"></i><label>Like</label></a>
</div>
</div>
</body>
</html>
"""
def launch_browser(p):
try:
return p.chromium.launch(headless=True)
except Exception:
return p.chromium.launch(headless=True, channel="chrome")
def main():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page(viewport={"width": 900, "height": 1200})
page.set_content(HTML_PAGE)
# Case 1: fi-* icon syntax, font-size scaling & color styling
actual_1 = page.evaluate("""() => {
const g = (id) => {
const s = getComputedStyle(document.getElementById(id));
return { font_size: s.fontSize, color: s.color };
};
return {
refresh_default: g('ic-ref-1'),
refresh_20: g('ic-ref-20'),
refresh_30_red: g('ic-ref-30'),
cloud_35_red: g('ic-cloud-styled')
};
}""")
expected_1 = {
"refresh_default": {"font_size": "16px", "color": "rgb(34, 34, 34)"},
"refresh_20": {"font_size": "20px", "color": "rgb(34, 34, 34)"},
"refresh_30_red": {"font_size": "30px", "color": "rgb(255, 0, 0)"},
"cloud_35_red": {"font_size": "35px", "color": "rgb(255, 0, 0)"},
}
cases.append({
"name": "icon_syntax_and_inline_styling",
"expected": expected_1,
"actual": actual_1,
"passed": actual_1 == expected_1,
})
# Case 2: icons inside buttons and links inherit color/size
actual_2 = page.evaluate("""() => {
const b1 = getComputedStyle(document.querySelector('#btn-home i'));
const b2 = getComputedStyle(document.querySelector('#btn-home-ok'));
const b3 = getComputedStyle(document.querySelector('#btn-home-info i'));
return {
btn_icon_color: b1.color,
success_btn_bg: b2.backgroundColor,
info_large_icon_size: b3.fontSize,
info_large_icon_color: b3.color
};
}""")
expected_2 = {
"btn_icon_color": "rgb(255, 255, 255)",
"success_btn_bg": "rgb(67, 172, 106)",
"info_large_icon_size": "20px",
"info_large_icon_color": "rgb(51, 51, 51)",
}
cases.append({
"name": "icons_inside_buttons_and_links",
"expected": expected_2,
"actual": actual_2,
"passed": actual_2 == expected_2,
})
# Case 3: .icon-bar.five-up horizontal widths & <label> layout
actual_3 = page.evaluate("""() => {
const items = [...document.querySelectorAll('#bar-five > .item')].map(el => Math.round(el.getBoundingClientRect().width));
const lbl = document.querySelector('#bar-labeled .item label');
const ic = document.querySelector('#bar-labeled .item i');
const barBg = getComputedStyle(document.getElementById('bar-five')).backgroundColor;
return {
bar_bg: barBg,
five_up_widths: items,
icon_font_size: getComputedStyle(ic).fontSize,
label_display: getComputedStyle(lbl).display,
label_below_icon: lbl.getBoundingClientRect().top >= ic.getBoundingClientRect().bottom
};
}""")
expected_3 = {
"bar_bg": "rgb(51, 51, 51)",
"five_up_widths": [160, 160, 160, 160, 160],
"icon_font_size": "30px",
"label_display": "block",
"label_below_icon": True,
}
cases.append({
"name": "icon_bar_five_up_and_labels",
"expected": expected_3,
"actual": actual_3,
"passed": actual_3 == expected_3,
})
# Case 4: .disabled items and .vertical icon bar
actual_4 = page.evaluate("""() => {
const dis = getComputedStyle(document.getElementById('item-dis-share'));
const vbar = document.getElementById('bar-vertical').getBoundingClientRect();
const vitems = [...document.querySelectorAll('#bar-vertical > .item')].map(el => el.getBoundingClientRect());
return {
disabled_opacity: dis.opacity,
disabled_pointer_events: dis.pointerEvents,
disabled_cursor: dis.cursor,
vertical_bar_width: Math.round(vbar.width),
vertical_item_count: vitems.length,
vertical_stacked: vitems[1].top > vitems[0].top && vitems[4].top > vitems[3].top,
vertical_item_width: Math.round(vitems[0].width)
};
}""")
expected_4 = {
"disabled_opacity": "0.7",
"disabled_pointer_events": "none",
"disabled_cursor": "default",
"vertical_bar_width": 112,
"vertical_item_count": 5,
"vertical_stacked": True,
"vertical_item_width": 112,
}
cases.append({
"name": "icon_bar_disabled_and_vertical",
"expected": expected_4,
"actual": actual_4,
"passed": actual_4 == expected_4,
})
browser.close()
print(json.dumps({"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/foundation-icons-lab/explore-icons.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-icons.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a739.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a754.zip