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

离线验证 Foundation 5 全局字号、响应式标题、语义标签与代码排版计算样式

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

FOUNDATION_TYPO_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 100%; }
body { margin: 0; padding: 16px; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; font-size: 1rem; line-height: 1.5; color: #222222; background: #ffffff; }
h1, h2, h3, h4, h5, h6 { font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; font-weight: normal; color: #222222; line-height: 1.4; margin-top: 0.2rem; margin-bottom: 0.5rem; }
h1 { font-size: 2.125rem; }
h2 { font-size: 1.6875rem; }
h3 { font-size: 1.375rem; }
h4 { font-size: 1.125rem; }
h5 { font-size: 1.125rem; }
h6 { font-size: 1rem; }
@media only screen and (min-width: 40.0625em) {
  h1 { font-size: 2.75rem; }
  h2 { font-size: 2.3125rem; }
  h3 { font-size: 1.6875rem; }
  h4 { font-size: 1.4375rem; }
  h5 { font-size: 1.125rem; }
  h6 { font-size: 1rem; }
}
.subheader { line-height: 1.4; color: #6f6f6f; font-weight: normal; margin-top: 0.2rem; margin-bottom: 0.5rem; }
h1 small, h2 small, h3 small, h4 small, h5 small, h6 small { font-size: 60%; color: #6f6f6f; line-height: 0; }
a { color: #008CBA; text-decoration: none; line-height: inherit; }
a:hover { color: #0078a0; }
abbr { text-transform: uppercase; font-size: 90%; color: #222222; cursor: help; border-bottom: 1px dotted #dddddd; }
blockquote { margin: 0 0 1.25rem; padding: 0.5625rem 1.25rem 0 1.1875rem; border-left: 1px solid #dddddd; }
blockquote, blockquote p { line-height: 1.6; color: #6f6f6f; }
blockquote cite { display: block; font-size: 0.8125rem; color: #555555; }
blockquote cite::before { content: "\\2014 \\0020"; }
dl dt { margin-bottom: 0.3rem; font-weight: bold; }
dl dd { margin-bottom: 0.75rem; margin-left: 0; }
code { font-family: Consolas, "Liberation Mono", Courier, monospace; font-weight: normal; color: #333333; background-color: #f8f8f8; border: 1px solid #dfdfdf; padding: 0.125rem 0.3125rem 0.0625rem; }
kbd { background-color: #ededed; border: 1px solid #dbdbdb; color: #222222; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.875rem; padding: 0.125rem 0.25rem 0; border-radius: 3px; }
hr { border: solid #dddddd; border-width: 1px 0 0; clear: both; height: 0; margin: 1.25rem 0 1.1875rem; }
"""

HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{FOUNDATION_TYPO_CSS}</style></head>
<body>
  <h1 id="h1">h1 标题</h1>
  <h2 id="h2">h2 标题</h2>
  <h3 id="h3">h3 标题</h3>
  <h4 id="h4">h4 标题</h4>
  <h5 id="h5">h5 标题</h5>
  <h6 id="h6">h6 标题</h6>
  <h2 id="h2-sub" class="subheader">h2.subheader</h2>
  <h1 id="h1-with-small">h1 heading <small id="h1-small">secondary text</small></h1>
  <p>这是一个 <a id="demo-link" class="a" href="#">链接</a>。</p>
  <p>The <abbr id="demo-abbr" title="World Health Organization">WHO</abbr> was founded in 1948.</p>
  <blockquote id="demo-quote">
    <p id="quote-p">学以致用,夯实前端排版基础。</p>
    <cite id="quote-cite">蓝色天空网</cite>
  </blockquote>
  <dl id="demo-dl">
    <dt id="dl-dt">Coffee</dt>
    <dd id="dl-dd">- black hot drink</dd>
    <dt>Milk</dt>
    <dd>- white cold drink</dd>
  </dl>
  <p>元素示例: <code id="demo-code">span</code>, <code>section</code>, <code>div</code></p>
  <p>快捷键: <kbd id="demo-kbd">ctrl + p</kbd></p>
  <hr id="demo-hr">
</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": 1024, "height": 900})
        page.set_content(HTML_PAGE)

        # Case 1: body defaults and h1-h6 desktop vs mobile sizing
        desktop_sizes = page.evaluate("""() => {
            const b = getComputedStyle(document.body);
            const hs = ['h1','h2','h3','h4','h5','h6'].map(id => getComputedStyle(document.getElementById(id)).fontSize);
            return { body_font_size: b.fontSize, body_line_height: b.lineHeight, body_font_first: b.fontFamily.split(',')[0].trim(), desktop_headings: hs };
        }""")
        page.set_viewport_size({"width": 480, "height": 900})
        mobile_sizes = page.evaluate("""() => ['h1','h2','h3','h4','h5','h6'].map(id => getComputedStyle(document.getElementById(id)).fontSize)""")
        page.set_viewport_size({"width": 1024, "height": 900})
        actual_1 = {**desktop_sizes, "mobile_headings": mobile_sizes}
        expected_1 = {
            "body_font_size": "16px",
            "body_line_height": "24px",
            "body_font_first": '"Helvetica Neue"',
            "desktop_headings": ["44px", "37px", "27px", "23px", "18px", "16px"],
            "mobile_headings": ["34px", "27px", "22px", "18px", "18px", "16px"],
        }
        cases.append({
            "name": "body_defaults_and_responsive_headings",
            "expected": expected_1,
            "actual": actual_1,
            "passed": actual_1 == expected_1,
        })

        # Case 2: .subheader and heading <small> secondary text
        actual_2 = page.evaluate("""() => {
            const h2 = getComputedStyle(document.getElementById('h2'));
            const sub = getComputedStyle(document.getElementById('h2-sub'));
            const sm = getComputedStyle(document.getElementById('h1-small'));
            return {
                normal_h2_color: h2.color,
                subheader_color: sub.color,
                small_color: sm.color,
                small_font_size: sm.fontSize,
                small_ratio_to_h1: Number((parseFloat(sm.fontSize) / 44).toFixed(2))
            };
        }""")
        expected_2 = {
            "normal_h2_color": "rgb(34, 34, 34)",
            "subheader_color": "rgb(111, 111, 111)",
            "small_color": "rgb(111, 111, 111)",
            "small_font_size": "26.4px",
            "small_ratio_to_h1": 0.6,
        }
        cases.append({
            "name": "subheader_and_small_secondary_heading",
            "expected": expected_2,
            "actual": actual_2,
            "passed": actual_2 == expected_2,
        })

        # Case 3: <a> link & <abbr> abbreviation
        actual_3 = page.evaluate("""() => {
            const a = getComputedStyle(document.getElementById('demo-link'));
            const abbr = getComputedStyle(document.getElementById('demo-abbr'));
            return {
                link_color: a.color,
                link_decoration: a.textDecorationLine,
                abbr_cursor: abbr.cursor,
                abbr_border_bottom: abbr.borderBottomWidth + ' ' + abbr.borderBottomStyle,
                abbr_text_transform: abbr.textTransform
            };
        }""")
        expected_3 = {
            "link_color": "rgb(0, 140, 186)",
            "link_decoration": "none",
            "abbr_cursor": "help",
            "abbr_border_bottom": "1px dotted",
            "abbr_text_transform": "uppercase",
        }
        cases.append({
            "name": "link_and_abbr_typography",
            "expected": expected_3,
            "actual": actual_3,
            "passed": actual_3 == expected_3,
        })

        # Case 4: <blockquote> + <cite> and <dl>/<dt>/<dd>
        actual_4 = page.evaluate("""() => {
            const bq = getComputedStyle(document.getElementById('demo-quote'));
            const cite = getComputedStyle(document.getElementById('quote-cite'));
            const citeBefore = getComputedStyle(document.getElementById('quote-cite'), '::before').content;
            const dt = getComputedStyle(document.getElementById('dl-dt'));
            const dd = getComputedStyle(document.getElementById('dl-dd'));
            return {
                blockquote_border_left: bq.borderLeftWidth + ' ' + bq.borderLeftStyle + ' ' + bq.borderLeftColor,
                cite_display: cite.display,
                cite_font_size: cite.fontSize,
                cite_prefix_has_emdash: citeBefore.includes('\\u2014'),
                dt_font_weight: dt.fontWeight,
                dd_margin_bottom: dd.marginBottom
            };
        }""")
        expected_4 = {
            "blockquote_border_left": "1px solid rgb(221, 221, 221)",
            "cite_display": "block",
            "cite_font_size": "13px",
            "cite_prefix_has_emdash": True,
            "dt_font_weight": "700",
            "dd_margin_bottom": "12px",
        }
        cases.append({
            "name": "blockquote_cite_and_definition_list",
            "expected": expected_4,
            "actual": actual_4,
            "passed": actual_4 == expected_4,
        })

        # Case 5: <code>, <kbd>, <hr>
        actual_5 = page.evaluate("""() => {
            const c = getComputedStyle(document.getElementById('demo-code'));
            const k = getComputedStyle(document.getElementById('demo-kbd'));
            const hr = getComputedStyle(document.getElementById('demo-hr'));
            return {
                code_bg: c.backgroundColor,
                code_border: c.borderTopWidth + ' ' + c.borderTopStyle + ' ' + c.borderTopColor,
                kbd_bg: k.backgroundColor,
                kbd_radius: k.borderRadius,
                kbd_font_size: k.fontSize,
                hr_border_top: hr.borderTopWidth + ' ' + hr.borderTopStyle + ' ' + hr.borderTopColor,
                hr_height: hr.height
            };
        }""")
        expected_5 = {
            "code_bg": "rgb(248, 248, 248)",
            "code_border": "1px solid rgb(223, 223, 223)",
            "kbd_bg": "rgb(237, 237, 237)",
            "kbd_radius": "3px",
            "kbd_font_size": "14px",
            "hr_border_top": "1px solid rgb(221, 221, 221)",
            "hr_height": "1px",
        }
        cases.append({
            "name": "code_kbd_and_hr_elements",
            "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-typography-lab/explore-typography.py

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

下载课程关联工程包:Foundation

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

下载课程关联工程包:Foundation

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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