蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线验证 Foundation 5 标签默认盒模型、六种语境色、圆角徽标与自定义字号缩放

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-labels.py
explore-labels.py174 行
"""Offline Playwright Chromium lab for Foundation Ch 08 (labels)."""
import json
from playwright.sync_api import sync_playwright

FOUNDATION_LABEL_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; }
.label {
  font-weight: normal; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif;
  text-align: center; text-decoration: none; line-height: 1; white-space: nowrap;
  display: inline-block; position: relative; padding: 0.25rem 0.5rem 0.25rem;
  font-size: 0.6875rem; background-color: #008CBA; color: #ffffff; border-radius: 0px;
}
.label.secondary { background-color: #e7e7e7; color: #333333; }
.label.success { background-color: #43AC6A; color: #ffffff; }
.label.info { background-color: #a0d3e8; color: #333333; }
.label.warning { background-color: #f08a24; color: #ffffff; }
.label.alert { background-color: #f04124; color: #ffffff; }
.label.radius { border-radius: 3px; }
.label.round { border-radius: 1000px; }
"""

HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{FOUNDATION_LABEL_CSS}</style></head>
<body>
  <h2>Example <span id="lbl-h2" class="label">New</span></h2>
  <h3>Example <span id="lbl-h3" class="label">New</span></h3>
  <h4>Example <span id="lbl-h4" class="label">New</span></h4>

  <div id="color-labels">
    <span id="l-default" class="label">Default Label</span>
    <span id="l-secondary" class="label secondary">Secondary Label</span>
    <span id="l-success" class="label success">Success Label</span>
    <span id="l-info" class="label info">Info Label</span>
    <span id="l-warning" class="label warning">Warning Label</span>
    <span id="l-alert" class="label alert">Alert Label</span>
  </div>

  <div id="corner-labels">
    <span id="c-default" class="label">Default Label</span>
    <span id="c-radius" class="label radius">Radius Label</span>
    <span id="c-round" class="label round">Round Label</span>
    <span id="c-badge" class="label success round">5</span>
  </div>

  <div id="size-labels">
    <h1>Example <span id="sz-36" class="label" style="font-size:36px;">New</span></h1>
    <h2>Example <span id="sz-30" class="label" style="font-size:30px;">New</span></h2>
    <h3>Example <span id="sz-20" class="label" style="font-size:20px;">New</span></h3>
    <h4>Example <span id="sz-def" class="label">New</span></h4>
  </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": 800, "height": 800})
        page.set_content(HTML_PAGE)

        # Case 1: default .label properties inside headings
        actual_1 = page.evaluate("""() => {
            const s = getComputedStyle(document.getElementById('lbl-h2'));
            return {
                display: s.display,
                bg: s.backgroundColor,
                color: s.color,
                font_size: s.fontSize,
                padding: s.paddingTop + ' ' + s.paddingRight,
                white_space: s.whiteSpace
            };
        }""")
        expected_1 = {
            "display": "inline-block",
            "bg": "rgb(0, 140, 186)",
            "color": "rgb(255, 255, 255)",
            "font_size": "11px",
            "padding": "4px 8px",
            "white_space": "nowrap",
        }
        cases.append({
            "name": "label_default_and_heading_nesting",
            "expected": expected_1,
            "actual": actual_1,
            "passed": actual_1 == expected_1,
        })

        # Case 2: 6 contextual label colors
        actual_2 = page.evaluate("""() => {
            const ids = ['l-default','l-secondary','l-success','l-info','l-warning','l-alert'];
            const out = {};
            for (const id of ids) {
                const s = getComputedStyle(document.getElementById(id));
                out[id.slice(2)] = { bg: s.backgroundColor, color: s.color };
            }
            return out;
        }""")
        expected_2 = {
            "default": {"bg": "rgb(0, 140, 186)", "color": "rgb(255, 255, 255)"},
            "secondary": {"bg": "rgb(231, 231, 231)", "color": "rgb(51, 51, 51)"},
            "success": {"bg": "rgb(67, 172, 106)", "color": "rgb(255, 255, 255)"},
            "info": {"bg": "rgb(160, 211, 232)", "color": "rgb(51, 51, 51)"},
            "warning": {"bg": "rgb(240, 138, 36)", "color": "rgb(255, 255, 255)"},
            "alert": {"bg": "rgb(240, 65, 36)", "color": "rgb(255, 255, 255)"},
        }
        cases.append({
            "name": "label_six_color_variants",
            "expected": expected_2,
            "actual": actual_2,
            "passed": actual_2 == expected_2,
        })

        # Case 3: .radius / .round corners & combined .success.round badge
        actual_3 = page.evaluate("""() => ({
            default_radius: getComputedStyle(document.getElementById('c-default')).borderRadius,
            radius_class: getComputedStyle(document.getElementById('c-radius')).borderRadius,
            round_class: getComputedStyle(document.getElementById('c-round')).borderRadius,
            badge_bg: getComputedStyle(document.getElementById('c-badge')).backgroundColor,
            badge_radius: getComputedStyle(document.getElementById('c-badge')).borderRadius
        })""")
        expected_3 = {
            "default_radius": "0px",
            "radius_class": "3px",
            "round_class": "1000px",
            "badge_bg": "rgb(67, 172, 106)",
            "badge_radius": "1000px",
        }
        cases.append({
            "name": "label_radius_and_round_corners",
            "expected": expected_3,
            "actual": actual_3,
            "passed": actual_3 == expected_3,
        })

        # Case 4: custom CSS font-size overrides
        actual_4 = page.evaluate("""() => {
            const ids = ['sz-36', 'sz-30', 'sz-20', 'sz-def'];
            const sizes = ids.map(id => getComputedStyle(document.getElementById(id)).fontSize);
            const heights = ids.map(id => Math.round(document.getElementById(id).getBoundingClientRect().height));
            return {
                font_sizes: sizes,
                heights_strictly_decreasing: heights[0] > heights[1] && heights[1] > heights[2] && heights[2] > heights[3]
            };
        }""")
        expected_4 = {
            "font_sizes": ["36px", "30px", "20px", "11px"],
            "heights_strictly_decreasing": True,
        }
        cases.append({
            "name": "label_custom_css_font_sizes",
            "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-labels-lab/explore-labels.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-labels.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Foundation

课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a739.zip

下载课程关联工程包:Foundation

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a758.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习