Skip to content

Commit 828a718

Browse files
committed
Core: introduce createHTMLDocument in parseHTML; Safari 8/IE8 left out
Close gh-1506
1 parent 51564bb commit 828a718

File tree

4 files changed

+83
-4
lines changed

4 files changed

+83
-4
lines changed

src/core/parseHTML.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
define([
22
"../core",
33
"./var/rsingleTag",
4-
"../manipulation" // buildFragment
5-
], function( jQuery, rsingleTag ) {
4+
5+
// This is the only module that needs core/support
6+
"./support",
7+
8+
// buildFragment
9+
"../manipulation"
10+
], function( jQuery, rsingleTag, support ) {
611

712
// data: string of html
813
// context (optional): If specified, the fragment will be created in this context,
@@ -16,7 +21,11 @@ jQuery.parseHTML = function( data, context, keepScripts ) {
1621
keepScripts = context;
1722
context = false;
1823
}
19-
context = context || document;
24+
// document.implementation stops scripts or inline event handlers from
25+
// being executed immediately
26+
context = context || ( support.createHTMLDocument ?
27+
document.implementation.createHTMLDocument( "" ) :
28+
document );
2029

2130
var parsed = rsingleTag.exec( data ),
2231
scripts = !keepScripts && [];

src/core/support.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
define([
2+
"../var/support"
3+
], function( support ) {
4+
5+
support.createHTMLDocument = (function() {
6+
if ( !document.implementation.createHTMLDocument ) {
7+
return false;
8+
}
9+
var doc = document.implementation.createHTMLDocument( "" );
10+
doc.body.innerHTML = "<form></form><form></form>";
11+
return doc.body.childNodes.length === 2;
12+
})();
13+
14+
return support;
15+
});

test/unit/core.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,22 @@ test("jQuery.parseHTML", function() {
13481348
equal( jQuery.parseHTML("<td><td>")[ 1 ].parentNode.nodeType, 11, "parentNode should be documentFragment" );
13491349
});
13501350

1351+
if ( jQuery.support.createHTMLDocument ) {
1352+
asyncTest("jQuery.parseHTML", function() {
1353+
expect ( 1 );
1354+
1355+
Globals.register("parseHTMLError");
1356+
1357+
jQuery.globalEval("parseHTMLError = false;");
1358+
jQuery.parseHTML( "<img src=x onerror='parseHTMLError = true'>" );
1359+
1360+
window.setTimeout(function() {
1361+
start();
1362+
equal( window.parseHTMLError, false, "onerror eventhandler has not been called." );
1363+
}, 2000);
1364+
});
1365+
}
1366+
13511367
test("jQuery.parseJSON", function() {
13521368
expect( 20 );
13531369

test/unit/support.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
7979
"checkOn": true,
8080
"clearCloneStyle": true,
8181
"cors": true,
82+
"createHTMLDocument": true,
8283
"cssFloat": true,
8384
"deleteExpando": true,
8485
"focusinBubbles": false,
@@ -108,6 +109,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
108109
"checkOn": true,
109110
"clearCloneStyle": false,
110111
"cors": true,
112+
"createHTMLDocument": true,
111113
"cssFloat": true,
112114
"deleteExpando": true,
113115
"focusinBubbles": true,
@@ -137,6 +139,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
137139
"checkOn": true,
138140
"clearCloneStyle": false,
139141
"cors": false,
142+
"createHTMLDocument": true,
140143
"cssFloat": true,
141144
"deleteExpando": true,
142145
"focusinBubbles": true,
@@ -166,6 +169,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
166169
"checkOn": true,
167170
"clearCloneStyle": true,
168171
"cors": false,
172+
"createHTMLDocument": false,
169173
"cssFloat": false,
170174
"deleteExpando": false,
171175
"focusinBubbles": true,
@@ -186,7 +190,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
186190
"style": false,
187191
"submitBubbles": false
188192
};
189-
} else if ( /(6|7|8)\.0(\.\d+|) safari/i.test( userAgent ) ) {
193+
} else if ( /(6|7)\.0(\.\d+|) safari/i.test( userAgent ) ) {
190194
expected = {
191195
"ajax": true,
192196
"boxSizingReliable": true,
@@ -195,6 +199,37 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
195199
"checkOn": true,
196200
"clearCloneStyle": true,
197201
"cors": true,
202+
"createHTMLDocument": true,
203+
"cssFloat": true,
204+
"deleteExpando": true,
205+
"focusinBubbles": false,
206+
"html5Clone": true,
207+
"htmlSerialize": true,
208+
"input": true,
209+
"leadingWhitespace": true,
210+
"noCloneChecked": true,
211+
"noCloneEvent": true,
212+
"opacity": true,
213+
"optDisabled": true,
214+
"optSelected": true,
215+
"ownLast": false,
216+
"pixelPosition": false,
217+
"radioValue": true,
218+
"reliableHiddenOffsets": true,
219+
"reliableMarginRight": true,
220+
"style": true,
221+
"submitBubbles": true
222+
};
223+
} else if ( /8.0(\.\d+|) safari/i.test( userAgent ) ) {
224+
expected = {
225+
"ajax": true,
226+
"boxSizingReliable": true,
227+
"changeBubbles": true,
228+
"checkClone": true,
229+
"checkOn": true,
230+
"clearCloneStyle": true,
231+
"cors": true,
232+
"createHTMLDocument": false,
198233
"cssFloat": true,
199234
"deleteExpando": true,
200235
"focusinBubbles": false,
@@ -224,6 +259,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
224259
"checkOn": true,
225260
"clearCloneStyle": true,
226261
"cors": true,
262+
"createHTMLDocument": true,
227263
"cssFloat": true,
228264
"deleteExpando": true,
229265
"focusinBubbles": false,
@@ -253,6 +289,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
253289
"checkOn": true,
254290
"clearCloneStyle": true,
255291
"cors": true,
292+
"createHTMLDocument": true,
256293
"cssFloat": true,
257294
"deleteExpando": true,
258295
"focusinBubbles": false,
@@ -282,6 +319,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
282319
"checkOn": false,
283320
"clearCloneStyle": true,
284321
"cors": true,
322+
"createHTMLDocument": true,
285323
"cssFloat": true,
286324
"deleteExpando": true,
287325
"focusinBubbles": false,
@@ -311,6 +349,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
311349
"checkOn": false,
312350
"clearCloneStyle": false,
313351
"cors": true,
352+
"createHTMLDocument": true,
314353
"cssFloat": true,
315354
"deleteExpando": true,
316355
"focusinBubbles": false,

0 commit comments

Comments
 (0)