"""Offline Playwright Chromium lab for jQuery UI Ch 01 (tutorial)."""
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": 1000, "height": 700})
# Case 1: Accordion fill-space demo behavior (default_example_case for ex_link)
page.set_content("""<!DOCTYPE html>
<html><head><style>
* { box-sizing: border-box; }
.accordion-resizer { width: 420px; height: 260px; padding: 10px; border: 1px solid #ccc; }
.ui-widget { font-family: Arial, sans-serif; font-size: 14px; }
.ui-accordion-header { height: 32px; line-height: 32px; padding: 0 10px; margin: 0; cursor: pointer;
background: #e6e6e6; border: 1px solid #d3d3d3; color: #555; }
.ui-accordion-header.ui-state-active { background: #ffffff; border-color: #aaa; color: #212121; font-weight: bold; }
.ui-accordion-content { padding: 10px; border: 1px solid #aaa; border-top: 0; overflow: auto; display: none; }
.ui-accordion-content.is-open { display: block; }
</style></head><body>
<div class="accordion-resizer ui-widget-content">
<div id="accordion" class="ui-accordion ui-widget">
<h3 class="ui-accordion-header ui-state-default ui-state-active" data-idx="0">Section 1</h3>
<div class="ui-accordion-content ui-widget-content is-open" data-idx="0">Content 1</div>
<h3 class="ui-accordion-header ui-state-default" data-idx="1">Section 2</h3>
<div class="ui-accordion-content ui-widget-content" data-idx="1">Content 2</div>
<h3 class="ui-accordion-header ui-state-default" data-idx="2">Section 3</h3>
<div class="ui-accordion-content ui-widget-content" data-idx="2">Content 3</div>
</div>
</div>
<script>
const resizer = document.querySelector('.accordion-resizer');
const headers = [...document.querySelectorAll('.ui-accordion-header')];
const panels = [...document.querySelectorAll('.ui-accordion-content')];
function applyFillSpace() {
const innerH = resizer.clientHeight - 20; // 10px top + 10px bottom padding
const headersH = headers.reduce((s, h) => s + h.offsetHeight, 0);
const fillH = innerH - headersH;
panels.forEach(p => { p.style.height = fillH + 'px'; });
}
applyFillSpace();
headers.forEach((h, idx) => {
h.addEventListener('click', () => {
headers.forEach((x, i) => x.classList.toggle('ui-state-active', i === idx));
panels.forEach((p, i) => p.classList.toggle('is-open', i === idx));
applyFillSpace();
});
});
</script></body></html>""")
before = page.evaluate("""() => ({
active: document.querySelector('.ui-accordion-header.ui-state-active').textContent,
openIdx: [...document.querySelectorAll('.ui-accordion-content')].findIndex(p => getComputedStyle(p).display === 'block'),
panelHeight: document.querySelector('.ui-accordion-content.is-open').offsetHeight
})""")
page.click('.ui-accordion-header[data-idx="1"]')
after = page.evaluate("""() => ({
active: document.querySelector('.ui-accordion-header.ui-state-active').textContent,
openIdx: [...document.querySelectorAll('.ui-accordion-content')].findIndex(p => getComputedStyle(p).display === 'block'),
panelHeight: document.querySelector('.ui-accordion-content.is-open').offsetHeight,
hiddenCount: [...document.querySelectorAll('.ui-accordion-content')].filter(p => getComputedStyle(p).display === 'none').length
})""")
act1 = {
"before_active": before["active"],
"before_height": before["panelHeight"],
"after_active": after["active"],
"after_open_idx": after["openIdx"],
"after_height": after["panelHeight"],
"hidden_count": after["hiddenCount"],
}
exp1 = {
"before_active": "Section 1",
"before_height": 142,
"after_active": "Section 2",
"after_open_idx": 1,
"after_height": 142,
"hidden_count": 2,
}
cases.append({
"name": "accordion_fillspace_behavior",
"expected": exp1,
"actual": act1,
"passed": act1 == exp1,
})
# Case 2: Stateful widget unified pattern across two widgets
page.set_content("""<!DOCTYPE html><html><body>
<div id="w1"></div><input id="w2" type="text" />
<script>
function createWidgetFactory(name, defaults) {
const store = new WeakMap();
return function(el, arg1, arg2, arg3) {
if (!store.has(el)) {
const opts = Object.assign({}, defaults, typeof arg1 === 'object' ? arg1 : {});
store.set(el, { name, opts });
el.classList.add('ui-widget', 'ui-' + name);
el.dataset.uiWidget = name;
return el;
}
const inst = store.get(el);
if (arg1 === 'option') {
if (arg3 === undefined) return inst.opts[arg2];
inst.opts[arg2] = arg3;
return el;
}
return inst.opts;
};
}
const progressbar = createWidgetFactory('progressbar', { value: 0, max: 100 });
const datepicker = createWidgetFactory('datepicker', { dateFormat: 'mm/dd/yy', changeMonth: false });
const e1 = document.getElementById('w1');
const e2 = document.getElementById('w2');
progressbar(e1, { value: 35 });
progressbar(e1, 'option', 'value', 70);
datepicker(e2, { dateFormat: 'yy-mm-dd' });
datepicker(e2, 'option', 'changeMonth', true);
window.__res = {
w1_classes: [...e1.classList].sort(),
w1_val: progressbar(e1, 'option', 'value'),
w2_classes: [...e2.classList].sort(),
w2_format: datepicker(e2, 'option', 'dateFormat'),
w2_changeMonth: datepicker(e2, 'option', 'changeMonth'),
};
</script></body></html>""")
act2 = page.evaluate("() => window.__res")
exp2 = {
"w1_classes": ["ui-progressbar", "ui-widget"],
"w1_val": 70,
"w2_classes": ["ui-datepicker", "ui-widget"],
"w2_format": "yy-mm-dd",
"w2_changeMonth": True,
}
cases.append({
"name": "stateful_widget_unified_pattern",
"expected": exp2,
"actual": act2,
"passed": act2 == exp2,
})
# Case 3: Demo theme switcher and bookmarkable URL hash
page.set_content("""<!DOCTYPE html><html><head><style>
.theme-smoothness .ui-widget-header { background: rgb(204, 204, 204); color: rgb(34, 34, 34); }
.theme-ui-darkness .ui-widget-header { background: rgb(51, 51, 51); color: rgb(255, 255, 255); }
</style></head><body class="theme-smoothness">
<div id="demo" class="ui-widget"><div class="ui-widget-header">Demo Header</div></div>
<select id="themeSel"><option value="smoothness">Smoothness</option><option value="ui-darkness">UI Darkness</option></select>
<script>
const sel = document.getElementById('themeSel');
sel.addEventListener('change', () => {
document.body.className = 'theme-' + sel.value;
location.hash = 'widget=accordion&theme=' + sel.value;
});
</script></body></html>""")
bg_light = page.evaluate("() => getComputedStyle(document.querySelector('.ui-widget-header')).backgroundColor")
page.select_option("#themeSel", "ui-darkness")
dark_state = page.evaluate("""() => ({
bg: getComputedStyle(document.querySelector('.ui-widget-header')).backgroundColor,
color: getComputedStyle(document.querySelector('.ui-widget-header')).color,
hash: location.hash
})""")
act3 = {"light_bg": bg_light, "dark_bg": dark_state["bg"], "dark_color": dark_state["color"], "hash": dark_state["hash"]}
exp3 = {
"light_bg": "rgb(204, 204, 204)",
"dark_bg": "rgb(51, 51, 51)",
"dark_color": "rgb(255, 255, 255)",
"hash": "#widget=accordion&theme=ui-darkness",
}
cases.append({
"name": "demo_theme_switch_and_bookmark_url",
"expected": exp3,
"actual": act3,
"passed": act3 == exp3,
})
# Case 4: Tutorial curriculum & API reference navigation catalog
page.set_content("""<!DOCTYPE html><html><body>
<nav id="curriculum">
<ul class="chapters">
<li>intro</li><li>download</li><li>use</li><li>custom</li><li>work</li>
<li>theme<ul><li>themeroller</li><li>theme-api</li><li>theme-design</li></ul></li>
<li>widget-factory<ul><li>extend</li><li>method</li><li>why</li><li>how</li></ul></li>
</ul>
<ul class="api-ref">
<li>Effects</li><li>Effects Core</li><li>Interactions</li><li>Method Overrides</li>
<li>Methods</li><li>Selectors</li><li>Theming</li><li>UI Core</li><li>Utilities</li><li>Widgets</li>
</ul>
</nav></body></html>""")
act4 = page.evaluate("""() => ({
top_chapters: document.querySelectorAll('.chapters > li').length,
theme_sub: document.querySelectorAll('.chapters > li:nth-child(6) li').length,
factory_sub: document.querySelectorAll('.chapters > li:nth-child(7) li').length,
api_categories: document.querySelectorAll('.api-ref > li').length,
})""")
exp4 = {"top_chapters": 7, "theme_sub": 3, "factory_sub": 4, "api_categories": 10}
cases.append({
"name": "tutorial_modules_and_api_catalog",
"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-tutorial-lab/explore-tutorial.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a952.zip
课程关联工程包(登记关联43章) · 含当前章节或实例 · mbskycom202612a916.zip