在线代码实验室
运行 ThemeRoller 面板调参、Gallery 永久链接复原、themes/all.css 集成与 ThemeRoller-Ready 自建组件离线实验
"""Offline Playwright Chromium lab for jQuery UI Ch 08 (theme-themeroller)."""
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: ThemeRoller 5 control panels updating live preview
page.set_content("""<!DOCTYPE html><html><head><style>
:root {
--ff: Arial, sans-serif; --fs: 13px; --rad: 4px;
--hdr-bg: rgb(204, 204, 204); --cnt-bg: rgb(255, 255, 255);
--def-bg: rgb(230, 230, 230); --act-bg: rgb(255, 255, 255);
--ov-op: 0.3; --sh-pad: 8px;
}
.ui-widget { font-family: var(--ff); font-size: var(--fs); }
.ui-corner-all { border-radius: var(--rad); }
.ui-widget-header { background: var(--hdr-bg); }
.ui-widget-content { background: var(--cnt-bg); }
.ui-state-default { background: var(--def-bg); }
.ui-state-active { background: var(--act-bg); }
.ui-widget-overlay { opacity: var(--ov-op); }
.ui-widget-shadow { padding: var(--sh-pad); }
</style></head><body>
<div id="w" class="ui-widget ui-widget-content ui-corner-all">
<div id="h" class="ui-widget-header ui-corner-all">Header</div>
<button id="b" class="ui-state-default ui-state-active">Btn</button>
</div>
<div id="ov" class="ui-widget-overlay"></div>
<div id="sh" class="ui-widget-shadow"></div>
<script>
window.applyTheme = (cfg) => {
const r = document.documentElement.style;
for (const [k, v] of Object.entries(cfg)) r.setProperty(k, v);
};
</script></body></html>""")
page.evaluate("""() => window.applyTheme({
'--fs': '15px', '--rad': '10px', '--hdr-bg': 'rgb(246, 168, 40)',
'--act-bg': 'rgb(255, 240, 165)', '--ov-op': '0.5', '--sh-pad': '6px'
})""")
act1 = page.evaluate("""() => ({
font_size: getComputedStyle(document.getElementById('w')).fontSize,
radius: getComputedStyle(document.getElementById('w')).borderRadius,
header_bg: getComputedStyle(document.getElementById('h')).backgroundColor,
active_bg: getComputedStyle(document.getElementById('b')).backgroundColor,
overlay_opacity: getComputedStyle(document.getElementById('ov')).opacity,
shadow_padding: getComputedStyle(document.getElementById('sh')).paddingTop
})""")
exp1 = {
"font_size": "15px",
"radius": "10px",
"header_bg": "rgb(246, 168, 40)",
"active_bg": "rgb(255, 240, 165)",
"overlay_opacity": "0.5",
"shadow_padding": "6px",
}
cases.append({
"name": "themeroller_panel_controls_simulation",
"expected": exp1,
"actual": act1,
"passed": act1 == exp1,
})
# Case 2: Gallery preset -> Roll Your Own customization -> Permalink URL restore
page.set_content("""<!DOCTYPE html><html><body><script>
const GALLERY = {
'ui-lightness': { bgColorHeader: 'f6a828', cornerRadius: '4px', fwDefault: 'bold' },
'ui-darkness': { bgColorHeader: '333333', cornerRadius: '6px', fwDefault: 'normal' }
};
const customized = Object.assign({}, GALLERY['ui-lightness'], { cornerRadius: '8px' });
const permalink = 'https://jqueryui.com/themeroller/?' + new URLSearchParams(customized).toString();
const parsed = Object.fromEntries(new URL(permalink).searchParams.entries());
window.__gal = {
gallery_count: Object.keys(GALLERY).length,
permalink_has_query: permalink.includes('bgColorHeader=f6a828'),
restored: parsed
};
</script></body></html>""")
act2 = page.evaluate("() => window.__gal")
exp2 = {
"gallery_count": 2,
"permalink_has_query": True,
"restored": {"bgColorHeader": "f6a828", "cornerRadius": "8px", "fwDefault": "bold"},
}
cases.append({
"name": "gallery_preset_and_permalink_restore",
"expected": exp2,
"actual": act2,
"passed": act2 == exp2,
})
# Case 3: themes/ folder and themes/all.css integration
page.set_content("""<!DOCTYPE html><html><head>
<style id="themes-all-css" data-href="themes/all.css">
.ui-widget { font-size: 14px; }
.ui-widget-header { background: rgb(92, 156, 204); color: rgb(255, 255, 255); }
.ui-widget-content { border: 1px solid rgb(166, 201, 226); background: rgb(252, 253, 253); }
.ui-icon { display: inline-block; width: 16px; height: 16px; }
</style></head><body>
<div id="box" class="ui-widget ui-widget-content">
<div id="hdr" class="ui-widget-header"><span id="ico" class="ui-icon"></span>Title</div>
</div></body></html>""")
act3 = page.evaluate("""() => ({
linked_href: document.getElementById('themes-all-css').dataset.href,
folder_name: 'themes',
header_bg: getComputedStyle(document.getElementById('hdr')).backgroundColor,
content_bg: getComputedStyle(document.getElementById('box')).backgroundColor,
icon_dim: [getComputedStyle(document.getElementById('ico')).width, getComputedStyle(document.getElementById('ico')).height]
})""")
exp3 = {
"linked_href": "themes/all.css",
"folder_name": "themes",
"header_bg": "rgb(92, 156, 204)",
"content_bg": "rgb(252, 253, 253)",
"icon_dim": ["16px", "16px"],
}
cases.append({
"name": "themes_folder_all_css_integration",
"expected": exp3,
"actual": act3,
"passed": act3 == exp3,
})
# Case 4: Custom ThemeRoller-Ready widget adapting automatically across themes
page.set_content("""<!DOCTYPE html><html><head><style>
.theme-light .ui-widget-header { background: rgb(224, 224, 224); color: rgb(34, 34, 34); }
.theme-light .ui-state-default { background: rgb(242, 242, 242); }
.theme-dark .ui-widget-header { background: rgb(51, 51, 51); color: rgb(255, 255, 255); }
.theme-dark .ui-state-default { background: rgb(85, 85, 85); }
.ui-helper-hidden { display: none; }
</style></head><body class="theme-light">
<section id="card" class="ui-widget ui-widget-content ui-corner-all">
<header id="cardHdr" class="ui-widget-header ui-state-default ui-corner-top">Custom Card</header>
<div id="cardBody" class="ui-widget-content">Body</div>
</section>
<script>
document.getElementById('cardHdr').addEventListener('click', () => {
document.getElementById('cardBody').classList.toggle('ui-helper-hidden');
});
</script></body></html>""")
light_hdr = page.evaluate("() => getComputedStyle(document.getElementById('cardHdr')).backgroundColor")
page.click("#cardHdr")
body_hidden = page.evaluate("() => getComputedStyle(document.getElementById('cardBody')).display")
page.evaluate("() => { document.body.className = 'theme-dark'; }")
dark_hdr = page.evaluate("() => getComputedStyle(document.getElementById('cardHdr')).backgroundColor")
act4 = {"light_bg": light_hdr, "dark_bg": dark_hdr, "body_after_toggle": body_hidden}
exp4 = {
"light_bg": "rgb(242, 242, 242)",
"dark_bg": "rgb(85, 85, 85)",
"body_after_toggle": "none",
}
cases.append({
"name": "themeroller_ready_custom_widget",
"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-themeroller-lab/explore-theme-themeroller.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-theme-themeroller.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a951.zip
课程关联工程包(登记关联43章) · 含当前章节或实例 · mbskycom202612a916.zip