"""Offline Playwright Chromium lab for jQuery UI Ch 10 (theme-design)."""
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")
DUAL_THEME_HTML = """<!DOCTYPE html>
<html>
<head>
<title>My Site</title>
<style id="css-base" data-href="themename/themename.css">
body.ui-themename { background: #111; color: snow; }
.ui-themename a, a.ui-themename { color: #68D; outline: none; }
.ui-themename a:visited, a.ui-themename:visited { color: #D66; }
.ui-themename a:hover, a.ui-themename:hover { color: #FFF; }
</style>
<style id="css-other" data-href="othertheme/othertheme.css">
.ui-othertheme { background: rgb(245, 247, 250); color: rgb(30, 30, 30); padding: 12px; }
.ui-othertheme a { color: rgb(0, 102, 204); }
</style>
<style id="css-other-dialog" data-href="othertheme/othertheme.dialog.css">
.ui-othertheme .ui-dialog { border: 2px solid rgb(0, 102, 204); background: rgb(255, 255, 255); color: rgb(20, 20, 20); padding: 14px; }
</style>
</head>
<body class="ui-themename">
<p id="mainText">Main themed text <a id="mainLink" href="#page">Page Link</a></p>
<div id="otherWrap" class="ui-othertheme">
<div id="modalDlg" class="ui-dialog">This is a modal dialog.</div>
<a id="otherLink" href="#dlg">Dialog Link</a>
</div>
<div id="outerDlg" class="ui-dialog">Unscoped dialog</div>
</body>
</html>"""
def main():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page(viewport={"width": 960, "height": 640})
# Case 1: Theme directory & plugin CSS naming convention
page.set_content("""<!DOCTYPE html><html><body><script>
const files = [
'themename/themename.css',
'themename/themename.tabs.css',
'themename/themename.dialog.css',
'themename/img.png'
];
const validCss = files.filter(f => f.endsWith('.css'));
const typoCheck = 'themename.tabs.js'.endsWith('.css');
window.__dir = {
folder: 'themename/',
base_css: 'themename/themename.css',
plugin_css_files: validCss.filter(f => f !== 'themename/themename.css'),
image_file: 'themename/img.png',
js_ext_is_css: typoCheck
};
</script></body></html>""")
act1 = page.evaluate("() => window.__dir")
exp1 = {
"folder": "themename/",
"base_css": "themename/themename.css",
"plugin_css_files": ["themename/themename.tabs.css", "themename/themename.dialog.css"],
"image_file": "themename/img.png",
"js_ext_is_css": False,
}
cases.append({
"name": "theme_directory_structure_convention",
"expected": exp1,
"actual": act1,
"passed": act1 == exp1,
})
# Case 2: Dual-theme root class scoping (.ui-themename on body + .ui-othertheme subtree)
page.set_content(DUAL_THEME_HTML)
act2 = page.evaluate("""() => ({
body_bg: getComputedStyle(document.body).backgroundColor,
body_color: getComputedStyle(document.body).color,
other_wrap_bg: getComputedStyle(document.getElementById('otherWrap')).backgroundColor,
modal_dlg_bg: getComputedStyle(document.getElementById('modalDlg')).backgroundColor,
modal_dlg_border: getComputedStyle(document.getElementById('modalDlg')).borderTopColor,
other_link_color: getComputedStyle(document.getElementById('otherLink')).color
})""")
exp2 = {
"body_bg": "rgb(17, 17, 17)",
"body_color": "rgb(255, 250, 250)",
"other_wrap_bg": "rgb(245, 247, 250)",
"modal_dlg_bg": "rgb(255, 255, 255)",
"modal_dlg_border": "rgb(0, 102, 204)",
"other_link_color": "rgb(0, 102, 204)",
}
cases.append({
"name": "dual_theme_root_class_scoping",
"expected": exp2,
"actual": act2,
"passed": act2 == exp2,
})
# Case 3: Base themename.css link states (:hover) and root-class enable/disable toggle
normal_link = page.evaluate("""() => ({
color: getComputedStyle(document.getElementById('mainLink')).color,
outlineStyle: getComputedStyle(document.getElementById('mainLink')).outlineStyle
})""")
page.hover("#mainLink")
hover_link = page.evaluate("() => getComputedStyle(document.getElementById('mainLink')).color")
page.evaluate("() => document.body.classList.remove('ui-themename')")
disabled_body_bg = page.evaluate("() => getComputedStyle(document.body).backgroundColor")
act3 = {
"link_normal_color": normal_link["color"],
"link_outline": normal_link["outlineStyle"],
"link_hover_color": hover_link,
"body_bg_when_root_class_removed": disabled_body_bg,
}
exp3 = {
"link_normal_color": "rgb(102, 136, 221)",
"link_outline": "none",
"link_hover_color": "rgb(255, 255, 255)",
"body_bg_when_root_class_removed": "rgba(0, 0, 0, 0)",
}
cases.append({
"name": "base_css_link_states_and_toggle",
"expected": exp3,
"actual": act3,
"passed": act3 == exp3,
})
# Case 4: Plugin CSS isolation from base themename.css
page.set_content(DUAL_THEME_HTML)
act4 = page.evaluate("""() => ({
scoped_dialog_padding: getComputedStyle(document.getElementById('modalDlg')).paddingTop,
unscoped_dialog_padding: getComputedStyle(document.getElementById('outerDlg')).paddingTop,
stylesheets_loaded: [...document.querySelectorAll('style[data-href]')].map(s => s.dataset.href)
})""")
exp4 = {
"scoped_dialog_padding": "14px",
"unscoped_dialog_padding": "0px",
"stylesheets_loaded": [
"themename/themename.css",
"othertheme/othertheme.css",
"othertheme/othertheme.dialog.css",
],
}
cases.append({
"name": "plugin_css_isolation_from_base",
"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-design-lab/explore-theme-design.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-theme-design.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a950.zip
课程关联工程包(登记关联43章) · 含当前章节或实例 · mbskycom202612a916.zip