@@ -75,7 +75,7 @@ define(function (require, exports, module) {
7575 * we only care about text changes or things like newlines, <br>, or formatting like <b>, <i>, etc.
7676 *
7777 * Here's the basic idea:
78- * - Parse both old and new HTML strings into DOM trees
78+ * - Parse both old and new HTML strings into document fragments using <template> elements
7979 * - Then walk both DOMs side by side and sync changes
8080 *
8181 * What we handle:
@@ -90,12 +90,14 @@ define(function (require, exports, module) {
9090 * This avoids the browser trying to “fix” broken HTML (which we don’t want)
9191 */
9292 function _syncTextContentChanges ( oldContent , newContent ) {
93- const parser = new DOMParser ( ) ;
94- const oldDoc = parser . parseFromString ( oldContent , "text/html" ) ;
95- const newDoc = parser . parseFromString ( newContent , "text/html" ) ;
93+ function parseFragment ( html ) {
94+ const t = document . createElement ( "template" ) ;
95+ t . innerHTML = html ;
96+ return t . content ;
97+ }
9698
97- const oldRoot = oldDoc . body ;
98- const newRoot = newDoc . body ;
99+ const oldRoot = parseFragment ( oldContent ) ;
100+ const newRoot = parseFragment ( newContent ) ;
99101
100102 // this function is to remove the phoenix internal attributes from leaking into the user's source code
101103 function cleanClonedElement ( clonedElement ) {
@@ -167,14 +169,16 @@ define(function (require, exports, module) {
167169 }
168170 }
169171
170- const oldEls = Array . from ( oldRoot . children ) ;
171- const newEls = Array . from ( newRoot . children ) ;
172+ const oldEls = Array . from ( oldRoot . childNodes ) ;
173+ const newEls = Array . from ( newRoot . childNodes ) ;
172174
173175 for ( let i = 0 ; i < Math . min ( oldEls . length , newEls . length ) ; i ++ ) {
174176 syncText ( oldEls [ i ] , newEls [ i ] ) ;
175177 }
176178
177- return oldRoot . innerHTML ;
179+ return Array . from ( oldRoot . childNodes ) . map ( node =>
180+ node . outerHTML || node . textContent
181+ ) . join ( "" ) ;
178182 }
179183
180184 /**
0 commit comments