-
Notifications
You must be signed in to change notification settings - Fork 361
/
Copy pathapp.js
66 lines (51 loc) · 2.25 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* global hljs */
'use strict';
(function () {
// Load syntax highlighting
hljs.highlightAll();
// Add usage warning to all examples
window.addEventListener('DOMContentLoaded', addExampleUsageWarning, false);
// Rewrite links so they point to the proper spec document
window.addEventListener('DOMContentLoaded', resolveSpecLinks, false);
// Support levels iframes should not show scrollbars, so a message with the
// correct height will be posted from the iframe.
window.addEventListener('message', fixIframeHeight);
async function addExampleUsageWarning() {
// Determine we are on an example page
if (!document.location.href.match(/examples\/[^/]+\.html/)) return;
const isExperimental =
document.querySelector('main')?.getAttribute('data-content-phase') ===
'experimental';
const usageWarningLink = isExperimental
? '/templates/experimental-example-usage-warning.html'
: '/templates/example-usage-warning.html';
// Generate the usage warning link using app.js link as a starting point
const scriptSource = document
.querySelector('[src$="app.js"]')
.getAttribute('src');
const fetchSource = scriptSource.replace('/js/app.js', usageWarningLink);
// Load and parse the usage warning and insert it before the h1
const html = await (await fetch(fetchSource)).text();
const doc = new DOMParser().parseFromString(html, 'text/html');
// Pull out the relevant part, the details element
const warningElement = doc.querySelector('details');
warningElement.classList.add('note'); // Needed for styling
// Insert the usage warning before the page's h1
const heading = document.querySelector('h1');
heading.parentNode.insertBefore(warningElement, heading.nextSibling);
}
async function resolveSpecLinks() {
const { specLinks } = await import('./specLinks.mjs');
const fixSpecLink = specLinks({ specStatus: 'ED' });
document.querySelectorAll('a[href]').forEach(fixSpecLink);
}
function fixIframeHeight(event) {
const data = event.data;
if (!data.iframe || !data.height || isNaN(data.height)) {
return;
}
const iframe = document.querySelector(`.${data.iframe}`);
if (!iframe) return;
iframe.style.height = data.height + 'px';
}
})();