Permalink
Please
sign in to comment.
Browse files
Move parsing methods to their own files (separates manipulation depen…
…dency from core)
- Loading branch information
Showing
with
82 additions
and 58 deletions.
- +2 −0 src/ajax.js
- +1 −0 src/ajax/load.js
- +8 −0 src/ajax/parseJSON.js
- +26 −0 src/ajax/parseXML.js
- +3 −58 src/core.js
- +38 −0 src/core/parseHTML.js
- +4 −0 src/var/rsingleTag.js
@@ -0,0 +1,8 @@ | ||
define([ | ||
"../core" | ||
], function( jQuery ) { | ||
|
||
jQuery.parseJSON = JSON.parse; | ||
|
||
return jQuery.parseJSON; | ||
}); |
@@ -0,0 +1,26 @@ | ||
define([ | ||
"../core" | ||
], function( jQuery ) { | ||
// Cross-browser xml parsing | ||
jQuery.parseXML = function( data ) { | ||
var xml, tmp; | ||
if ( !data || typeof data !== "string" ) { | ||
return null; | ||
} | ||
|
||
// Support: IE9 | ||
try { | ||
tmp = new DOMParser(); | ||
xml = tmp.parseFromString( data , "text/xml" ); | ||
} catch ( e ) { | ||
xml = undefined; | ||
} | ||
|
||
if ( !xml || xml.getElementsByTagName( "parsererror" ).length ) { | ||
jQuery.error( "Invalid XML: " + data ); | ||
} | ||
return xml; | ||
}; | ||
|
||
return jQuery.parseXML; | ||
}); |
@@ -0,0 +1,38 @@ | ||
define([ | ||
"../core", | ||
"../var/rsingleTag", | ||
"../manipulation" // buildFragment | ||
], function( jQuery, rsingleTag ) { | ||
|
||
// data: string of html | ||
// context (optional): If specified, the fragment will be created in this context, defaults to document | ||
// keepScripts (optional): If true, will include scripts passed in the html string | ||
jQuery.parseHTML = function( data, context, keepScripts ) { | ||
if ( !data || typeof data !== "string" ) { | ||
return null; | ||
} | ||
if ( typeof context === "boolean" ) { | ||
keepScripts = context; | ||
context = false; | ||
} | ||
context = context || document; | ||
|
||
var parsed = rsingleTag.exec( data ), | ||
scripts = !keepScripts && []; | ||
|
||
// Single tag | ||
if ( parsed ) { | ||
return [ context.createElement( parsed[1] ) ]; | ||
} | ||
|
||
parsed = jQuery.buildFragment( [ data ], context, scripts ); | ||
|
||
if ( scripts && scripts.length ) { | ||
jQuery( scripts ).remove(); | ||
} | ||
|
||
return jQuery.merge( [], parsed.childNodes ); | ||
}; | ||
|
||
return jQuery.parseHTML; | ||
}); |
@@ -0,0 +1,4 @@ | ||
define(function() { | ||
// Match a standalone tag | ||
return (/^<(\w+)\s*\/?>(?:<\/\1>|)$/); | ||
}); |
0 comments on commit
99c123b