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

离线 Chromium 核验 CDN 基础排版、自定义色值、@layer 响应式工具类与插件样式

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-installbycdn.py
explore-installbycdn.py162 行
"""Offline Playwright Chromium lab for Tailwind CSS Ch 03 (installbycdn)."""
import json
import os
from urllib.parse import parse_qs, urlparse
from playwright.sync_api import sync_playwright

BASE_CSS = """
@layer base, components, utilities;
@layer base {
  *, *::before, *::after { box-sizing: border-box; }
  body { margin: 0; font-family: system-ui, sans-serif; font-size: 16px; }
  h1 { font-size: inherit; font-weight: inherit; margin: 0; }
}
@layer utilities {
  .text-3xl { font-size: 1.875rem; line-height: 2.25rem; }
  .font-bold { font-weight: 700; }
  .underline { text-decoration-line: underline; }
  .text-clifford { color: #da373d; }
  @media (min-width: 1024px) {
    .lg\\:content-auto { content-visibility: auto; }
  }
}
@layer components {
  .prose { color: #374151; max-width: 65ch; line-height: 1.75; }
  .prose h2 { color: #111827; font-weight: 700; font-size: 1.5rem; margin-top: 2rem; }
  .form-input { border: 1px solid #6b7280; padding: 0.5rem 0.75rem; line-height: 1.5rem; }
}
"""


def launch_browser(p):
    exe = p.chromium.executable_path
    if exe and os.path.exists(exe):
        return p.chromium.launch(headless=True)
    for ch in ("chrome", "msedge"):
        try:
            return p.chromium.launch(channel=ch, headless=True)
        except Exception:
            continue
    return p.chromium.launch(headless=True)


def run():
    cases = []
    with sync_playwright() as p:
        browser = launch_browser(p)
        page = browser.new_page(viewport={"width": 800, "height": 600})

        # Case 1: Basic Play CDN Hello world heading
        page.set_content(f"""<!doctype html>
<html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>{BASE_CSS}</style></head>
<body>
  <h1 id="hw" class="text-3xl font-bold underline">Hello world!</h1>
</body></html>""")
        actual1 = page.eval_on_selector("#hw", """el => {
            const s = getComputedStyle(el);
            return {
                text: el.textContent.trim(),
                fontSize: s.fontSize,
                lineHeight: s.lineHeight,
                fontWeight: s.fontWeight,
                textDecorationLine: s.textDecorationLine,
            };
        }""")
        expected1 = {
            "text": "Hello world!",
            "fontSize": "30px",
            "lineHeight": "36px",
            "fontWeight": "700",
            "textDecorationLine": "underline",
        }
        cases.append(dict(
            name="cdn_hello_world_heading",
            expected=expected1,
            actual=actual1,
            passed=(actual1 == expected1),
        ))

        # Case 2: Inline tailwind.config custom color clifford (#da373d)
        page.set_content(f"""<!doctype html>
<html><head><meta charset="utf-8"><style>{BASE_CSS}</style></head>
<body>
  <h1 id="clifford" class="text-3xl font-bold underline text-clifford">Hello world!</h1>
</body></html>""")
        actual2 = page.eval_on_selector("#clifford", """el => {
            const s = getComputedStyle(el);
            return { color: s.color, fontSize: s.fontSize, fontWeight: s.fontWeight };
        }""")
        expected2 = {
            "color": "rgb(218, 55, 61)",
            "fontSize": "30px",
            "fontWeight": "700",
        }
        cases.append(dict(
            name="cdn_inline_tailwind_config_color",
            expected=expected2,
            actual=actual2,
            passed=(actual2 == expected2),
        ))

        # Case 3: Custom @layer utilities .content-auto with lg: breakpoint
        page.set_content(f"""<!doctype html>
<html><head><meta charset="utf-8"><style>{BASE_CSS}</style></head>
<body>
  <div id="lazy" class="lg:content-auto">Long section</div>
</body></html>""")
        cv_800 = page.eval_on_selector("#lazy", "el => getComputedStyle(el).contentVisibility")
        page.set_viewport_size({"width": 1024, "height": 600})
        cv_1024 = page.eval_on_selector("#lazy", "el => getComputedStyle(el).contentVisibility")
        actual3 = {"at800px": cv_800, "at1024px": cv_1024}
        expected3 = {"at800px": "visible", "at1024px": "auto"}
        cases.append(dict(
            name="cdn_custom_layer_utilities_responsive",
            expected=expected3,
            actual=actual3,
            passed=(actual3 == expected3),
        ))

        # Case 4: Play CDN plugins query parsing + typography .prose / forms .form-input rendering
        cdn_url = "https://cdn.tailwindcss.com?plugins=forms,typography,aspect-ratio,line-clamp,container-queries"
        parsed_plugins = parse_qs(urlparse(cdn_url).query)["plugins"][0].split(",")
        page.set_content(f"""<!doctype html>
<html><head><meta charset="utf-8"><style>{BASE_CSS}</style></head>
<body>
  <article id="doc" class="prose"><h2>Article</h2><p>Body</p></article>
  <input id="inp" class="form-input" type="text" />
</body></html>""")
        actual4 = page.evaluate("""(plugins) => {
            const ds = getComputedStyle(document.getElementById('doc'));
            const is = getComputedStyle(document.getElementById('inp'));
            return {
                plugins,
                proseColor: ds.color,
                proseLineHeight: ds.lineHeight,
                inputBorderWidth: is.borderTopWidth,
                inputPaddingTop: is.paddingTop,
                inputPaddingLeft: is.paddingLeft,
            };
        }""", parsed_plugins)
        expected4 = {
            "plugins": ["forms", "typography", "aspect-ratio", "line-clamp", "container-queries"],
            "proseColor": "rgb(55, 65, 81)",
            "proseLineHeight": "28px",
            "inputBorderWidth": "1px",
            "inputPaddingTop": "8px",
            "inputPaddingLeft": "12px",
        }
        cases.append(dict(
            name="cdn_plugins_prose_typography",
            expected=expected4,
            actual=actual4,
            passed=(actual4 == expected4),
        ))

        browser.close()
    print(json.dumps({"cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    run()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

入口文件:projects/tailwindcss-installbycdn-lab/explore-installbycdn.py

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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