@hyperframes/core 0.9.41 → 0.9.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { parseHTML } from "linkedom";
1
+ import { readPage } from "./pageTree.js";
2
2
  import postcss from "postcss";
3
3
  function screenStylesheet(media) {
4
4
  return !media?.trim() || media.split(",").some((query) => /^(all|screen)$/i.test(query.trim()));
@@ -64,7 +64,7 @@ function stylesheetUrls(element) {
64
64
  }
65
65
  export function authoredGoogleFontStylesheets(html) {
66
66
  const byFamily = new Map();
67
- const { document } = parseHTML(html);
67
+ const document = readPage(html);
68
68
  for (const element of document.querySelectorAll("link, style")) {
69
69
  for (const url of stylesheetUrls(element)) {
70
70
  for (const family of googleStylesheetFamilies(url)) {
@@ -6,7 +6,7 @@ import { fontShorthandFamilies } from "@hyperframes/parsers/composition";
6
6
  import { FONT_ALIAS_MAP, resolveAliasDisplayName } from "./aliases.js";
7
7
  import { extractSfntFace } from "./sfntNames.js";
8
8
  import { locateSystemFontVariants, SYSTEM_FONT_SIZE_LIMIT } from "./systemFontLocator.js";
9
- import { parseHTML } from "linkedom";
9
+ import { forgetPage, readPage, takePage } from "./pageTree.js";
10
10
  import postcss from "postcss";
11
11
  import { EMBEDDED_FONT_DATA } from "./fontData.generated.js";
12
12
  import { fontToDataUri } from "./fontCompression.js";
@@ -174,7 +174,7 @@ function normalizeInlineStyleAttribute(style, replacePrimary) {
174
174
  * primary slot while preserving the original stack as fallbacks.
175
175
  */
176
176
  export function normalizeSystemFontPrimaryFamilies(html, deterministicPrimary = "Inter") {
177
- const { document } = parseHTML(html);
177
+ const document = readPage(html);
178
178
  const customProperties = collectFontFamilyCustomProperties(html);
179
179
  const replacePrimary = (value) => systemPrimaryReplacement(value, deterministicPrimary, customProperties);
180
180
  let changed = false;
@@ -202,7 +202,10 @@ export function normalizeSystemFontPrimaryFamilies(html, deterministicPrimary =
202
202
  el.setAttribute("data-font-family", next);
203
203
  changed = true;
204
204
  }
205
- return changed ? document.toString() : html;
205
+ if (!changed)
206
+ return html;
207
+ forgetPage();
208
+ return document.toString();
206
209
  }
207
210
  function collectCssCustomProperties(css, customProperties) {
208
211
  if (!css.includes("--"))
@@ -256,7 +259,7 @@ function* iterateInlineStyleFontFamilyDeclarations(style) {
256
259
  * `font-family: var(--font)`.
257
260
  */
258
261
  export function collectFontFamilyCustomProperties(html) {
259
- const { document } = parseHTML(html);
262
+ const document = readPage(html);
260
263
  const customProperties = new Map();
261
264
  for (const styleEl of Array.from(document.querySelectorAll("style"))) {
262
265
  collectCssCustomProperties(styleEl.textContent ?? "", customProperties);
@@ -324,7 +327,7 @@ export function resolveFontFamilyDeclarationFamilies(declaration, customProperti
324
327
  * plan-time validator so they read the same surface area.
325
328
  */
326
329
  export function* iterateFontFamilyDeclarations(html) {
327
- const { document } = parseHTML(html);
330
+ const document = readPage(html);
328
331
  for (const styleEl of Array.from(document.querySelectorAll("style"))) {
329
332
  yield* iterateCssFontFamilyDeclarations(styleEl.textContent ?? "");
330
333
  }
@@ -767,7 +770,7 @@ function declareLinkedFontFamilies(link, register) {
767
770
  register(family);
768
771
  }
769
772
  function collectDeclaredFontFamilies(html) {
770
- const { document } = parseHTML(html);
773
+ const document = readPage(html);
771
774
  const declared = new Map();
772
775
  const register = (family, fontFaceRule) => {
773
776
  const key = normalizeFamilyName(family);
@@ -1482,7 +1485,7 @@ function hasTextTransformKeyword(html, keyword) {
1482
1485
  }
1483
1486
  }
1484
1487
  function extractGoogleFontsText(html) {
1485
- const { document } = parseHTML(html);
1488
+ const document = readPage(html);
1486
1489
  const decodedBodyText = document.body?.textContent ?? "";
1487
1490
  const locales = collectLangAttributes(document);
1488
1491
  // Intentional over-approximation: raw html includes base64, scripts, and
@@ -1573,7 +1576,7 @@ export async function injectDeterministicFontFaces(html, options = {}) {
1573
1576
  }
1574
1577
  return html;
1575
1578
  }
1576
- const { document } = parseHTML(html);
1579
+ const document = takePage(html);
1577
1580
  const head = document.querySelector("head");
1578
1581
  if (!head) {
1579
1582
  return html;
@@ -0,0 +1,7 @@
1
+ import { parseHTML } from "linkedom";
2
+ export type PageDocument = ReturnType<typeof parseHTML>["document"];
3
+ export declare function readPage(html: string): PageDocument;
4
+ /** The page's tree for a step that edits it: handed over, and no longer the page the readers share. */
5
+ export declare function takePage(html: string): PageDocument;
6
+ /** The tree handed out was edited: it no longer reads as the page it came from. */
7
+ export declare function forgetPage(): void;
@@ -0,0 +1,20 @@
1
+ import { parseHTML } from "linkedom";
2
+ /** The tree of the page the font step read last, so its readers share one parse of it. */
3
+ let lastPage;
4
+ export function readPage(html) {
5
+ if (lastPage?.html === html)
6
+ return lastPage.document;
7
+ const { document } = parseHTML(html);
8
+ lastPage = { html, document };
9
+ return document;
10
+ }
11
+ /** The page's tree for a step that edits it: handed over, and no longer the page the readers share. */
12
+ export function takePage(html) {
13
+ const document = readPage(html);
14
+ lastPage = undefined;
15
+ return document;
16
+ }
17
+ /** The tree handed out was edited: it no longer reads as the page it came from. */
18
+ export function forgetPage() {
19
+ lastPage = undefined;
20
+ }