"""Offline Chromium verification for Tailwind CSS borders chapter."""
import json
from playwright.sync_api import sync_playwright
CSS = """
*, *::before, *::after { box-sizing: border-box; border-width: 0; border-style: solid; border-color: #e5e7eb; }
.border { border-width: 1px; }
.border-2 { border-width: 2px; }
.border-4 { border-width: 4px; }
.border-t-4 { border-top-width: 4px; }
.border-r-2 { border-right-width: 2px; }
.border-b { border-bottom-width: 1px; }
.border-l-\\[3px\\] { border-left-width: 3px; }
.border-red-500 { border-color: rgb(239 68 68); }
.border-blue-500 { border-color: rgb(59 130 246); }
.border-t-blue-300 { border-top-color: rgb(147 197 253); }
.border-r-gray-400 { border-right-color: rgb(156 163 175); }
.border-b-green-600 { border-bottom-color: rgb(22 163 74); }
.border-l-yellow-500 { border-left-color: rgb(234 179 8); }
.border-solid { border-style: solid; }
.border-dashed { border-style: dashed; }
.border-dotted { border-style: dotted; }
.border-double { border-style: double; }
.rounded-lg { border-radius: 0.5rem; }
.rounded-full { border-radius: 9999px; }
.rounded-tl-sm { border-top-left-radius: 0.125rem; }
.rounded-tr-md { border-top-right-radius: 0.375rem; }
.rounded-br-lg { border-bottom-right-radius: 0.5rem; }
.rounded-bl-xl { border-bottom-left-radius: 0.75rem; }
.box-border { box-sizing: border-box; }
.box-content { box-sizing: content-box; }
.w-50 { width: 200px; }
.p-4 { padding: 1rem; }
.divide-y-2 > :not([hidden]) ~ :not([hidden]) { border-top-width: 2px; }
.divide-gray-200 > :not([hidden]) ~ :not([hidden]) { border-color: rgb(229 231 235); }
.ring-2 { box-shadow: 0 0 0 2px rgb(59 130 246); }
"""
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 pw:
browser = launch_browser(pw)
page = browser.new_page()
page.set_content(f"""<!doctype html><html><head><style>{CSS}</style></head><body>
<div id="w-1" class="border">1px</div>
<div id="w-2" class="border-2">2px</div>
<div id="w-4" class="border-4">4px</div>
<div id="w-sides" class="border-t-4 border-r-2 border-b border-l-[3px] border-blue-500">Sides</div>
<div id="c-all" class="border-2 border-red-500 border-solid">All</div>
<div id="c-sides" class="border-2 border-t-blue-300 border-r-gray-400 border-b-green-600 border-l-yellow-500">SideColors</div>
<div id="s-dashed" class="border-4 border-dashed">Dashed</div>
<div id="s-dotted" class="border-2 border-dotted">Dotted</div>
<div id="s-double" class="border-4 border-double">Double</div>
<div id="r-lg" class="border-4 rounded-lg">LG</div>
<div id="r-corners" class="border-2 rounded-tl-sm rounded-tr-md rounded-br-lg rounded-bl-xl">Corners</div>
<div id="r-full" class="rounded-full">Full</div>
<div id="b-border" class="box-border w-50 p-4 border-4 border-red-500">BorderBox</div>
<div id="b-content" class="box-content w-50 p-4 border-4 border-red-500">ContentBox</div>
<div id="div-list" class="divide-y-2 divide-gray-200"><div id="d1">A</div><div id="d2">B</div></div>
<button id="ring-btn" class="ring-2">Ring</button>
</body></html>""")
c1_act = page.evaluate("""() => {
const st = id => getComputedStyle(document.getElementById(id));
const s = st('w-sides');
return {
b1: st('w-1').borderTopWidth,
b2: st('w-2').borderTopWidth,
b4: st('w-4').borderTopWidth,
sides: [s.borderTopWidth, s.borderRightWidth, s.borderBottomWidth, s.borderLeftWidth],
};
}""")
c1_exp = {"b1": "1px", "b2": "2px", "b4": "4px", "sides": ["4px", "2px", "1px", "3px"]}
cases.append(dict(name="border_widths_and_sides", expected=c1_exp, actual=c1_act, passed=(c1_act == c1_exp)))
c2_act = page.evaluate("""() => {
const st = id => getComputedStyle(document.getElementById(id));
const cs = st('c-sides');
return {
red_all: st('c-all').borderTopColor,
side_colors: [cs.borderTopColor, cs.borderRightColor, cs.borderBottomColor, cs.borderLeftColor],
styles: [st('c-all').borderTopStyle, st('s-dashed').borderTopStyle, st('s-dotted').borderTopStyle, st('s-double').borderTopStyle],
};
}""")
c2_exp = {
"red_all": "rgb(239, 68, 68)",
"side_colors": ["rgb(147, 197, 253)", "rgb(156, 163, 175)", "rgb(22, 163, 74)", "rgb(234, 179, 8)"],
"styles": ["solid", "dashed", "dotted", "double"],
}
cases.append(dict(name="border_colors_and_styles", expected=c2_exp, actual=c2_act, passed=(c2_act == c2_exp)))
c3_act = page.evaluate("""() => {
const st = id => getComputedStyle(document.getElementById(id));
const rc = st('r-corners');
return {
lg: st('r-lg').borderTopLeftRadius,
corners: [rc.borderTopLeftRadius, rc.borderTopRightRadius, rc.borderBottomRightRadius, rc.borderBottomLeftRadius],
full: st('r-full').borderTopLeftRadius,
};
}""")
c3_exp = {"lg": "8px", "corners": ["2px", "6px", "8px", "12px"], "full": "9999px"}
cases.append(dict(name="border_radius_and_corners", expected=c3_exp, actual=c3_act, passed=(c3_act == c3_exp)))
c4_act = page.evaluate("""() => {
const bb = document.getElementById('b-border');
const bc = document.getElementById('b-content');
return {
border_box_sizing: getComputedStyle(bb).boxSizing,
border_box_width: Math.round(bb.getBoundingClientRect().width),
content_box_sizing: getComputedStyle(bc).boxSizing,
content_box_width: Math.round(bc.getBoundingClientRect().width),
divide_first_top: getComputedStyle(document.getElementById('d1')).borderTopWidth,
divide_second_top: getComputedStyle(document.getElementById('d2')).borderTopWidth,
ring_shadow: getComputedStyle(document.getElementById('ring-btn')).boxShadow,
};
}""")
c4_exp = {
"border_box_sizing": "border-box",
"border_box_width": 200,
"content_box_sizing": "content-box",
"content_box_width": 240,
"divide_first_top": "0px",
"divide_second_top": "2px",
"ring_shadow": "rgb(59, 130, 246) 0px 0px 0px 2px",
}
cases.append(dict(name="box_sizing_divide_and_ring", expected=c4_exp, actual=c4_act, passed=(c4_act == c4_exp)))
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/tailwindcss-borders-lab/explore-borders.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-borders.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a642.zip
课程关联工程包(登记关联24章) · 含当前章节或实例 · mbskycom202612a639.zip