@aglyn/plugins-ai 1.12.0 → 1.14.0

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.
Files changed (61) hide show
  1. package/package.json +17 -17
  2. package/src/lib/components/ai-media-create.component.js +1 -1
  3. package/src/lib/components/ai-media-create.component.js.map +1 -1
  4. package/src/lib/components/ai-upsell-dialog.component.js +1 -1
  5. package/src/lib/components/ai-upsell-dialog.component.js.map +1 -1
  6. package/src/lib/jobs/ai-job-import-step.js +11 -1
  7. package/src/lib/jobs/ai-job-import-step.js.map +1 -1
  8. package/src/lib/jobs/ai-site-import-compile.js +23 -3
  9. package/src/lib/jobs/ai-site-import-compile.js.map +1 -1
  10. package/src/lib/jobs/ai-site-import-look.d.ts +19 -4
  11. package/src/lib/jobs/ai-site-import-look.js +142 -45
  12. package/src/lib/jobs/ai-site-import-look.js.map +1 -1
  13. package/src/lib/jobs/ai-theme-brand-inputs.d.ts +12 -0
  14. package/src/lib/jobs/ai-theme-brand-inputs.js +1 -1
  15. package/src/lib/jobs/ai-theme-brand-inputs.js.map +1 -1
  16. package/src/lib/layout-language/ai-layout-compiler.js +90 -8
  17. package/src/lib/layout-language/ai-layout-compiler.js.map +1 -1
  18. package/src/lib/layout-language/ai-layout-language.d.ts +8 -2
  19. package/src/lib/layout-language/ai-layout-language.js +4 -2
  20. package/src/lib/layout-language/ai-layout-language.js.map +1 -1
  21. package/src/lib/model/ai-site-import-fidelity.d.ts +9 -0
  22. package/src/lib/model/ai-site-import-fidelity.js +64 -6
  23. package/src/lib/model/ai-site-import-fidelity.js.map +1 -1
  24. package/src/lib/model/ai-site-import.d.ts +36 -1
  25. package/src/lib/model/ai-site-import.js.map +1 -1
  26. package/src/lib/model/ai-upsell-copy.js +1 -1
  27. package/src/lib/model/ai-upsell-copy.js.map +1 -1
  28. package/src/lib/runtime/site-inventory.js +2 -3
  29. package/src/lib/runtime/site-inventory.js.map +1 -1
  30. package/src/lib/server/assist-view-context.js +3 -3
  31. package/src/lib/server/assist-view-context.js.map +1 -1
  32. package/src/lib/site-import/fixtures/site-import-fidelity-replay.d.ts +12 -1
  33. package/src/lib/site-import/fixtures/site-import-fidelity-replay.js +19 -9
  34. package/src/lib/site-import/fixtures/site-import-fidelity-replay.js.map +1 -1
  35. package/src/lib/site-import/fixtures/site-layout-recordings.d.ts +32 -0
  36. package/src/lib/site-import/fixtures/site-layout-recordings.js +55 -0
  37. package/src/lib/site-import/fixtures/site-layout-recordings.js.map +1 -1
  38. package/src/lib/site-import/site-import-concealed.d.ts +30 -0
  39. package/src/lib/site-import/site-import-concealed.js +231 -0
  40. package/src/lib/site-import/site-import-concealed.js.map +1 -0
  41. package/src/lib/site-import/site-import-crawl.d.ts +5 -1
  42. package/src/lib/site-import/site-import-crawl.js +35 -18
  43. package/src/lib/site-import/site-import-crawl.js.map +1 -1
  44. package/src/lib/site-import/site-import-css.d.ts +81 -0
  45. package/src/lib/site-import/site-import-css.js +456 -27
  46. package/src/lib/site-import/site-import-css.js.map +1 -1
  47. package/src/lib/site-import/site-import-data-forms.d.ts +32 -0
  48. package/src/lib/site-import/site-import-data-forms.js +531 -0
  49. package/src/lib/site-import/site-import-data-forms.js.map +1 -0
  50. package/src/lib/site-import/site-import-html.d.ts +36 -1
  51. package/src/lib/site-import/site-import-html.js +264 -54
  52. package/src/lib/site-import/site-import-html.js.map +1 -1
  53. package/src/lib/site-import/site-import-layout.d.ts +1 -1
  54. package/src/lib/site-import/site-import-layout.js +490 -44
  55. package/src/lib/site-import/site-import-layout.js.map +1 -1
  56. package/src/lib/site-import/site-import-nav.js +17 -3
  57. package/src/lib/site-import/site-import-nav.js.map +1 -1
  58. package/src/lib/site-import/site-import-page.js +2 -0
  59. package/src/lib/site-import/site-import-page.js.map +1 -1
  60. package/src/lib/testing/stand-in-dataset-index.js +6 -2
  61. package/src/lib/testing/stand-in-dataset-index.js.map +1 -1
@@ -22,12 +22,14 @@
22
22
  * filled button. This is a small cascade, enough for that and no more:
23
23
  *
24
24
  * - **The sheets.** The page's own `<style>` sheets and the ones it links,
25
- * read in order. `@media` rules apply as a 1280-pixel screen in light mode
25
+ * every one to a byte budget (`siteImportReadStylesheets`, AGL-4152), read
26
+ * in order. `@media` rules apply as a 1280-pixel screen in light mode
26
27
  * sees them, so a grid's desktop columns win over its phone ones;
27
28
  * `@layer`, `@supports` and `@container` are read through; keyframes, font
28
29
  * faces (their families noted) and page rules are skipped.
29
30
  * - **Selectors.** Tags, classes (escaped ones too, as utility frameworks
30
- * write them), ids and attributes, joined by any combinator. A rule that
31
+ * write them), ids and attributes, joined by any combinator, and `:is()`
32
+ * and `:where()` spelled out (AGL-4152). A rule that
31
33
  * only applies on hover, focus or to a generated part (`::before`) is a
32
34
  * state the page is not in and is not read.
33
35
  * - **The cascade.** Specificity, then order, `!important` over both, and the
@@ -72,6 +74,17 @@
72
74
  'display',
73
75
  'grid-template-columns',
74
76
  'grid-column',
77
+ // Where a grid places its children, and the order a row shows them in (AGL-4152).
78
+ 'grid-template-rows',
79
+ 'grid-template-areas',
80
+ 'grid-auto-flow',
81
+ 'grid-area',
82
+ 'grid-row',
83
+ 'grid-row-start',
84
+ 'grid-row-end',
85
+ 'grid-column-start',
86
+ 'grid-column-end',
87
+ 'order',
75
88
  'flex',
76
89
  'flex-direction',
77
90
  'flex-wrap',
@@ -97,8 +110,19 @@
97
110
  'padding-right',
98
111
  'object-fit',
99
112
  'justify-content',
100
- 'align-items'
113
+ 'align-items',
114
+ // What says a visitor cannot see an element (AGL-4152): clipped, off-screen, its words pushed away; and how its lines break.
115
+ 'clip',
116
+ 'clip-path',
117
+ 'overflow',
118
+ 'left',
119
+ 'right',
120
+ 'top',
121
+ 'transform',
122
+ 'text-indent',
123
+ 'white-space'
101
124
  ]);
125
+ /** What a rule says of a child's place in its grid, which a breakpoint nested in the rule may say again (AGL-4152). */ const GRID_PLACEMENT = /^(?:grid-area|grid-row|grid-row-start|grid-row-end|grid-column|grid-column-start|grid-column-end|order)$/;
102
126
  /** What an element takes from its parent when it says nothing itself. */ const INHERITED = new Set([
103
127
  'color',
104
128
  'font-family',
@@ -107,7 +131,8 @@
107
131
  'text-transform',
108
132
  'letter-spacing',
109
133
  'text-align',
110
- 'visibility'
134
+ 'visibility',
135
+ 'white-space'
111
136
  ]);
112
137
  /*------------------------------------------
113
138
  * Parsing
@@ -190,6 +215,53 @@ const SIMPLE = new RegExp(String.raw`^(?:(\*|${IDENT})|#(${IDENT})|\.(${IDENT})|
190
215
  specificity: ids * 1000000 + classes * 1000 + tags
191
216
  };
192
217
  }
218
+ /** The most selectors one written with `:is()` or `:where()` is spelled out into. */ const SELECTOR_VARIANTS = 24;
219
+ /**
220
+ * A selector with its `:is()` and `:where()` spelled out (AGL-4152), each with
221
+ * the weight to take off for a `:where()`, which counts for nothing. A variant
222
+ * two compounds cannot be joined into (`.x:is(div)`, `.x:is(.a .b)`) is left out.
223
+ */ function spelledOut(text) {
224
+ const match = /:(?:is|where)\(/i.exec(text);
225
+ if (!match) return [
226
+ {
227
+ text,
228
+ discount: 0
229
+ }
230
+ ];
231
+ const inner = match.index + match[0].length;
232
+ let depth = 1;
233
+ let end = inner;
234
+ while(end < text.length && depth > 0){
235
+ if (text[end] === '(') depth += 1;
236
+ else if (text[end] === ')') depth -= 1;
237
+ end += 1;
238
+ }
239
+ if (depth) return [
240
+ {
241
+ text,
242
+ discount: 0
243
+ }
244
+ ];
245
+ const head = text.slice(0, match.index);
246
+ const tail = text.slice(end);
247
+ const zeroed = /where/i.test(match[0]);
248
+ const joined = !head || /[\s>+~]$/.test(head);
249
+ const out = [];
250
+ for (const option of splitList(text.slice(inner, end - 1))){
251
+ var _ref;
252
+ var _parseSelector;
253
+ if (!joined && (/\s/.test(option) || /^[\w*]/.test(option))) continue;
254
+ const discount = zeroed ? (_ref = (_parseSelector = parseSelector(option.replace(/:(?:is|where)\([^()]*\)/gi, ''))) == null ? void 0 : _parseSelector.specificity) != null ? _ref : 0 : 0;
255
+ for (const variant of spelledOut(`${head}${option}${tail}`)){
256
+ if (out.length >= SELECTOR_VARIANTS) return out;
257
+ out.push({
258
+ text: variant.text,
259
+ discount: discount + variant.discount
260
+ });
261
+ }
262
+ }
263
+ return out;
264
+ }
193
265
  /** Splits at top-level commas: none inside brackets or parentheses. */ function splitList(text) {
194
266
  const out = [];
195
267
  let depth = 0;
@@ -317,7 +389,8 @@ function declarationsOf(body) {
317
389
  rules: [],
318
390
  index: new Map(),
319
391
  fontFaces: [],
320
- darkRules: 0
392
+ darkRules: 0,
393
+ variableUses: new Map()
321
394
  };
322
395
  const scheme = (_options_scheme = options.scheme) != null ? _options_scheme : 'light';
323
396
  let order = 0;
@@ -325,25 +398,33 @@ function declarationsOf(body) {
325
398
  const addRule = (prelude, body)=>{
326
399
  const decls = declarationsOf(body);
327
400
  if (!decls.length) return;
401
+ for (const [, value] of decls){
402
+ var _css_variableUses_get;
403
+ if (!value.includes('var(')) continue;
404
+ for (const use of value.matchAll(/var\(\s*(--[\w-]+)/g))css.variableUses.set(use[1], ((_css_variableUses_get = css.variableUses.get(use[1])) != null ? _css_variableUses_get : 0) + 1);
405
+ }
328
406
  for (const text of splitList(prelude)){
329
407
  const lifted = scheme === 'dark' ? withoutDarkScope(text) : null;
330
- const selector = parseSelector(lifted != null ? lifted : text);
331
- if (!selector) continue;
332
- if (scheme === 'dark' && (lifted !== null || darkGroups.includes(true))) css.darkRules += 1;
333
- // The mark it lost still counts: a dark rule outranks the same rule of the light scheme.
334
- if (lifted !== null) selector.specificity += 1000;
335
- const rule = {
336
- selector,
337
- order: order++,
338
- decls
339
- };
340
- css.rules.push(rule);
341
- const key = keyOf(selector);
342
- const list = css.index.get(key);
343
- if (list) list.push(rule);
344
- else css.index.set(key, [
345
- rule
346
- ]);
408
+ for (const variant of spelledOut(lifted != null ? lifted : text)){
409
+ const selector = parseSelector(variant.text);
410
+ if (!selector) continue;
411
+ selector.specificity = Math.max(0, selector.specificity - variant.discount);
412
+ if (scheme === 'dark' && (lifted !== null || darkGroups.includes(true))) css.darkRules += 1;
413
+ // The mark it lost still counts: a dark rule outranks the same rule of the light scheme.
414
+ if (lifted !== null) selector.specificity += 1000;
415
+ const rule = {
416
+ selector,
417
+ order: order++,
418
+ decls
419
+ };
420
+ css.rules.push(rule);
421
+ const key = keyOf(selector);
422
+ const list = css.index.get(key);
423
+ if (list) list.push(rule);
424
+ else css.index.set(key, [
425
+ rule
426
+ ]);
427
+ }
347
428
  }
348
429
  };
349
430
  for (const sheet of sheets){
@@ -404,7 +485,14 @@ function declarationsOf(body) {
404
485
  if (family && !css.fontFaces.includes(family)) css.fontFaces.push(family);
405
486
  } else if (!prelude.startsWith('@') && applies && prelude) {
406
487
  // A rule nested inside a rule (`&:hover`) is a state: only the outer declarations are read.
407
- addRule(prelude, body.replace(/[^;{}]*\{[^{}]*\}/g, ''));
488
+ // A breakpoint nested inside one that holds at this width places its child as a top-level one does.
489
+ const placed = [
490
+ ...body.matchAll(/@media([^{};]*)\{([^{}]*)\}/g)
491
+ ].filter((match)=>mediaHolds(match[1], viewport, scheme)).flatMap((match)=>match[2].split(';').filter((part)=>GRID_PLACEMENT.test(part.slice(0, part.indexOf(':')).trim().toLowerCase())));
492
+ addRule(prelude, [
493
+ body.replace(/[^;{}]*\{[^{}]*\}/g, ''),
494
+ ...placed
495
+ ].join(';'));
408
496
  }
409
497
  start = at = end;
410
498
  }
@@ -494,9 +582,10 @@ function compoundMatches(compound, element, rootId) {
494
582
  return walk(selector.compounds.length - 1, element);
495
583
  }
496
584
  const EMPTY_STYLE = new Map();
585
+ /** How far a variable that names a variable is followed: a site builder's palette goes several deep. */ const VARIABLE_DEPTH = 8;
497
586
  /** Replaces each `var(--x, fallback)` with what the custom properties hold, to a few levels. */ function withVariables(value, custom) {
498
587
  let out = value;
499
- for(let pass = 0; pass < 4 && out.includes('var('); pass += 1){
588
+ for(let pass = 0; pass < VARIABLE_DEPTH && out.includes('var('); pass += 1){
500
589
  out = out.replace(/var\(\s*(--[\w-]+)\s*(?:,\s*((?:[^()]|\([^()]*\))*))?\)/g, (_, name, fallback)=>{
501
590
  var _ref, _custom_get;
502
591
  return (_ref = (_custom_get = custom.get(name)) != null ? _custom_get : fallback == null ? void 0 : fallback.trim()) != null ? _ref : '';
@@ -584,8 +673,11 @@ const EMPTY_STYLE = new Map();
584
673
  if (element.attrs.id) candidates.push(...(_css_index_get3 = css.index.get(`#${element.attrs.id}`)) != null ? _css_index_get3 : []);
585
674
  const matched = candidates.filter((rule)=>selectorMatches(rule.selector, element, elements, rootStyle.element));
586
675
  const own = new Map();
587
- const custom = new Map(parent.custom);
588
- apply(matched, inlineDeclarations(element.attrs.style), own, custom);
676
+ const inline = inlineDeclarations(element.attrs.style);
677
+ // A palette can be thousands of properties: only an element that declares one takes a copy of them.
678
+ const declares = inline.some(([property])=>property.startsWith('--')) || matched.some((rule)=>rule.decls.some(([property])=>property.startsWith('--')));
679
+ const custom = declares ? new Map(parent.custom) : parent.custom;
680
+ apply(matched, inline, own, custom);
589
681
  const style = new Map();
590
682
  for (const property of INHERITED){
591
683
  const value = parent.style.get(property);
@@ -620,6 +712,14 @@ const EMPTY_STYLE = new Map();
620
712
  of (element) {
621
713
  if (element === -2) return rootStyle.style;
622
714
  return elements[element] ? resolve(element).style : EMPTY_STYLE;
715
+ },
716
+ rootVariables () {
717
+ return new Map([
718
+ ...rootStyle.custom
719
+ ].map(([name, value])=>[
720
+ name,
721
+ withVariables(value, rootStyle.custom)
722
+ ]));
623
723
  }
624
724
  };
625
725
  }
@@ -658,7 +758,7 @@ const NAMED_COLORS = {
658
758
  const text = value.toLowerCase();
659
759
  const hex = /#([0-9a-f]{8}|[0-9a-f]{6}|[0-9a-f]{4}|[0-9a-f]{3})\b/.exec(text);
660
760
  const rgb = /rgba?\(\s*(-?[\d.]+%?)\s*[,\s]\s*(-?[\d.]+%?)\s*[,\s]\s*(-?[\d.]+%?)\s*(?:[,/]\s*(-?[\d.]+%?)\s*)?\)/.exec(text);
661
- const hsl = /hsla?\(\s*(-?[\d.]+)(?:deg)?\s*[,\s]\s*([\d.]+)%\s*[,\s]\s*([\d.]+)%\s*(?:[,/]\s*([\d.]+%?)\s*)?\)/.exec(text);
761
+ const hsl = /hsla?\(\s*(-?[\d.]+)(?:deg)?\s*[,\s]\s*([\d.]+)%?\s*[,\s]\s*([\d.]+)%?\s*(?:[,/]\s*([\d.]+%?)\s*)?\)/.exec(text);
662
762
  const named = /\b(transparent|[a-z]+)\b/.exec(text.replace(/[a-z-]+\(/g, ' ('));
663
763
  const first = [
664
764
  hex,
@@ -717,6 +817,36 @@ const NAMED_COLORS = {
717
817
  }
718
818
  return null;
719
819
  }
820
+ /** A variable's name that says it holds a brand's color. */ const BRAND_VARIABLE = /primary|secondary|brand|accent|highlight/i;
821
+ /** A custom property's whole value as a color: a color function, or the triple a color function reads through `var()`. */ function colorOfVariable(value) {
822
+ const text = value.trim();
823
+ if (/^(?:#[0-9a-f]{3,8}|(?:rgb|hsl)a?\([^()]*\))$/i.test(text)) return siteImportColorOf(text);
824
+ if (/^-?[\d.]+(?:deg)?[\s,]+[\d.]+%[\s,]+[\d.]+%$/.test(text)) return siteImportColorOf(`hsl(${text})`);
825
+ if (/^\d{1,3}[\s,]+\d{1,3}[\s,]+\d{1,3}$/.test(text)) return siteImportColorOf(`rgb(${text})`);
826
+ return null;
827
+ }
828
+ /** The most colors of a palette kept. */ const PALETTE_COLORS = 6;
829
+ /**
830
+ * The brand colors a page's root declares (AGL-4152): the custom properties
831
+ * named for a brand that hold a color, a triple included, none too pale, dark
832
+ * or grey to carry one, the most read first. What the page says its brand is,
833
+ * even where nothing on this page happens to draw it.
834
+ */ export function siteImportPaletteOf(styles, uses) {
835
+ const tally = new Map();
836
+ for (const [name, value] of styles.rootVariables()){
837
+ var _tally_get, _uses_get;
838
+ if (!BRAND_VARIABLE.test(name)) continue;
839
+ const color = colorOfVariable(value);
840
+ if (!color || color.a < 0.5 || siteImportSaturation(color) < 0.2) continue;
841
+ const light = siteImportLuminance(color);
842
+ if (light < 0.01 || light > 0.9) continue;
843
+ const hex = siteImportHex(color);
844
+ tally.set(hex, ((_tally_get = tally.get(hex)) != null ? _tally_get : 0) + ((_uses_get = uses.get(name)) != null ? _uses_get : 0));
845
+ }
846
+ return [
847
+ ...tally.entries()
848
+ ].sort((a, b)=>b[1] - a[1]).slice(0, PALETTE_COLORS).map(([hex])=>hex);
849
+ }
720
850
  /** `#rrggbb` of a color, its strength ignored. */ export function siteImportHex(color) {
721
851
  return `#${[
722
852
  color.r,
@@ -944,6 +1074,234 @@ const SIDES = [
944
1074
  });
945
1075
  return weights.length ? weights : null;
946
1076
  }
1077
+ /** A template's top-level words: a track, a `repeat()` or a `[line name]`. */ function gridTemplateWords(template) {
1078
+ const words = [];
1079
+ let depth = 0;
1080
+ let word = '';
1081
+ for (const char of template){
1082
+ if (char === '(' || char === '[') depth += 1;
1083
+ if (char === ')' || char === ']') depth -= 1;
1084
+ if (/\s/.test(char) && depth === 0) {
1085
+ if (word) words.push(word);
1086
+ word = '';
1087
+ } else word += char;
1088
+ }
1089
+ if (word) words.push(word);
1090
+ return words;
1091
+ }
1092
+ /**
1093
+ * How many tracks a grid template draws (AGL-4152), each `repeat()` opened at
1094
+ * its whole count; `null` for none, or where the screen decides (`auto-fill`).
1095
+ */ export function siteImportGridTrackCount(template) {
1096
+ var _ref;
1097
+ const text = (_ref = template == null ? void 0 : template.trim()) != null ? _ref : '';
1098
+ if (!text || /^(?:none|subgrid|masonry|inherit|initial|unset)$/.test(text)) return null;
1099
+ let count = 0;
1100
+ for (const word of gridTemplateWords(text)){
1101
+ if (word.startsWith('[')) continue;
1102
+ const repeat = /^repeat\(([\s\S]*)\)$/.exec(word);
1103
+ if (!repeat) {
1104
+ count += 1;
1105
+ continue;
1106
+ }
1107
+ const [times, ...tracks] = splitList(repeat[1]);
1108
+ const inner = siteImportGridTrackCount(tracks.join(','));
1109
+ if (!/^\d+$/.test(times != null ? times : '') || inner === null) return null;
1110
+ count += Number(times) * inner;
1111
+ }
1112
+ return count || null;
1113
+ }
1114
+ /** One written line of a placement: a line number, a span, or nothing a reader can place (`auto`, a named line). */ function gridLineOf(value, tracks) {
1115
+ var _ref;
1116
+ const text = (_ref = value == null ? void 0 : value.trim()) != null ? _ref : '';
1117
+ const span = /^span\s+(\d+)$/.exec(text);
1118
+ if (span) return {
1119
+ span: Math.max(1, Number(span[1]))
1120
+ };
1121
+ if (!/^-?\d+$/.test(text)) return {};
1122
+ const line = Number(text);
1123
+ if (line > 0) return {
1124
+ line
1125
+ };
1126
+ // A line counted from the end: the last there is, where the template does not say how many.
1127
+ if (line < 0) return {
1128
+ line: tracks === null ? Number.POSITIVE_INFINITY : Math.max(1, tracks + 2 + line)
1129
+ };
1130
+ return {};
1131
+ }
1132
+ function gridAxisOf(startValue, endValue, tracks) {
1133
+ var _end_span, _ref, _start_span;
1134
+ const start = gridLineOf(startValue, tracks);
1135
+ const end = gridLineOf(endValue, tracks);
1136
+ if (start.line === Number.POSITIVE_INFINITY) return {
1137
+ start: null,
1138
+ span: 1
1139
+ };
1140
+ if (start.line !== undefined && end.line !== undefined) {
1141
+ const from = Math.min(start.line, end.line);
1142
+ return {
1143
+ start: from,
1144
+ span: Math.max(1, Math.max(start.line, end.line) - from)
1145
+ };
1146
+ }
1147
+ if (start.line !== undefined) return {
1148
+ start: start.line,
1149
+ span: (_end_span = end.span) != null ? _end_span : 1
1150
+ };
1151
+ if (end.line !== undefined && end.line !== Number.POSITIVE_INFINITY) {
1152
+ var _start_span1;
1153
+ const span = (_start_span1 = start.span) != null ? _start_span1 : 1;
1154
+ return {
1155
+ start: Math.max(1, end.line - span),
1156
+ span
1157
+ };
1158
+ }
1159
+ return {
1160
+ start: null,
1161
+ span: (_ref = (_start_span = start.span) != null ? _start_span : end.span) != null ? _ref : 1
1162
+ };
1163
+ }
1164
+ /** The boxes a `grid-template-areas` names, by name. */ function gridAreasOf(template) {
1165
+ const areas = new Map();
1166
+ const rows = [
1167
+ ...(template != null ? template : '').matchAll(/"([^"]*)"|'([^']*)'/g)
1168
+ ].map((match)=>{
1169
+ var _ref, _match_;
1170
+ return ((_ref = (_match_ = match[1]) != null ? _match_ : match[2]) != null ? _ref : '').trim().split(/\s+/);
1171
+ });
1172
+ rows.forEach((names, row)=>{
1173
+ names.forEach((name, column)=>{
1174
+ if (!name || /^\.+$/.test(name)) return;
1175
+ const held = areas.get(name);
1176
+ if (!held) areas.set(name, {
1177
+ rowStart: row + 1,
1178
+ rowEnd: row + 2,
1179
+ columnStart: column + 1,
1180
+ columnEnd: column + 2
1181
+ });
1182
+ else {
1183
+ held.rowEnd = Math.max(held.rowEnd, row + 2);
1184
+ held.columnEnd = Math.max(held.columnEnd, column + 2);
1185
+ }
1186
+ });
1187
+ });
1188
+ return areas;
1189
+ }
1190
+ /** A flex or grid child's `order`, 0 where it sets none. */ export function siteImportOrderOf(style) {
1191
+ var _style_get;
1192
+ const order = Number.parseInt((_style_get = style.get('order')) != null ? _style_get : '', 10);
1193
+ return Number.isNaN(order) ? 0 : order;
1194
+ }
1195
+ /**
1196
+ * Where a grid places its children (AGL-4152), each child's cells by grid
1197
+ * line, as a browser lays them out: the lines each child names (by
1198
+ * `grid-area`, a named area, `grid-row` or `grid-column`, a longhand over the
1199
+ * shorthand; lines from the end read from the template), then the children it
1200
+ * leaves to the grid's flow, in `order`, each in the first cells free.
1201
+ *
1202
+ * `null` where it cannot say, or there is nothing to say: no child names a
1203
+ * place (a `span` says how wide, and `1 / -1` the whole row, not where), the
1204
+ * rest of the children flow by columns, or through tracks the screen decides.
1205
+ */ export function siteImportGridBoxes(container, items) {
1206
+ var _siteImportGridTrackCount, _siteImportGridTrackCount1, _container_get;
1207
+ const areas = gridAreasOf(container.get('grid-template-areas'));
1208
+ const named = [
1209
+ ...areas.values()
1210
+ ];
1211
+ const columns = (_siteImportGridTrackCount = siteImportGridTrackCount(container.get('grid-template-columns'))) != null ? _siteImportGridTrackCount : named.length ? Math.max(...named.map((area)=>area.columnEnd)) - 1 : null;
1212
+ const rows = (_siteImportGridTrackCount1 = siteImportGridTrackCount(container.get('grid-template-rows'))) != null ? _siteImportGridTrackCount1 : named.length ? Math.max(...named.map((area)=>area.rowEnd)) - 1 : null;
1213
+ const places = items.map((item)=>{
1214
+ var _item_get, _ref, _ref1, _item_get1, _item_get2, _item_get3, _item_get4;
1215
+ var _item_get5, _item_get6;
1216
+ const [rowStart, columnStart, rowEnd, columnEnd] = ((_item_get = item.get('grid-area')) != null ? _item_get : '').split('/').map((part)=>part.trim());
1217
+ const area = rowStart && columnStart === undefined ? areas.get(rowStart) : undefined;
1218
+ const row = (_ref = (_item_get5 = item.get('grid-row')) == null ? void 0 : _item_get5.split('/')) != null ? _ref : [
1219
+ rowStart,
1220
+ rowEnd
1221
+ ];
1222
+ const column = (_ref1 = (_item_get6 = item.get('grid-column')) == null ? void 0 : _item_get6.split('/')) != null ? _ref1 : [
1223
+ columnStart,
1224
+ columnEnd
1225
+ ];
1226
+ return {
1227
+ row: area ? {
1228
+ start: area.rowStart,
1229
+ span: area.rowEnd - area.rowStart
1230
+ } : gridAxisOf((_item_get1 = item.get('grid-row-start')) != null ? _item_get1 : row[0], (_item_get2 = item.get('grid-row-end')) != null ? _item_get2 : row[1], rows),
1231
+ column: area ? {
1232
+ start: area.columnStart,
1233
+ span: area.columnEnd - area.columnStart
1234
+ } : gridAxisOf((_item_get3 = item.get('grid-column-start')) != null ? _item_get3 : column[0], (_item_get4 = item.get('grid-column-end')) != null ? _item_get4 : column[1], columns)
1235
+ };
1236
+ });
1237
+ const wholeRow = (axis)=>axis.start === 1 && (axis.span === Number.POSITIVE_INFINITY || columns !== null && axis.span >= columns);
1238
+ if (!places.some((place)=>place.row.start !== null || place.column.start !== null && !wholeRow(place.column))) return null;
1239
+ const flows = places.some((place)=>place.row.start === null || place.column.start === null);
1240
+ if (flows && (columns === null || /\bcolumn\b/.test((_container_get = container.get('grid-auto-flow')) != null ? _container_get : ''))) return null;
1241
+ const finite = (value)=>Number.isFinite(value) ? value : 0;
1242
+ const lastRow = Math.max(rows === null ? 1 : rows + 1, ...places.map((place)=>{
1243
+ var _place_row_start;
1244
+ return ((_place_row_start = place.row.start) != null ? _place_row_start : 0) + finite(place.row.span);
1245
+ }));
1246
+ const lastColumn = columns === null ? Math.max(...places.map((place)=>{
1247
+ var _place_column_start;
1248
+ return ((_place_column_start = place.column.start) != null ? _place_column_start : 0) + finite(place.column.span);
1249
+ })) : columns + 1;
1250
+ const boxes = places.map(()=>null);
1251
+ const free = (row, column, height, width)=>boxes.every((box)=>!box || box.rowEnd <= row || box.rowStart >= row + height || box.columnEnd <= column || box.columnStart >= column + width);
1252
+ const put = (at, row, column, height, width)=>{
1253
+ boxes[at] = {
1254
+ rowStart: row,
1255
+ rowEnd: row + height,
1256
+ columnStart: column,
1257
+ columnEnd: column + width
1258
+ };
1259
+ };
1260
+ const heightOf = (axis, row)=>Number.isFinite(axis.span) ? axis.span : Math.max(1, lastRow - row);
1261
+ const widthOf = (axis, column)=>Math.max(1, Math.min(axis.span, lastColumn - column));
1262
+ const inOrder = places.map((_, at)=>at).sort((a, b)=>siteImportOrderOf(items[a]) - siteImportOrderOf(items[b]) || a - b);
1263
+ // Rows and columns both named first, whatever their order: nothing flows into them.
1264
+ for (const at of inOrder){
1265
+ const { row, column } = places[at];
1266
+ if (row.start !== null && column.start !== null) put(at, row.start, column.start, heightOf(row, row.start), widthOf(column, column.start));
1267
+ }
1268
+ // A row named and its column not: the first cells free in that row.
1269
+ for (const at of inOrder){
1270
+ const { row, column } = places[at];
1271
+ if (row.start === null || column.start !== null) continue;
1272
+ const height = heightOf(row, row.start);
1273
+ const width = widthOf(column, 1);
1274
+ let left = 1;
1275
+ while(left + width < lastColumn && !free(row.start, left, height, width))left += 1;
1276
+ put(at, row.start, left, height, width);
1277
+ }
1278
+ // The rest, in the order they flow: a cursor down the grid, never back.
1279
+ let cursorRow = 1;
1280
+ let cursorColumn = 1;
1281
+ for (const at of inOrder){
1282
+ if (boxes[at]) continue;
1283
+ const { row, column } = places[at];
1284
+ const height = heightOf(row, 1);
1285
+ if (column.start !== null) {
1286
+ const width = widthOf(column, column.start);
1287
+ if (column.start < cursorColumn) cursorRow += 1;
1288
+ cursorColumn = column.start;
1289
+ while(!free(cursorRow, cursorColumn, height, width))cursorRow += 1;
1290
+ put(at, cursorRow, cursorColumn, height, width);
1291
+ continue;
1292
+ }
1293
+ const width = widthOf(column, 1);
1294
+ while(cursorColumn + width > lastColumn || !free(cursorRow, cursorColumn, height, width)){
1295
+ cursorColumn += 1;
1296
+ if (cursorColumn + width > lastColumn) {
1297
+ cursorColumn = 1;
1298
+ cursorRow += 1;
1299
+ }
1300
+ }
1301
+ put(at, cursorRow, cursorColumn, height, width);
1302
+ }
1303
+ return boxes;
1304
+ }
947
1305
  /** Weights scaled to twelfths, each at least 1: `[420, 788]` is `[4, 8]`. */ export function siteImportTwelfths(weights) {
948
1306
  const total = weights.reduce((sum, weight)=>sum + Math.max(0, weight), 0);
949
1307
  if (!total) return weights.map(()=>1);
@@ -957,5 +1315,76 @@ const SIDES = [
957
1315
  if (!value) return [];
958
1316
  return splitList(value).map((family)=>family.replace(/^["']|["']$/g, '').trim()).filter((family)=>family && !/^(?:serif|sans-serif|monospace|cursive|fantasy|system-ui|ui-[\w-]+|-apple-system|blinkmacsystemfont|inherit|initial|emoji|math|var\(.*)$/i.test(family));
959
1317
  }
1318
+ /*------------------------------------------
1319
+ * Reading a page's stylesheets
1320
+ *-----------------------------------------*/ /**
1321
+ * How much of a page's stylesheets is read (AGL-4152). A site builder's own
1322
+ * sheet can be its fifth link and 1.3 MB, with half a megabyte more behind it,
1323
+ * and every color and font of the site is in it. So:
1324
+ *
1325
+ * - `firstLookBytes`: every link is first read this far. Most sheets end
1326
+ * inside it, and those are done; a larger one shows here whether it declares
1327
+ * `:root` custom properties, which they do at their top.
1328
+ * - `budgetBytes`: what all the page's sheets may hold together, first looks
1329
+ * included. Three times the sheet that set it, with room for what comes with
1330
+ * it. What does not fit is cut, and a cut sheet is read as far as it goes.
1331
+ * - `atOnce`: first looks asked together, so twenty links are not twenty
1332
+ * connections to one host.
1333
+ *
1334
+ * So one page's read costs at most its links' first looks and the budget:
1335
+ * 3.8 MB at the most links a page lists, however large its sheets are.
1336
+ */ export const SITE_IMPORT_SHEET_LIMITS = {
1337
+ firstLookBytes: 32 * 1024,
1338
+ budgetBytes: 3 * 1024 * 1024,
1339
+ atOnce: 6
1340
+ };
1341
+ /** Whether a sheet declares custom properties on the page's root: its theme, not a widget's. */ const ROOT_VARIABLES = /(?:^|[},;\s])(?::root|html)\b[^{}]*\{[^{}]*--[\w-]+\s*:/;
1342
+ /**
1343
+ * Every stylesheet a page links, to a byte budget (AGL-4152), in link order,
1344
+ * as the cascade needs them. Each is read a first look; then the ones that go
1345
+ * on are read whole, the ones declaring `:root` custom properties first, then
1346
+ * the rest in link order, each to what is left of the budget. A read that fails
1347
+ * leaves its sheet out, and never the page.
1348
+ */ export async function siteImportReadStylesheets(urls, read, options = {}) {
1349
+ var _options_budgetBytes, _options_outOfTime;
1350
+ const budget = (_options_budgetBytes = options.budgetBytes) != null ? _options_budgetBytes : SITE_IMPORT_SHEET_LIMITS.budgetBytes;
1351
+ const outOfTime = (_options_outOfTime = options.outOfTime) != null ? _options_outOfTime : ()=>false;
1352
+ const unique = [
1353
+ ...new Set(urls)
1354
+ ];
1355
+ const looks = unique.map(()=>null);
1356
+ const ask = (url, maxBytes)=>read(url, maxBytes).then((answer)=>(answer == null ? void 0 : answer.ok) ? answer : null, ()=>null);
1357
+ let next = 0;
1358
+ await Promise.all(Array.from({
1359
+ length: Math.min(SITE_IMPORT_SHEET_LIMITS.atOnce, unique.length)
1360
+ }, async ()=>{
1361
+ while(next < unique.length && !outOfTime()){
1362
+ const at = next;
1363
+ next += 1;
1364
+ looks[at] = await ask(unique[at], SITE_IMPORT_SHEET_LIMITS.firstLookBytes);
1365
+ }
1366
+ }));
1367
+ let room = budget - looks.reduce((sum, look)=>{
1368
+ var _ref;
1369
+ return sum + ((_ref = look == null ? void 0 : look.bytes) != null ? _ref : 0);
1370
+ }, 0);
1371
+ const rest = unique.map((_, at)=>at).filter((at)=>{
1372
+ var _looks_at;
1373
+ return (_looks_at = looks[at]) == null ? void 0 : _looks_at.truncated;
1374
+ });
1375
+ const theme = (at)=>ROOT_VARIABLES.test(looks[at].text) ? 0 : 1;
1376
+ rest.sort((a, b)=>theme(a) - theme(b) || a - b);
1377
+ for (const at of rest){
1378
+ if (room <= 0 || outOfTime()) break;
1379
+ const look = looks[at];
1380
+ const whole = await ask(unique[at], look.bytes + room);
1381
+ if (!whole) continue;
1382
+ room -= whole.bytes - look.bytes;
1383
+ looks[at] = whole;
1384
+ }
1385
+ return looks.flatMap((look)=>look ? [
1386
+ look.text
1387
+ ] : []);
1388
+ }
960
1389
 
961
1390
  //# sourceMappingURL=site-import-css.js.map