"""Offline Playwright Chromium lab for Foundation Ch 32 (grid-stacked-to-horizontal)."""
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-1 { width: 8.33333%; }
.small-4 { width: 33.33333%; }
.small-6 { width: 50%; }
.small-7 { width: 58.33333%; }
@media only screen and (min-width: 40.0625em) {
.medium-6 { width: 50%; }
.medium-8 { width: 66.66667%; }
}
.demo-col { min-height: 32px; }
"""
HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{FOUNDATION_GRID_CSS}</style></head>
<body>
<div id="row-main" class="row">
<div id="m-left" class="medium-6 columns demo-col" style="background-color:yellow;"><p>左栏内容</p></div>
<div id="m-right" class="medium-6 columns demo-col" style="background-color:pink;"><p>右栏内容</p></div>
</div>
<div id="row-spans" class="row">
<div id="s-1" class="small-1 columns demo-col">1</div>
<div id="s-4" class="small-4 columns demo-col">4</div>
<div id="s-7" class="small-7 columns demo-col">7</div>
</div>
<div id="row-half" class="row">
<div id="s-6a" class="small-6 columns demo-col">6A</div>
<div id="s-6b" class="small-6 columns demo-col">6B</div>
</div>
<div id="row-overflow" class="row">
<div id="ov-1" class="medium-6 columns demo-col">6</div>
<div id="ov-2" class="medium-8 columns demo-col">8</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: .medium-6 stacks vertically on small (<641px), aligns horizontally on medium/large (>=641px)
actual_1 = {}
for vw in (375, 640, 641, 768, 1200):
page.set_viewport_size({"width": vw, "height": 800})
actual_1[str(vw)] = page.evaluate("""() => {
const a = document.getElementById('m-left').getBoundingClientRect();
const b = document.getElementById('m-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_1 = {
"375": {"widths": [375.0, 375.0], "same_row": False, "stacked": True},
"640": {"widths": [640.0, 640.0], "same_row": False, "stacked": True},
"641": {"widths": [320.5, 320.5], "same_row": True, "stacked": False},
"768": {"widths": [384.0, 384.0], "same_row": True, "stacked": False},
"1200": {"widths": [500.0, 500.0], "same_row": True, "stacked": False},
}
cases.append({
"name": "medium_6_stacked_on_small_horizontal_on_medium_large",
"expected": expected_1,
"actual": actual_1,
"passed": actual_1 == expected_1,
})
# Case 2: .small-1, .small-4, .small-6 widths and 12-column sum
page.set_viewport_size({"width": 1200, "height": 800})
actual_2 = page.evaluate("""() => {
const r = id => document.getElementById(id).getBoundingClientRect();
const r1 = r('s-1'), r4 = r('s-4'), r7 = r('s-7'), r6a = r('s-6a'), r6b = r('s-6b');
return {
small_1_w: Math.round(r1.width * 10) / 10,
small_4_w: Math.round(r4.width * 10) / 10,
small_7_w: Math.round(r7.width * 10) / 10,
row_1_4_7_same_row: Math.abs(r1.top - r4.top) < 1 && Math.abs(r4.top - r7.top) < 1,
small_6_pair_w: [Math.round(r6a.width * 10) / 10, Math.round(r6b.width * 10) / 10],
small_6_same_row: Math.abs(r6a.top - r6b.top) < 1,
};
}""")
expected_2 = {
"small_1_w": 83.3,
"small_4_w": 333.3,
"small_7_w": 583.3,
"row_1_4_7_same_row": True,
"small_6_pair_w": [500.0, 500.0],
"small_6_same_row": True,
}
cases.append({
"name": "column_span_numbers_and_12_sum",
"expected": expected_2,
"actual": actual_2,
"passed": actual_2 == expected_2,
})
# Case 3: Exceeding 12 columns (6 + 8 = 14) wraps to next line
page.set_viewport_size({"width": 768, "height": 800})
actual_3 = page.evaluate("""() => {
const a = document.getElementById('ov-1').getBoundingClientRect();
const b = document.getElementById('ov-2').getBoundingClientRect();
return {
widths: [Math.round(a.width * 10) / 10, Math.round(b.width * 10) / 10],
same_row: Math.abs(a.top - b.top) < 1,
wrapped_below: b.top >= a.bottom - 1,
};
}""")
expected_3 = {
"widths": [384.0, 512.0],
"same_row": False,
"wrapped_below": True,
}
cases.append({
"name": "overflow_over_12_columns_wraps_to_next_line",
"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-stacked-to-horizontal-lab/explore-grid-stacked-to-horizontal.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-grid-stacked-to-horizontal.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a739.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a752.zip