// The older-browser layer (2026-11-21). It is loaded only by the legacy build, which a browser runs when it // can't run the modern one (a TV's built-in browser from 2021–2023). Modern browsers never download it. // It does three things: // - swaps the stylesheet for its copy rewritten for older browsers (built by vite.config.js), e.g. `inset` // becomes top/right/bottom/left; // - gives the signage slides their container sizes (startCqShim: the fallback of cq() in @xenia/signage); // - spaces flex items where the browser has no flex `gap` (Chrome before 84): it adds the gap as a margin // on each item after the first. // It imports nothing from the apps' code, so the modern build's files stay exactly as they were. // the same check as @xenia/signage's cq() const CQ_NATIVE = typeof CSS === 'undefined' || typeof CSS.supports !== 'function' || CSS.supports('width', '1cqmin') /** What runs here, for the support page and the tests: . */ const active = ['legacy'] function swapStylesheets() { for (const link of document.querySelectorAll('link[rel="stylesheet"]')) { const href = link.getAttribute('href') || '' if (!/\/assets\/[^/]+\.css$/.test(href) || href.endsWith('.compat.css')) continue const copy = link.cloneNode() copy.setAttribute('href', href.replace(/\.css$/, '.compat.css')) // the old sheet stays until the new one has loaded: no unstyled flash copy.onload = () => link.remove() copy.onerror = () => copy.remove() link.after(copy) if (!active.includes('css')) active.push('css') } } function hasFlexGap() { const box = document.createElement('div') box.style.cssText = 'display:flex;flex-direction:column;row-gap:1px;position:absolute;visibility:hidden' box.appendChild(document.createElement('div')) box.appendChild(document.createElement('div')) document.body.appendChild(box) const ok = box.scrollHeight === 1 box.remove() return ok } function setVars(el, size) { if (!size || el.nodeType !== 1) return el.style.setProperty('--xcqw', `${size.w / 100}px`) el.style.setProperty('--xcqh', `${size.h / 100}px`) el.style.setProperty('--xcqmin', `${Math.min(size.w, size.h) / 100}px`) } /** * Older browsers only: give the children of every container its size as --xcqw / --xcqh / --xcqmin. * The variables are on the children, not on the container: the container's own sizes refer to the * container around it, as with the real units. Nothing happens where the browser has container units. * Returns a stop function. */ function startCqShim(root = document.body) { if ( !root || typeof ResizeObserver === 'undefined' || typeof MutationObserver === 'undefined') return () => {} const sizes = new WeakMap() const watched = new WeakSet() const ro = new ResizeObserver(entries => { for (const e of entries) { const size = { w: e.contentRect.width, h: e.contentRect.height } sizes.set(e.target, size) for (const child of e.target.children) setVars(child, size) } }) const watch = el => { if (watched.has(el)) return watched.add(el) ro.observe(el) } const scan = node => { if (node.nodeType !== 1) return const parent = node.parentElement if (parent && parent.hasAttribute('data-cq')) setVars(node, sizes.get(parent)) if (node.hasAttribute('data-cq')) watch(node) for (const el of node.querySelectorAll('[data-cq]')) watch(el) } const mo = new MutationObserver(list => { for (const m of list) for (const n of m.addedNodes) scan(n) }) mo.observe(root, { childList: true, subtree: true }) scan(root) return () => { mo.disconnect(); ro.disconnect() } } const SIDE = { row: 'marginLeft', 'row-reverse': 'marginRight', column: 'marginTop', 'column-reverse': 'marginBottom' } function gapItems(el) { const cs = getComputedStyle(el) if (cs.display !== 'flex' && cs.display !== 'inline-flex') return const dir = cs.flexDirection || 'row' const gap = dir.startsWith('row') ? cs.columnGap : cs.rowGap const side = SIDE[dir] let first = true for (const child of el.children) { const ccs = getComputedStyle(child) if (ccs.display === 'none' || ccs.position === 'absolute' || ccs.position === 'fixed') continue const want = !first && gap && gap !== 'normal' && gap !== '0px' ? gap : '' // an item's own margin stays (a zero one doesn't count); a write only when it changes, as a // write fires the observer again if (want && (child.dataset.xgap || !parseFloat(child.style[side]))) { if (child.style[side] !== want) child.style[side] = want child.dataset.xgap = '1' } else if (!want && child.dataset.xgap) { child.style[side] = ''; delete child.dataset.xgap } first = false } } function startGapShim() { const dirty = new Set() let queued = false const flush = () => { queued = false const els = [...dirty] dirty.clear() for (const el of els) if (el.isConnected) gapItems(el) } const mark = el => { if (!el || el.nodeType !== 1) return dirty.add(el) if (!queued) { queued = true; requestAnimationFrame(flush) } } const markTree = el => { if (el.nodeType !== 1) return mark(el) for (const d of el.querySelectorAll('*')) mark(d) } new MutationObserver(list => { for (const m of list) { if (m.type === 'attributes') { mark(m.target); mark(m.target.parentElement) } else { mark(m.target); for (const n of m.addedNodes) markTree(n) } } }).observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['class', 'style'] }) markTree(document.body) } export function startCompat() { // the tests force a part on a modern browser: window.__XENIA_COMPAT_FORCE = ['gap'] const force = window.__XENIA_COMPAT_FORCE || [] swapStylesheets() if (!CQ_NATIVE) { startCqShim(); active.push('cq') } if (force.includes('gap') || !hasFlexGap()) { startGapShim(); active.push('gap') } document.documentElement.setAttribute('data-compat', active.join(' ')) }