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

离线验证 Foundation 5 按钮色彩、多标签适配、四档尺寸、圆角延展与禁用行为

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

FOUNDATION_BTN_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; }
.container { width: 400px; }
button, .button {
  display: inline-block; position: relative; text-align: center; text-decoration: none; cursor: pointer;
  font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; font-weight: normal; line-height: normal;
  margin: 0 0 1.25rem; border: 0 solid #007095; padding: 1rem 2rem 1.0625rem 2rem; font-size: 1rem;
  background-color: #008CBA; color: #ffffff; border-radius: 0px;
}
.button.secondary { background-color: #e7e7e7; color: #333333; }
.button.success { background-color: #43AC6A; color: #ffffff; }
.button.info { background-color: #a0d3e8; color: #333333; }
.button.warning { background-color: #f08a24; color: #ffffff; }
.button.alert { background-color: #f04124; color: #ffffff; }
.button.large { padding: 1.125rem 2.25rem 1.1875rem 2.25rem; font-size: 1.25rem; }
.button.small { padding: 0.875rem 1.75rem 0.9375rem 1.75rem; font-size: 0.8125rem; }
.button.tiny { padding: 0.625rem 1.25rem 0.6875rem 1.25rem; font-size: 0.6875rem; }
.button.radius { border-radius: 3px; }
.button.round { border-radius: 1000px; }
.button.expand { padding-left: 0; padding-right: 0; width: 100%; }
.button.disabled, .button[disabled] { cursor: default; opacity: 0.7; box-shadow: none; }
"""

HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{FOUNDATION_BTN_CSS}</style></head>
<body>
  <div class="container">
    <div id="styles-box">
      <button id="b-default" type="button" class="button">Default</button>
      <button id="b-secondary" type="button" class="button secondary">Secondary</button>
      <button id="b-success" type="button" class="button success">Success</button>
      <button id="b-info" type="button" class="button info">Info</button>
      <button id="b-warning" type="button" class="button warning">Warning</button>
      <button id="b-alert" type="button" class="button alert">Alert</button>
    </div>
    <div id="elements-box">
      <a id="el-a" href="#" class="button info" role="button">Link Button</a>
      <button id="el-button" type="button" class="button info">Button</button>
      <input id="el-input-btn" type="button" class="button info" value="Input Button">
      <input id="el-input-submit" type="submit" class="button info" value="Submit Button">
    </div>
    <div id="sizes-box">
      <button id="s-large" type="button" class="button large">Large</button>
      <button id="s-default" type="button" class="button">Default</button>
      <button id="s-small" type="button" class="button small">Small</button>
      <button id="s-tiny" type="button" class="button tiny">Tiny</button>
    </div>
    <div id="corners-box">
      <button id="c-default" type="button" class="button">Default</button>
      <button id="c-radius" type="button" class="button radius">Radius</button>
      <button id="c-round" type="button" class="button round">Round</button>
    </div>
    <div id="expand-box">
      <button id="e-default" type="button" class="button">Default</button>
      <button id="e-expand" type="button" class="button expand">Expanded</button>
    </div>
    <div id="disabled-box">
      <button id="d-class" type="button" class="button disabled">Class Disabled</button>
      <button id="d-attr" type="button" class="button disabled" disabled>Attr Disabled</button>
    </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": 800, "height": 900})
        page.set_content(HTML_PAGE)

        # Case 1: 6 button color variants
        actual_1 = page.evaluate("""() => {
            const ids = ['b-default','b-secondary','b-success','b-info','b-warning','b-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_1 = {
            "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": "six_button_color_styles",
            "expected": expected_1,
            "actual": actual_1,
            "passed": actual_1 == expected_1,
        })

        # Case 2: <a>, <button>, <input type="button|submit"> parity & href="#"
        actual_2 = page.evaluate("""() => {
            const ids = ['el-a', 'el-button', 'el-input-btn', 'el-input-submit'];
            return {
                bgs: ids.map(id => getComputedStyle(document.getElementById(id)).backgroundColor),
                displays: ids.map(id => getComputedStyle(document.getElementById(id)).display),
                a_href: document.getElementById('el-a').getAttribute('href'),
                a_role: document.getElementById('el-a').getAttribute('role')
            };
        }""")
        expected_2 = {
            "bgs": ["rgb(160, 211, 232)"] * 4,
            "displays": ["inline-block"] * 4,
            "a_href": "#",
            "a_role": "button",
        }
        cases.append({
            "name": "button_across_a_button_input_elements",
            "expected": expected_2,
            "actual": actual_2,
            "passed": actual_2 == expected_2,
        })

        # Case 3: large / default / small / tiny sizes
        actual_3 = page.evaluate("""() => {
            const ids = ['s-large', 's-default', 's-small', 's-tiny'];
            const out = {};
            for (const id of ids) {
                const s = getComputedStyle(document.getElementById(id));
                out[id.slice(2)] = { font_size: s.fontSize, pad_top: s.paddingTop, pad_right: s.paddingRight };
            }
            return out;
        }""")
        expected_3 = {
            "large": {"font_size": "20px", "pad_top": "18px", "pad_right": "36px"},
            "default": {"font_size": "16px", "pad_top": "16px", "pad_right": "32px"},
            "small": {"font_size": "13px", "pad_top": "14px", "pad_right": "28px"},
            "tiny": {"font_size": "11px", "pad_top": "10px", "pad_right": "20px"},
        }
        cases.append({
            "name": "button_size_classes",
            "expected": expected_3,
            "actual": actual_3,
            "passed": actual_3 == expected_3,
        })

        # Case 4: radius / round corners and .expand 100% width
        actual_4 = 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,
            expand_width: Math.round(document.getElementById('e-expand').getBoundingClientRect().width),
            default_narrower: document.getElementById('e-default').getBoundingClientRect().width < 400
        })""")
        expected_4 = {
            "default_radius": "0px",
            "radius_class": "3px",
            "round_class": "1000px",
            "expand_width": 400,
            "default_narrower": True,
        }
        cases.append({
            "name": "button_corners_and_expand_width",
            "expected": expected_4,
            "actual": actual_4,
            "passed": actual_4 == expected_4,
        })

        # Case 5: .disabled style vs native disabled attribute
        actual_5 = page.evaluate("""() => {
            const dc = document.getElementById('d-class');
            const da = document.getElementById('d-attr');
            let classClicks = 0, attrClicks = 0;
            dc.addEventListener('click', () => classClicks++);
            da.addEventListener('click', () => attrClicks++);
            dc.click();
            da.click();
            const sc = getComputedStyle(dc);
            return {
                opacity: sc.opacity,
                cursor: sc.cursor,
                class_only_click_fired: classClicks,
                native_disabled_click_fired: attrClicks
            };
        }""")
        expected_5 = {
            "opacity": "0.7",
            "cursor": "default",
            "class_only_click_fired": 1,
            "native_disabled_click_fired": 0,
        }
        cases.append({
            "name": "button_disabled_class_vs_attribute",
            "expected": expected_5,
            "actual": actual_5,
            "passed": actual_5 == expected_5,
        })

        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-buttons-lab/explore-buttons.py

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

下载课程关联工程包:Foundation

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

下载课程关联工程包:Foundation

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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