"""Offline Playwright Chromium lab for Foundation Ch 35 (grid-large)."""
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-9 { width: 75%; }
@media only screen and (min-width: 40.0625em) {
.medium-6 { width: 50%; }
}
@media only screen and (min-width: 64.0625em) {
.large-4 { width: 33.33333%; }
.large-6 { width: 50%; }
.large-8 { width: 66.66667%; }
}
.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-3tier" class="row">
<div id="t-left" class="small-3 medium-6 large-4 columns demo-col" style="background-color:yellow;"><p>左栏</p></div>
<div id="t-right" class="small-9 medium-6 large-8 columns demo-col" style="background-color:pink;"><p>右栏</p></div>
</div>
<div id="row-lrg-only" class="row">
<div id="lo-left" class="large-6 columns demo-col" style="background-color:yellow;"><p>仅大屏左</p></div>
<div id="lo-right" class="large-6 columns demo-col" style="background-color:pink;"><p>仅大屏右</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: Three-tier responsive ratios across 375 / 768 / 1200px
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('t-left').getBoundingClientRect();
const b = document.getElementById('t-right').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": [384.0, 384.0], "same_row": True},
"1200": {"widths": [333.3, 666.7], "same_row": True},
}
cases.append({
"name": "three_tier_small_medium_large_ratios",
"expected": expected_1,
"actual": actual_1,
"passed": actual_1 == expected_1,
})
# Case 2: Large-only (.large-6) stacks on small & medium, splits 50/50 on 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('lo-left').getBoundingClientRect();
const b = document.getElementById('lo-right').getBoundingClientRect();
return {
widths: [Math.round(a.width * 10) / 10, Math.round(b.width * 10) / 10],
same_row: Math.abs(a.top - b.top) < 1,
stacked: b.top >= a.bottom - 1,
};
}""")
expected_2 = {
"375": {"widths": [375.0, 375.0], "same_row": False, "stacked": True},
"768": {"widths": [768.0, 768.0], "same_row": False, "stacked": True},
"1200": {"widths": [500.0, 500.0], "same_row": True, "stacked": False},
}
cases.append({
"name": "large_only_stacks_on_small_and_medium",
"expected": expected_2,
"actual": actual_2,
"passed": actual_2 == expected_2,
})
# Case 3: Exact 64.0625em (1025px) threshold check (1024px vs 1025px)
actual_3 = {}
for vw in (1024, 1025):
page.set_viewport_size({"width": vw, "height": 800})
actual_3[str(vw)] = page.evaluate("""() => {
const t1 = document.getElementById('t-left').getBoundingClientRect();
const t2 = document.getElementById('t-right').getBoundingClientRect();
const l1 = document.getElementById('lo-left').getBoundingClientRect();
const l2 = document.getElementById('lo-right').getBoundingClientRect();
return {
three_tier_w: [Math.round(t1.width * 10) / 10, Math.round(t2.width * 10) / 10],
large_only_w: [Math.round(l1.width * 10) / 10, Math.round(l2.width * 10) / 10],
large_only_same_row: Math.abs(l1.top - l2.top) < 1,
};
}""")
expected_3 = {
"1024": {
"three_tier_w": [500.0, 500.0],
"large_only_w": [1000.0, 1000.0],
"large_only_same_row": False,
},
"1025": {
"three_tier_w": [333.3, 666.7],
"large_only_w": [500.0, 500.0],
"large_only_same_row": True,
},
}
cases.append({
"name": "large_breakpoint_exact_threshold_1024_vs_1025",
"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-large-lab/explore-grid-large.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-grid-large.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a739.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a749.zip