"""Offline Playwright Chromium lab for jQuery UI Ch 07 (theme)."""
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")
def main():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page(viewport={"width": 960, "height": 640})
# Case 1: Semantic state classes consistency across tab, accordion, and button
page.set_content("""<!DOCTYPE html><html><head><style>
.ui-state-default { background: rgb(230, 230, 230); border: 1px solid rgb(211, 211, 211); color: rgb(85, 85, 85); padding: 6px 10px; }
.ui-state-hover { background: rgb(218, 218, 218); border: 1px solid rgb(153, 153, 153); color: rgb(33, 33, 33); }
.ui-state-active { background: rgb(255, 255, 255); border: 1px solid rgb(170, 170, 170); color: rgb(33, 33, 33); }
</style></head><body>
<a id="tab" class="ui-state-default">Tab</a>
<h3 id="acc" class="ui-state-default">Accordion</h3>
<button id="btn" class="ui-state-default">Button</button>
<script>
['tab', 'acc', 'btn'].forEach(id => {
const el = document.getElementById(id);
el.addEventListener('mouseenter', () => el.classList.add('ui-state-hover'));
el.addEventListener('mouseleave', () => el.classList.remove('ui-state-hover'));
el.addEventListener('click', () => el.classList.add('ui-state-active'));
});
</script></body></html>""")
def_bgs = page.evaluate("""() => ['tab', 'acc', 'btn'].map(id => getComputedStyle(document.getElementById(id)).backgroundColor)""")
page.hover("#acc")
hover_bg = page.evaluate("() => getComputedStyle(document.getElementById('acc')).backgroundColor")
page.click("#btn")
active_bg = page.evaluate("() => getComputedStyle(document.getElementById('btn')).backgroundColor")
act1 = {"all_default_same": len(set(def_bgs)) == 1, "default_bg": def_bgs[0], "hover_bg": hover_bg, "active_bg": active_bg}
exp1 = {
"all_default_same": True,
"default_bg": "rgb(230, 230, 230)",
"hover_bg": "rgb(218, 218, 218)",
"active_bg": "rgb(255, 255, 255)",
}
cases.append({
"name": "semantic_state_classes_consistency",
"expected": exp1,
"actual": act1,
"passed": act1 == exp1,
})
# Case 2: Two-layer CSS separation (structural themes/base/jquery.ui.accordion.css vs skin ui.theme.css)
page.set_content("""<!DOCTYPE html><html><head>
<style id="plugin-structural" data-file="themes/base/jquery.ui.accordion.css">
.ui-accordion .ui-accordion-header { display: block; cursor: pointer; position: relative; margin: 2px 0 0 0; padding: 8px 12px; }
</style>
<style id="theme-skin" data-file="ui.theme.css">
.ui-state-default { background: rgb(230, 230, 230); color: rgb(85, 85, 85); border: 1px solid rgb(211, 211, 211); }
</style></head><body>
<div class="ui-accordion"><h3 id="hdr" class="ui-accordion-header ui-state-default">Panel</h3></div>
</body></html>""")
before2 = page.evaluate("""() => {
const s = getComputedStyle(document.getElementById('hdr'));
return { padding: s.padding, position: s.position, bg: s.backgroundColor, color: s.color };
}""")
page.evaluate("""() => {
document.getElementById('theme-skin').textContent =
'.ui-state-default { background: rgb(34, 34, 34); color: rgb(238, 238, 238); border: 1px solid rgb(68, 68, 68); }';
}""")
after2 = page.evaluate("""() => {
const s = getComputedStyle(document.getElementById('hdr'));
return { padding: s.padding, position: s.position, bg: s.backgroundColor, color: s.color };
}""")
act2 = {
"structure_unchanged": before2["padding"] == after2["padding"] and before2["position"] == after2["position"],
"padding": after2["padding"],
"smoothness_bg": before2["bg"],
"dark_theme_bg": after2["bg"],
"dark_theme_color": after2["color"],
}
exp2 = {
"structure_unchanged": True,
"padding": "8px 12px",
"smoothness_bg": "rgb(230, 230, 230)",
"dark_theme_bg": "rgb(34, 34, 34)",
"dark_theme_color": "rgb(238, 238, 238)",
}
cases.append({
"name": "two_layer_css_separation",
"expected": exp2,
"actual": act2,
"passed": act2 == exp2,
})
# Case 3: Comparing 3 theming approaches (ThemeRoller / CSS override / Custom scratch CSS)
page.set_content("""<!DOCTYPE html><html><head><style>
.ui-corner-all { border-radius: 4px; }
.ui-state-default { background: rgb(230, 230, 230); color: rgb(85, 85, 85); }
/* Approach 2: targeted button radius override */
.ui-button.ui-corner-all { border-radius: 14px; }
/* Approach 3: completely custom CSS without framework classes */
.custom-btn { border-radius: 0px; background: rgb(16, 108, 200); color: rgb(255, 255, 255); }
</style></head><body>
<div id="m1" class="ui-state-default ui-corner-all">ThemeRoller Box</div>
<button id="m2" class="ui-button ui-state-default ui-corner-all">Modified Button</button>
<button id="m3" class="custom-btn">Custom Scratch Button</button>
</body></html>""")
act3 = page.evaluate("""() => ({
themeroller_radius: getComputedStyle(document.getElementById('m1')).borderRadius,
override_btn_radius: getComputedStyle(document.getElementById('m2')).borderRadius,
custom_radius: getComputedStyle(document.getElementById('m3')).borderRadius,
custom_bg: getComputedStyle(document.getElementById('m3')).backgroundColor
})""")
exp3 = {
"themeroller_radius": "4px",
"override_btn_radius": "14px",
"custom_radius": "0px",
"custom_bg": "rgb(16, 108, 200)",
}
cases.append({
"name": "three_theming_approaches_comparison",
"expected": exp3,
"actual": act3,
"passed": act3 == exp3,
})
# Case 4: Icon sprite path override & multi-theme CSS scoping on one page
page.set_content("""<!DOCTYPE html><html><head><style>
.theme-smoothness .ui-state-default { background: rgb(230, 230, 230); }
.theme-smoothness .ui-icon { background-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg'/>"); width: 16px; height: 16px; display: inline-block; }
.theme-brand .ui-state-default { background: rgb(25, 118, 210); color: rgb(255, 255, 255); }
</style></head><body>
<div class="theme-smoothness"><span id="s1" class="ui-state-default"><i id="ic1" class="ui-icon"></i>S1</span></div>
<div class="theme-brand"><span id="s2" class="ui-state-default">S2</span></div>
</body></html>""")
act4 = page.evaluate("""() => ({
smoothness_bg: getComputedStyle(document.getElementById('s1')).backgroundColor,
brand_bg: getComputedStyle(document.getElementById('s2')).backgroundColor,
icon_has_bg_image: getComputedStyle(document.getElementById('ic1')).backgroundImage.startsWith('url('),
icon_size: [getComputedStyle(document.getElementById('ic1')).width, getComputedStyle(document.getElementById('ic1')).height]
})""")
exp4 = {
"smoothness_bg": "rgb(230, 230, 230)",
"brand_bg": "rgb(25, 118, 210)",
"icon_has_bg_image": True,
"icon_size": ["16px", "16px"],
}
cases.append({
"name": "sprite_path_and_scoped_multi_theme",
"expected": exp4,
"actual": act4,
"passed": act4 == exp4,
})
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-lab/explore-theme.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-theme.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a949.zip
课程关联工程包(登记关联43章) · 含当前章节或实例 · mbskycom202612a916.zip