"""Offline Playwright Chromium lab for Bootstrap 5 Chapter 05 (typography)."""
import json
from playwright.sync_api import sync_playwright
BS5_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body {
margin: 0;
font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
color: #212529;
}
p { margin-top: 0; margin-bottom: 1rem; }
h1, .h1, h2, .h2, h3, .h3, h4, .h4, h5, .h5, h6, .h6 {
margin-top: 0; margin-bottom: 0.5rem; font-weight: 500; line-height: 1.2;
}
h1, .h1 { font-size: 2.5rem; }
h2, .h2 { font-size: 2rem; }
h3, .h3 { font-size: 1.75rem; }
h4, .h4 { font-size: 1.5rem; }
h5, .h5 { font-size: 1.25rem; }
h6, .h6 { font-size: 1rem; }
.display-1 { font-size: 5rem; font-weight: 300; line-height: 1.2; }
.display-2 { font-size: 4.5rem; font-weight: 300; line-height: 1.2; }
.display-3 { font-size: 4rem; font-weight: 300; line-height: 1.2; }
.display-4 { font-size: 3.5rem; font-weight: 300; line-height: 1.2; }
.display-5 { font-size: 3rem; font-weight: 300; line-height: 1.2; }
.display-6 { font-size: 2.5rem; font-weight: 300; line-height: 1.2; }
small, .small { font-size: 0.875em; }
mark, .mark { padding: 0.2em; background-color: #fcf8e3; }
abbr[title] { text-decoration: underline dotted; cursor: help; }
.initialism { font-size: 0.875em; text-transform: uppercase; }
.blockquote { margin-bottom: 1rem; font-size: 1.25rem; }
.blockquote-footer { margin-top: -1rem; margin-bottom: 1rem; font-size: 0.875em; color: #6c757d; }
.blockquote-footer::before { content: "\\2014\\00A0"; }
dt { font-weight: 700; }
dd { margin-bottom: 0.5rem; margin-left: 0; }
code { font-size: 0.875em; color: #d63384; font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; }
kbd { padding: 0.2rem 0.4rem; font-size: 0.875em; color: #fff; background-color: #212529; border-radius: 0.2rem; }
pre { display: block; margin-top: 0; margin-bottom: 1rem; overflow: auto; font-size: 0.875em; white-space: pre; font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; }
.lead { font-size: 1.25rem; font-weight: 300; }
.text-start { text-align: left !important; }
.text-center { text-align: center !important; }
.text-end { text-align: right !important; }
.text-nowrap { white-space: nowrap !important; }
.text-lowercase { text-transform: lowercase !important; }
.text-uppercase { text-transform: uppercase !important; }
.text-capitalize { text-transform: capitalize !important; }
.list-unstyled { padding-left: 0; list-style: none; }
.list-inline { padding-left: 0; list-style: none; }
.list-inline-item { display: inline-block; }
.list-inline-item:not(:last-child) { margin-right: 0.5rem; }
"""
HTML_DOC = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{BS5_CSS}</style></head>
<body>
<p id="p-def">默认段落</p>
<h1 id="t-h1">H1 <small id="h1-small">sub</small></h1>
<h2 id="t-h2">H2</h2><h3 id="t-h3">H3</h3><h4 id="t-h4">H4</h4><h5 id="t-h5">H5</h5><h6 id="t-h6">H6</h6>
<p id="c-h1" class="h1">p.h1</p><p id="c-h2" class="h2">p.h2</p><p id="c-h3" class="h3">p.h3</p>
<p id="c-h4" class="h4">p.h4</p><p id="c-h5" class="h5">p.h5</p><p id="c-h6" class="h6">p.h6</p>
<h1 id="d1" class="display-1">D1</h1><h1 id="d2" class="display-2">D2</h1>
<h1 id="d3" class="display-3">D3</h1><h1 id="d4" class="display-4">D4</h1>
<h1 id="d5" class="display-5">D5</h1><h1 id="d6" class="display-6">D6</h1>
<p><small id="sm-p">small</small> <mark id="mk">mark</mark>
<abbr id="ab" title="World Health Organization">WHO</abbr>
<abbr id="ab-init" class="initialism" title="HyperText Markup Language">html</abbr></p>
<blockquote id="bq" class="blockquote">
<p>引用正文</p>
<footer id="bqf" class="blockquote-footer">出处说明</footer>
</blockquote>
<dl><dt id="dt1">Coffee</dt><dd id="dd1">black hot drink</dd></dl>
<p><code id="cd1">div</code> <kbd id="kb1">ctrl + p</kbd></p>
<pre id="pr1">line 1
line 2</pre>
<p id="u-lead" class="lead">导语段落</p>
<p id="u-start" class="text-start">start</p>
<p id="u-center" class="text-center">center</p>
<p id="u-end" class="text-end">end</p>
<div style="width:80px"><p id="u-nowrap" class="text-nowrap">long text without wrapping</p></div>
<p id="u-low" class="text-lowercase">ABC</p>
<p id="u-up" class="text-uppercase">abc</p>
<p id="u-cap" class="text-capitalize">hello world</p>
<ul id="u-unstyle" class="list-unstyled">
<li>A<ul id="u-nested"><li>A1</li></ul></li>
</ul>
<ul id="u-inline" class="list-inline">
<li class="list-inline-item">One</li>
<li class="list-inline-item">Two</li>
</ul>
</body>
</html>
"""
def launch_browser(pw):
try:
return pw.chromium.launch(headless=True)
except Exception:
return pw.chromium.launch(headless=True, channel="chrome")
def main():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page(viewport={"width": 1200, "height": 900})
page.set_content(HTML_DOC)
# Case 1: Defaults, h1-h6 vs .h1-.h6, and .display-1..6
c1 = page.evaluate("""() => {
const b = getComputedStyle(document.body);
const pd = getComputedStyle(document.getElementById('p-def'));
const tagSizes = [1,2,3,4,5,6].map(i => getComputedStyle(document.getElementById('t-h'+i)).fontSize);
const clsSizes = [1,2,3,4,5,6].map(i => getComputedStyle(document.getElementById('c-h'+i)).fontSize);
const dispSizes = [1,2,3,4,5,6].map(i => getComputedStyle(document.getElementById('d'+i)).fontSize);
return {
body_font_size: b.fontSize,
body_line_height: b.lineHeight,
p_margins: [pd.marginTop, pd.marginBottom],
heading_sizes: tagSizes,
heading_class_matches_tag: JSON.stringify(tagSizes) === JSON.stringify(clsSizes),
display_sizes: dispSizes,
display_weight: getComputedStyle(document.getElementById('d1')).fontWeight
};
}""")
exp1 = {
"body_font_size": "16px",
"body_line_height": "24px",
"p_margins": ["0px", "16px"],
"heading_sizes": ["40px", "32px", "28px", "24px", "20px", "16px"],
"heading_class_matches_tag": True,
"display_sizes": ["80px", "72px", "64px", "56px", "48px", "40px"],
"display_weight": "300",
}
cases.append({
"name": "defaults_headings_and_display",
"passed": c1 == exp1,
"expected": exp1,
"actual": c1,
})
# Case 2: Inline semantic tags (<small>, <mark>, <abbr>, .initialism)
c2 = page.evaluate("""() => {
const sm = getComputedStyle(document.getElementById('sm-p'));
const h1sm = getComputedStyle(document.getElementById('h1-small'));
const mk = getComputedStyle(document.getElementById('mk'));
const ab = getComputedStyle(document.getElementById('ab'));
const abi = getComputedStyle(document.getElementById('ab-init'));
return {
small_px: sm.fontSize,
h1_small_px: h1sm.fontSize,
mark_bg: mk.backgroundColor,
abbr_deco_style: ab.textDecorationStyle,
abbr_cursor: ab.cursor,
initialism: [abi.fontSize, abi.textTransform]
};
}""")
exp2 = {
"small_px": "14px",
"h1_small_px": "35px",
"mark_bg": "rgb(252, 248, 227)",
"abbr_deco_style": "dotted",
"abbr_cursor": "help",
"initialism": ["14px", "uppercase"],
}
cases.append({
"name": "inline_semantic_elements",
"passed": c2 == exp2,
"expected": exp2,
"actual": c2,
})
# Case 3: Blockquote, dl, code, kbd, pre
c3 = page.evaluate("""() => {
const bq = getComputedStyle(document.getElementById('bq'));
const bqf = getComputedStyle(document.getElementById('bqf'));
const bqfBefore = getComputedStyle(document.getElementById('bqf'), '::before').content;
const dt = getComputedStyle(document.getElementById('dt1'));
const cd = getComputedStyle(document.getElementById('cd1'));
const kb = getComputedStyle(document.getElementById('kb1'));
const pr = document.getElementById('pr1');
return {
blockquote_size: bq.fontSize,
footer_color: bqf.color,
footer_has_emdash: bqfBefore.includes('\\u2014'),
dt_weight: dt.fontWeight,
code_color: cd.color,
kbd_colors: [kb.backgroundColor, kb.color],
pre_whitespace: getComputedStyle(pr).whiteSpace,
pre_preserves_multiline: pr.textContent.includes('\\n line 2')
};
}""")
exp3 = {
"blockquote_size": "20px",
"footer_color": "rgb(108, 117, 125)",
"footer_has_emdash": True,
"dt_weight": "700",
"code_color": "rgb(214, 51, 132)",
"kbd_colors": ["rgb(33, 37, 41)", "rgb(255, 255, 255)"],
"pre_whitespace": "pre",
"pre_preserves_multiline": True,
}
cases.append({
"name": "blockquote_dl_code_kbd_pre",
"passed": c3 == exp3,
"expected": exp3,
"actual": c3,
})
# Case 4: Additional typography utilities
c4 = page.evaluate("""() => {
const inlineItems = [...document.querySelectorAll('#u-inline > li')];
const r0 = inlineItems[0].getBoundingClientRect();
const r1 = inlineItems[1].getBoundingClientRect();
return {
lead: [getComputedStyle(document.getElementById('u-lead')).fontSize, getComputedStyle(document.getElementById('u-lead')).fontWeight],
align: [
getComputedStyle(document.getElementById('u-start')).textAlign,
getComputedStyle(document.getElementById('u-center')).textAlign,
getComputedStyle(document.getElementById('u-end')).textAlign
],
nowrap: getComputedStyle(document.getElementById('u-nowrap')).whiteSpace,
transforms: [
getComputedStyle(document.getElementById('u-low')).textTransform,
getComputedStyle(document.getElementById('u-up')).textTransform,
getComputedStyle(document.getElementById('u-cap')).textTransform
],
list_unstyled: getComputedStyle(document.getElementById('u-unstyle')).listStyleType,
nested_list_keeps_marker: getComputedStyle(document.getElementById('u-nested')).listStyleType !== 'none',
inline_same_row: Math.abs(r0.top - r1.top) < 1 && r1.left > r0.left
};
}""")
exp4 = {
"lead": ["20px", "300"],
"align": ["left", "center", "right"],
"nowrap": "nowrap",
"transforms": ["lowercase", "uppercase", "capitalize"],
"list_unstyled": "none",
"nested_list_keeps_marker": True,
"inline_same_row": True,
}
cases.append({
"name": "more_typography_utilities",
"passed": c4 == exp4,
"expected": exp4,
"actual": c4,
})
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/bootstrap5-typography-lab/explore-typography.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-typography.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联38章) · 含当前章节或实例 · mbskycom202612a700.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a736.zip