"""Offline Playwright Chromium lab for Foundation 5 Ch 15 (lists)."""
import json
from playwright.sync_api import sync_playwright
FOUNDATION_LISTS_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 0; padding: 20px; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; color: #222; background: #fff; }
ul {
font-size: 1rem;
line-height: 1.6;
margin-bottom: 1.25rem;
margin-left: 1.1rem;
padding-left: 0;
list-style-position: outside;
list-style-type: disc;
}
ul.disc { list-style-type: disc; }
ul.circle { list-style-type: circle; }
ul.square { list-style-type: square; }
ul.no-bullet { list-style-type: none; margin-left: 0; }
.inline-list {
margin: 0 auto 1.0625rem auto;
margin-left: -1.375rem;
margin-right: 0;
padding: 0;
list-style: none;
overflow: hidden;
}
.inline-list > li {
list-style: none;
float: left;
margin-left: 1.375rem;
display: block;
}
.inline-list > li > * {
display: block;
}
"""
HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{FOUNDATION_LISTS_CSS}</style></head>
<body>
<ul id="ul-default">
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
<li>List item 4</li>
</ul>
<ul id="ul-circle" class="circle">
<li>Circle 1</li>
<li>Circle 2</li>
</ul>
<ul id="ul-square" class="square">
<li>Square 1</li>
<li>Square 2</li>
</ul>
<ul id="ul-nobullet" class="no-bullet">
<li>Plain 1</li>
<li>Plain 2</li>
</ul>
<ul id="ul-inline" class="inline-list">
<li><a href="#">Inline 1</a></li>
<li><a href="#">Inline 2</a></li>
<li><a href="#">Inline 3</a></li>
<li><a href="#">Inline 4</a></li>
</ul>
<ul id="ul-menu">
<li><a href="#home">Home</a></li>
<li><a href="#m1">Menu 1</a></li>
<li><a href="#m2">Menu 2</a></li>
<li><a href="#m3">Menu 3</a></li>
</ul>
</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": 900, "height": 800})
page.set_content(HTML_PAGE)
# Case 1: default_and_disc_list
act_1 = page.evaluate("""() => {
const ul = document.getElementById('ul-default');
const s = getComputedStyle(ul);
const tops = Array.from(ul.children).map(li => Math.round(li.getBoundingClientRect().top));
const strictlyDown = tops.every((v, i) => i === 0 || v > tops[i - 1]);
return {
list_style_type: s.listStyleType,
item_count: ul.children.length,
vertical_stack: strictlyDown,
};
}""")
exp_1 = {
"list_style_type": "disc",
"item_count": 4,
"vertical_stack": True,
}
cases.append({
"name": "default_and_disc_list",
"expected": exp_1,
"actual": act_1,
"passed": act_1 == exp_1,
})
# Case 2: bullet_style_variants
act_2 = page.evaluate("""() => ({
circle_type: getComputedStyle(document.getElementById('ul-circle')).listStyleType,
square_type: getComputedStyle(document.getElementById('ul-square')).listStyleType,
nobullet_type: getComputedStyle(document.getElementById('ul-nobullet')).listStyleType,
nobullet_margin_left: getComputedStyle(document.getElementById('ul-nobullet')).marginLeft,
})""")
exp_2 = {
"circle_type": "circle",
"square_type": "square",
"nobullet_type": "none",
"nobullet_margin_left": "0px",
}
cases.append({
"name": "bullet_style_variants",
"expected": exp_2,
"actual": act_2,
"passed": act_2 == exp_2,
})
# Case 3: inline_list_horizontal_layout
act_3 = page.evaluate("""() => {
const ul = document.getElementById('ul-inline');
const items = Array.from(ul.children);
const rects = items.map(li => li.getBoundingClientRect());
const sameRow = rects.every(r => Math.abs(r.top - rects[0].top) <= 1);
const strictlyRight = rects.every((r, i) => i === 0 || r.left > rects[i - 1].right);
return {
list_style_type: getComputedStyle(ul).listStyleType,
li_float: getComputedStyle(items[0]).float,
li_margin_left: getComputedStyle(items[0]).marginLeft,
same_row: sameRow,
strictly_left_to_right: strictlyRight,
};
}""")
exp_3 = {
"list_style_type": "none",
"li_float": "left",
"li_margin_left": "22px",
"same_row": True,
"strictly_left_to_right": True,
}
cases.append({
"name": "inline_list_horizontal_layout",
"expected": exp_3,
"actual": act_3,
"passed": act_3 == exp_3,
})
# Case 4: unordered_list_menu_links
act_4 = page.evaluate("""() => {
const ul = document.getElementById('ul-menu');
const links = Array.from(ul.querySelectorAll('li > a'));
const tops = links.map(a => Math.round(a.getBoundingClientRect().top));
return {
labels: links.map(a => a.textContent),
vertical_stack: tops.every((v, i) => i === 0 || v > tops[i - 1]),
list_style_type: getComputedStyle(ul).listStyleType,
};
}""")
exp_4 = {
"labels": ["Home", "Menu 1", "Menu 2", "Menu 3"],
"vertical_stack": True,
"list_style_type": "disc",
}
cases.append({
"name": "unordered_list_menu_links",
"expected": exp_4,
"actual": act_4,
"passed": act_4 == exp_4,
})
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-lists-lab/explore-lists.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-lists.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a739.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a759.zip