Skip to content

Commit 50096f3

Browse files
Sebmasterdomenic
authored andcommitted
Use fragment parsing if setting Element.innerHTML
With the new capabilities of parse5 we have to differentiate if we're parsing a document fragment (i.e. when setting Element.innerHTML) or a complete document (i.e. the initial parsing stage).
1 parent f738e7f commit 50096f3

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/jsdom/browser/htmltodom.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,26 @@ function HtmlToDom(parser) {
9595

9696
return element;
9797
};
98+
this.appendHtmlToDocument = this.appendHtmlToElement;
99+
100+
if (parser.Parser && parser.TreeAdapters) {
101+
this.appendHtmlToElement = function (html, element) {
102+
103+
if (typeof html !== 'string') {
104+
html += '';
105+
}
106+
107+
var instance = new parser.Parser(parser.TreeAdapters.htmlparser2);
108+
var dom = instance.parseFragment(html, element);
109+
var parsed = dom.children;
110+
111+
for (var i = 0; i < parsed.length; i++) {
112+
setChild(element, parsed[i]);
113+
}
114+
115+
return element;
116+
};
117+
}
98118

99119
} else if (parser && parser.moduleName == 'HTML5') { /* HTML5 parser */
100120
this.appendHtmlToElement = function(html, element) {

lib/jsdom/browser/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ var browserAugmentation = exports.browserAugmentation = function(dom, options) {
499499
}
500500
}
501501
if (html !== "" && html != null) {
502-
htmltodom.appendHtmlToElement(html, this);
502+
htmltodom.appendHtmlToDocument(html, this);
503503
}
504504
return html;
505505
});

0 commit comments

Comments
 (0)