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

在离线 Chromium 中验证分页激活/禁用态、三档尺寸、Flex 对齐及面包屑斜杠分隔符

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-pagination.py
explore-pagination.py203 行
"""Offline Playwright Chromium verification for Bootstrap 5 Pagination & Breadcrumb (Ch 16)."""
import json
from playwright.sync_api import sync_playwright

HTML = """<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<style>
  *, *::before, *::after { box-sizing: border-box; }
  html { font-size: 16px; }
  body { margin: 16px; font-family: system-ui, -apple-system, "Segoe UI", sans-serif; line-height: 1.5; }
  .box { width: 600px; }
  .pagination {
    display: flex; padding-left: 0; list-style: none; margin: 12px 0;
  }
  .page-link {
    position: relative; display: block; padding: 0.375rem 0.75rem; font-size: 1rem;
    color: #0d6efd; text-decoration: none; background-color: #fff;
    border: 1px solid #dee2e6;
  }
  .page-item:not(:first-child) .page-link { margin-left: -1px; }
  .page-item:first-child .page-link { border-top-left-radius: 0.375rem; border-bottom-left-radius: 0.375rem; }
  .page-item:last-child .page-link { border-top-right-radius: 0.375rem; border-bottom-right-radius: 0.375rem; }
  .page-item.active .page-link { z-index: 3; color: #fff; background-color: #0d6efd; border-color: #0d6efd; }
  .page-item.disabled .page-link { color: #6c757d; pointer-events: none; background-color: #fff; border-color: #dee2e6; }
  .pagination-lg .page-link { padding: 0.75rem 1.5rem; font-size: 1.25rem; }
  .pagination-sm .page-link { padding: 0.25rem 0.5rem; font-size: 0.875rem; }
  .justify-content-center { justify-content: center !important; }
  .justify-content-end { justify-content: flex-end !important; }

  .breadcrumb {
    display: flex; flex-wrap: wrap; padding: 0; margin-bottom: 1rem; list-style: none;
  }
  .breadcrumb-item + .breadcrumb-item { padding-left: 0.5rem; }
  .breadcrumb-item + .breadcrumb-item::before {
    float: left; padding-right: 0.5rem; color: #6c757d; content: "/";
  }
  .breadcrumb-item.active { color: #6c757d; }
</style>
</head>
<body>
<div id="wrap" class="box">
  <ul id="pg-states" class="pagination">
    <li class="page-item disabled"><a id="pg-prev" class="page-link" href="#">Previous</a></li>
    <li class="page-item"><a id="pg-1" class="page-link" href="#">1</a></li>
    <li class="page-item active"><a id="pg-2" class="page-link" href="#">2</a></li>
    <li class="page-item"><a class="page-link" href="#">3</a></li>
    <li class="page-item"><a class="page-link" href="#">Next</a></li>
  </ul>

  <ul id="pg-lg" class="pagination pagination-lg">
    <li class="page-item"><a id="lg-link" class="page-link" href="#">1</a></li>
    <li class="page-item"><a class="page-link" href="#">2</a></li>
  </ul>
  <ul id="pg-sm" class="pagination pagination-sm">
    <li class="page-item"><a id="sm-link" class="page-link" href="#">1</a></li>
    <li class="page-item"><a class="page-link" href="#">2</a></li>
  </ul>

  <ul id="al-start" class="pagination">
    <li class="page-item"><a class="page-link" href="#">1</a></li>
    <li class="page-item"><a class="page-link" href="#">2</a></li>
  </ul>
  <ul id="al-center" class="pagination justify-content-center">
    <li class="page-item"><a class="page-link" href="#">1</a></li>
    <li class="page-item"><a class="page-link" href="#">2</a></li>
  </ul>
  <ul id="al-end" class="pagination justify-content-end">
    <li class="page-item"><a class="page-link" href="#">1</a></li>
    <li class="page-item"><a class="page-link" href="#">2</a></li>
  </ul>

  <ul id="bc" class="breadcrumb">
    <li id="bc-1" class="breadcrumb-item"><a href="#">Photos</a></li>
    <li id="bc-2" class="breadcrumb-item"><a href="#">Summer 2017</a></li>
    <li id="bc-3" class="breadcrumb-item"><a href="#">Italy</a></li>
    <li id="bc-4" class="breadcrumb-item active">Rome</li>
  </ul>
</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():
    with sync_playwright() as p:
        browser = launch_browser(p)
        page = browser.new_page(viewport={"width": 800, "height": 600})
        page.set_content(HTML, wait_until="load")

        c1_actual = page.evaluate("""() => {
            const ul = getComputedStyle(document.getElementById("pg-states"));
            const prev = getComputedStyle(document.getElementById("pg-prev"));
            const p1 = getComputedStyle(document.getElementById("pg-1"));
            const p2 = getComputedStyle(document.getElementById("pg-2"));
            return {
                display: ul.display,
                list_style: ul.listStyleType,
                second_margin_left: p1.marginLeft,
                active_bg: p2.backgroundColor,
                active_color: p2.color,
                disabled_pointer_events: prev.pointerEvents,
                disabled_color: prev.color,
            };
        }""")
        c1_expected = {
            "display": "flex",
            "list_style": "none",
            "second_margin_left": "-1px",
            "active_bg": "rgb(13, 110, 253)",
            "active_color": "rgb(255, 255, 255)",
            "disabled_pointer_events": "none",
            "disabled_color": "rgb(108, 117, 125)",
        }

        c2_actual = page.evaluate("""() => {
            const m = id => {
                const el = document.getElementById(id);
                const s = getComputedStyle(el);
                return { fs: s.fontSize, pad: s.padding, h: Math.round(el.getBoundingClientRect().height) };
            };
            const lg = m("lg-link"), md = m("pg-1"), sm = m("sm-link");
            return {
                lg_fs: lg.fs, md_fs: md.fs, sm_fs: sm.fs,
                lg_pad: lg.pad, md_pad: md.pad, sm_pad: sm.pad,
                strict_height_order: lg.h > md.h && md.h > sm.h,
            };
        }""")
        c2_expected = {
            "lg_fs": "20px",
            "md_fs": "16px",
            "sm_fs": "14px",
            "lg_pad": "12px 24px",
            "md_pad": "6px 12px",
            "sm_pad": "4px 8px",
            "strict_height_order": True,
        }

        c3_actual = page.evaluate("""() => {
            const box = document.getElementById("wrap").getBoundingClientRect();
            const bounds = id => {
                const items = Array.from(document.querySelectorAll(`#${id} .page-item`));
                return {
                    left: Math.round(items[0].getBoundingClientRect().left - box.left),
                    rightGap: Math.round(box.right - items[items.length - 1].getBoundingClientRect().right),
                };
            };
            const s = bounds("al-start"), c = bounds("al-center"), e = bounds("al-end");
            return {
                start_left_offset: s.left,
                center_symmetric: Math.abs(c.left - c.rightGap) <= 1,
                end_right_gap: e.rightGap,
            };
        }""")
        c3_expected = {
            "start_left_offset": 0,
            "center_symmetric": True,
            "end_right_gap": 0,
        }

        c4_actual = page.evaluate("""() => {
            const bc = getComputedStyle(document.getElementById("bc"));
            const b1 = getComputedStyle(document.getElementById("bc-1"), "::before").content;
            const b2 = getComputedStyle(document.getElementById("bc-2"), "::before").content;
            const b4 = getComputedStyle(document.getElementById("bc-4"));
            return {
                display: bc.display,
                first_before: b1,
                second_before: b2,
                active_color: b4.color,
                item_count: document.querySelectorAll("#bc .breadcrumb-item").length,
            };
        }""")
        c4_expected = {
            "display": "flex",
            "first_before": "none",
            "second_before": '"/"',
            "active_color": "rgb(108, 117, 125)",
            "item_count": 4,
        }

        browser.close()

    cases = [
        dict(name="basic_active_disabled_pagination", description="核对 .pagination 弹性布局、-1px 边框重叠及 .active/.disabled 态", expected=c1_expected, actual=c1_actual, passed=c1_actual == c1_expected),
        dict(name="pagination_sizes", description="核对 .pagination-lg / 默认 / .pagination-sm 字号、内边距与高度梯度", expected=c2_expected, actual=c2_actual, passed=c2_actual == c2_expected),
        dict(name="pagination_alignment", description="核对左对齐、.justify-content-center 居中与 .justify-content-end 右对齐", expected=c3_expected, actual=c3_actual, passed=c3_actual == c3_expected),
        dict(name="breadcrumb_navigation", description="核对 .breadcrumb 弹性列表、::before 斜杠分隔符与 .active 当前层级样式", expected=c4_expected, actual=c4_actual, passed=c4_actual == c4_expected),
    ]
    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/bootstrap5-pagination-lab/explore-pagination.py

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

下载课程关联工程包:Bootstrap5

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

下载课程关联工程包:Bootstrap5

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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