// audit-page.js -- injected by audit.py into a copy of a built page. // // In light, dark, light + high contrast and dark + high contrast it measures // * the contrast of every visible text node against its composited ground // (opacity included; a disabled control is exempt, WCAG 1.4.3), and // * horizontal overflow: text ranges and element boxes past the viewport, // ignoring content inside a box that scrolls itself. // Every
is opened first, so hints and solutions are measured too. // The result goes, as JSON, into body[data-audit]. (function () { var cv = document.createElement('canvas'); cv.width = cv.height = 1; var cx = cv.getContext('2d', { willReadFrequently: true }); // any css colour (oklch, color-mix ...) -> [r, g, b, a], via a 1x1 canvas function rgb(c) { cx.clearRect(0, 0, 1, 1); cx.fillStyle = '#000'; cx.fillStyle = c; cx.fillRect(0, 0, 1, 1); var d = cx.getImageData(0, 0, 1, 1).data; return [d[0], d[1], d[2], d[3] / 255]; } function lum(p) { var a = p.slice(0, 3).map(function (v) { v /= 255; return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4); }); return 0.2126 * a[0] + 0.7152 * a[1] + 0.0722 * a[2]; } function blend(fg, bg) { var a = fg[3]; return [fg[0] * a + bg[0] * (1 - a), fg[1] * a + bg[1] * (1 - a), fg[2] * a + bg[2] * (1 - a), 1]; } // the ground under an element: its translucent layers composited on the page function bgOf(el) { var layers = []; for (var e = el; e; e = e.parentElement) { var c = rgb(getComputedStyle(e).backgroundColor); if (c[3] > 0) { layers.push(c); if (c[3] >= 0.99) break; } } var base = rgb(getComputedStyle(document.body).backgroundColor); if (base[3] < 0.99) base = [255, 255, 255, 1]; for (var i = layers.length - 1; i >= 0; i--) base = blend(layers[i], base); return base; } function ratio(f, b) { var L1 = lum(f), L2 = lum(b); return (Math.max(L1, L2) + 0.05) / (Math.min(L1, L2) + 0.05); } function label(el) { var s = el.tagName.toLowerCase(); if (el.id) s += '#' + el.id; var cl = (typeof el.className === 'string' ? el.className : '').trim().split(/\s+/).filter(Boolean).slice(0, 2); if (cl.length) s += '.' + cl.join('.'); var p = el.parentElement; if (p) { var pc = (typeof p.className === 'string' ? p.className : '').trim().split(/\s+/).filter(Boolean)[0]; s = (p.tagName.toLowerCase() + (pc ? '.' + pc : '')) + ' > ' + s; } var box = el.closest('.exo, .exo-hint, .exo-sol, .further, .callout, .quiz, .gls-inline, .calc-note, .cell-output, nav, .sidebar, header, footer'); if (box) s += ' [in ' + box.tagName.toLowerCase() + '.' + String(box.className).trim().split(/\s+/)[0] + ']'; return s; } function visible(el) { if (!el.getClientRects().length) return false; var cs = getComputedStyle(el); if (cs.visibility === 'hidden' || +cs.opacity === 0) return false; for (var e = el; e; e = e.parentElement) if (+getComputedStyle(e).opacity < 0.05) return false; return true; } function scrollsX(el) { for (var e = el; e && e !== document.body; e = e.parentElement) { var o = getComputedStyle(e).overflowX; if (o === 'auto' || o === 'scroll' || o === 'hidden' || o === 'clip') return true; } return false; } function measure() { var W = window.innerWidth, worst = [], over = []; var walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT); var seen = new Set(), n; while ((n = walker.nextNode())) { if (!n.nodeValue.trim()) continue; var el = n.parentElement; if (!el || seen.has(el)) continue; seen.add(el); if (/^(SCRIPT|STYLE|NOSCRIPT|TEMPLATE)$/.test(el.tagName)) continue; if (el.closest('mjx-container, .MathJax, svg, [aria-hidden="true"], .visually-hidden, .sr-only')) continue; if (!visible(el)) continue; // a disabled control is exempt from the contrast minimum (WCAG 1.4.3) if (el.closest('[disabled], [aria-disabled="true"], .exo-locked')) continue; var cs = getComputedStyle(el), f = rgb(cs.color); // opacity on the element or any ancestor fades the text into its ground var op = 1; for (var oe = el; oe; oe = oe.parentElement) op *= +getComputedStyle(oe).opacity; f = [f[0], f[1], f[2], f[3] * op]; var b = bgOf(el); f = blend(f, b); var r = ratio(f, b); worst.push({ r: +r.toFixed(2), el: label(el), text: n.nodeValue.trim().slice(0, 40), fg: cs.color }); if (!scrollsX(el)) { var rg = document.createRange(); rg.selectNodeContents(n); var rr = rg.getBoundingClientRect(); if (rr.width && (rr.right > W + 1 || rr.left < -1)) over.push({ el: label(el), right: Math.round(rr.right), text: n.nodeValue.trim().slice(0, 40) }); } } document.querySelectorAll('body *').forEach(function (el) { if (!el.getClientRects().length || scrollsX(el)) return; var r = el.getBoundingClientRect(); if (r.width && r.right > W + 1 && getComputedStyle(el).position !== 'fixed') over.push({ el: label(el), right: Math.round(r.right) }); }); worst.sort(function (a, b) { return a.r - b.r; }); var fails = worst.filter(function (w) { return w.r < 4.5; }); return { worst: worst.slice(0, 3), fails: fails.length, failList: fails.slice(0, 8), overflow: over.slice(0, 6), overflowCount: over.length, docWidth: document.documentElement.scrollWidth, W: W }; } function run() { var st = document.createElement('style'); st.textContent = '*,*::before,*::after{transition:none!important;animation:none!important}'; document.head.appendChild(st); // measure what a reader can unfold: hints, solutions, notes document.querySelectorAll('details').forEach(function (d) { d.open = true; }); var hasToggle = typeof window.quartoToggleColorScheme === 'function'; var out = { hasDarkToggle: hasToggle, hasHcButton: !!document.querySelector('.notes-contrast-toggle'), modes: [] }; var M = [[false, false], [true, false], [false, true], [true, true]], i = 0; // Quarto loads the dark stylesheet only when the toggle asks for it: // measuring before it arrives reads dark colours on the light ground function sheetsReady() { return Array.prototype.every.call(document.querySelectorAll('link[rel~="stylesheet"]'), function (l) { if (l.disabled) return true; try { return !!l.sheet && l.sheet.cssRules !== null; } catch (e) { return true; } }); } function step() { if (i === M.length) { document.body.setAttribute('data-audit', JSON.stringify(out)); return; } var m = M[i]; if (m[0] && !hasToggle) { out.modes.push({ dark: true, hc: m[1], skipped: 'no dark theme' }); i++; return step(); } if (hasToggle && document.body.classList.contains('quarto-dark') !== m[0]) window.quartoToggleColorScheme(); document.documentElement.classList.toggle('hc', m[1]); var tries = 0; (function wait() { if (!sheetsReady() && tries++ < 40) return setTimeout(wait, 100); setTimeout(function () { var r = measure(); r.dark = document.body.classList.contains('quarto-dark'); r.hc = document.documentElement.classList.contains('hc'); r.bodyBg = getComputedStyle(document.body).backgroundColor; out.modes.push(r); i++; step(); }, 500); })(); } step(); } window.addEventListener('load', function () { setTimeout(run, 400); }); })();