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

离线 Chromium 验证 table-auto/fixed 列宽、边框折叠、colspan 及响应式列/表题

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-table.py
explore-table.py223 行
"""Offline Playwright Chromium lab for Tailwind CSS Ch 19: Table."""
import json
from playwright.sync_api import sync_playwright

CSS = """
*, ::before, ::after { box-sizing: border-box; }
body { margin: 0; font-size: 16px; }
.table-auto { table-layout: auto; }
.table-fixed { table-layout: fixed; }
.w-full { width: 100%; }
.border { border: 1px solid #cbd5e1; }
.border-collapse { border-collapse: collapse; }
.border-separate { border-collapse: separate; }
.border-spacing-2 { border-spacing: 0.5rem; }
.px-4 { padding-left: 1rem; padding-right: 1rem; }
.py-2 { padding-top: 0.5rem; padding-bottom: 0.5rem; }
.px-6 { padding-left: 1.5rem; padding-right: 1.5rem; }
.py-4 { padding-top: 1rem; padding-bottom: 1rem; }
.mt-8 { margin-top: 2rem; }
.text-left { text-align: left; }
.text-center { text-align: center; }
.text-right { text-align: right; }
.font-semibold { font-weight: 600; }
.font-bold { font-weight: 700; }
.uppercase { text-transform: uppercase; }
.lowercase { text-transform: lowercase; }
.capitalize { text-transform: capitalize; }
.hidden { display: none; }
.caption-top { caption-side: top; }
.caption-bottom { caption-side: bottom; }
@media (min-width: 768px) {
  .md\\:table-cell { display: table-cell; }
}
"""


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": 900})
        page.set_content(f"""
        <!doctype html><html><head><style>{CSS}</style></head>
        <body>
          <div style="width:600px">
            <table id="t-auto" class="table-auto w-full border-collapse">
              <thead><tr><th id="ta1" class="border px-4 py-2">Song</th><th id="ta2" class="border px-4 py-2">Artist</th><th id="ta3" class="border px-4 py-2">Year</th></tr></thead>
              <tbody><tr><td class="border px-4 py-2">The Sliding Mr. Bones (Next Stop, Pottersville)</td><td class="border px-4 py-2">Malcolm Lockyer</td><td class="border px-4 py-2">1961</td></tr></tbody>
            </table>
            <table id="t-fixed" class="table-fixed w-full border-collapse">
              <thead><tr><th id="tf1" class="border px-4 py-2">Song</th><th id="tf2" class="border px-4 py-2">Artist</th><th id="tf3" class="border px-4 py-2">Year</th></tr></thead>
              <tbody><tr><td class="border px-4 py-2">The Sliding Mr. Bones (Next Stop, Pottersville)</td><td class="border px-4 py-2">Malcolm Lockyer</td><td class="border px-4 py-2">1961</td></tr></tbody>
            </table>
          </div>

          <table id="t-sep" class="table-auto border-separate border-spacing-2 mt-8">
            <tbody><tr><td id="cell-lg" class="border px-6 py-4">Cell</td></tr></tbody>
          </table>

          <div style="width:600px">
            <table id="t-span" class="table-fixed w-full border-collapse">
              <thead>
                <tr>
                  <th id="th-span" class="border px-4 py-2 text-center font-bold uppercase" colspan="2">Header 1 & 2</th>
                  <th id="th-single" class="border px-4 py-2 text-right font-semibold capitalize">header 3</th>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <td class="border px-4 py-2">C1</td>
                  <td class="border px-4 py-2">C2</td>
                  <td class="border px-4 py-2">C3</td>
                </tr>
              </tbody>
            </table>
          </div>

          <table class="table-auto w-full border-collapse">
            <thead><tr><th class="border">H1</th><th id="resp-col" class="border hidden md:table-cell">H2</th><th class="border">H3</th></tr></thead>
          </table>

          <table id="t-ctop">
            <caption id="cap-top" class="caption-top">Top Caption</caption>
            <tbody id="tb-top"><tr><td>Row</td></tr></tbody>
          </table>
          <table id="t-cbot">
            <caption id="cap-bot" class="caption-bottom">Bottom Caption</caption>
            <tbody id="tb-bot"><tr><td>Row</td></tr></tbody>
          </table>
        </body></html>
        """)

        # Case 1: table-auto vs table-fixed
        act1 = page.evaluate("""() => {
            const w = id => Math.round(document.getElementById(id).getBoundingClientRect().width);
            const auto1 = w('ta1'), auto3 = w('ta3');
            const fix = [w('tf1'), w('tf2'), w('tf3')];
            return {
                auto_layout: getComputedStyle(document.getElementById('t-auto')).tableLayout,
                fixed_layout: getComputedStyle(document.getElementById('t-fixed')).tableLayout,
                auto_col1_wider_than_col3: auto1 > auto3 * 2,
                fixed_equal_cols: fix,
            };
        }""")
        exp1 = {
            "auto_layout": "auto",
            "fixed_layout": "fixed",
            "auto_col1_wider_than_col3": True,
            "fixed_equal_cols": [200, 200, 200],
        }
        cases.append({
            "name": "table_auto_vs_table_fixed_widths",
            "passed": act1 == exp1,
            "expected": exp1,
            "actual": act1,
        })

        # Case 2: border-collapse, border-separate/spacing, cell padding & table margin
        act2 = page.evaluate("""() => {
            const sAuto = getComputedStyle(document.getElementById('t-auto'));
            const sSep = getComputedStyle(document.getElementById('t-sep'));
            const sCell = getComputedStyle(document.getElementById('cell-lg'));
            return {
                collapse_mode: sAuto.borderCollapse,
                separate_mode: sSep.borderCollapse,
                border_spacing: sSep.borderSpacing,
                table_mt_8: sSep.marginTop,
                cell_px6_py4: [sCell.paddingTop, sCell.paddingRight],
            };
        }""")
        exp2 = {
            "collapse_mode": "collapse",
            "separate_mode": "separate",
            "border_spacing": "8px",
            "table_mt_8": "32px",
            "cell_px6_py4": ["16px", "24px"],
        }
        cases.append({
            "name": "border_collapse_spacing_and_padding",
            "passed": act2 == exp2,
            "expected": exp2,
            "actual": act2,
        })

        # Case 3: alignment, font weight, text transform & colspan
        act3 = page.evaluate("""() => {
            const s1 = getComputedStyle(document.getElementById('th-span'));
            const s2 = getComputedStyle(document.getElementById('th-single'));
            return {
                span_align: s1.textAlign,
                span_weight: s1.fontWeight,
                span_transform: s1.textTransform,
                single_align: s2.textAlign,
                single_weight: s2.fontWeight,
                single_transform: s2.textTransform,
                colspan2_vs_single_width: [
                    Math.round(document.getElementById('th-span').getBoundingClientRect().width),
                    Math.round(document.getElementById('th-single').getBoundingClientRect().width),
                ],
            };
        }""")
        exp3 = {
            "span_align": "center",
            "span_weight": "700",
            "span_transform": "uppercase",
            "single_align": "right",
            "single_weight": "600",
            "single_transform": "capitalize",
            "colspan2_vs_single_width": [399, 200],
        }
        cases.append({
            "name": "alignment_typography_and_colspan",
            "passed": act3 == exp3,
            "expected": exp3,
            "actual": act3,
        })

        # Case 4: Responsive hidden md:table-cell & caption-top vs caption-bottom
        md_disp = page.evaluate("() => getComputedStyle(document.getElementById('resp-col')).display")
        page.set_viewport_size({"width": 500, "height": 900})
        sm_disp = page.evaluate("() => getComputedStyle(document.getElementById('resp-col')).display")
        cap_info = page.evaluate("""() => {
            const ct = document.getElementById('cap-top').getBoundingClientRect();
            const tt = document.getElementById('tb-top').getBoundingClientRect();
            const cb = document.getElementById('cap-bot').getBoundingClientRect();
            const tb = document.getElementById('tb-bot').getBoundingClientRect();
            return {
                caption_top_side: getComputedStyle(document.getElementById('cap-top')).captionSide,
                caption_bottom_side: getComputedStyle(document.getElementById('cap-bot')).captionSide,
                top_above_body: ct.bottom <= tt.top + 1,
                bottom_below_body: cb.top >= tb.bottom - 1,
            };
        }""")
        act4 = {"sm_display": sm_disp, "md_display": md_disp, **cap_info}
        exp4 = {
            "sm_display": "none",
            "md_display": "table-cell",
            "caption_top_side": "top",
            "caption_bottom_side": "bottom",
            "top_above_body": True,
            "bottom_below_body": True,
        }
        cases.append({
            "name": "responsive_column_and_caption_position",
            "passed": act4 == exp4,
            "expected": exp4,
            "actual": act4,
        })

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

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

下载课程关联工程包:Tailwind CSS

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

下载课程关联工程包:Tailwind CSS

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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