-
Notifications
You must be signed in to change notification settings - Fork 219
Switch MDN annos to ship as just a code string, and get hydrated on load. #3319 #3324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tabatkins
wants to merge
4
commits into
main
Choose a base branch
from
mdn
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8635b2a
Switch MDN annos to ship as just a code string, and get hydrated on l…
tabatkins 6ab992d
Always use 6px top margin on .feature, now that I'm not needing to cl…
tabatkins bd6cb0f
Update 'copied from' link
tabatkins 84cbbc2
wip, addressing review comments
tabatkins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| 'use strict'; | ||
| // File copied from https://github.com/whatwg/whatwg.org/blob/main/resources.whatwg.org/standard-mdn-annos.js | ||
|
|
||
| // Builds MDN annotation panels on demand from the compact `data-mdn` attribute written by | ||
| // the spec generator (wattsi's EncodeMDNAnnotations; Bikeshed's mdnspeclinks.py builds its | ||
| // panels eagerly for now). Only the wire format is shared: the browser labels, the | ||
| // engine-support strings and the render grouping all live here. | ||
| // | ||
| // Archived commit snapshots load this file forever, so every format version this script | ||
| // has ever understood has to keep working. Evolve the format by adding a branch in | ||
| // `hydrate`, never by redefining a version that has already shipped. | ||
|
|
||
| const MDN_URL = 'https://developer.mozilla.org/en-US/docs/Web/'; | ||
| const CANIUSE_URL = 'https://caniuse.com/#feat='; | ||
|
|
||
| // Render groups, in the order they appear in a panel, separated by <hr>. | ||
| const CURRENT_ENGINES = 0; | ||
| const BORROWED_ENGINES = 1; | ||
| const RETIRED_ENGINES = 2; | ||
| const MOBILE = 3; | ||
| const NON_BROWSER = 4; | ||
| const GROUP_COUNT = 5; | ||
|
|
||
| // Cell slots, in the order they appear in `data-mdn`. APPEND-ONLY: a new browser goes at | ||
| // the end and is never inserted, so that old data still lines up and old copies of this | ||
| // script can keep ignoring the cells they don't know about. `group` is what decides where | ||
| // a row is rendered, so appending to this list doesn't constrain where the row shows up. | ||
| const SLOTS = [ | ||
| { id: 'firefox', label: 'Firefox', group: CURRENT_ENGINES }, | ||
| { id: 'safari', label: 'Safari', group: CURRENT_ENGINES }, | ||
| { id: 'chrome', label: 'Chrome', group: CURRENT_ENGINES }, | ||
| { id: 'opera', label: 'Opera', group: BORROWED_ENGINES, mirrors: 'Chrome' }, | ||
| { id: 'edge_blink', label: 'Edge', group: BORROWED_ENGINES }, | ||
| { id: 'edge', label: 'Edge (Legacy)', group: RETIRED_ENGINES }, | ||
| { id: 'ie', label: 'IE', group: RETIRED_ENGINES }, | ||
| { id: 'firefox_android', label: 'Firefox for Android', group: MOBILE, mirrors: 'Firefox' }, | ||
| { id: 'safari_ios', label: 'iOS Safari', group: MOBILE, mirrors: 'Safari' }, | ||
| { id: 'chrome_android', label: 'Chrome for Android', group: MOBILE, mirrors: 'Chrome' }, | ||
| { id: 'webview_android', label: 'Android WebView', group: MOBILE, mirrors: 'Chrome' }, | ||
| { id: 'samsunginternet_android', label: 'Samsung Internet', group: MOBILE, mirrors: 'Chrome' }, | ||
| { id: 'opera_android', label: 'Opera Mobile', group: MOBILE, mirrors: 'Chrome' }, | ||
| { id: 'nodejs', label: 'Node.js', group: NON_BROWSER }, | ||
| ]; | ||
|
|
||
| // One mnemonic character per engine-support note. | ||
| const LEVELS = { | ||
| '0': { class: 'less-than-two-engines-text', text: 'In no current engines.' }, | ||
| '1': { class: 'less-than-two-engines-text', text: 'In only one current engine.' }, | ||
| '9': { class: 'all-engines-text', text: 'In all current engines.' }, | ||
| 's': { class: 'less-than-two-engines-text', text: 'Support in one engine under other name.' }, | ||
| 'S': { class: 'less-than-two-engines-text', text: 'Support in some engines under other name.' }, | ||
| 'v': { class: 'less-than-two-engines-text', text: 'Prefixed support in one engine.' }, | ||
| 'V': { class: 'less-than-two-engines-text', text: 'Prefixed support in some engines.' }, | ||
| 'p': { class: 'less-than-two-engines-text', text: 'Partial support in one engine.' }, | ||
| 'P': { class: 'less-than-two-engines-text', text: 'Partial support in some engines.' }, | ||
| }; | ||
|
|
||
| // Leading character of a cell, qualifying how the version after it should be read. `flag` | ||
| // is whether to mark the version with 🔰 as well as explaining it in a tooltip. | ||
| const CAVEATS = { | ||
| '*': { flag: true, title: () => 'Partial implementation.' }, | ||
| '^': { flag: true, title: () => 'Requires setting a user preference or runtime flag.' }, | ||
| '$': { flag: true, title: () => 'Requires a prefix or alternative name.' }, | ||
| // Mirrored support is far too common to flag every occurrence; the tooltip is enough. | ||
| '@': { flag: false, title: (slot) => `Support is mirrored from ${slot.mirrors ?? 'the upstream browser'}.` }, | ||
| }; | ||
|
|
||
| const FLAG = '\u{1F530}'; // 🔰, marks a version that comes with a caveat | ||
| const EN_DASH = '–'; | ||
|
|
||
| function element(name, attributes, ...children) { | ||
| const node = document.createElement(name); | ||
| for (const [attribute, value] of Object.entries(attributes)) { | ||
| if (value !== undefined) { | ||
| node.setAttribute(attribute, value); | ||
| } | ||
| } | ||
| node.append(...children); | ||
| return node; | ||
| } | ||
|
|
||
| function splitOnce(string, separator) { | ||
| const at = string.indexOf(separator); | ||
| return at === -1 ? [string, undefined] : [string.slice(0, at), string.slice(at + 1)]; | ||
| } | ||
|
|
||
| // A version body says both whether the browser supports the feature and since when. | ||
| function readVersion(body) { | ||
| if (body === '?') { | ||
| return { state: 'unknown', version: '?' }; | ||
| } | ||
| if (body === '-') { | ||
| return { state: 'no', version: 'None' }; | ||
| } | ||
| if (body === '!') { | ||
| return { state: 'yes', version: 'Yes' }; | ||
| } | ||
| if (body.startsWith('=')) { | ||
| // A version that will never be superseded, so no trailing "+". | ||
| return { state: 'yes', version: body.slice(1) }; | ||
| } | ||
| const [added, removed] = body.split('-'); | ||
| if (removed !== undefined) { | ||
| return { state: 'no', version: added + EN_DASH + removed }; | ||
| } | ||
| return { state: 'yes', version: added + '+' }; | ||
| } | ||
|
|
||
| function readCell(cell, slot) { | ||
| const caveat = CAVEATS[cell[0]]; | ||
| const { state, version } = readVersion(caveat ? cell.slice(1) : cell); | ||
| return { | ||
| state, | ||
| version: caveat?.flag ? `${FLAG} ${version}` : version, | ||
| title: caveat?.title(slot), | ||
| }; | ||
| } | ||
|
|
||
| // A browser row is the browser's name in one cell and its version in the next. The logo | ||
| // comes from CSS, keyed off the browser id in the row's class. | ||
| function browserRow(slot, cell) { | ||
| const { state, version, title } = readCell(cell, slot); | ||
| return element('span', { class: `${slot.id} ${state}` }, | ||
| element('span', {}, slot.label), | ||
| element('span', { title }, version)); | ||
| } | ||
|
|
||
| // The caniuse row has a single cell instead, since its logo needs no label beside it. | ||
| function caniuseRow(field) { | ||
| const [feature, title] = splitOnce(field, ','); | ||
| const link = element('a', { href: CANIUSE_URL + feature, title }, 'caniuse.com table'); | ||
| return element('span', { class: 'caniuse' }, element('span', {}, link)); | ||
| } | ||
|
|
||
| function supportTable(cells, caniuse) { | ||
| const groups = Array.from({ length: GROUP_COUNT }, () => []); | ||
| SLOTS.forEach((slot, slotIndex) => { | ||
| // An empty cell means this browser gets no row at all. Cells beyond SLOTS are | ||
| // browsers added after this copy of the script was written; ignore them. | ||
| if (cells[slotIndex]) { | ||
| groups[slot.group].push(browserRow(slot, cells[slotIndex])); | ||
| } | ||
| }); | ||
| if (caniuse !== undefined) { | ||
| groups.push([caniuseRow(caniuse)]); | ||
| } | ||
|
|
||
| const table = element('div', { class: 'support' }); | ||
| for (const rows of groups.filter((rows) => rows.length > 0)) { | ||
| if (table.hasChildNodes()) { | ||
| table.append(element('hr', {})); | ||
| } | ||
| table.append(...rows); | ||
| } | ||
| return table; | ||
| } | ||
|
|
||
| function featurePanel(record) { | ||
| const [slug, level, cells, caniuse] = record.split('|'); | ||
|
|
||
| const article = element('a', { href: MDN_URL + slug }, slug.slice(slug.indexOf('/') + 1)); | ||
| const panel = element('div', { class: 'feature' }, element('p', {}, article)); | ||
|
|
||
| if (level in LEVELS) { | ||
| panel.append(element('p', { class: LEVELS[level].class }, LEVELS[level].text)); | ||
| } | ||
| if (cells) { | ||
| panel.append(supportTable(cells.split(','), caniuse)); | ||
| } | ||
| return panel; | ||
| } | ||
|
|
||
| function hydrate(anno) { | ||
| const [version, features] = splitOnce(anno.dataset.mdn, '|'); | ||
| delete anno.dataset.mdn; // Only ever hydrate a panel once. | ||
|
|
||
| if (version !== 'v1') { | ||
| console.warn(`Unsupported MDN annotation data version "${version}".`, anno); | ||
| return; | ||
| } | ||
| anno.append(...features.split('~').map(featurePanel)); | ||
| } | ||
|
|
||
| function hydrateIfNeeded(node) { | ||
| if (node instanceof Element && node.matches('.mdn-anno[data-mdn]')) { | ||
| hydrate(node); | ||
| } | ||
| } | ||
|
|
||
| // A click on the summary arrives before the browser opens the <details>, so hydrating | ||
| // from it means the panel never flashes empty. That covers pointers and the keyboard, | ||
| // since activating a summary dispatches a click either way. | ||
| document.addEventListener('click', (event) => { | ||
| if (event.target instanceof Element) { | ||
| hydrateIfNeeded(event.target.closest('summary')?.parentElement); | ||
| } | ||
| }, true); | ||
|
|
||
| // And `toggle` is the catch-all for a panel opened some other way, such as find-in-page | ||
| // or a fragment navigation into it. It fires a task after the state has already changed, | ||
| // and it doesn't bubble, so it has to be a capture listener. | ||
| document.addEventListener('toggle', (event) => hydrateIfNeeded(event.target), true); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file needs to be updated to include latest changes in whatwg/whatwg.org#506, see comments in whatwg/wattsi#170