Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion scripts/check-editor-browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@
const response = await send('Runtime.evaluate', {expression: '({status: document.querySelector("#checks")?.dataset.result, text: document.querySelector("#checks")?.textContent})', returnByValue: true});
result = response.result.value;
} while ((!result?.status || result.status === 'running') && Date.now() < deadline);
if (result?.status === 'passed' && new URL(url).searchParams.has('layout')) {
// Check computed accessible names, not title attributes or textContent:
// display:none labels and accidental aria-label overrides must fail too.
const {root} = await send('DOM.getDocument');
const kind = new URL(url).searchParams.get('layout');
const names = kind === 'index' ? [] : [
['reset-btn', /^Reset$/], ['format-btn', /^Format$/],
...(kind === 'exercise' ? [['vscode-btn', /^Open in Web Editor$/], ['copy-btn', /^Copy code$/]] : []),
];
names.push(['run-btn', /\bRun\b/]);
if (kind === 'exercise') names.push(['submit-btn', /\bSubmit(?:ted)?\b/]);
try {
for (const [role, expected] of names) {
const {nodeId} = await send('DOM.querySelector', {nodeId: root.nodeId, selector: `.exercise-section [data-role="${role}"]`});
if (!nodeId) throw new Error(`Missing accessible control: ${role}`);
const {nodes} = await send('Accessibility.getPartialAXTree', {nodeId, fetchRelatives: false});
const control = nodes[0];
// Submit is intentionally hidden again after a later source revision.
if (role === 'submit-btn' && control?.ignored) continue;
if (!control || control.ignored || !expected.test(control.name?.value || '')) {
throw new Error(`${role}: expected accessible name ${expected}, got ${JSON.stringify(control)}`);
}
}
result.text += '\nProduction toolbar accessible names verified in Chrome accessibility tree.';
} catch (error) {
result = {status: 'failed', text: result.text + '\nFAILED: ' + error.message};
}
}
if (screenshot) {
const image = await send('Page.captureScreenshot');
await writeFile(screenshot, Buffer.from(image.data, 'base64'));
Expand Down Expand Up @@ -183,7 +211,7 @@ def main():
cases.append(("workflow", "", (1440, 1400), args.screenshot))
if args.suite != "workflow":
for template in [args.template] if args.template else ["exercise", "playground", "tour", "index"]:
for width, height in [(375, 500), (800, 450), (1440, 900)]:
for width, height in [(320, 500), (375, 500), (800, 450), (1440, 900)]:
name = f"{template}-{width}x{height}"
screenshot = args.screenshot_dir / f"{name}.png" if args.screenshot_dir else None
cases.append((name, f"?layout={template}", (width, height), screenshot))
Expand Down
67 changes: 67 additions & 0 deletions scripts/editor-browser.html
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,77 @@ <h1>Inline editor regression checks</h1>
const toolbar = role("editor-toolbar");
const panel = role("output-panel");
const rect = element => element.getBoundingClientRect();
const mobile = matchMedia("(max-width: 640px)").matches;
const compactActions = kind === "index" ? [] : [
["reset-btn", "Reset"], ["format-btn", "Format"],
...(kind === "exercise" ? [["vscode-btn", "Open in Web Editor"]] : []),
];
// Copy is positioned over the code, outside the sticky toolbar. It need not
// be in the viewport during output checks, but must still be a touch target.
const copy = role("copy-btn");
if (kind === "exercise") assert(!!copy, "Exercise has a code copy control");
if (copy) {
assert(!toolbar.contains(copy), "Code copy control is outside the toolbar");
if (mobile) assert(rect(copy).width >= 44 && rect(copy).height >= 44, "Mobile code copy target is at least 44×44");
}
const visibleControls = label => {
const head = rect(toolbar);
const bar = rect(topbar);
assert(head.top >= bar.bottom - 1 && head.bottom <= innerHeight, `${label}: toolbar fits below navigation`);
const actions = toolbar.querySelector(".exercise-actions");
const buttons = [...toolbar.querySelectorAll(".btn")].filter(button => button.getClientRects().length);
if (mobile) {
const layout = getComputedStyle(toolbar);
const actionLayout = getComputedStyle(actions);
const area = rect(actions);
assert(layout.display === "flex" && layout.flexDirection === "column", `${label}: mobile toolbar stacks vertically`);
assert(actionLayout.display === "flex" && actionLayout.flexWrap === "wrap" && actionLayout.justifyContent === "flex-start", `${label}: mobile actions wrap and align left`);
const contentLeft = head.left + parseFloat(layout.borderLeftWidth) + parseFloat(layout.paddingLeft);
const contentRight = head.right - parseFloat(layout.borderRightWidth) - parseFloat(layout.paddingRight);
assert(Math.abs(area.left - contentLeft) <= 1 && Math.abs(area.right - contentRight) <= 1, `${label}: mobile actions span the toolbar content width`);
for (const button of buttons) {
const r = rect(button);
assert(r.width >= 44 && r.height >= 44, `${label}: ${button.dataset.role} mobile target is at least 44×44`);
const firstInRow = !buttons.some(other => Math.abs(rect(other).top - r.top) <= 1 && rect(other).left < r.left);
if (firstInRow) assert(Math.abs(r.left - area.left) <= 1, `${label}: mobile button row starts at the left edge`);
}
const status = role("action-status");
const statusLayout = getComputedStyle(status);
if (!status.textContent.trim()) {
assert(statusLayout.display === "none" && status.getClientRects().length === 0, `${label}: empty mobile status occupies no space`);
} else {
const r = rect(status);
assert(statusLayout.order === "1" && statusLayout.flexBasis === "100%", `${label}: mobile status is ordered as a full row`);
assert(r.height > 0 && statusLayout.visibility === "visible" && Number(statusLayout.opacity) > 0, `${label}: nonempty mobile status is visible`);
assert(Math.abs(r.left - area.left) <= 1 && Math.abs(r.right - area.right) <= 1 && r.bottom <= head.bottom + 1, `${label}: mobile status spans the action row within the toolbar`);
assert(buttons.every(button => r.top >= rect(button).bottom - 1), `${label}: mobile status occupies a separate row after all buttons`);
}
for (const cluster of actions.querySelectorAll(".actions-cluster-start")) {
assert(getComputedStyle(cluster).marginLeft === "0px", `${label}: mobile fullscreen cluster has no leading margin`);
}
}
for (const [name, text] of compactActions) {
const button = role(name);
const icon = button?.querySelector("svg.editor-action-icon");
const caption = button?.querySelector(".editor-action-label");
assert(!!icon && !!caption && caption.textContent.trim() === text, `${label}: ${name} retains its SVG and text label`);
assert(icon.getAttribute("aria-hidden") === "true", `${label}: ${name} icon is decorative`);
const captionStyle = getComputedStyle(caption);
const iconStyle = getComputedStyle(icon);
assert(!caption.closest('[aria-hidden="true"], [hidden], [inert]') && captionStyle.display !== "none" && captionStyle.visibility === "visible", `${label}: ${name} label remains available to accessibility`);
if (mobile) {
assert(iconStyle.display !== "none" && rect(icon).width > 1 && rect(icon).height > 1, `${label}: ${name} mobile icon is visible`);
assert(captionStyle.position === "absolute" && rect(caption).width <= 1 && rect(caption).height <= 1 && captionStyle.overflow === "hidden" && (captionStyle.clipPath !== "none" || captionStyle.clip !== "auto"), `${label}: ${name} mobile label is visually hidden, not removed`);
} else {
assert(iconStyle.display === "none" && icon.getClientRects().length === 0, `${label}: ${name} desktop icon is hidden`);
assert(rect(caption).width > 1 && rect(caption).height > 1 && captionStyle.clipPath === "none" && captionStyle.clip === "auto" && Number(captionStyle.opacity) > 0, `${label}: ${name} desktop label is presented normally`);
}
}
for (const name of ["run-btn", "submit-btn"]) {
const button = role(name);
if (!button?.getClientRects().length) continue;
assert(/Run|Submit/.test(button.innerText) && !button.querySelector(".editor-action-label"), `${label}: ${name} keeps its visible text label`);
}
for (const button of toolbar.querySelectorAll("button, a")) {
if (!button.getClientRects().length) continue;
const r = rect(button);
Expand Down
63 changes: 63 additions & 0 deletions static/css/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,69 @@ samp,
flex-wrap: wrap;
min-width: 0;
}
.editor-action-icon {
display: none;
}
@media (max-width: 640px) {
.exercise-section > [data-role="editor-toolbar"] {
flex-direction: column;
align-items: stretch;
gap: 0.5rem;
}
[data-role="editor-toolbar"] > :empty {
display: none;
}
[data-role="editor-toolbar"] .exercise-actions {
width: 100%;
justify-content: flex-start;
gap: 0.5rem;
}
[data-role="editor-toolbar"] .exercise-actions .action-status {
order: 1;
flex-basis: 100%;
height: auto;
padding: 0.25rem 0;
white-space: normal;
}
[data-role="editor-toolbar"] .action-status:empty {
display: none;
}
[data-role="editor-toolbar"] .btn,
.exercise-section [data-role="copy-btn"] {
min-width: 44px;
min-height: 44px;
justify-content: center;
}
/* Saving changes the label to "Submitted"; keep its row stable. */
[data-role="editor-toolbar"] [data-role="submit-btn"] {
min-width: 8rem;
}
[data-role="editor-toolbar"] .btn:has(.editor-action-icon) {
width: 44px;
padding: 0;
}
[data-role="editor-toolbar"] .editor-action-icon {
display: block;
width: 20px;
height: 20px;
flex-shrink: 0;
}
/* Keep the name available to screen readers when only the icon is visible. */
[data-role="editor-toolbar"] .editor-action-label {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}
[data-role="editor-toolbar"] .exercise-actions .actions-cluster-start {
margin-left: 0;
}
}
[data-role="output-panel"] {
scroll-margin-top: calc(
var(--editor-topbar-height, 3rem) + var(--editor-toolbar-height, 0px) + 1rem
Expand Down
9 changes: 6 additions & 3 deletions templates/exercise.html
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ <h2 class="section-title">{{ title }}</h2>
data-role="reset-btn"
title="Replace your edits with the original starter code"
>
Reset
<svg class="editor-action-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 10a9 9 0 1 1 2 8M3 4v6h6" /></svg>
<span class="editor-action-label">Reset</span>
</button>
{% endif %} {% if exercise.shows_button("vscode") %}
<a
Expand All @@ -216,7 +217,8 @@ <h2 class="section-title">{{ title }}</h2>
rel="noopener"
title="Open this file on github.dev (a browser-based VS Code)"
>
Open in Web Editor
<svg class="editor-action-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="3" y="3" width="18" height="18" rx="2" /><path d="m9 8-4 4 4 4m6-8 4 4-4 4" /></svg>
<span class="editor-action-label">Open in Web Editor</span>
</a>
{% endif %} {% if exercise.shows_button("format") %}
<button
Expand All @@ -225,7 +227,8 @@ <h2 class="section-title">{{ title }}</h2>
data-role="format-btn"
title="Format the code with rustfmt"
>
Format
<svg class="editor-action-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 5h16M10 10h10M10 15h10M4 20h16M3 10l3 2.5L3 15" /></svg>
<span class="editor-action-label">Format</span>
</button>
{% endif %} {% if exercise.shows_button("run") %}
<button
Expand Down
6 changes: 4 additions & 2 deletions templates/playground.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ <h1 class="exercise-title">Rust Playground</h1>
data-role="reset-btn"
title="Replace the buffer with the starter snippet"
>
Reset
<svg class="editor-action-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 10a9 9 0 1 1 2 8M3 4v6h6" /></svg>
<span class="editor-action-label">Reset</span>
</button>
<button
type="button"
Expand Down Expand Up @@ -78,7 +79,8 @@ <h1 class="exercise-title">Rust Playground</h1>
data-role="format-btn"
title="Format the code with rustfmt"
>
Format
<svg class="editor-action-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 5h16M10 10h10M10 15h10M4 20h16M3 10l3 2.5L3 15" /></svg>
<span class="editor-action-label">Format</span>
</button>
<button
type="button"
Expand Down
6 changes: 4 additions & 2 deletions templates/tour.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@ <h1 class="exercise-title">A Quick Tour of Rust</h1>
data-role="reset-btn"
title="Restore the original tour code"
>
Reset
<svg class="editor-action-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 10a9 9 0 1 1 2 8M3 4v6h6" /></svg>
<span class="editor-action-label">Reset</span>
</button>
<button
type="button"
class="btn btn-secondary"
data-role="format-btn"
title="Format the code with rustfmt"
>
Format
<svg class="editor-action-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 5h16M10 10h10M10 15h10M4 20h16M3 10l3 2.5L3 15" /></svg>
<span class="editor-action-label">Format</span>
</button>
<button
type="button"
Expand Down
Loading