@@ -104,7 +104,6 @@ jQuery.fn = jQuery.prototype = {
104
104
105
105
// Handle HTML strings
106
106
if ( typeof selector === "string" ) {
107
- // Are we dealing with HTML string or an ID?
108
107
if ( selector . charAt ( 0 ) === "<" && selector . charAt ( selector . length - 1 ) === ">" && selector . length >= 3 ) {
109
108
// Assume that strings that start and end with <> are HTML and skip the regex check
110
109
match = [ null , selector , null ] ;
@@ -118,19 +117,10 @@ jQuery.fn = jQuery.prototype = {
118
117
context = context instanceof jQuery ? context [ 0 ] : context ;
119
118
doc = ( context && context . nodeType ? context . ownerDocument || context : document ) ;
120
119
121
- // If a single string is passed in and it's a single tag
122
- // just do a createElement and skip the rest
123
- ret = rsingleTag . exec ( selector ) ;
124
-
125
- if ( ret ) {
126
- selector = [ doc . createElement ( ret [ 1 ] ) ] ;
127
- if ( jQuery . isPlainObject ( context ) ) {
128
- this . attr . call ( selector , context , true ) ;
129
- }
130
-
131
- } else {
132
- ret = jQuery . buildFragment ( [ match [ 1 ] ] , doc ) ;
133
- selector = ( ret . cacheable ? jQuery . clone ( ret . fragment ) : ret . fragment ) . childNodes ;
120
+ // scripts is true for back-compat
121
+ selector = jQuery . parseHTML ( match [ 1 ] , doc , true ) ;
122
+ if ( rsingleTag . test ( match [ 1 ] ) && jQuery . isPlainObject ( context ) ) {
123
+ this . attr . call ( selector , context , true ) ;
134
124
}
135
125
136
126
return jQuery . merge ( this , selector ) ;
@@ -459,8 +449,32 @@ jQuery.extend({
459
449
throw new Error ( msg ) ;
460
450
} ,
461
451
452
+ // data: string of html
453
+ // context (optional): If specified, the fragment will be created in this context, defaults to document
454
+ // scripts (optional): If true, will include scripts passed in the html string
455
+ parseHTML : function ( data , context , scripts ) {
456
+ var parsed ;
457
+ if ( ! data || typeof data !== "string" ) {
458
+ return null ;
459
+ }
460
+ if ( typeof context === "boolean" ) {
461
+ scripts = context ;
462
+ context = 0 ;
463
+ }
464
+ context = context || document ;
465
+
466
+ // Single tag
467
+ if ( ( parsed = rsingleTag . exec ( data ) ) ) {
468
+ return [ context . createElement ( parsed [ 1 ] ) ] ;
469
+ }
470
+
471
+ parsed = jQuery . buildFragment ( [ data ] , context , scripts ? null : [ ] ) ;
472
+ return jQuery . merge ( [ ] ,
473
+ ( parsed . cacheable ? jQuery . clone ( parsed . fragment ) : parsed . fragment ) . childNodes ) ;
474
+ } ,
475
+
462
476
parseJSON : function ( data ) {
463
- if ( typeof data !== "string" || ! data ) {
477
+ if ( ! data || typeof data !== "string" ) {
464
478
return null ;
465
479
}
466
480
@@ -486,10 +500,10 @@ jQuery.extend({
486
500
487
501
// Cross-browser xml parsing
488
502
parseXML : function ( data ) {
489
- if ( typeof data !== "string" || ! data ) {
503
+ var xml , tmp ;
504
+ if ( ! data || typeof data !== "string" ) {
490
505
return null ;
491
506
}
492
- var xml , tmp ;
493
507
try {
494
508
if ( window . DOMParser ) { // Standard
495
509
tmp = new DOMParser ( ) ;
0 commit comments