@lexical/html 0.12.2 → 0.12.4

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.
@@ -11,86 +11,78 @@ var utils = require('@lexical/utils');
11
11
  var lexical = require('lexical');
12
12
 
13
13
  /** @module @lexical/html */
14
+
14
15
  /**
15
16
  * How you parse your html string to get a document is left up to you. In the browser you can use the native
16
17
  * DOMParser API to generate a document (see clipboard.ts), but to use in a headless environment you can use JSDom
17
18
  * or an equivilant library and pass in the document here.
18
19
  */
19
-
20
20
  function $generateNodesFromDOM(editor, dom) {
21
21
  const elements = dom.body ? dom.body.childNodes : [];
22
22
  let lexicalNodes = [];
23
-
24
23
  for (let i = 0; i < elements.length; i++) {
25
24
  const element = elements[i];
26
-
27
25
  if (!IGNORE_TAGS.has(element.nodeName)) {
28
26
  const lexicalNode = $createNodesFromDOM(element, editor);
29
-
30
27
  if (lexicalNode !== null) {
31
28
  lexicalNodes = lexicalNodes.concat(lexicalNode);
32
29
  }
33
30
  }
34
31
  }
35
-
36
32
  return lexicalNodes;
37
33
  }
38
34
  function $generateHtmlFromNodes(editor, selection) {
39
35
  if (typeof document === 'undefined' || typeof window === 'undefined') {
40
36
  throw new Error('To use $generateHtmlFromNodes in headless mode please initialize a headless browser implementation such as JSDom before calling this function.');
41
37
  }
42
-
43
38
  const container = document.createElement('div');
44
39
  const root = lexical.$getRoot();
45
40
  const topLevelChildren = root.getChildren();
46
-
47
41
  for (let i = 0; i < topLevelChildren.length; i++) {
48
42
  const topLevelNode = topLevelChildren[i];
49
43
  $appendNodesToHTML(editor, topLevelNode, container, selection);
50
44
  }
51
-
52
45
  return container.innerHTML;
53
46
  }
54
-
55
47
  function $appendNodesToHTML(editor, currentNode, parentElement, selection$1 = null) {
56
48
  let shouldInclude = selection$1 != null ? currentNode.isSelected(selection$1) : true;
57
49
  const shouldExclude = lexical.$isElementNode(currentNode) && currentNode.excludeFromCopy('html');
58
50
  let target = currentNode;
59
-
60
51
  if (selection$1 !== null) {
61
52
  let clone = selection.$cloneWithProperties(currentNode);
62
53
  clone = lexical.$isTextNode(clone) && selection$1 != null ? selection.$sliceSelectedTextNodeContent(selection$1, clone) : clone;
63
54
  target = clone;
64
55
  }
65
-
66
56
  const children = lexical.$isElementNode(target) ? target.getChildren() : [];
57
+ const registeredNode = editor._nodes.get(target.getType());
58
+ let exportOutput;
59
+
60
+ // Use HTMLConfig overrides, if available.
61
+ if (registeredNode && registeredNode.exportDOM !== undefined) {
62
+ exportOutput = registeredNode.exportDOM(editor, target);
63
+ } else {
64
+ exportOutput = target.exportDOM(editor);
65
+ }
67
66
  const {
68
67
  element,
69
68
  after
70
- } = target.exportDOM(editor);
71
-
69
+ } = exportOutput;
72
70
  if (!element) {
73
71
  return false;
74
72
  }
75
-
76
73
  const fragment = document.createDocumentFragment();
77
-
78
74
  for (let i = 0; i < children.length; i++) {
79
75
  const childNode = children[i];
80
76
  const shouldIncludeChild = $appendNodesToHTML(editor, childNode, fragment, selection$1);
81
-
82
77
  if (!shouldInclude && lexical.$isElementNode(currentNode) && shouldIncludeChild && currentNode.extractWithChild(childNode, selection$1, 'html')) {
83
78
  shouldInclude = true;
84
79
  }
85
80
  }
86
-
87
81
  if (shouldInclude && !shouldExclude) {
88
82
  if (utils.isHTMLElement(element)) {
89
83
  element.append(fragment);
90
84
  }
91
-
92
85
  parentElement.append(element);
93
-
94
86
  if (after) {
95
87
  const newElement = after.call(target, element);
96
88
  if (newElement) element.replaceWith(newElement);
@@ -98,83 +90,64 @@ function $appendNodesToHTML(editor, currentNode, parentElement, selection$1 = nu
98
90
  } else {
99
91
  parentElement.append(fragment);
100
92
  }
101
-
102
93
  return shouldInclude;
103
94
  }
104
-
105
95
  function getConversionFunction(domNode, editor) {
106
96
  const {
107
97
  nodeName
108
98
  } = domNode;
109
-
110
99
  const cachedConversions = editor._htmlConversions.get(nodeName.toLowerCase());
111
-
112
100
  let currentConversion = null;
113
-
114
101
  if (cachedConversions !== undefined) {
115
102
  for (const cachedConversion of cachedConversions) {
116
103
  const domConversion = cachedConversion(domNode);
117
-
118
104
  if (domConversion !== null && (currentConversion === null || currentConversion.priority < domConversion.priority)) {
119
105
  currentConversion = domConversion;
120
106
  }
121
107
  }
122
108
  }
123
-
124
109
  return currentConversion !== null ? currentConversion.conversion : null;
125
110
  }
126
-
127
111
  const IGNORE_TAGS = new Set(['STYLE', 'SCRIPT']);
128
-
129
112
  function $createNodesFromDOM(node, editor, forChildMap = new Map(), parentLexicalNode) {
130
113
  let lexicalNodes = [];
131
-
132
114
  if (IGNORE_TAGS.has(node.nodeName)) {
133
115
  return lexicalNodes;
134
116
  }
135
-
136
117
  let currentLexicalNode = null;
137
118
  const transformFunction = getConversionFunction(node, editor);
138
119
  const transformOutput = transformFunction ? transformFunction(node) : null;
139
120
  let postTransform = null;
140
-
141
121
  if (transformOutput !== null) {
142
122
  postTransform = transformOutput.after;
143
123
  const transformNodes = transformOutput.node;
144
124
  currentLexicalNode = Array.isArray(transformNodes) ? transformNodes[transformNodes.length - 1] : transformNodes;
145
-
146
125
  if (currentLexicalNode !== null) {
147
126
  for (const [, forChildFunction] of forChildMap) {
148
127
  currentLexicalNode = forChildFunction(currentLexicalNode, parentLexicalNode);
149
-
150
128
  if (!currentLexicalNode) {
151
129
  break;
152
130
  }
153
131
  }
154
-
155
132
  if (currentLexicalNode) {
156
133
  lexicalNodes.push(...(Array.isArray(transformNodes) ? transformNodes : [currentLexicalNode]));
157
134
  }
158
135
  }
159
-
160
136
  if (transformOutput.forChild != null) {
161
137
  forChildMap.set(node.nodeName, transformOutput.forChild);
162
138
  }
163
- } // If the DOM node doesn't have a transformer, we don't know what
164
- // to do with it but we still need to process any childNodes.
165
-
139
+ }
166
140
 
141
+ // If the DOM node doesn't have a transformer, we don't know what
142
+ // to do with it but we still need to process any childNodes.
167
143
  const children = node.childNodes;
168
144
  let childLexicalNodes = [];
169
-
170
145
  for (let i = 0; i < children.length; i++) {
171
146
  childLexicalNodes.push(...$createNodesFromDOM(children[i], editor, new Map(forChildMap), currentLexicalNode));
172
147
  }
173
-
174
148
  if (postTransform != null) {
175
149
  childLexicalNodes = postTransform(childLexicalNodes);
176
150
  }
177
-
178
151
  if (currentLexicalNode == null) {
179
152
  // If it hasn't been converted to a LexicalNode, we hoist its children
180
153
  // up to the same level as it.
@@ -186,7 +159,6 @@ function $createNodesFromDOM(node, editor, forChildMap = new Map(), parentLexica
186
159
  currentLexicalNode.append(...childLexicalNodes);
187
160
  }
188
161
  }
189
-
190
162
  return lexicalNodes;
191
163
  }
192
164
 
@@ -4,10 +4,10 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- 'use strict';var m=require("@lexical/selection"),q=require("@lexical/utils"),r=require("lexical");
8
- function u(c,d,h,a=null){let e=null!=a?d.isSelected(a):!0,k=r.$isElementNode(d)&&d.excludeFromCopy("html");var f=d;null!==a&&(f=m.$cloneWithProperties(d),f=r.$isTextNode(f)&&null!=a?m.$sliceSelectedTextNodeContent(a,f):f);let g=r.$isElementNode(f)?f.getChildren():[],{element:b,after:l}=f.exportDOM(c);if(!b)return!1;let n=document.createDocumentFragment();for(let p=0;p<g.length;p++){let t=g[p],x=u(c,t,n,a);!e&&r.$isElementNode(d)&&x&&d.extractWithChild(t,a,"html")&&(e=!0)}e&&!k?(q.isHTMLElement(b)&&
9
- b.append(n),h.append(b),l&&(c=l.call(f,b))&&b.replaceWith(c)):h.append(n);return e}let v=new Set(["STYLE","SCRIPT"]);
10
- function w(c,d,h=new Map,a){let e=[];if(v.has(c.nodeName))return e;let k=null;var f,{nodeName:g}=c,b=d._htmlConversions.get(g.toLowerCase());g=null;if(void 0!==b)for(f of b)b=f(c),null!==b&&(null===g||g.priority<b.priority)&&(g=b);g=(f=null!==g?g.conversion:null)?f(c):null;f=null;if(null!==g){f=g.after;b=g.node;k=Array.isArray(b)?b[b.length-1]:b;if(null!==k){for(var [,l]of h)if(k=l(k,a),!k)break;k&&e.push(...(Array.isArray(b)?b:[k]))}null!=g.forChild&&h.set(c.nodeName,g.forChild)}c=c.childNodes;a=
11
- [];for(l=0;l<c.length;l++)a.push(...w(c[l],d,new Map(h),k));null!=f&&(a=f(a));null==k?e=e.concat(a):r.$isElementNode(k)&&k.append(...a);return e}
12
- exports.$generateHtmlFromNodes=function(c,d){if("undefined"===typeof document||"undefined"===typeof window)throw Error("To use $generateHtmlFromNodes in headless mode please initialize a headless browser implementation such as JSDom before calling this function.");let h=document.createElement("div"),a=r.$getRoot().getChildren();for(let e=0;e<a.length;e++)u(c,a[e],h,d);return h.innerHTML};
13
- exports.$generateNodesFromDOM=function(c,d){d=d.body?d.body.childNodes:[];let h=[];for(let e=0;e<d.length;e++){var a=d[e];v.has(a.nodeName)||(a=w(a,c),null!==a&&(h=h.concat(a)))}return h}
7
+ 'use strict';var m=require("@lexical/selection"),p=require("@lexical/utils"),q=require("lexical");
8
+ function u(c,e,h,a=null){let f=null!=a?e.isSelected(a):!0,k=q.$isElementNode(e)&&e.excludeFromCopy("html");var d=e;null!==a&&(d=m.$cloneWithProperties(e),d=q.$isTextNode(d)&&null!=a?m.$sliceSelectedTextNodeContent(a,d):d);let g=q.$isElementNode(d)?d.getChildren():[];var b=c._nodes.get(d.getType());b=b&&void 0!==b.exportDOM?b.exportDOM(c,d):d.exportDOM(c);let {element:l,after:r}=b;if(!l)return!1;b=document.createDocumentFragment();for(let n=0;n<g.length;n++){let t=g[n],x=u(c,t,b,a);!f&&q.$isElementNode(e)&&
9
+ x&&e.extractWithChild(t,a,"html")&&(f=!0)}f&&!k?(p.isHTMLElement(l)&&l.append(b),h.append(l),r&&(c=r.call(d,l))&&l.replaceWith(c)):h.append(b);return f}let v=new Set(["STYLE","SCRIPT"]);
10
+ function w(c,e,h=new Map,a){let f=[];if(v.has(c.nodeName))return f;let k=null;var d,{nodeName:g}=c,b=e._htmlConversions.get(g.toLowerCase());g=null;if(void 0!==b)for(d of b)b=d(c),null!==b&&(null===g||g.priority<b.priority)&&(g=b);g=(d=null!==g?g.conversion:null)?d(c):null;d=null;if(null!==g){d=g.after;b=g.node;k=Array.isArray(b)?b[b.length-1]:b;if(null!==k){for(var [,l]of h)if(k=l(k,a),!k)break;k&&f.push(...(Array.isArray(b)?b:[k]))}null!=g.forChild&&h.set(c.nodeName,g.forChild)}c=c.childNodes;a=
11
+ [];for(l=0;l<c.length;l++)a.push(...w(c[l],e,new Map(h),k));null!=d&&(a=d(a));null==k?f=f.concat(a):q.$isElementNode(k)&&k.append(...a);return f}
12
+ exports.$generateHtmlFromNodes=function(c,e){if("undefined"===typeof document||"undefined"===typeof window)throw Error("To use $generateHtmlFromNodes in headless mode please initialize a headless browser implementation such as JSDom before calling this function.");let h=document.createElement("div"),a=q.$getRoot().getChildren();for(let f=0;f<a.length;f++)u(c,a[f],h,e);return h.innerHTML};
13
+ exports.$generateNodesFromDOM=function(c,e){e=e.body?e.body.childNodes:[];let h=[];for(let f=0;f<e.length;f++){var a=e[f];v.has(a.nodeName)||(a=w(a,c),null!==a&&(h=h.concat(a)))}return h}
package/package.json CHANGED
@@ -8,10 +8,10 @@
8
8
  "html"
9
9
  ],
10
10
  "license": "MIT",
11
- "version": "0.12.2",
11
+ "version": "0.12.4",
12
12
  "main": "LexicalHtml.js",
13
13
  "peerDependencies": {
14
- "lexical": "0.12.2"
14
+ "lexical": "0.12.4"
15
15
  },
16
16
  "repository": {
17
17
  "type": "git",
@@ -19,6 +19,7 @@
19
19
  "directory": "packages/lexical-html"
20
20
  },
21
21
  "dependencies": {
22
- "@lexical/selection": "0.12.2"
22
+ "@lexical/selection": "0.12.4",
23
+ "@lexical/utils": "0.12.4"
23
24
  }
24
25
  }