You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Have you considered directly building a DomFragment instead of returning strings from your parser ?
This would allow you to avoid unnecessary serialization/parsing/sanitization steps.
Something like this:
// in dom.tsfunctionparseFromString(html: string): Node{consttemplate=document.createElement('template');template.innerHTML=html;returntemplate.content;}// parser.tsfunctiontranslateHTMLString(html: string): Node{if(html==='')returnnewDocumentFragment();constfragment=parseFromString(html);if(Parser.hasChildTextNode(fragment)){constwrapper=document.createElement('span');wrapper.append(...fragment.childNodes);fragment.appendChild(wrapper);}this.applyElement(fragment.firstChildasHTMLElement);returnfragment;}// in budoux-base.tssync(){consttranslated=this.parser.translateHTMLString(this.innerHTML);this.shadow.textContent='';this.shadow.appendChild(translated);}
You can even avoid having to parse anything at all if you clone the existing nodes instead of grabbing this.innerHTML.
// in budoux-base.tssync(){lettranslated: HTMLElement;if(Parser.hasChildTextNode(this)){translated=document.createElement('span');translated.append(...this.childNodes.map(node=>node.cloneNode(true)));}else{translated=this.firstElementChild!.cloneNode(true)asHTMLElement;}this.parser.applyElement(translated);this.shadow.textContent='';this.shadow.appendChild(translated);}
This is also likely to be more performant than parsing and serializing the tree multiple times.
Have you considered directly building a
DomFragment
instead of returning strings from your parser ?This would allow you to avoid unnecessary serialization/parsing/sanitization steps.
Something like this:
You can even avoid having to parse anything at all if you clone the existing nodes instead of grabbing
this.innerHTML
.This is also likely to be more performant than parsing and serializing the tree multiple times.
Originally posted by @engelsdamien in google/safevalues#256 (comment)
The text was updated successfully, but these errors were encountered: