From ec6913c8e2f261e356331cd2efeef5c21e61cdcb Mon Sep 17 00:00:00 2001 From: Sid Vishnoi Date: Mon, 22 Feb 2021 23:18:01 +0530 Subject: [PATCH 1/5] refactor: move obvious global code to prepare() --- src/core/algorithms.js | 4 +--- src/core/biblio.js | 17 ++++++++++------- src/core/data-type.js | 4 +--- src/core/examples.js | 4 +--- src/core/highlight-vars.js | 4 +--- src/core/highlight.js | 4 +--- src/core/issues-notes.js | 4 +--- src/core/style.js | 14 ++++++++------ src/core/webidl.js | 4 +--- src/core/xref.js | 18 ++++++++++-------- 10 files changed, 35 insertions(+), 42 deletions(-) diff --git a/src/core/algorithms.js b/src/core/algorithms.js index a693248ca9..8e2d94a87a 100644 --- a/src/core/algorithms.js +++ b/src/core/algorithms.js @@ -7,8 +7,6 @@ import { fetchAsset } from "./text-loader.js"; export const name = "core/algorithms"; -const cssPromise = loadStyle(); - async function loadStyle() { try { return (await import("text!../../assets/algorithms.css")).default; @@ -24,7 +22,7 @@ export async function run() { .forEach(li => li.classList.add("assert")); if (document.querySelector(".assert")) { const style = document.createElement("style"); - style.textContent = await cssPromise; + style.textContent = await loadStyle(); document.head.appendChild(style); } } diff --git a/src/core/biblio.js b/src/core/biblio.js index 77217f65ce..c13d2dd02d 100644 --- a/src/core/biblio.js +++ b/src/core/biblio.js @@ -18,13 +18,16 @@ export const name = "core/biblio"; const bibrefsURL = new URL("https://specref.herokuapp.com/bibrefs?refs="); -// Opportunistically dns-prefetch to bibref server, as we don't know yet -// if we will actually need to download references yet. -const link = createResourceHint({ - hint: "dns-prefetch", - href: bibrefsURL.origin, -}); -document.head.appendChild(link); +export async function prepare() { + // Opportunistically dns-prefetch to bibref server, as we don't know yet + // if we will actually need to download references yet. + const link = createResourceHint({ + hint: "dns-prefetch", + href: bibrefsURL.origin, + }); + document.head.appendChild(link); +} + let doneResolver; /** @type {Promise} */ diff --git a/src/core/data-type.js b/src/core/data-type.js index 25742a0827..aaa184b64f 100644 --- a/src/core/data-type.js +++ b/src/core/data-type.js @@ -9,8 +9,6 @@ import { fetchAsset } from "./text-loader.js"; export const name = "core/data-type"; -const tooltipStylePromise = loadStyle(); - async function loadStyle() { try { return (await import("text!../../assets/datatype.css")).default; @@ -25,7 +23,7 @@ export async function run(conf) { } const style = document.createElement("style"); - style.textContent = await tooltipStylePromise; + style.textContent = await loadStyle(); document.head.appendChild(style); let section = null; diff --git a/src/core/examples.js b/src/core/examples.js index 7fa1a4f489..2908c0b061 100644 --- a/src/core/examples.js +++ b/src/core/examples.js @@ -39,8 +39,6 @@ const localizationStrings = { const l10n = getIntlData(localizationStrings); -const cssPromise = loadStyle(); - async function loadStyle() { try { return (await import("text!../../assets/examples.css")).default; @@ -80,7 +78,7 @@ export async function run() { ); if (!examples.length) return; - const css = await cssPromise; + const css = await loadStyle(); document.head.insertBefore( html``; + document.head.appendChild(style); +} + export async function run(conf) { // Nothing to highlight if (conf.noHighlightCSS) return; @@ -94,16 +103,12 @@ export async function run(conf) { ); // Nothing to highlight if (!highlightables.length) { + document.getElementById("respec-css-highlight").remove(); return; } const promisesToHighlight = highlightables .filter(elem => elem.textContent.trim()) .map(highlightElement); - const ghCss = await loadStyle(); - document.head.appendChild( - html`` - ); + await Promise.all(promisesToHighlight); } diff --git a/src/core/issues-notes.js b/src/core/issues-notes.js index e10ab0a11e..67f69d29a6 100644 --- a/src/core/issues-notes.js +++ b/src/core/issues-notes.js @@ -375,22 +375,24 @@ async function fetchAndStoreGithubIssues(github) { return new Map(Object.entries(issues)); } +export async function prepare() { + document.head.insertBefore( + html``, + document.head.querySelector("link") + ); +} + export async function run(conf) { const query = ".issue, .note, .warning, .ednote"; /** @type {NodeListOf} */ const issuesAndNotes = document.querySelectorAll(query); if (!issuesAndNotes.length) { + document.getElementById("respec-css-issues-notes").remove(); return; // nothing to do. } const ghIssues = await fetchAndStoreGithubIssues(conf.github); - const css = await loadStyle(); - const { head: headElem } = document; - headElem.insertBefore( - html``, - headElem.querySelector("link") - ); handleIssues(issuesAndNotes, ghIssues, conf); const ednotes = document.querySelectorAll(".ednote"); ednotes.forEach(ednote => { From f7d0c6e1035d9cada1020c2210dbfef6a9e4a401 Mon Sep 17 00:00:00 2001 From: Sid Vishnoi Date: Wed, 10 Mar 2021 00:10:39 +0530 Subject: [PATCH 5/5] webidl: load style early in prepare(), remove some redundant* logic --- src/core/webidl.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/core/webidl.js b/src/core/webidl.js index eecdd24f1a..ae8637eceb 100644 --- a/src/core/webidl.js +++ b/src/core/webidl.js @@ -375,19 +375,20 @@ async function loadStyle() { } } +export async function prepare() { + document.querySelector("head link, head :last-child").before( + html`` + ); +} + export async function run() { const idls = document.querySelectorAll("pre.idl, pre.webidl"); if (!idls.length) { + document.getElementById("respec-css-webidl").remove(); return; } - if (!document.querySelector(".idl:not(pre), .webidl:not(pre)")) { - const link = document.querySelector("head link"); - if (link) { - const style = document.createElement("style"); - style.textContent = await loadStyle(); - link.before(style); - } - } const astArray = [...idls].map(renderWebIDL);