@lexical/html 0.8.1 → 0.9.1

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