"""Offline Playwright Chromium lab for Foundation Ch 24 (input-sizing)."""
import json
from playwright.sync_api import sync_playwright
FOUNDATION_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 16px; color: #222; background: #fff; }
.row { width: 100%; max-width: 960px; margin: 0 auto; }
.row::before, .row::after { content: " "; display: table; }
.row::after { clear: both; }
.row .row { width: auto; max-width: none; margin: 0 -15px; }
.row.collapse > .columns { padding-left: 0; padding-right: 0; }
.row .row.collapse { margin: 0; }
.columns { position: relative; padding-left: 15px; padding-right: 15px; float: left; width: 100%; }
.small-3 { width: 25%; }
.small-5 { width: 41.66667%; }
.small-8 { width: 66.66667%; float: left; }
.small-9 { width: 75%; }
@media only screen and (min-width: 40.063em) {
.medium-3 { width: 25%; }
.medium-4 { width: 33.33333%; }
.medium-7 { width: 58.33333%; }
}
@media only screen and (min-width: 64.063em) {
.large-6 { width: 50%; }
.large-10 { width: 83.33333%; }
}
label { display: block; font-size: 14px; color: #4d4d4d; margin-bottom: 4px; }
label.inline { margin: 0 0 16px 0; padding: 9px 0; line-height: 19px; }
label.right { float: right; text-align: right; }
input[type="text"] {
display: block; width: 100%; height: 37px; padding: 8px; margin: 0 0 16px 0;
border: 1px solid #cccccc; background: #fff; font-size: 14px;
}
span.prefix, span.postfix, a.button.prefix, a.button.postfix {
display: block; position: relative; z-index: 2; text-align: center;
width: 100%; height: 37px; line-height: 35px; font-size: 14px;
border: 1px solid #cccccc; text-decoration: none;
}
span.prefix, span.postfix { background: #f2f2f2; color: #333333; }
span.prefix { border-right: none; }
span.postfix { border-left: none; }
a.button.prefix, a.button.postfix { background: #008cba; border-color: #007095; color: #ffffff; }
.prefix-radius .prefix { border-radius: 3px 0 0 3px; }
.prefix-radius input { border-radius: 0 3px 3px 0; }
.postfix-radius input { border-radius: 3px 0 0 3px; }
.postfix-radius .postfix { border-radius: 0 3px 3px 0; }
.prefix-round .prefix { border-radius: 1000px 0 0 1000px; }
.prefix-round input { border-radius: 0 1000px 1000px 0; }
.postfix-round input { border-radius: 1000px 0 0 1000px; }
.postfix-round .postfix { border-radius: 0 1000px 1000px 0; }
"""
HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{FOUNDATION_CSS}</style></head>
<body>
<form id="f-grid">
<div class="row"><div id="col-l10-m7" class="large-10 medium-7 columns"><label>A<input type="text"></label></div></div>
<div class="row"><div id="col-s5" class="small-5 columns"><label>B<input type="text"></label></div></div>
<div class="row"><div id="col-m3" class="medium-3 columns"><label>C<input type="text"></label></div></div>
</form>
<form id="f-equal">
<div class="row">
<div id="eq1" class="medium-4 columns"><label>M4-1<input type="text"></label></div>
<div id="eq2" class="medium-4 columns"><label>M4-2<input type="text"></label></div>
<div id="eq3" class="medium-4 columns"><label>M4-3<input type="text"></label></div>
</div>
</form>
<form id="f-inline">
<div class="row">
<div class="small-8">
<div class="row">
<div class="small-3 columns"><label id="inl-lbl" for="inl-name" class="inline right">Name</label></div>
<div class="small-9 columns"><input type="text" id="inl-name" placeholder="First Name.."></div>
</div>
</div>
</div>
</form>
<form id="f-addons">
<div class="row">
<div class="large-6 columns">
<div id="row-pre-rad" class="row collapse prefix-radius">
<div id="pre-rad-col" class="small-3 columns"><span id="pre-rad-span" class="prefix">Prefix</span></div>
<div class="small-9 columns"><input id="pre-rad-inp" type="text" placeholder="Value"></div>
</div>
</div>
<div class="large-6 columns">
<div class="row collapse postfix-radius">
<div class="small-9 columns"><input id="post-rad-inp" type="text" placeholder="Value"></div>
<div class="small-3 columns"><span id="post-rad-span" class="postfix">Postfix</span></div>
</div>
</div>
</div>
<div class="row">
<div class="large-6 columns">
<div class="row collapse prefix-round">
<div class="small-3 columns"><a id="pre-rnd-btn" href="#" class="button prefix">Go</a></div>
<div class="small-9 columns"><input id="pre-rnd-inp" type="text" placeholder="Value"></div>
</div>
</div>
<div class="large-6 columns">
<div class="row collapse postfix-round">
<div class="small-9 columns"><input id="post-rnd-inp" type="text" placeholder="Value"></div>
<div class="small-3 columns"><a id="post-rnd-btn" href="#" class="button postfix">Go</a></div>
</div>
</div>
</div>
</form>
</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": 480, "height": 900})
page.set_content(HTML_PAGE)
# Case 1: grid column input sizing across small (480), medium (768), large (1200 -> max 960 row)
def measure_grid():
return page.evaluate("""() => ({
l10_m7: Math.round(document.getElementById('col-l10-m7').getBoundingClientRect().width),
s5: Math.round(document.getElementById('col-s5').getBoundingClientRect().width),
m3: Math.round(document.getElementById('col-m3').getBoundingClientRect().width),
})""")
small_w = measure_grid()
page.set_viewport_size({"width": 768, "height": 900})
med_w = measure_grid()
page.set_viewport_size({"width": 1200, "height": 900})
large_w = measure_grid()
c1 = {"at_480": small_w, "at_768": med_w, "at_1200_row960": large_w}
exp1 = {
"at_480": {"l10_m7": 480, "s5": 200, "m3": 480},
"at_768": {"l10_m7": 448, "s5": 320, "m3": 192},
"at_1200_row960": {"l10_m7": 800, "s5": 400, "m3": 240},
}
cases.append(dict(name="grid_column_input_sizing_breakpoints", passed=(c1 == exp1), expected=exp1, actual=c1))
# Case 2: equal 3-up columns stack on small, align on medium+
page.set_viewport_size({"width": 480, "height": 900})
eq_small = page.evaluate("""() => {
const rects = ['#eq1', '#eq2', '#eq3'].map(s => document.querySelector(s).getBoundingClientRect());
return {
widths: rects.map(r => Math.round(r.width)),
stacked_vertically: rects[0].top < rects[1].top && rects[1].top < rects[2].top,
};
}""")
page.set_viewport_size({"width": 960, "height": 900})
eq_desk = page.evaluate("""() => {
const rects = ['#eq1', '#eq2', '#eq3'].map(s => document.querySelector(s).getBoundingClientRect());
return {
widths: rects.map(r => Math.round(r.width)),
same_row: Math.round(rects[0].top) === Math.round(rects[1].top) && Math.round(rects[1].top) === Math.round(rects[2].top),
};
}""")
c2 = {"mobile_480": eq_small, "desktop_960": eq_desk}
exp2 = {
"mobile_480": {"widths": [480, 480, 480], "stacked_vertically": True},
"desktop_960": {"widths": [320, 320, 320], "same_row": True},
}
cases.append(dict(name="equal_columns_three_up_and_stacking", passed=(c2 == exp2), expected=exp2, actual=c2))
# Case 3: inline right label alignment and focus
page.click("#inl-lbl")
c3 = page.evaluate("""() => {
const lbl = document.getElementById('inl-lbl');
const inp = document.getElementById('inl-name');
const ls = getComputedStyle(lbl);
const lr = lbl.getBoundingClientRect();
const ir = inp.getBoundingClientRect();
return {
active_id: document.activeElement.id,
label_align: ls.textAlign,
label_padding_top: ls.paddingTop,
same_top: Math.round(lr.top) === Math.round(ir.top),
label_height: Math.round(lr.height),
input_height: Math.round(ir.height),
};
}""")
exp3 = {
"active_id": "inl-name",
"label_align": "right",
"label_padding_top": "9px",
"same_top": True,
"label_height": 37,
"input_height": 37,
}
cases.append(dict(name="inline_right_label_alignment", passed=(c3 == exp3), expected=exp3, actual=c3))
# Case 4: prefix/postfix collapse, button, radius & round corners
page.set_viewport_size({"width": 1200, "height": 900})
c4 = page.evaluate("""() => {
const colPad = getComputedStyle(document.getElementById('pre-rad-col')).paddingRight;
const preSpan = document.getElementById('pre-rad-span');
const preInp = document.getElementById('pre-rad-inp');
const postInp = document.getElementById('post-rad-inp');
const postSpan = document.getElementById('post-rad-span');
const preBtn = document.getElementById('pre-rnd-btn');
const postBtn = document.getElementById('post-rnd-btn');
return {
collapse_col_padding_right: colPad,
prefix_flush_with_input: Math.round(preSpan.getBoundingClientRect().right) === Math.round(preInp.getBoundingClientRect().left),
postfix_flush_with_input: Math.round(postInp.getBoundingClientRect().right) === Math.round(postSpan.getBoundingClientRect().left),
prefix_radius: getComputedStyle(preSpan).borderTopLeftRadius + '/' + getComputedStyle(preSpan).borderTopRightRadius,
postfix_radius: getComputedStyle(postSpan).borderTopLeftRadius + '/' + getComputedStyle(postSpan).borderTopRightRadius,
prefix_round_btn_bg: getComputedStyle(preBtn).backgroundColor,
prefix_round_radius: getComputedStyle(preBtn).borderTopLeftRadius + '/' + getComputedStyle(preBtn).borderTopRightRadius,
postfix_round_radius: getComputedStyle(postBtn).borderTopLeftRadius + '/' + getComputedStyle(postBtn).borderTopRightRadius,
};
}""")
exp4 = {
"collapse_col_padding_right": "0px",
"prefix_flush_with_input": True,
"postfix_flush_with_input": True,
"prefix_radius": "3px/0px",
"postfix_radius": "0px/3px",
"prefix_round_btn_bg": "rgb(0, 140, 186)",
"prefix_round_radius": "1000px/0px",
"postfix_round_radius": "0px/1000px",
}
cases.append(dict(name="prefix_postfix_collapse_buttons_and_radius", passed=(c4 == exp4), expected=exp4, actual=c4))
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-input-sizing-lab/explore-input-sizing.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-input-sizing.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a739.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a756.zip