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

运行 Bootstrap 4 徽章缩放、8 种语义色、药丸圆角与按钮内嵌 Chromium 实验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-badges.py
explore-badges.py173 行
"""Offline Playwright Chromium verification for Bootstrap 4 Badges."""
import json
from playwright.sync_api import sync_playwright

CSS = """
*, *::before, *::after { box-sizing: border-box; }
body { margin: 16px; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; font-size: 16px; line-height: 1.5; color: #212529; }
h1 { font-size: 2.5rem; margin: 0 0 8px; }
h2 { font-size: 2rem; margin: 0 0 8px; }
h3 { font-size: 1.75rem; margin: 0 0 8px; }
h4 { font-size: 1.5rem; margin: 0 0 8px; }
h5 { font-size: 1.25rem; margin: 0 0 8px; }
h6 { font-size: 1rem; margin: 0 0 8px; }
.badge {
  display: inline-block; padding: 0.25em 0.4em; font-size: 75%; font-weight: 700;
  line-height: 1; text-align: center; white-space: nowrap; vertical-align: baseline;
  border-radius: 0.25rem;
}
.badge-pill { padding-right: 0.6em; padding-left: 0.6em; border-radius: 10rem; }
.badge-primary { color: #fff; background-color: #007bff; }
.badge-secondary { color: #fff; background-color: #6c757d; }
.badge-success { color: #fff; background-color: #28a745; }
.badge-danger { color: #fff; background-color: #dc3545; }
.badge-warning { color: #212529; background-color: #ffc107; }
.badge-info { color: #fff; background-color: #17a2b8; }
.badge-light { color: #212529; background-color: #f8f9fa; }
.badge-dark { color: #fff; background-color: #343a40; }
.btn {
  display: inline-block; padding: 0.375rem 0.75rem; font-size: 1rem; line-height: 1.5;
  border: 1px solid transparent; border-radius: 0.25rem;
}
.btn-primary { color: #fff; background-color: #007bff; border-color: #007bff; }
.btn .badge { position: relative; top: -1px; }
"""

HTML = f"""<!doctype html>
<html lang="zh-CN"><head><meta charset="utf-8"><style>{CSS}</style></head>
<body>
  <div id="headings">
    <h1>测试标题 <span class="badge badge-secondary" id="bh1">New</span></h1>
    <h2>测试标题 <span class="badge badge-secondary" id="bh2">New</span></h2>
    <h3>测试标题 <span class="badge badge-secondary" id="bh3">New</span></h3>
    <h4>测试标题 <span class="badge badge-secondary" id="bh4">New</span></h4>
    <h5>测试标题 <span class="badge badge-secondary" id="bh5">New</span></h5>
    <h6>测试标题 <span class="badge badge-secondary" id="bh6">New</span></h6>
  </div>

  <div id="colors">
    <span class="badge badge-primary" id="c-primary">主要</span>
    <span class="badge badge-secondary" id="c-secondary">次要</span>
    <span class="badge badge-success" id="c-success">成功</span>
    <span class="badge badge-danger" id="c-danger">危险</span>
    <span class="badge badge-warning" id="c-warning">警告</span>
    <span class="badge badge-info" id="c-info">信息</span>
    <span class="badge badge-light" id="c-light">浅色</span>
    <span class="badge badge-dark" id="c-dark">深色</span>
  </div>

  <div id="pills">
    <span class="badge badge-pill badge-default" id="p-default">默认</span>
    <span class="badge badge-pill badge-primary" id="p-primary">主要</span>
    <span class="badge badge-pill badge-success" id="p-success">成功</span>
    <span class="badge badge-pill badge-info" id="p-info">信息</span>
    <span class="badge badge-pill badge-warning" id="p-warning">警告</span>
    <span class="badge badge-pill badge-danger" id="p-danger">危险</span>
  </div>

  <button type="button" class="btn btn-primary" id="btn-msg">
    Messages <span class="badge badge-light" id="btn-badge">4</span>
  </button>
</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():
    with sync_playwright() as p:
        browser = launch_browser(p)
        try:
            page = browser.new_page(viewport={"width": 1000, "height": 800})
            page.set_content(HTML, wait_until="load")

            c1_actual = page.evaluate("""() => {
              const sizes = [1,2,3,4,5,6].map(i => parseFloat(getComputedStyle(document.getElementById('bh'+i)).fontSize));
              const parents = [1,2,3,4,5,6].map(i => parseFloat(getComputedStyle(document.querySelector('#headings h'+i)).fontSize));
              const ratios = sizes.map((s, idx) => Number((s / parents[idx]).toFixed(2)));
              return { badge_px: sizes, parent_px: parents, ratios, strictly_decreasing: sizes.every((v, i) => i === 0 || v < sizes[i-1]) };
            }""")
            c1_exp = {
                "badge_px": [30, 24, 21, 18, 15, 12],
                "parent_px": [40, 32, 28, 24, 20, 16],
                "ratios": [0.75, 0.75, 0.75, 0.75, 0.75, 0.75],
                "strictly_decreasing": True,
            }

            c2_actual = page.evaluate("""() => {
              const keys = ['primary','secondary','success','danger','warning','info','light','dark'];
              const out = {};
              for (const k of keys) {
                const st = getComputedStyle(document.getElementById('c-'+k));
                out[k] = [st.backgroundColor, st.color];
              }
              return out;
            }""")
            c2_exp = {
                "primary": ["rgb(0, 123, 255)", "rgb(255, 255, 255)"],
                "secondary": ["rgb(108, 117, 125)", "rgb(255, 255, 255)"],
                "success": ["rgb(40, 167, 69)", "rgb(255, 255, 255)"],
                "danger": ["rgb(220, 53, 69)", "rgb(255, 255, 255)"],
                "warning": ["rgb(255, 193, 7)", "rgb(33, 37, 41)"],
                "info": ["rgb(23, 162, 184)", "rgb(255, 255, 255)"],
                "light": ["rgb(248, 249, 250)", "rgb(33, 37, 41)"],
                "dark": ["rgb(52, 58, 64)", "rgb(255, 255, 255)"],
            }

            c3_actual = page.evaluate("""() => {
              const std = getComputedStyle(document.getElementById('c-primary'));
              const pill = getComputedStyle(document.getElementById('p-primary'));
              const defPill = getComputedStyle(document.getElementById('p-default'));
              return {
                std_radius: std.borderRadius,
                pill_radius: pill.borderRadius,
                pill_padding_x_gt_std: parseFloat(pill.paddingLeft) > parseFloat(std.paddingLeft),
                badge_default_bg_transparent: defPill.backgroundColor
              };
            }""")
            c3_exp = {
                "std_radius": "4px",
                "pill_radius": "160px",
                "pill_padding_x_gt_std": True,
                "badge_default_bg_transparent": "rgba(0, 0, 0, 0)",
            }

            c4_actual = page.evaluate("""() => {
              const btn = document.getElementById('btn-msg');
              const bd = document.getElementById('btn-badge');
              const rb = btn.getBoundingClientRect(), rbd = bd.getBoundingClientRect();
              const st = getComputedStyle(bd);
              return {
                position: st.position,
                top: st.top,
                bg: st.backgroundColor,
                text: bd.textContent,
                inside_button: rbd.left >= rb.left && rbd.right <= rb.right && rbd.top >= rb.top && rbd.bottom <= rb.bottom
              };
            }""")
            c4_exp = {
                "position": "relative",
                "top": "-1px",
                "bg": "rgb(248, 249, 250)",
                "text": "4",
                "inside_button": True,
            }

            cases = [
                {"name": "heading_badge_scaling", "input": "h1-h6 with .badge.badge-secondary", "expected": c1_exp, "actual": c1_actual, "passed": c1_actual == c1_exp},
                {"name": "contextual_badge_colors", "input": "8 Bootstrap 4 .badge-* color classes", "expected": c2_exp, "actual": c2_actual, "passed": c2_actual == c2_exp},
                {"name": "pill_badges_and_default_note", "input": ".badge-pill vs standard .badge & legacy .badge-default", "expected": c3_exp, "actual": c3_actual, "passed": c3_actual == c3_exp},
                {"name": "badge_inside_button", "input": ".btn.btn-primary containing .badge.badge-light", "expected": c4_exp, "actual": c4_actual, "passed": c4_actual == c4_exp},
            ]
            print(json.dumps({"cases": cases}, ensure_ascii=False))
        finally:
            browser.close()


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

入口文件:projects/bootstrap4-badges-lab/explore-badges.py

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

下载课程关联工程包:Bootstrap4

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

下载课程关联工程包:Bootstrap4

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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