| 1 | // Fiesta's rhythm audit: the runtime half of the spacing rule |
| 2 | // (DESIGN.md, "Checking the rhythm"). It walks a page's content as the |
| 3 | // browser laid it out and reports what a reader sees and a stylesheet |
| 4 | // check cannot: |
| 5 | // |
| 6 | // off-scale two blocks, one under the other, whose gap is not a |
| 7 | // step of the spacing scale (0, or --rst-sp-1 to 6); |
| 8 | // touching text directly above or below a control (a button, a |
| 9 | // field, a disclosure) with less than --rst-sp-2 |
| 10 | // between, unless the text is the control's own <label>; |
| 11 | // heading a heading further from its own content than it is |
| 12 | // from what comes before it, so it reads as belonging to |
| 13 | // the block above (a heading whose content opens with a |
| 14 | // heading or a section is not held to it: the one that |
| 15 | // follows takes its own opening, and is held instead); |
| 16 | // bent a stack (rst-stack) that an app's own rule has made |
| 17 | // something other than the stack's column: its display, |
| 18 | // flex-direction, flex-wrap, align-items or gap is not |
| 19 | // the stack's, which its margins are measured against; |
| 20 | // out-of-flow a stack's child that is absolute or fixed and that the |
| 21 | // stack does not pass over (rst-stack-out, the modal |
| 22 | // overlay, .rst-sr-only, a dialog, a popover), so the stack gives it a margin or |
| 23 | // reads it as the block before the next. |
| 24 | // |
| 25 | // It is one function expression, evaluated in the page: the Go package |
| 26 | // beside it wraps it as rhythm.Expression for a chromedp drive, and |
| 27 | // any other driver can evaluate the file and call it. It reads the |
| 28 | // layout and changes nothing. |
| 29 | // |
| 30 | // It walks the content flow: the root, and every element in it that |
| 31 | // holds blocks, except Fiesta's components (an element carrying an |
| 32 | // rst- name other than the layout ones), whose insides are Fiesta's to |
| 33 | // space and its own gates to check, text blocks (p, headings, lists, |
| 34 | // tables, pre), controls, whatever options.skip names, and the content |
| 35 | // of a closed <details>, which shows only its summary. The two stack |
| 36 | // checks read every stack under the root, components' included, but |
| 37 | // none in a skipped region or a closed <details>. |
| 38 | // |
| 39 | // It returns the findings. With options.result true it returns |
| 40 | // { findings, pairs, headings, stacks } instead: the findings and how |
| 41 | // much it read, the pairs of neighbouring blocks it measured, the |
| 42 | // headings it held to the heading rule and the stacks it checked, so a |
| 43 | // test can tell a page that passed from one it barely saw. |
| 44 | (function fiestaRhythm(root, options) { |
| 45 | options = options || {}; |
| 46 | var tolerance = typeof options.tolerance === "number" && options.tolerance > 0 ? options.tolerance : 0.5; |
| 47 | var skip = options.skip || []; |
| 48 | var start = typeof root === "string" ? document.querySelector(root) : root; |
| 49 | if (!start) { |
| 50 | var missing = [{ kind: "missing", gap: 0, before: "", after: "", message: "no element matches " + root }]; |
| 51 | return options.result ? { findings: missing, pairs: 0, headings: 0, stacks: 0 } : missing; |
| 52 | } |
| 53 | |
| 54 | var htmlStyle = getComputedStyle(document.documentElement); |
| 55 | var rem = parseFloat(htmlStyle.fontSize) || 16; |
| 56 | var defaults = [0.25, 0.5, 0.75, 1, 1.5, 2.5]; |
| 57 | var scale = [0]; |
| 58 | for (var step = 1; step <= 6; step++) { |
| 59 | scale.push(length(htmlStyle.getPropertyValue("--rst-sp-" + step), defaults[step - 1] * rem)); |
| 60 | } |
| 61 | |
| 62 | // length reads a token's value: a rem, px or bare zero length. |
| 63 | function length(value, fallback) { |
| 64 | var m = /^\s*(-?[0-9.]+)(rem|px)?\s*$/.exec(value || ""); |
| 65 | if (!m) return fallback; |
| 66 | var n = parseFloat(m[1]); |
| 67 | return m[2] === "rem" ? n * rem : n; |
| 68 | } |
| 69 | |
| 70 | var layoutNames = { "rst-page": 1, "rst-stack": 1, "rst-section": 1 }; |
| 71 | var textTags = { P: 1, H1: 1, H2: 1, H3: 1, H4: 1, H5: 1, H6: 1, UL: 1, OL: 1, DL: 1, TABLE: 1, PRE: 1, BLOCKQUOTE: 1, FIGURE: 1, SVG: 1, IMG: 1, VIDEO: 1, CANVAS: 1, IFRAME: 1, svg: 1 }; |
| 72 | var headingTags = { H1: 1, H2: 1, H3: 1, H4: 1, H5: 1, H6: 1 }; |
| 73 | |
| 74 | function skipped(el) { |
| 75 | for (var i = 0; i < skip.length; i++) { |
| 76 | if (el.matches(skip[i])) return true; |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | // rstNames is every rst- name an element carries, attribute or class. |
| 82 | function rstNames(el) { |
| 83 | var names = []; |
| 84 | for (var i = 0; i < el.attributes.length; i++) { |
| 85 | var name = el.attributes[i].name; |
| 86 | if (name.indexOf("rst-") === 0) names.push(name); |
| 87 | } |
| 88 | el.classList.forEach(function (c) { |
| 89 | if (c.indexOf("rst-") === 0) names.push(c.split("__")[0].split("--")[0]); |
| 90 | }); |
| 91 | return names; |
| 92 | } |
| 93 | |
| 94 | function isComponent(el) { |
| 95 | var names = rstNames(el); |
| 96 | for (var i = 0; i < names.length; i++) { |
| 97 | if (!layoutNames[names[i]]) return true; |
| 98 | } |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | function isControl(el) { |
| 103 | var tag = el.tagName; |
| 104 | if (tag === "BUTTON" || tag === "SELECT" || tag === "TEXTAREA" || tag === "SUMMARY") return true; |
| 105 | if (tag === "INPUT") return el.type !== "hidden"; |
| 106 | if (el.hasAttribute("rst-btn") || el.classList.contains("rst-btn")) return true; |
| 107 | var role = el.getAttribute("role"); |
| 108 | return role === "button" || role === "switch" || role === "checkbox" || role === "radio" || role === "textbox" || role === "combobox"; |
| 109 | } |
| 110 | |
| 111 | function isHeading(el) { |
| 112 | return !!headingTags[el.tagName] || el.getAttribute("role") === "heading"; |
| 113 | } |
| 114 | |
| 115 | function opensSection(el) { |
| 116 | return el.hasAttribute("rst-section") || el.classList.contains("rst-section"); |
| 117 | } |
| 118 | |
| 119 | // closed is a <details> that is shut: of its children only the |
| 120 | // summary is shown, and the rest is not the page's to measure. |
| 121 | function closed(el) { |
| 122 | return el.tagName === "DETAILS" && !el.open; |
| 123 | } |
| 124 | |
| 125 | // shows says whether a child of parent is drawn as far as a closed |
| 126 | // <details> goes. |
| 127 | function shows(parent, child) { |
| 128 | return !closed(parent) || child.tagName === "SUMMARY"; |
| 129 | } |
| 130 | |
| 131 | function shown(el) { |
| 132 | var style = getComputedStyle(el); |
| 133 | if (style.display === "none" || style.visibility === "hidden") return false; |
| 134 | if (style.position === "absolute" || style.position === "fixed" || style.float !== "none") return false; |
| 135 | var box = el.getBoundingClientRect(); |
| 136 | return box.height > 0 && box.width > 0; |
| 137 | } |
| 138 | |
| 139 | var atomic = { "inline-block": 1, "inline-flex": 1, "inline-grid": 1, "inline-table": 1 }; |
| 140 | |
| 141 | // flow is a container's blocks in order: its block-level children, |
| 142 | // and a control that sits on a line of its own, looking through |
| 143 | // display: contents. A boxless wrapper that is skipped or a component |
| 144 | // is a boundary, null: its children are not the page's to space, and |
| 145 | // the blocks either side of it are not neighbours. |
| 146 | function flow(container) { |
| 147 | var out = []; |
| 148 | for (var el = container.firstElementChild; el; el = el.nextElementSibling) { |
| 149 | if (!shows(container, el)) continue; |
| 150 | var style = getComputedStyle(el); |
| 151 | if (style.display === "contents") { |
| 152 | if (skipped(el) || isComponent(el)) { |
| 153 | if (el.firstElementChild || el.textContent.trim() !== "") out.push(null); |
| 154 | } else { |
| 155 | out.push.apply(out, flow(el)); |
| 156 | } |
| 157 | continue; |
| 158 | } |
| 159 | if (el.tagName === "SCRIPT" || el.tagName === "TEMPLATE") continue; |
| 160 | if (style.display.indexOf("inline") === 0 && !(atomic[style.display] && isControl(el))) continue; |
| 161 | if (shown(el)) out.push(el); |
| 162 | } |
| 163 | return out; |
| 164 | } |
| 165 | |
| 166 | // lineBox is the band a text node's lines take: the content box of |
| 167 | // the block they sit in, found through inline and boxless wrappers, |
| 168 | // so the block's padding and border are space, not text. |
| 169 | function lineBox(node) { |
| 170 | var el = node.parentElement; |
| 171 | while (el && el.parentElement) { |
| 172 | var display = getComputedStyle(el).display; |
| 173 | if (display !== "contents" && display.indexOf("inline") !== 0) break; |
| 174 | el = el.parentElement; |
| 175 | } |
| 176 | return el ? contentBox(el) : null; |
| 177 | } |
| 178 | |
| 179 | // edge finds what an element begins (first) or ends (!first) with, |
| 180 | // looking through wrappers: { kind: "text" or "control", node, box: |
| 181 | // the text's own lines or the control's border box }, or null. |
| 182 | // A skipped region is { kind: "excluded" }: what it holds is not the |
| 183 | // page's, and neither is what lies past it. |
| 184 | function edge(el, first) { |
| 185 | if (skipped(el)) return { kind: "excluded" }; |
| 186 | if (isControl(el)) return { kind: "control", node: el, box: el.getBoundingClientRect() }; |
| 187 | var nodes = el.childNodes; |
| 188 | for (var k = 0; k < nodes.length; k++) { |
| 189 | var node = nodes[first ? k : nodes.length - 1 - k]; |
| 190 | if (node.nodeType === 3) { |
| 191 | if (closed(el) || node.textContent.trim() === "") continue; |
| 192 | var box = lineBox(node); |
| 193 | if (box) return { kind: "text", node: node, box: box }; |
| 194 | continue; |
| 195 | } |
| 196 | if (node.nodeType !== 1 || !shows(el, node)) continue; |
| 197 | var style = getComputedStyle(node); |
| 198 | if (style.display === "none" || style.visibility === "hidden") continue; |
| 199 | var found = edge(node, first); |
| 200 | if (found) return found; |
| 201 | } |
| 202 | return null; |
| 203 | } |
| 204 | |
| 205 | function describe(el) { |
| 206 | var out = el.tagName.toLowerCase(); |
| 207 | if (el.id) out += "#" + el.id; |
| 208 | for (var i = 0; i < el.attributes.length; i++) { |
| 209 | var a = el.attributes[i].name; |
| 210 | if (a !== "id" && a !== "class" && a !== "style" && a.indexOf("aria-") !== 0 && a.indexOf("-") > 0) { |
| 211 | out += "[" + a + "]"; |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | var text = (el.textContent || "").replace(/\s+/g, " ").trim(); |
| 216 | if (text) out += " “" + (text.length > 40 ? text.slice(0, 39) + "…" : text) + "”"; |
| 217 | return out; |
| 218 | } |
| 219 | |
| 220 | // labels says whether a text edge is the label of a control edge: a |
| 221 | // field's own name sits as close to it as the field's design says. |
| 222 | function labels(text, control) { |
| 223 | var label = text.node.parentElement && text.node.parentElement.closest("label"); |
| 224 | return !!label && !!control.node.labels && Array.prototype.indexOf.call(control.node.labels, label) >= 0; |
| 225 | } |
| 226 | |
| 227 | // contentBox is an element's border box less its padding and border: |
| 228 | // where its own text starts and ends. |
| 229 | function contentBox(el) { |
| 230 | var r = el.getBoundingClientRect(); |
| 231 | var cs = getComputedStyle(el); |
| 232 | return { |
| 233 | top: r.top + parseFloat(cs.paddingTop) + parseFloat(cs.borderTopWidth), |
| 234 | bottom: r.bottom - parseFloat(cs.paddingBottom) - parseFloat(cs.borderBottomWidth) |
| 235 | }; |
| 236 | } |
| 237 | |
| 238 | // content is the first (or last) thing a block shows, and where it |
| 239 | // starts and ends: through generic wrappers, whose padding is space, |
| 240 | // to a control or a component (its border box), or |
| 241 | // to text, raw or in inline elements (the content box it sits in). |
| 242 | // It is null when the edge is a region the audit does not read (a |
| 243 | // skipped or component region with no box): what is past it is not |
| 244 | // the neighbour, so nothing is measured across it. |
| 245 | function content(el, first) { |
| 246 | for (;;) { |
| 247 | if (skipped(el)) return null; |
| 248 | if (isComponent(el) || isControl(el)) { |
| 249 | var r = el.getBoundingClientRect(); |
| 250 | return { el: el, top: r.top, bottom: r.bottom }; |
| 251 | } |
| 252 | var text = !!textTags[el.tagName]; |
| 253 | for (var n = el.firstChild; n && !text && !closed(el); n = n.nextSibling) { |
| 254 | if (n.nodeType === 3 && n.textContent.trim() !== "") text = true; |
| 255 | } |
| 256 | var kids = text ? [] : flow(el); |
| 257 | var next = first ? kids[0] : kids[kids.length - 1]; |
| 258 | if (next === null) return null; |
| 259 | if (!next) { |
| 260 | var box = contentBox(el); |
| 261 | return { el: el, top: box.top, bottom: box.bottom }; |
| 262 | } |
| 263 | el = next; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | function onScale(gap) { |
| 268 | for (var i = 0; i < scale.length; i++) { |
| 269 | if (Math.abs(gap - scale[i]) <= tolerance) return true; |
| 270 | } |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | function round(n) { |
| 275 | return Math.round(n * 10) / 10; |
| 276 | } |
| 277 | |
| 278 | var findings = []; |
| 279 | var gapBefore = new Map(); |
| 280 | var nextOf = new Map(); |
| 281 | var firstChildOf = new Map(); |
| 282 | var lastChildOf = new Map(); |
| 283 | var excludedBefore = new Set(); |
| 284 | var headings = []; |
| 285 | var pairs = 0; |
| 286 | var held = 0; |
| 287 | |
| 288 | function walk(container) { |
| 289 | var kids = flow(container); |
| 290 | if (kids.length && kids[0]) firstChildOf.set(kids[0], container); |
| 291 | if (kids.length && kids[kids.length - 1]) lastChildOf.set(kids[kids.length - 1], container); |
| 292 | for (var i = 0; i < kids.length; i++) { |
| 293 | var b = kids[i]; |
| 294 | if (!b) continue; |
| 295 | if (i > 0 && (!kids[i - 1] || skipped(kids[i - 1]))) excludedBefore.add(b); |
| 296 | if (isHeading(b)) headings.push(b); |
| 297 | if (i > 0 && kids[i - 1]) measure(kids[i - 1], b); |
| 298 | if (!skipped(b) && !isComponent(b) && !isControl(b) && !textTags[b.tagName]) walk(b); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | function measure(a, b) { |
| 303 | var ra = a.getBoundingClientRect(); |
| 304 | var rb = b.getBoundingClientRect(); |
| 305 | var stacked = rb.top >= ra.bottom - tolerance && rb.left < ra.right && ra.left < rb.right; |
| 306 | if (!stacked || skipped(a) || skipped(b)) return; |
| 307 | var gap = rb.top - ra.bottom; |
| 308 | pairs++; |
| 309 | gapBefore.set(b, { gap: gap, from: a }); |
| 310 | nextOf.set(a, b); |
| 311 | if (!onScale(gap)) { |
| 312 | findings.push({ |
| 313 | kind: "off-scale", gap: round(gap), before: describe(a), after: describe(b), |
| 314 | message: round(gap) + "px between blocks is not a step of the spacing scale" |
| 315 | }); |
| 316 | } |
| 317 | var above = edge(a, false); |
| 318 | var below = edge(b, true); |
| 319 | if (!above || !below || above.kind === below.kind || above.kind === "excluded" || below.kind === "excluded") return; |
| 320 | if (above.kind === "text" ? labels(above, below) : labels(below, above)) return; |
| 321 | var space = below.box.top - above.box.bottom; |
| 322 | if (space < scale[2] - tolerance) { |
| 323 | findings.push({ |
| 324 | kind: "touching", gap: round(space), before: describe(a), after: describe(b), |
| 325 | message: "text and a control are " + round(space) + "px apart; they need --rst-sp-2 (" + round(scale[2]) + "px) or more" |
| 326 | }); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | // before is the space that opens a heading: from the bottom of the |
| 331 | // block before it, or, when it opens a container, before the |
| 332 | // container, and so on up, to the heading's own top, so a section's |
| 333 | // padding counts. |
| 334 | // |
| 335 | // It is undefined when nothing comes before the heading, and null |
| 336 | // when what does is a region the audit does not read: then the space |
| 337 | // is unknown, and the heading is not held to anything. |
| 338 | function before(h) { |
| 339 | for (var at = h; at && at !== start; at = firstChildOf.get(at)) { |
| 340 | if (excludedBefore.has(at)) return null; |
| 341 | if (gapBefore.has(at)) { |
| 342 | var last = content(gapBefore.get(at).from, false); |
| 343 | return last ? h.getBoundingClientRect().top - last.bottom : null; |
| 344 | } |
| 345 | if (!firstChildOf.has(at)) return undefined; |
| 346 | } |
| 347 | return undefined; |
| 348 | } |
| 349 | |
| 350 | // after is the block a heading's content starts with and the space |
| 351 | // to it: its next block, or, when it ends a wrapper (a header |
| 352 | // holding only the heading), the block after the wrapper. It is null |
| 353 | // when that content opens with a heading or a section: a heading or |
| 354 | // a section after a heading takes its own opening (DESIGN.md, "The |
| 355 | // stack"), and the heading that follows is held to its content. |
| 356 | function after(h) { |
| 357 | for (var at = h; at && at !== start; at = lastChildOf.get(at)) { |
| 358 | if (nextOf.has(at)) { |
| 359 | var block = nextOf.get(at); |
| 360 | if (isHeading(block) || opensSection(block)) return null; |
| 361 | var next = content(block, true); |
| 362 | if (!next || isHeading(next.el)) return null; |
| 363 | return { el: next.el, gap: next.top - h.getBoundingClientRect().bottom }; |
| 364 | } |
| 365 | if (!lastChildOf.has(at)) return null; |
| 366 | } |
| 367 | return null; |
| 368 | } |
| 369 | |
| 370 | walk(start); |
| 371 | |
| 372 | // The space that opens a section is the space before each heading of |
| 373 | // a level; a heading with nothing before it on the page is held to |
| 374 | // the smallest of those for its level. |
| 375 | var opening = {}; |
| 376 | headings.forEach(function (h) { |
| 377 | var gap = before(h); |
| 378 | if (typeof gap === "number") { |
| 379 | opening[h.tagName] = Math.min(opening[h.tagName] === undefined ? Infinity : opening[h.tagName], gap); |
| 380 | } |
| 381 | }); |
| 382 | headings.forEach(function (h) { |
| 383 | var next = after(h); |
| 384 | if (!next) return; |
| 385 | var open = before(h); |
| 386 | if (open === null) return; |
| 387 | if (open === undefined) open = opening[h.tagName]; |
| 388 | if (open === undefined) return; |
| 389 | held++; |
| 390 | if (next.gap >= open - tolerance) { |
| 391 | findings.push({ |
| 392 | kind: "heading", gap: round(next.gap), before: describe(h), after: describe(next.el), |
| 393 | message: "a heading is " + round(next.gap) + "px from its content and " + round(open) + |
| 394 | "px from what comes before it; it should sit closer to its content" |
| 395 | }); |
| 396 | } |
| 397 | }); |
| 398 | |
| 399 | // The stacks under the root, the root included, that are drawn and |
| 400 | // the page's: none in a skipped region or a closed <details>. |
| 401 | function outside(el) { |
| 402 | if (el.closest("details:not([open]) > :not(summary)")) return true; |
| 403 | for (var at = el; at; at = at === start ? null : at.parentElement) { |
| 404 | if (skipped(at)) return true; |
| 405 | } |
| 406 | return false; |
| 407 | } |
| 408 | var stackSel = "[rst-stack], .rst-stack"; |
| 409 | var stacks = Array.prototype.slice.call(start.querySelectorAll(stackSel)); |
| 410 | if (start.matches(stackSel)) stacks.unshift(start); |
| 411 | stacks = stacks.filter(function (el) { |
| 412 | return el.getClientRects().length > 0 && getComputedStyle(el).visibility !== "hidden" && !outside(el); |
| 413 | }); |
| 414 | |
| 415 | // passedOver is what the stack's own rules pass over (tokens.css): |
| 416 | // not drawn, or marked as out of the column. Each spelling of the |
| 417 | // stack passes over its own spelling of the marks, so a child is |
| 418 | // passed over only if every spelling the stack carries passes it: |
| 419 | // under class="rst-stack", [rst-stack-out] still takes the margin. |
| 420 | var passed = ', .rst-sr-only, dialog, [popover], script, style, template'; |
| 421 | var passedBy = { |
| 422 | attr: '[hidden], [type="hidden"], [rst-stack-out], [rst-modal-overlay]' + passed, |
| 423 | cls: '[hidden], [type="hidden"], .rst-stack-out, .rst-modal-overlay' + passed |
| 424 | }; |
| 425 | function passedOver(stack, child) { |
| 426 | if (stack.hasAttribute("rst-stack") && !child.matches(passedBy.attr)) return false; |
| 427 | if (stack.classList.contains("rst-stack") && !child.matches(passedBy.cls)) return false; |
| 428 | return true; |
| 429 | } |
| 430 | stacks.forEach(function (stack) { |
| 431 | var s = getComputedStyle(stack); |
| 432 | var gap = parseFloat(s.rowGap); |
| 433 | if (s.display !== "flex" || s.flexDirection !== "column" || s.flexWrap !== "nowrap" || |
| 434 | (s.alignItems !== "normal" && s.alignItems !== "stretch") || !(Math.abs(gap - scale[2]) <= tolerance)) { |
| 435 | findings.push({ |
| 436 | kind: "bent", gap: isNaN(gap) ? 0 : round(gap), before: describe(stack), after: "", |
| 437 | message: "a stack is " + s.display + ", " + s.flexDirection + ", " + s.flexWrap + ", align-items " + s.alignItems + |
| 438 | ", gap " + s.rowGap + "; the stack is a flex column, nowrap, align-items normal, gap " + round(scale[2]) + |
| 439 | "px: an app rule overrides it" |
| 440 | }); |
| 441 | } |
| 442 | for (var child = stack.firstElementChild; child; child = child.nextElementSibling) { |
| 443 | var cs = getComputedStyle(child); |
| 444 | if (!shows(stack, child) || cs.display === "none" || |
| 445 | (cs.position !== "absolute" && cs.position !== "fixed") || passedOver(stack, child)) continue; |
| 446 | var margin = parseFloat(cs.marginBlockStart) || 0; |
| 447 | findings.push({ |
| 448 | kind: "out-of-flow", gap: round(margin), before: describe(child), after: "", |
| 449 | message: "a stack's child is " + cs.position + " and the stack does not pass it over, so it takes a margin (" + |
| 450 | round(margin) + "px) or stands for the block before the next; mark it rst-stack-out" |
| 451 | }); |
| 452 | } |
| 453 | }); |
| 454 | |
| 455 | return options.result ? { findings: findings, pairs: pairs, headings: held, stacks: stacks.length } : findings; |
| 456 | }) |
| 457 | |