UNPKG

@lexical/html

Version:

This package contains HTML helpers and functionality for Lexical.

10 lines (8 loc) 3.35 kB
/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * */ import{$sliceSelectedTextNodeContent as e}from"@lexical/selection";import{isHTMLElement as n,isBlockDomNode as t}from"@lexical/utils";import{$getRoot as o,$isElementNode as l,$cloneWithProperties as r,$isTextNode as i,getRegisteredNode as s,isDocumentFragment as c,$isRootOrShadowRoot as u,$isBlockElementNode as f,$createLineBreakNode as a,ArtificialNode__DO_NOT_USE as d,isInlineDomNode as p,$createParagraphNode as h}from"lexical";function m(e,n){const t=n.body?n.body.childNodes:[];let o=[];const l=[];for(let n=0;n<t.length;n++){const r=t[n];if(!y.has(r.nodeName)){const n=w(r,e,l,!1);null!==n&&(o=o.concat(n))}}return function(e){for(const n of e)n.getNextSibling()instanceof d&&n.insertAfter(a());for(const n of e){const e=n.getChildren();for(const t of e)n.insertBefore(t);n.remove()}}(l),o}function g(e,n){if("undefined"==typeof document||"undefined"==typeof window&&void 0===global.window)throw new Error("To use $generateHtmlFromNodes in headless mode please initialize a headless browser implementation such as JSDom before calling this function.");const t=document.createElement("div"),l=o().getChildren();for(let o=0;o<l.length;o++){x(e,l[o],t,n)}return t.innerHTML}function x(t,o,u,f=null){let a=null===f||o.isSelected(f);const d=l(o)&&o.excludeFromCopy("html");let p=o;if(null!==f){let n=r(o);n=i(n)&&null!==f?e(f,n):n,p=n}const h=l(p)?p.getChildren():[],m=s(t,p.getType());let g;g=m&&void 0!==m.exportDOM?m.exportDOM(t,p):p.exportDOM(t);const{element:y,after:w}=g;if(!y)return!1;const b=document.createDocumentFragment();for(let e=0;e<h.length;e++){const n=h[e],r=x(t,n,b,f);!a&&l(o)&&r&&o.extractWithChild(n,f,"html")&&(a=!0)}if(a&&!d){if((n(y)||c(y))&&y.append(b),u.append(y),w){const e=w.call(p,y);e&&(c(y)?y.replaceChildren(e):y.replaceWith(e))}}else u.append(b);return a}const y=new Set(["STYLE","SCRIPT"]);function w(e,n,o,r,i=new Map,s){let c=[];if(y.has(e.nodeName))return c;let m=null;const g=function(e,n){const{nodeName:t}=e,o=n._htmlConversions.get(t.toLowerCase());let l=null;if(void 0!==o)for(const n of o){const t=n(e);null!==t&&(null===l||(l.priority||0)<=(t.priority||0))&&(l=t)}return null!==l?l.conversion:null}(e,n),x=g?g(e):null;let C=null;if(null!==x){C=x.after;const n=x.node;if(m=Array.isArray(n)?n[n.length-1]:n,null!==m){for(const[,e]of i)if(m=e(m,s),!m)break;m&&c.push(...Array.isArray(n)?n:[m])}null!=x.forChild&&i.set(e.nodeName,x.forChild)}const S=e.childNodes;let v=[];const N=(null==m||!u(m))&&(null!=m&&f(m)||r);for(let e=0;e<S.length;e++)v.push(...w(S[e],n,o,N,new Map(i),m));return null!=C&&(v=C(v)),t(e)&&(v=b(e,v,N?()=>{const e=new d;return o.push(e),e}:h)),null==m?v.length>0?c=c.concat(v):t(e)&&function(e){if(null==e.nextSibling||null==e.previousSibling)return!1;return p(e.nextSibling)&&p(e.previousSibling)}(e)&&(c=c.concat(a())):l(m)&&m.append(...v),c}function b(e,n,t){const o=e.style.textAlign,l=[];let r=[];for(let e=0;e<n.length;e++){const i=n[e];if(f(i))o&&!i.getFormat()&&i.setFormat(o),l.push(i);else if(r.push(i),e===n.length-1||e<n.length-1&&f(n[e+1])){const e=t();e.setFormat(o),e.append(...r),l.push(e),r=[]}}return l}export{g as $generateHtmlFromNodes,m as $generateNodesFromDOM};