@lexical/html 0.44.1-nightly.20260519.0 → 0.45.1-dev.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 (66) hide show
  1. package/{DOMRenderExtension.d.ts → dist/DOMRenderExtension.d.ts} +12 -1
  2. package/dist/DOMRenderRuntime.d.ts +51 -0
  3. package/dist/LexicalHtml.dev.js +3289 -0
  4. package/dist/LexicalHtml.dev.mjs +3242 -0
  5. package/{LexicalHtml.js.flow → dist/LexicalHtml.js.flow} +16 -16
  6. package/dist/LexicalHtml.mjs +57 -0
  7. package/dist/LexicalHtml.node.mjs +55 -0
  8. package/dist/LexicalHtml.prod.js +9 -0
  9. package/dist/LexicalHtml.prod.mjs +9 -0
  10. package/dist/RenderContext.d.ts +68 -0
  11. package/{compileDOMRenderConfigOverrides.d.ts → dist/compileDOMRenderConfigOverrides.d.ts} +1 -1
  12. package/{constants.d.ts → dist/constants.d.ts} +2 -0
  13. package/dist/domOverride.d.ts +23 -0
  14. package/dist/import/CoreImportExtension.d.ts +11 -0
  15. package/dist/import/DOMImportExtension.d.ts +82 -0
  16. package/dist/import/HorizontalRuleImportExtension.d.ts +28 -0
  17. package/dist/import/ImportContext.d.ts +208 -0
  18. package/dist/import/compileImportRules.d.ts +50 -0
  19. package/dist/import/coreImportRules.d.ts +25 -0
  20. package/dist/import/defineImportRule.d.ts +32 -0
  21. package/dist/import/defineOverlayRules.d.ts +66 -0
  22. package/dist/import/index.d.ts +38 -0
  23. package/dist/import/inlineStylesFromStyleSheets.d.ts +28 -0
  24. package/dist/import/parseCss.d.ts +18 -0
  25. package/dist/import/runImport.d.ts +19 -0
  26. package/dist/import/schemas.d.ts +106 -0
  27. package/dist/import/sel.d.ts +74 -0
  28. package/dist/import/types.d.ts +394 -0
  29. package/dist/index.d.ts +44 -0
  30. package/{types.d.ts → dist/types.d.ts} +96 -8
  31. package/package.json +33 -18
  32. package/src/ContextRecord.ts +243 -0
  33. package/src/DOMRenderExtension.ts +96 -0
  34. package/src/DOMRenderRuntime.ts +265 -0
  35. package/src/RenderContext.ts +168 -0
  36. package/src/compileDOMRenderConfigOverrides.ts +416 -0
  37. package/src/constants.ts +18 -0
  38. package/src/domOverride.ts +46 -0
  39. package/src/import/CoreImportExtension.ts +26 -0
  40. package/src/import/DOMImportExtension.ts +221 -0
  41. package/src/import/HorizontalRuleImportExtension.ts +52 -0
  42. package/src/import/ImportContext.ts +339 -0
  43. package/src/import/compileImportRules.ts +178 -0
  44. package/src/import/coreImportRules.ts +545 -0
  45. package/src/import/defineImportRule.ts +40 -0
  46. package/src/import/defineOverlayRules.ts +105 -0
  47. package/src/import/index.ts +97 -0
  48. package/src/import/inlineStylesFromStyleSheets.ts +104 -0
  49. package/src/import/parseCss.ts +219 -0
  50. package/src/import/runImport.ts +245 -0
  51. package/src/import/schemas.ts +280 -0
  52. package/src/import/sel.ts +314 -0
  53. package/src/import/types.ts +471 -0
  54. package/src/index.ts +561 -0
  55. package/src/types.ts +470 -0
  56. package/LexicalHtml.dev.js +0 -914
  57. package/LexicalHtml.dev.mjs +0 -900
  58. package/LexicalHtml.mjs +0 -24
  59. package/LexicalHtml.node.mjs +0 -22
  60. package/LexicalHtml.prod.js +0 -9
  61. package/LexicalHtml.prod.mjs +0 -9
  62. package/RenderContext.d.ts +0 -32
  63. package/domOverride.d.ts +0 -18
  64. package/index.d.ts +0 -32
  65. /package/{ContextRecord.d.ts → dist/ContextRecord.d.ts} +0 -0
  66. /package/{LexicalHtml.js → dist/LexicalHtml.js} +0 -0
@@ -0,0 +1,178 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ import type {AnyDOMImportRule, DOMImportFn} from './types';
9
+
10
+ import {isDOMTextNode, isHTMLElement} from 'lexical';
11
+
12
+ import {getSelectorImpl, type Predicate, type SelectorImpl} from './sel';
13
+
14
+ const __DEV__ = process.env.NODE_ENV !== 'production';
15
+
16
+ /** @internal */
17
+ export interface CompiledRule {
18
+ readonly name: string;
19
+ readonly predicate: Predicate;
20
+ readonly $import: DOMImportFn<Node, Record<string, RegExpMatchArray>>;
21
+ }
22
+
23
+ /** @internal */
24
+ export interface CompiledDispatch {
25
+ /** All rules in registration order. Index = registration order. */
26
+ readonly rules: readonly CompiledRule[];
27
+ /**
28
+ * For each (uppercased) HTML tag name, the ordered list of rule indices
29
+ * considered when dispatching that tag. Includes interleaved wildcard
30
+ * element rules so a single iteration handles both.
31
+ */
32
+ readonly byTag: ReadonlyMap<string, readonly number[]>;
33
+ /** Indices of rules whose match has no tag restriction. */
34
+ readonly wildcardIndices: readonly number[];
35
+ /** Indices of rules whose match is `sel.text()`. */
36
+ readonly textIndices: readonly number[];
37
+ /** Indices of rules whose match is `sel.comment()`. */
38
+ readonly commentIndices: readonly number[];
39
+ }
40
+
41
+ function mergeSortedAsc(a: readonly number[], b: readonly number[]): number[] {
42
+ const out: number[] = [];
43
+ let i = 0;
44
+ let j = 0;
45
+ while (i < a.length && j < b.length) {
46
+ if (a[i] <= b[j]) {
47
+ out.push(a[i++]);
48
+ } else {
49
+ out.push(b[j++]);
50
+ }
51
+ }
52
+ while (i < a.length) {
53
+ out.push(a[i++]);
54
+ }
55
+ while (j < b.length) {
56
+ out.push(b[j++]);
57
+ }
58
+ return out;
59
+ }
60
+
61
+ /**
62
+ * Compile an ordered list of {@link DOMImportRule}s into the dispatch tables
63
+ * used by the import runtime. The rule at index 0 is the highest-priority
64
+ * (`mergeConfig` prepends partial.rules so later-merged extensions land
65
+ * first).
66
+ *
67
+ * @internal
68
+ */
69
+ export function compileImportRules(
70
+ rules: readonly AnyDOMImportRule[],
71
+ ): CompiledDispatch {
72
+ const compiled: CompiledRule[] = [];
73
+ const byTag = new Map<string, number[]>();
74
+ const wildcardIndices: number[] = [];
75
+ const textIndices: number[] = [];
76
+ const commentIndices: number[] = [];
77
+ const seenNames = new Set<string>();
78
+
79
+ rules.forEach((rule, i) => {
80
+ const sel = getSelectorImpl(rule.match);
81
+ const name = rule.name || defaultRuleName(sel, i);
82
+ if (__DEV__ && typeof rule.name === 'string' && seenNames.has(rule.name)) {
83
+ console.warn(
84
+ `[lexical] duplicate DOMImportRule name "${rule.name}" — keep names unique to aid debugging.`,
85
+ );
86
+ }
87
+ if (rule.name) {
88
+ seenNames.add(rule.name);
89
+ }
90
+ compiled.push({
91
+ $import: rule.$import as DOMImportFn<
92
+ Node,
93
+ Record<string, RegExpMatchArray>
94
+ >,
95
+ name,
96
+ predicate: sel.predicate,
97
+ });
98
+
99
+ if (sel.kind === 'text') {
100
+ textIndices.push(i);
101
+ } else if (sel.kind === 'comment') {
102
+ commentIndices.push(i);
103
+ } else if (sel.tags.size === 0) {
104
+ wildcardIndices.push(i);
105
+ } else {
106
+ for (const tag of sel.tags) {
107
+ let list = byTag.get(tag);
108
+ if (!list) {
109
+ list = [];
110
+ byTag.set(tag, list);
111
+ }
112
+ list.push(i);
113
+ }
114
+ }
115
+ });
116
+
117
+ // Interleave wildcard-element indices into each tag's list in registration
118
+ // (ascending-index) order, so iterating a tag bucket visits both tag-
119
+ // specific and wildcard rules in the same priority sequence.
120
+ const finalByTag = new Map<string, readonly number[]>();
121
+ if (wildcardIndices.length === 0) {
122
+ for (const [tag, list] of byTag) {
123
+ finalByTag.set(tag, list);
124
+ }
125
+ } else {
126
+ for (const [tag, list] of byTag) {
127
+ finalByTag.set(tag, mergeSortedAsc(list, wildcardIndices));
128
+ }
129
+ }
130
+
131
+ return {
132
+ byTag: finalByTag,
133
+ commentIndices,
134
+ rules: compiled,
135
+ textIndices,
136
+ wildcardIndices,
137
+ };
138
+ }
139
+
140
+ function defaultRuleName(sel: SelectorImpl, index: number): string {
141
+ if (sel.kind === 'text') {
142
+ return `#text@${index}`;
143
+ }
144
+ if (sel.kind === 'comment') {
145
+ return `#comment@${index}`;
146
+ }
147
+ if (sel.tags.size === 0) {
148
+ return `*@${index}`;
149
+ }
150
+ const tagList = Array.from(sel.tags).join(',').toLowerCase();
151
+ return `${tagList}@${index}`;
152
+ }
153
+
154
+ /**
155
+ * Look up the (already interleaved) rule indices relevant to `node`. Element
156
+ * nodes hit `byTag` (with wildcards merged in) or fall back to the wildcard
157
+ * bucket if no tag-specific rules exist; text and comment nodes use their
158
+ * own buckets.
159
+ *
160
+ * @internal
161
+ */
162
+ export function getDispatchIndices(
163
+ dispatch: CompiledDispatch,
164
+ node: Node,
165
+ ): readonly number[] {
166
+ if (isDOMTextNode(node)) {
167
+ return dispatch.textIndices;
168
+ }
169
+ if (node.nodeType === 8 /* COMMENT_NODE */) {
170
+ return dispatch.commentIndices;
171
+ }
172
+ if (isHTMLElement(node)) {
173
+ return dispatch.byTag.get(node.nodeName) || dispatch.wildcardIndices;
174
+ }
175
+ return EMPTY_INDICES;
176
+ }
177
+
178
+ const EMPTY_INDICES: readonly number[] = Object.freeze([]);