蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线验证 Foundation 5 表格边框、偶数行斑马条纹与窄屏容器溢出行为

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-tables.py
explore-tables.py150 行
"""Offline Playwright Chromium lab for Foundation Ch 04 (tables)."""
import json
from playwright.sync_api import sync_playwright

FOUNDATION_TABLE_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 0; padding: 16px; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; color: #222222; }
table { background: #ffffff; border: 1px solid #dddddd; margin-bottom: 1.25rem; table-layout: auto; border-collapse: separate; border-spacing: 0; }
table thead { background: #f5f5f5; }
table thead tr th { padding: 0.5rem 0.625rem 0.625rem; font-size: 0.875rem; font-weight: bold; color: #222222; text-align: left; }
table tr td { padding: 0.5625rem 0.625rem; font-size: 0.875rem; color: #222222; text-align: left; }
table tr.even, table tr.alt, table tr:nth-of-type(even) { background: #f9f9f9; }
"""

HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{FOUNDATION_TABLE_CSS}</style></head>
<body>
  <table id="basic-table">
    <thead>
      <tr id="head-row">
        <th>Firstname</th><th>Lastname</th><th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr id="row-1"><td>John</td><td>Doe</td><td>john@example.com</td></tr>
      <tr id="row-2"><td>Mary</td><td>Moe</td><td>mary@example.com</td></tr>
      <tr id="row-3"><td>July</td><td>Dooley</td><td>july@example.com</td></tr>
    </tbody>
  </table>

  <div id="wrap-hidden" style="overflow-x:hidden; width:300px;">
    <table id="wide-hidden" style="min-width:600px;">
      <thead><tr><th>ID</th><th>Firstname</th><th>Lastname</th><th>Department</th><th>Email</th></tr></thead>
      <tbody><tr><td>1</td><td>John</td><td>Doe</td><td>Engineering</td><td>john.doe@example.com</td></tr></tbody>
    </table>
  </div>

  <div id="wrap-auto" style="overflow-x:auto; width:300px;">
    <table id="wide-auto" style="min-width:600px;">
      <thead><tr><th>ID</th><th>Firstname</th><th>Lastname</th><th>Department</th><th>Email</th></tr></thead>
      <tbody><tr><td>1</td><td>John</td><td>Doe</td><td>Engineering</td><td>john.doe@example.com</td></tr></tbody>
    </table>
  </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": 800, "height": 600})
        page.set_content(HTML_PAGE)

        # Case 1: four borders and thead background
        actual_1 = page.evaluate("""() => {
            const tbl = getComputedStyle(document.getElementById('basic-table'));
            const thead = getComputedStyle(document.querySelector('#basic-table thead'));
            const th = getComputedStyle(document.querySelector('#basic-table th'));
            return {
                borders: [tbl.borderTopWidth, tbl.borderRightWidth, tbl.borderBottomWidth, tbl.borderLeftWidth],
                border_color: tbl.borderTopColor,
                table_bg: tbl.backgroundColor,
                thead_bg: thead.backgroundColor,
                th_font_weight: th.fontWeight,
                th_font_size: th.fontSize
            };
        }""")
        expected_1 = {
            "borders": ["1px", "1px", "1px", "1px"],
            "border_color": "rgb(221, 221, 221)",
            "table_bg": "rgb(255, 255, 255)",
            "thead_bg": "rgb(245, 245, 245)",
            "th_font_weight": "700",
            "th_font_size": "14px",
        }
        cases.append({
            "name": "basic_table_borders_and_header",
            "expected": expected_1,
            "actual": actual_1,
            "passed": actual_1 == expected_1,
        })

        # Case 2: gray zebra striping across tbody rows
        actual_2 = page.evaluate("""() => ({
            row1_bg: getComputedStyle(document.getElementById('row-1')).backgroundColor,
            row2_bg: getComputedStyle(document.getElementById('row-2')).backgroundColor,
            row3_bg: getComputedStyle(document.getElementById('row-3')).backgroundColor,
            td_padding: getComputedStyle(document.querySelector('#row-1 td')).paddingTop + ' ' + getComputedStyle(document.querySelector('#row-1 td')).paddingLeft
        })""")
        expected_2 = {
            "row1_bg": "rgba(0, 0, 0, 0)",
            "row2_bg": "rgb(249, 249, 249)",
            "row3_bg": "rgba(0, 0, 0, 0)",
            "td_padding": "9px 10px",
        }
        cases.append({
            "name": "zebra_striping_even_rows",
            "expected": expected_2,
            "actual": actual_2,
            "passed": actual_2 == expected_2,
        })

        # Case 3: overflow-x:hidden clipping vs overflow-x:auto horizontal scroll
        actual_3 = page.evaluate("""() => {
            const wh = document.getElementById('wrap-hidden');
            const wa = document.getElementById('wrap-auto');
            wa.scrollLeft = 120;
            wh.scrollLeft = 120;
            return {
                hidden_overflow_x: getComputedStyle(wh).overflowX,
                hidden_client_w: wh.clientWidth,
                hidden_scroll_w: wh.scrollWidth,
                hidden_clipped_px: wh.scrollWidth - wh.clientWidth,
                auto_overflow_x: getComputedStyle(wa).overflowX,
                auto_scroll_left_after_scroll: wa.scrollLeft
            };
        }""")
        expected_3 = {
            "hidden_overflow_x": "hidden",
            "hidden_client_w": 300,
            "hidden_scroll_w": 600,
            "hidden_clipped_px": 300,
            "auto_overflow_x": "auto",
            "auto_scroll_left_after_scroll": 120,
        }
        cases.append({
            "name": "responsive_table_overflow_hidden_vs_auto",
            "expected": expected_3,
            "actual": actual_3,
            "passed": actual_3 == expected_3,
        })

        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-tables-lab/explore-tables.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-tables.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Foundation

课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a739.zip

下载课程关联工程包:Foundation

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a770.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习