"""Offline Playwright Chromium lab for jQuery UI Ch 09 (theme-api)."""
import json
from playwright.sync_api import sync_playwright
def launch_browser(p):
try:
return p.chromium.launch(headless=True)
except Exception:
return p.chromium.launch(headless=True, channel="chrome")
FRAMEWORK_CSS = """
/* ui.core.css - structural layout helpers */
.ui-helper-hidden { display: none; }
.ui-helper-hidden-accessible { border: 0; clip: rect(0 0 0 0); height: 1px; margin: -1px; overflow: hidden; padding: 0; position: absolute; width: 1px; }
.ui-helper-reset { margin: 0; padding: 0; border: 0; outline: 0; line-height: 1.3; text-decoration: none; font-size: 100%; list-style: none; }
.ui-helper-clearfix::before, .ui-helper-clearfix::after { content: ""; display: table; border-collapse: collapse; }
.ui-helper-clearfix::after { clear: both; }
.ui-helper-zfix { width: 100%; height: 100%; top: 0; left: 0; position: absolute; opacity: 0; }
.ui-icon { display: block; text-indent: -99999px; overflow: hidden; background-repeat: no-repeat; width: 16px; height: 16px; }
/* ui.theme.css - themeable visual styles */
.ui-widget { font-family: Verdana, Arial, sans-serif; font-size: 16px; }
.ui-widget input, .ui-widget select, .ui-widget textarea, .ui-widget button { font-family: Verdana, Arial, sans-serif; font-size: 1em; }
.ui-widget-content { border: 1px solid rgb(170, 170, 170); background: rgb(255, 255, 255); color: rgb(34, 34, 34); }
.ui-widget-content a { color: rgb(34, 34, 34); }
.ui-widget-header { border: 1px solid rgb(170, 170, 170); background: rgb(204, 204, 204); color: rgb(34, 34, 34); font-weight: bold; }
.ui-widget-header a { color: rgb(34, 34, 34); }
.ui-state-default { border: 1px solid rgb(211, 211, 211); background: rgb(230, 230, 230); color: rgb(85, 85, 85); }
.ui-state-hover, .ui-state-focus { border: 1px solid rgb(153, 153, 153); background: rgb(218, 218, 218); color: rgb(33, 33, 33); }
.ui-state-active { border: 1px solid rgb(170, 170, 170); background: rgb(255, 255, 255); color: rgb(33, 33, 33); }
.ui-state-highlight { border: 1px solid rgb(252, 239, 161); background: rgb(251, 249, 238); color: rgb(54, 54, 54); }
.ui-state-error { border: 1px solid rgb(205, 10, 10); background: rgb(254, 241, 236); color: rgb(205, 10, 10); }
.ui-state-error-text { color: rgb(205, 10, 10); background: transparent; }
.ui-state-disabled { opacity: 0.35; }
.ui-priority-primary { font-weight: 700; }
.ui-priority-secondary { opacity: 0.7; font-weight: 400; }
/* Contextual icon sprite sheets */
.ui-widget-content .ui-icon { background-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' data-tone='content'/>"); }
.ui-widget-header .ui-icon { background-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' data-tone='header'/>"); }
.ui-state-default .ui-icon { background-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' data-tone='default'/>"); }
.ui-state-error .ui-icon, .ui-state-error-text .ui-icon { background-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' data-tone='error'/>"); }
.ui-icon-triangle-1-e { background-position: -32px -16px; }
/* Corner radius helpers */
.ui-corner-tl { border-top-left-radius: 4px; }
.ui-corner-tr { border-top-right-radius: 4px; }
.ui-corner-bl { border-bottom-left-radius: 4px; }
.ui-corner-br { border-bottom-right-radius: 4px; }
.ui-corner-top { border-top-left-radius: 4px; border-top-right-radius: 4px; }
.ui-corner-bottom { border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; }
.ui-corner-right { border-top-right-radius: 4px; border-bottom-right-radius: 4px; }
.ui-corner-left { border-top-left-radius: 4px; border-bottom-left-radius: 4px; }
.ui-corner-all { border-radius: 4px; }
/* Overlay & Shadow */
.ui-widget-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgb(170, 170, 170); opacity: 0.3; }
.ui-widget-shadow { margin: -8px 0 0 -8px; padding: 8px; background: rgb(170, 170, 170); opacity: 0.3; border-radius: 8px; }
"""
def main():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page(viewport={"width": 800, "height": 600})
page.set_content(f"""<!DOCTYPE html><html><head><style>{FRAMEWORK_CSS}</style></head><body>
<div id="h-hidden" class="ui-helper-hidden">Hidden</div>
<div id="h-acc" class="ui-helper-hidden-accessible">SR Only</div>
<ul id="h-reset" class="ui-helper-reset"><li>Item</li></ul>
<div id="h-clearfix" class="ui-helper-clearfix"><div style="float:left;height:40px;width:40px;">F</div></div>
<iframe id="h-zfix" class="ui-helper-zfix"></iframe>
<div id="w-root" class="ui-widget">
<div id="w-hdr" class="ui-widget-header"><a id="w-hdr-a" href="#">H</a><span id="ic-hdr" class="ui-icon"></span></div>
<div id="w-cnt" class="ui-widget-content">
<input id="w-inp" type="text" />
<span id="ic-cnt" class="ui-icon ui-icon-triangle-1-e">next</span>
</div>
</div>
<button id="st-def" class="ui-state-default"><span id="ic-def" class="ui-icon"></span>D</button>
<button id="st-hov" class="ui-state-hover">H</button>
<button id="st-foc" class="ui-state-focus">F</button>
<button id="st-act" class="ui-state-active">A</button>
<div id="cue-hi" class="ui-state-highlight">Hi</div>
<div id="cue-err" class="ui-state-error"><span id="ic-err" class="ui-icon"></span>Err</div>
<span id="cue-err-txt" class="ui-state-error-text">ErrTxt</span>
<button id="cue-dis" class="ui-state-default ui-state-disabled">Dis</button>
<button id="pri-1" class="ui-priority-primary">P1</button>
<button id="pri-2" class="ui-priority-secondary">P2</button>
<div id="c-tl" class="ui-corner-tl"></div>
<div id="c-tr" class="ui-corner-tr"></div>
<div id="c-bl" class="ui-corner-bl"></div>
<div id="c-br" class="ui-corner-br"></div>
<div id="c-top" class="ui-corner-top"></div>
<div id="c-bot" class="ui-corner-bottom"></div>
<div id="c-right" class="ui-corner-right"></div>
<div id="c-left" class="ui-corner-left"></div>
<div id="c-all" class="ui-corner-all"></div>
<div id="ov" class="ui-widget-overlay"></div>
<div id="sh" class="ui-widget-shadow"></div>
</body></html>""")
# Case 1: Layout helpers (ui.core.css)
act1 = page.evaluate("""() => ({
hidden_display: getComputedStyle(document.getElementById('h-hidden')).display,
acc_pos_wh: [
getComputedStyle(document.getElementById('h-acc')).position,
getComputedStyle(document.getElementById('h-acc')).width,
getComputedStyle(document.getElementById('h-acc')).height
],
reset_props: [
getComputedStyle(document.getElementById('h-reset')).marginTop,
getComputedStyle(document.getElementById('h-reset')).paddingLeft,
getComputedStyle(document.getElementById('h-reset')).listStyleType
],
clearfix_height: document.getElementById('h-clearfix').offsetHeight,
zfix_pos_opacity: [
getComputedStyle(document.getElementById('h-zfix')).position,
getComputedStyle(document.getElementById('h-zfix')).opacity
]
})""")
exp1 = {
"hidden_display": "none",
"acc_pos_wh": ["absolute", "1px", "1px"],
"reset_props": ["0px", "0px", "none"],
"clearfix_height": 40,
"zfix_pos_opacity": ["absolute", "0"],
}
cases.append({
"name": "layout_helpers_core_css",
"expected": exp1,
"actual": act1,
"passed": act1 == exp1,
})
# Case 2: Widget containers & form control 1em inheritance
act2 = page.evaluate("""() => ({
widget_font_size: getComputedStyle(document.getElementById('w-root')).fontSize,
input_font_size: getComputedStyle(document.getElementById('w-inp')).fontSize,
header_bg: getComputedStyle(document.getElementById('w-hdr')).backgroundColor,
header_link_color: getComputedStyle(document.getElementById('w-hdr-a')).color,
content_bg: getComputedStyle(document.getElementById('w-cnt')).backgroundColor
})""")
exp2 = {
"widget_font_size": "16px",
"input_font_size": "16px",
"header_bg": "rgb(204, 204, 204)",
"header_link_color": "rgb(34, 34, 34)",
"content_bg": "rgb(255, 255, 255)",
}
cases.append({
"name": "widget_containers_and_form_inheritance",
"expected": exp2,
"actual": act2,
"passed": act2 == exp2,
})
# Case 3: Interaction states (4) & cues (6)
act3 = page.evaluate("""() => ({
default_bg: getComputedStyle(document.getElementById('st-def')).backgroundColor,
hover_equals_focus: getComputedStyle(document.getElementById('st-hov')).backgroundColor === getComputedStyle(document.getElementById('st-foc')).backgroundColor,
active_bg: getComputedStyle(document.getElementById('st-act')).backgroundColor,
highlight_bg: getComputedStyle(document.getElementById('cue-hi')).backgroundColor,
error_bg: getComputedStyle(document.getElementById('cue-err')).backgroundColor,
error_text_bg: getComputedStyle(document.getElementById('cue-err-txt')).backgroundColor,
error_text_color: getComputedStyle(document.getElementById('cue-err-txt')).color,
disabled_opacity: getComputedStyle(document.getElementById('cue-dis')).opacity,
primary_weight: getComputedStyle(document.getElementById('pri-1')).fontWeight,
secondary_weight_opacity: [
getComputedStyle(document.getElementById('pri-2')).fontWeight,
getComputedStyle(document.getElementById('pri-2')).opacity
]
})""")
exp3 = {
"default_bg": "rgb(230, 230, 230)",
"hover_equals_focus": True,
"active_bg": "rgb(255, 255, 255)",
"highlight_bg": "rgb(251, 249, 238)",
"error_bg": "rgb(254, 241, 236)",
"error_text_bg": "rgba(0, 0, 0, 0)",
"error_text_color": "rgb(205, 10, 10)",
"disabled_opacity": "0.35",
"primary_weight": "700",
"secondary_weight_opacity": ["400", "0.7"],
}
cases.append({
"name": "interaction_states_and_cues",
"expected": exp3,
"actual": act3,
"passed": act3 == exp3,
})
# Case 4: Icons - 16px box, text hiding, contextual sprite, directional naming
act4 = page.evaluate("""() => {
const ic = getComputedStyle(document.getElementById('ic-cnt'));
return {
size: [ic.width, ic.height],
text_indent: ic.textIndent,
triangle_1_e_pos: ic.backgroundPosition,
tone_content: getComputedStyle(document.getElementById('ic-cnt')).backgroundImage.includes('content'),
tone_header: getComputedStyle(document.getElementById('ic-hdr')).backgroundImage.includes('header'),
tone_default: getComputedStyle(document.getElementById('ic-def')).backgroundImage.includes('default'),
tone_error: getComputedStyle(document.getElementById('ic-err')).backgroundImage.includes('error')
};
}""")
exp4 = {
"size": ["16px", "16px"],
"text_indent": "-99999px",
"triangle_1_e_pos": "-32px -16px",
"tone_content": True,
"tone_header": True,
"tone_default": True,
"tone_error": True,
}
cases.append({
"name": "icon_sizing_context_and_naming",
"expected": exp4,
"actual": act4,
"passed": act4 == exp4,
})
# Case 5: All 9 corner radius helpers + overlay & shadow
act5 = page.evaluate("""() => {
const corners = (id) => {
const s = getComputedStyle(document.getElementById(id));
return [s.borderTopLeftRadius, s.borderTopRightRadius, s.borderBottomRightRadius, s.borderBottomLeftRadius];
};
const ov = document.getElementById('ov');
const sh = getComputedStyle(document.getElementById('sh'));
return {
tl: corners('c-tl'), tr: corners('c-tr'), bl: corners('c-bl'), br: corners('c-br'),
top: corners('c-top'), bottom: corners('c-bot'), right: corners('c-right'), left: corners('c-left'),
all: corners('c-all'),
overlay_rect: [ov.offsetWidth, ov.offsetHeight, getComputedStyle(ov).opacity],
shadow_metrics: [sh.paddingTop, sh.marginTop, sh.marginLeft, sh.opacity]
};
}""")
exp5 = {
"tl": ["4px", "0px", "0px", "0px"],
"tr": ["0px", "4px", "0px", "0px"],
"bl": ["0px", "0px", "0px", "4px"],
"br": ["0px", "0px", "4px", "0px"],
"top": ["4px", "4px", "0px", "0px"],
"bottom": ["0px", "0px", "4px", "4px"],
"right": ["0px", "4px", "4px", "0px"],
"left": ["4px", "0px", "0px", "4px"],
"all": ["4px", "4px", "4px", "4px"],
"overlay_rect": [800, 600, "0.3"],
"shadow_metrics": ["8px", "-8px", "-8px", "0.3"],
}
cases.append({
"name": "corner_radius_overlay_and_shadow",
"expected": exp5,
"actual": act5,
"passed": act5 == exp5,
})
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/jqueryui-theme-api-lab/explore-theme-api.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-theme-api.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联43章) · 含当前章节或实例 · mbskycom202612a916.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a948.zip