@perkyzz/emvb 1.2.0 → 1.4.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 (150) hide show
  1. package/README.md +2 -2
  2. package/THIRD_PARTY_NOTICES.md +2 -2
  3. package/package.json +4 -4
  4. package/src/admin/bulk-api.ts +5 -1
  5. package/src/admin/editor/A11yCopilot.tsx +3 -1
  6. package/src/admin/editor/Editor.tsx +22 -2
  7. package/src/admin/editor/Timeline.tsx +1 -8
  8. package/src/admin/editor/canvas/DeviceBar.tsx +7 -2
  9. package/src/admin/editor/canvas/EmptyCanvas.tsx +11 -2
  10. package/src/admin/editor/canvas/use-canvas-dnd.ts +1 -20
  11. package/src/admin/editor/canvas/vnode-react.tsx +5 -0
  12. package/src/admin/editor/co-design.ts +36 -1
  13. package/src/admin/editor/dnd/dragged-source.ts +22 -0
  14. package/src/admin/editor/dnd/layer-drop.ts +141 -0
  15. package/src/admin/editor/dnd/new-element.ts +76 -0
  16. package/src/admin/editor/editor-css.ts +9 -1
  17. package/src/admin/editor/element-chrome.ts +34 -0
  18. package/src/admin/editor/feature-css.ts +22 -0
  19. package/src/admin/editor/inert-links.ts +8 -0
  20. package/src/admin/editor/panels/ColorControl.tsx +2 -2
  21. package/src/admin/editor/panels/FontFaces.tsx +342 -0
  22. package/src/admin/editor/panels/LayersPanel.tsx +56 -6
  23. package/src/admin/editor/panels/LeftPanel.tsx +153 -29
  24. package/src/admin/editor/panels/SettingsPanel.tsx +1 -0
  25. package/src/admin/editor/panels/SiteStylesDrawer.tsx +5 -0
  26. package/src/admin/editor/panels/TemplatesPanel.tsx +205 -0
  27. package/src/admin/editor/panels/TokenScales.tsx +2 -0
  28. package/src/admin/editor/panels/TriggersEditor.tsx +203 -9
  29. package/src/admin/editor/panels/VariableSection.tsx +111 -7
  30. package/src/admin/editor/panels/color-space.ts +71 -6
  31. package/src/admin/editor/panels/settings/AudienceEditor.tsx +3 -0
  32. package/src/admin/editor/panels/settings/FieldControl.tsx +18 -0
  33. package/src/admin/editor/panels/settings/ItemList.tsx +33 -0
  34. package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +9 -3
  35. package/src/admin/editor/panels/settings/StickyControl.tsx +97 -0
  36. package/src/admin/editor/panels/settings/StyleRow.tsx +14 -0
  37. package/src/admin/editor/panels/settings/style-sections.ts +19 -2
  38. package/src/admin/editor/panels/url-rules.ts +43 -0
  39. package/src/admin/editor/panels/use-layers-drop.ts +232 -0
  40. package/src/admin/editor/store.ts +4 -2
  41. package/src/admin/editor/useExtensions.ts +3 -1
  42. package/src/admin/editor/useNodeActions.ts +74 -1
  43. package/src/admin/editor/useSave.ts +116 -85
  44. package/src/admin/export-api.ts +25 -2
  45. package/src/admin/extension-outputs.ts +179 -0
  46. package/src/admin/font-files-api.ts +38 -0
  47. package/src/admin/pages/ImportButton.tsx +15 -4
  48. package/src/admin/pages/ImportElementorDialog.tsx +22 -188
  49. package/src/admin/pages/ImportHtmlDialog.tsx +209 -0
  50. package/src/admin/pages/ImportImageCopies.tsx +167 -0
  51. package/src/admin/pages/ImportReport.tsx +63 -0
  52. package/src/admin/pages/NewPageDialog.tsx +54 -32
  53. package/src/admin/pages/PageList.tsx +9 -0
  54. package/src/admin/pages/ThemePartList.tsx +9 -0
  55. package/src/astro/EmVBPage.astro +5 -2
  56. package/src/astro/extension-styles.ts +35 -0
  57. package/src/astro/forms/EmVBFormsClient.astro +3 -0
  58. package/src/astro/forms/enhance.ts +107 -0
  59. package/src/astro/popups/runtime.ts +47 -2
  60. package/src/astro/render.ts +19 -4
  61. package/src/astro/request-data.ts +6 -0
  62. package/src/astro/resolve-theme.ts +11 -4
  63. package/src/astro/resolve.ts +9 -0
  64. package/src/astro/widgets/EmVBCountdownRuntime.astro +8 -0
  65. package/src/astro/widgets/EmVBCounterRuntime.astro +8 -0
  66. package/src/astro/widgets/EmVBHeadlineRuntime.astro +9 -0
  67. package/src/astro/widgets/EmVBMapRuntime.astro +8 -0
  68. package/src/astro/widgets/EmVBShareRuntime.astro +9 -0
  69. package/src/astro/widgets/EmVBStickyRuntime.astro +9 -0
  70. package/src/astro/widgets/EmVBThemeRuntime.astro +9 -0
  71. package/src/astro/widgets/EmVBWidgetRuntimes.astro +22 -0
  72. package/src/astro/widgets/alerts.ts +16 -2
  73. package/src/astro/widgets/carousel.ts +65 -24
  74. package/src/astro/widgets/countdown.ts +91 -0
  75. package/src/astro/widgets/counter.ts +98 -0
  76. package/src/astro/widgets/extension-runtimes.ts +27 -0
  77. package/src/astro/widgets/headline.ts +99 -0
  78. package/src/astro/widgets/map.ts +37 -0
  79. package/src/astro/widgets/share.ts +21 -0
  80. package/src/astro/widgets/sticky.ts +87 -0
  81. package/src/astro/widgets/theme.ts +52 -0
  82. package/src/constants.ts +1 -1
  83. package/src/core/a11y/audit.ts +177 -6
  84. package/src/core/arrange.ts +71 -1
  85. package/src/core/canvas-collections.ts +28 -0
  86. package/src/core/css/font-face.ts +72 -0
  87. package/src/core/css/generate.ts +35 -1
  88. package/src/core/elements/counter-format.ts +15 -0
  89. package/src/core/elements/form-elements.ts +119 -9
  90. package/src/core/elements/icon-svg.ts +14 -0
  91. package/src/core/elements/index.ts +54 -4
  92. package/src/core/elements/input-limits.ts +44 -0
  93. package/src/core/elements/marketing.ts +1490 -0
  94. package/src/core/elements/menu.ts +109 -47
  95. package/src/core/elements/site.ts +108 -0
  96. package/src/core/elements/widgets.ts +40 -5
  97. package/src/core/export/runtimes.generated.ts +10 -3
  98. package/src/core/export/site.ts +55 -4
  99. package/src/core/extensions/classes.ts +114 -0
  100. package/src/core/extensions/index.ts +17 -2
  101. package/src/core/extensions/normalize.ts +13 -0
  102. package/src/core/extensions/registry.ts +134 -10
  103. package/src/core/extensions/render-ext.ts +66 -21
  104. package/src/core/extensions/safe-nodes.ts +67 -11
  105. package/src/core/extensions/types.ts +48 -1
  106. package/src/core/fonts/files.ts +74 -0
  107. package/src/core/fonts/google.ts +112 -0
  108. package/src/core/fonts/test-font.ts +9 -0
  109. package/src/core/import/elementor/context.ts +13 -4
  110. package/src/core/import/elementor/index.ts +32 -21
  111. package/src/core/import/elementor/widgets.ts +532 -293
  112. package/src/core/import/html/blocks.ts +602 -0
  113. package/src/core/import/html/index.ts +155 -0
  114. package/src/core/import/html/map.ts +958 -0
  115. package/src/core/import/html/parse.ts +430 -0
  116. package/src/core/index.ts +45 -0
  117. package/src/core/limits.ts +6 -0
  118. package/src/core/migrate/v18.ts +3 -0
  119. package/src/core/public-attributes.ts +17 -0
  120. package/src/core/render/extras.ts +31 -7
  121. package/src/core/render/form.ts +103 -10
  122. package/src/core/render/index.ts +172 -16
  123. package/src/core/render/section.ts +31 -1
  124. package/src/core/render/spacer-basis.ts +102 -0
  125. package/src/core/render/theme-boot.ts +16 -0
  126. package/src/core/render/vnode.ts +1 -1
  127. package/src/core/sanitize/css.ts +68 -3
  128. package/src/core/sanitize/motion.ts +12 -0
  129. package/src/core/schema/design.ts +40 -0
  130. package/src/core/schema/layout.ts +534 -22
  131. package/src/core/schema/style.ts +28 -0
  132. package/src/core/templates/index.ts +218 -0
  133. package/src/core/templates/pages.ts +29 -0
  134. package/src/core/templates/sections.ts +794 -0
  135. package/src/core/templates/types.ts +9 -0
  136. package/src/core/theme/dynamic.ts +11 -6
  137. package/src/core/theme/popup-markup.ts +21 -2
  138. package/src/core/theme/popup-rules.ts +36 -0
  139. package/src/core/theme/triggers.ts +64 -5
  140. package/src/core/translatable.ts +5 -2
  141. package/src/extension-build.ts +331 -0
  142. package/src/index.ts +25 -3
  143. package/src/mcp/brief.ts +191 -0
  144. package/src/mcp/prompts.ts +33 -0
  145. package/src/mcp/read.ts +29 -1
  146. package/src/mcp/tools.ts +40 -1
  147. package/src/mcp/write.ts +41 -1
  148. package/src/server/extension-routes.ts +73 -0
  149. package/src/server/font-routes.ts +301 -0
  150. package/src/server/plugin.ts +11 -1
@@ -59,6 +59,23 @@ export type ExtensionField = Pick<
59
59
  "key" | "kind" | "label" | "options" | "message" | "optional" | "max" | "min"
60
60
  >;
61
61
 
62
+ /**
63
+ * W-1086: a node an extension's render() returns. Built-in node fields, plus `extClasses`: class
64
+ * names from the extension's own `classes.allow` list. Names not on the list are dropped. This is
65
+ * render output only; it is never stored in a layout.
66
+ */
67
+ export type ExtensionOutputNode = LayoutNode & { extClasses?: readonly string[] };
68
+
69
+ /**
70
+ * W-1086: the static class allowlist an extension declares. Every name starts with `prefix`
71
+ * (lowercase letters, digits and hyphens, ending in `-`, never `emvb…`), so extension classes
72
+ * can't clash with EmVB's `emvb-*` classes or a site's own design classes.
73
+ */
74
+ export type ExtensionClasses = {
75
+ prefix: string;
76
+ allow: readonly string[];
77
+ };
78
+
62
79
  /** Context passed to an element render function (read-only source values). */
63
80
  export type ExtensionRenderContext = {
64
81
  /** Resolved values from this extension's sources, keyed by source name then field. */
@@ -80,7 +97,7 @@ export type ExtensionElement = {
80
97
  render: (
81
98
  props: Record<string, unknown>,
82
99
  ctx: ExtensionRenderContext,
83
- ) => LayoutNode | readonly LayoutNode[];
100
+ ) => ExtensionOutputNode | readonly ExtensionOutputNode[];
84
101
  };
85
102
 
86
103
  /**
@@ -118,6 +135,32 @@ export type EmVBExtension = {
118
135
  elements?: readonly ExtensionElement[];
119
136
  panels?: readonly ExtensionPanel[];
120
137
  sources?: readonly ExtensionSource[];
138
+ /**
139
+ * W-1086: the module that exports this extension, as `import.meta.url`. EmVB bundles it into
140
+ * the site's server build (Cloudflare Workers and production builds included), and resolves
141
+ * `css` and `runtime` against it at build time.
142
+ */
143
+ module?: string;
144
+ /** W-1086: class names render() may put on its nodes through `extClasses`. */
145
+ classes?: ExtensionClasses;
146
+ /**
147
+ * W-1086: a stylesheet in the extension's own package, relative to `module` (`"./hello.css"`).
148
+ * Read once at build time; written on pages (and in the editor canvas) that render one of the
149
+ * extension's elements.
150
+ */
151
+ css?: string;
152
+ /**
153
+ * W-1086: a self-contained browser script in the extension's own package, relative to
154
+ * `module` (`"./runtime.js"`): no `import` or `export`. Read once at build time; loaded only on
155
+ * public pages that render one of the extension's elements, never in the admin.
156
+ */
157
+ runtime?: string;
158
+ };
159
+
160
+ /** W-1086: the file contents EmVB read at build time for `css` and `runtime`. */
161
+ export type ExtensionAssets = {
162
+ css?: string;
163
+ runtime?: string;
121
164
  };
122
165
 
123
166
  /**
@@ -156,4 +199,8 @@ export type ExtensionManifest = {
156
199
  elements: readonly ExtensionElementManifest[];
157
200
  panels: readonly ExtensionPanelManifest[];
158
201
  sources: readonly ExtensionSourceManifest[];
202
+ /** W-1086: the checked class allowlist; absent when the extension declares none. */
203
+ classes?: ExtensionClasses;
204
+ /** W-1086: stylesheet and runtime text read at build time; absent when there are none. */
205
+ assets?: ExtensionAssets;
159
206
  };
@@ -0,0 +1,74 @@
1
+ /**
2
+ * W-1072: custom font files. EmDash's media library (1.4 and 1.5) refuses `font/woff2` uploads
3
+ * (its default upload allowlist has images, video, audio and PDF only), so EmVB keeps font files
4
+ * in its own plugin storage and serves them from a public route with a long cache.
5
+ */
6
+
7
+ /**
8
+ * The largest font file EmVB stores: 1 MiB. Base64 in a plugin storage row, that stays under
9
+ * Cloudflare D1's 2 MB row limit. A Latin WOFF2 is usually 15 to 150 KB.
10
+ */
11
+ export const FONT_MAX_BYTES = 1024 * 1024;
12
+
13
+ /** The most font files a site keeps (uploads and Google downloads together). */
14
+ export const FONT_FILE_LIMIT = 300;
15
+
16
+ /** A stored font file's id: the first 16 hex digits of the SHA-256 of its bytes. */
17
+ export const FONT_ID = /^[0-9a-f]{16}$/;
18
+
19
+ /** The public URL a stored font file is served from (the plugin id is `emvb`). */
20
+ export const fontFileUrl = (id: string) => `/_emdash/api/plugins/emvb/font?id=${id}`;
21
+
22
+ const WOFF2_HEADER_BYTES = 48;
23
+ /** sfnt flavours: TrueType (0x00010000), CFF (`OTTO`) and collections (`ttcf`). */
24
+ const FLAVORS = new Set([0x00010000, 0x4f54544f, 0x74746366]);
25
+
26
+ /**
27
+ * A real WOFF2 file: the `wOF2` signature, a known sfnt flavour, and a header length that matches
28
+ * the file. Anything else (a renamed TTF, an HTML error page, a truncated download) is refused.
29
+ */
30
+ export function isWoff2(bytes: Uint8Array): boolean {
31
+ if (bytes.byteLength < WOFF2_HEADER_BYTES) return false;
32
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
33
+ return (
34
+ view.getUint32(0) === 0x774f4632 &&
35
+ FLAVORS.has(view.getUint32(4)) &&
36
+ view.getUint32(8) === bytes.byteLength
37
+ );
38
+ }
39
+
40
+ /** Why a font file is refused, or undefined when it can be stored. */
41
+ export function fontFileProblem(bytes: Uint8Array): string | undefined {
42
+ if (bytes.byteLength > FONT_MAX_BYTES) {
43
+ return `This font is ${Math.ceil(bytes.byteLength / 1024)} KB. Use a WOFF2 file up to ${FONT_MAX_BYTES / 1024} KB.`;
44
+ }
45
+ if (!isWoff2(bytes)) return "This isn't a WOFF2 font file. Convert the font to WOFF2 first.";
46
+ return undefined;
47
+ }
48
+
49
+ /** The id a font file is stored under (content-addressed, so the same file is stored once). */
50
+ export async function fontFileId(bytes: Uint8Array): Promise<string> {
51
+ const digest = await crypto.subtle.digest("SHA-256", new Uint8Array(bytes));
52
+ return [...new Uint8Array(digest).slice(0, 8)]
53
+ .map((b) => b.toString(16).padStart(2, "0"))
54
+ .join("");
55
+ }
56
+
57
+ const CHUNK = 0x8000;
58
+
59
+ export function bytesToBase64(bytes: Uint8Array): string {
60
+ let binary = "";
61
+ for (let i = 0; i < bytes.byteLength; i += CHUNK) {
62
+ binary += String.fromCharCode(...bytes.subarray(i, i + CHUNK));
63
+ }
64
+ return btoa(binary);
65
+ }
66
+
67
+ /** The bytes of a base64 string, or undefined when it isn't base64. */
68
+ export function base64ToBytes(text: string): Uint8Array | undefined {
69
+ if (!/^[A-Za-z0-9+/]*={0,2}$/.test(text) || text.length % 4 !== 0) return undefined;
70
+ const binary = atob(text);
71
+ const bytes = new Uint8Array(binary.length);
72
+ for (let i = 0; i < binary.length; i += 1) bytes[i] = binary.charCodeAt(i);
73
+ return bytes;
74
+ }
@@ -0,0 +1,112 @@
1
+ /**
2
+ * W-1072: Google fonts, self-hosted. The server asks Google's CSS API for one family, weight and
3
+ * style, downloads the WOFF2 files it lists and stores them like uploads, so public pages only
4
+ * load the site's own URLs. Google Fonts are under the SIL Open Font License (a few under Apache
5
+ * 2.0 or the Ubuntu Font Licence), which allow self-hosting.
6
+ */
7
+
8
+ export const GOOGLE_CSS_HOST = "fonts.googleapis.com";
9
+ export const GOOGLE_FILE_HOST = "fonts.gstatic.com";
10
+
11
+ /** Google family names: Latin letters, digits and single spaces ("Open Sans", "Noto Sans JP"). */
12
+ export const GOOGLE_FAMILY = /^[A-Za-z0-9](?:[A-Za-z0-9]| (?=[A-Za-z0-9])){0,59}$/;
13
+
14
+ /** Subset labels as the CSS API writes them in its comments ("latin-ext", "[12]"). */
15
+ export const GOOGLE_SUBSET = /^(?:[a-z0-9-]{1,30}|\[\d{1,3}\])$/;
16
+
17
+ /** The subsets a new import downloads unless the editor picks others. */
18
+ export const DEFAULT_GOOGLE_SUBSETS = ["latin", "latin-ext"] as const;
19
+
20
+ /** The most files one weight and style may bring (one per subset). */
21
+ export const GOOGLE_FILES_PER_FACE = 12;
22
+
23
+ export const GOOGLE_WEIGHTS = [100, 200, 300, 400, 500, 600, 700, 800, 900] as const;
24
+
25
+ /**
26
+ * A modern browser's User-Agent: the CSS API picks the file format from it, and only answers
27
+ * WOFF2 to browsers it knows support it.
28
+ */
29
+ export const GOOGLE_USER_AGENT =
30
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36";
31
+
32
+ /** The CSS API URL for one family, weight and style. */
33
+ export function googleCssUrl(family: string, weight: number, style: "normal" | "italic"): string {
34
+ const name = family.replace(/ /g, "+");
35
+ const axes = style === "italic" ? `ital,wght@1,${weight}` : `wght@${weight}`;
36
+ return `https://${GOOGLE_CSS_HOST}/css2?family=${name}:${axes}&display=swap`;
37
+ }
38
+
39
+ export type GoogleFace = {
40
+ style: "normal" | "italic";
41
+ weight: number;
42
+ /** A fonts.gstatic.com HTTPS URL. */
43
+ url: string;
44
+ unicodeRange?: string;
45
+ /** The subset label from the comment before the rule, when there is one. */
46
+ subset?: string;
47
+ };
48
+
49
+ /** A font file URL on fonts.gstatic.com over HTTPS, with no credentials or port, or undefined. */
50
+ export function googleFileUrl(raw: string): string | undefined {
51
+ let url: URL;
52
+ try {
53
+ url = new URL(raw);
54
+ } catch {
55
+ return undefined;
56
+ }
57
+ if (url.protocol !== "https:" || url.hostname !== GOOGLE_FILE_HOST) return undefined;
58
+ if (url.port || url.username || url.password) return undefined;
59
+ return url.href;
60
+ }
61
+
62
+ const UNICODE_RANGE_VALUE =
63
+ /^U\+[0-9A-Fa-f?]{1,6}(?:-[0-9A-Fa-f]{1,6})?(?:, ?U\+[0-9A-Fa-f?]{1,6}(?:-[0-9A-Fa-f]{1,6})?)*$/;
64
+
65
+ /**
66
+ * The WOFF2 faces in a CSS API answer. Rules that don't read as expected (another format, another
67
+ * host, a weight range) are skipped, never guessed at.
68
+ */
69
+ export function parseGoogleFontCss(css: string): GoogleFace[] {
70
+ const faces: GoogleFace[] = [];
71
+ const rule = /(?:\/\*\s*([^*]{1,40}?)\s*\*\/\s*)?@font-face\s*\{([^{}]{1,2000})\}/g;
72
+ for (const match of css.matchAll(rule)) {
73
+ const label = match[1]?.trim();
74
+ const body = match[2] ?? "";
75
+ const style = /(?:^|;)\s*font-style:\s*(normal|italic)\s*(?:;|$)/.exec(body)?.[1];
76
+ const weight = /(?:^|;)\s*font-weight:\s*(\d{1,4})\s*(?:;|$)/.exec(body)?.[1];
77
+ const src =
78
+ /(?:^|;)\s*src:\s*url\(([^()\s'"]{1,500})\)\s*format\(['"]woff2['"]\)\s*(?:;|$)/.exec(
79
+ body,
80
+ )?.[1];
81
+ const range = /(?:^|;)\s*unicode-range:\s*([^;]{1,1000}?)\s*(?:;|$)/.exec(body)?.[1];
82
+ if (!style || !weight || !src) continue;
83
+ const url = googleFileUrl(src);
84
+ const n = Number(weight);
85
+ if (!url || n < 1 || n > 1000) continue;
86
+ if (range !== undefined && !UNICODE_RANGE_VALUE.test(range)) continue;
87
+ faces.push({
88
+ style: style === "italic" ? "italic" : "normal",
89
+ weight: n,
90
+ url,
91
+ ...(range !== undefined ? { unicodeRange: range } : {}),
92
+ ...(label && GOOGLE_SUBSET.test(label) ? { subset: label } : {}),
93
+ });
94
+ }
95
+ return faces;
96
+ }
97
+
98
+ /**
99
+ * The faces of one import: the requested weight and style, in the chosen subsets. A rule without
100
+ * a subset label is kept (an answer with a single file has none).
101
+ */
102
+ export function pickGoogleFaces(
103
+ faces: readonly GoogleFace[],
104
+ wanted: { weight: number; style: "normal" | "italic"; subsets: readonly string[] },
105
+ ): GoogleFace[] {
106
+ return faces.filter(
107
+ (face) =>
108
+ face.weight === wanted.weight &&
109
+ face.style === wanted.style &&
110
+ (face.subset === undefined || wanted.subsets.includes(face.subset)),
111
+ );
112
+ }
@@ -0,0 +1,9 @@
1
+ /** A minimal byte string that passes the WOFF2 header check (tests only). */
2
+ export function fakeWoff2(size = 64, fill = 0): Uint8Array {
3
+ const bytes = new Uint8Array(size).fill(fill);
4
+ const view = new DataView(bytes.buffer);
5
+ view.setUint32(0, 0x774f4632);
6
+ view.setUint32(4, 0x00010000);
7
+ view.setUint32(8, size);
8
+ return bytes;
9
+ }
@@ -87,6 +87,8 @@ const DEFAULT_TYPOGRAPHY: Record<string, GlobalTypography> = {
87
87
 
88
88
  /** Old-site images listed for copying; an export with more lists the first ones. */
89
89
  const MAX_IMAGES = 300;
90
+ /** The longest old-site address (`https://old.example/blog`) looked for before `/wp-content/`. */
91
+ const MAX_SITE_URL = 512;
90
92
 
91
93
  /** Notes kept per import; the rest are counted into one line. */
92
94
  const MAX_NOTES = 300;
@@ -213,16 +215,23 @@ export class ImportContext {
213
215
  */
214
216
  findOldSite(text: string): void {
215
217
  const found = new Map<string, URL>();
216
- for (const match of text.matchAll(
217
- /https?:\/\/[^"'<>\s/\\]+(?:\/[^"'<>/\\]*)*?(?=\/wp-content\/)/gi,
218
- )) {
218
+ // W-1074: only the text just before each `/wp-content/` is searched, so input full of
219
+ // addresses that never reach one (`http://http://…`) reads in linear time, not quadratic.
220
+ const marker = "/wp-content/";
221
+ let at = 0;
222
+ for (let hits = 0; hits < 200 && found.size < 5; hits++) {
223
+ const hit = text.indexOf(marker, at);
224
+ if (hit === -1) break;
225
+ at = hit + marker.length;
226
+ const window = text.slice(Math.max(0, hit - MAX_SITE_URL), hit + marker.length);
227
+ const match = /https?:\/\/[^"'<>\s/\\]+(?:\/[^"'<>/\\]*)*?(?=\/wp-content\/$)/i.exec(window);
228
+ if (!match) continue;
219
229
  try {
220
230
  const url = new URL(match[0]);
221
231
  found.set(url.href.toLowerCase(), url);
222
232
  } catch {
223
233
  // Not an address: ignore it.
224
234
  }
225
- if (found.size >= 5) break;
226
235
  }
227
236
  this.oldSites = [...found.values()];
228
237
  }
@@ -911,32 +911,43 @@ export function importElementor(input: unknown, options: ImportOptions = {}): El
911
911
  });
912
912
  if (pageStyle.devices) root["devices"] = pageStyle.devices;
913
913
  if (options.media) pointAtCopies(root, ctx);
914
+ const checked = validImport(root, ctx);
915
+ if (!checked.ok) return checked;
916
+ const partType = source.type ? PART_TYPES[source.type] : undefined;
917
+ return {
918
+ ok: true,
919
+ layout: checked.layout,
920
+ ...(source.title ? { title: source.title } : {}),
921
+ ...(partType ? { partType } : {}),
922
+ elements: ctx.elements,
923
+ mapped: ctx.mapped,
924
+ notes: ctx.report(),
925
+ fonts: [...ctx.fonts].toSorted(),
926
+ svgFiles: [...ctx.svgFiles],
927
+ pageLinks: [...ctx.pageLinks].toSorted((a, b) => a - b),
928
+ menus: [...ctx.menus].toSorted(),
929
+ images: [...ctx.images],
930
+ classes: usedClasses(checked.layout, ctx).map((cls) =>
931
+ options.media ? (pointAtCopies(structuredClone(cls), ctx) as StyleClass) : cls,
932
+ ),
933
+ };
934
+ }
935
+
936
+ /**
937
+ * The imported tree as a layout EmVB accepts: whatever validation or the nesting rules refuse is
938
+ * pruned (and noted) until it passes. Shared with the HTML import (W-1074).
939
+ */
940
+ export function validImport(
941
+ root: N,
942
+ ctx: ImportContext,
943
+ ): { ok: true; layout: Layout } | { ok: false; error: string } {
914
944
  const candidate: Record<string, unknown> = { schemaVersion: LAYOUT_SCHEMA_VERSION, root };
915
945
  const tried = new Set<string>();
916
946
  for (let attempt = 0; attempt < 500; attempt++) {
917
947
  const checked = validateLayout(candidate);
918
948
  // The save hook also refuses misplaced elements (a field outside a form, …): prune those too.
919
949
  const misplaced = checked.ok ? nestingIssues(checked.layout, 1)[0] : undefined;
920
- if (checked.ok && !misplaced) {
921
- const partType = source.type ? PART_TYPES[source.type] : undefined;
922
- return {
923
- ok: true,
924
- layout: checked.layout,
925
- ...(source.title ? { title: source.title } : {}),
926
- ...(partType ? { partType } : {}),
927
- elements: ctx.elements,
928
- mapped: ctx.mapped,
929
- notes: ctx.report(),
930
- fonts: [...ctx.fonts].toSorted(),
931
- svgFiles: [...ctx.svgFiles],
932
- pageLinks: [...ctx.pageLinks].toSorted((a, b) => a - b),
933
- menus: [...ctx.menus].toSorted(),
934
- images: [...ctx.images],
935
- classes: usedClasses(checked.layout, ctx).map((cls) =>
936
- options.media ? (pointAtCopies(structuredClone(cls), ctx) as StyleClass) : cls,
937
- ),
938
- };
939
- }
950
+ if (checked.ok && !misplaced) return { ok: true, layout: checked.layout };
940
951
  const issue = checked.ok ? misplaced : checked.issues[0];
941
952
  if (!issue || !prune(root, issue, ctx, tried)) {
942
953
  return {
@@ -952,7 +963,7 @@ export function importElementor(input: unknown, options: ImportOptions = {}): El
952
963
  * W-958: points every use of an old-site image at its media library copy: image sources (which
953
964
  * also get the library id), backgrounds and any other stored address equal to it.
954
965
  */
955
- function pointAtCopies(value: unknown, ctx: ImportContext): unknown {
966
+ export function pointAtCopies(value: unknown, ctx: ImportContext): unknown {
956
967
  if (typeof value === "string")
957
968
  return /^https?:\/\//i.test(value) ? (ctx.copied(value)?.url ?? value) : value;
958
969
  if (Array.isArray(value)) {