"""Offline Playwright Chromium lab for Foundation Ch 33 (grid-small)."""
import json
from playwright.sync_api import sync_playwright
FOUNDATION_GRID_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 0; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; }
.row { width: 100%; margin: 0 auto; max-width: 62.5rem; }
.row::before, .row::after { content: " "; display: table; }
.row::after { clear: both; }
.column, .columns { padding-left: 0.9375rem; padding-right: 0.9375rem; width: 100%; float: left; position: relative; }
.small-3 { width: 25%; }
.small-4 { width: 33.33333%; }
.small-5 { width: 41.66667%; }
.small-8 { width: 66.66667%; }
.small-9 { width: 75%; }
@media only screen and (min-width: 40.0625em) {
.medium-probe { width: 50%; }
}
.demo-col { min-height: 28px; }
"""
HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{FOUNDATION_GRID_CSS}</style></head>
<body>
<div id="row-3-9" class="row">
<div id="s3" class="small-3 columns demo-col" style="background-color:yellow;"><p>25%</p></div>
<div id="s9" class="small-9 columns demo-col" style="background-color:pink;"><p>75%</p></div>
</div>
<div id="row-4-8" class="row">
<div id="s4" class="small-4 columns demo-col" style="background-color:yellow;"><p>33.3%</p></div>
<div id="s8" class="small-8 columns demo-col" style="background-color:pink;"><p>66.7%</p></div>
</div>
<div id="row-over" class="row">
<div id="ov5" class="small-5 columns demo-col">5</div>
<div id="ov8" class="small-8 columns demo-col">8</div>
</div>
<div class="row">
<div id="probe" class="medium-probe columns demo-col">P</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": 375, "height": 800})
page.set_content(HTML_PAGE)
# Case 1: .small-3 / .small-9 (25%/75%) inherited across small, medium, large
actual_1 = {}
for vw in (375, 768, 1200):
page.set_viewport_size({"width": vw, "height": 800})
actual_1[str(vw)] = page.evaluate("""() => {
const a = document.getElementById('s3').getBoundingClientRect();
const b = document.getElementById('s9').getBoundingClientRect();
return {
widths: [Math.round(a.width * 10) / 10, Math.round(b.width * 10) / 10],
same_row: Math.abs(a.top - b.top) < 1,
};
}""")
expected_1 = {
"375": {"widths": [93.8, 281.3], "same_row": True},
"768": {"widths": [192.0, 576.0], "same_row": True},
"1200": {"widths": [250.0, 750.0], "same_row": True},
}
cases.append({
"name": "small_3_and_9_mobile_first_inheritance",
"expected": expected_1,
"actual": actual_1,
"passed": actual_1 == expected_1,
})
# Case 2: .small-4 / .small-8 (33.3%/66.7%) across small, medium, large
actual_2 = {}
for vw in (375, 768, 1200):
page.set_viewport_size({"width": vw, "height": 800})
actual_2[str(vw)] = page.evaluate("""() => {
const a = document.getElementById('s4').getBoundingClientRect();
const b = document.getElementById('s8').getBoundingClientRect();
return {
widths: [Math.round(a.width * 10) / 10, Math.round(b.width * 10) / 10],
same_row: Math.abs(a.top - b.top) < 1,
};
}""")
expected_2 = {
"375": {"widths": [125.0, 250.0], "same_row": True},
"768": {"widths": [256.0, 512.0], "same_row": True},
"1200": {"widths": [333.3, 666.7], "same_row": True},
}
cases.append({
"name": "small_4_and_8_one_third_two_thirds",
"expected": expected_2,
"actual": actual_2,
"passed": actual_2 == expected_2,
})
# Case 3: 40.0625em (641px) boundary & 12-column sum check
page.set_viewport_size({"width": 640, "height": 800})
w640 = page.evaluate("() => Math.round(document.getElementById('probe').getBoundingClientRect().width * 10) / 10")
page.set_viewport_size({"width": 641, "height": 800})
w641 = page.evaluate("() => Math.round(document.getElementById('probe').getBoundingClientRect().width * 10) / 10")
page.set_viewport_size({"width": 375, "height": 800})
over = page.evaluate("""() => {
const a = document.getElementById('ov5').getBoundingClientRect();
const b = document.getElementById('ov8').getBoundingClientRect();
return { sum_13_same_row: Math.abs(a.top - b.top) < 1, wrapped: b.top >= a.bottom - 1 };
}""")
actual_3 = {"probe_640_w": w640, "probe_641_w": w641, **over}
expected_3 = {"probe_640_w": 640.0, "probe_641_w": 320.5, "sum_13_same_row": False, "wrapped": True}
cases.append({
"name": "small_threshold_and_12_col_sum",
"expected": expected_3,
"actual": actual_3,
"passed": actual_3 == expected_3,
})
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/foundation-grid-small-lab/explore-grid-small.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-grid-small.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a739.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a751.zip