diff options
author | Timmy Willison <timmywillisn@gmail.com> | 2013-09-08 21:05:07 -0400 |
---|---|---|
committer | Timmy Willison <timmywillisn@gmail.com> | 2013-09-08 21:12:58 -0400 |
commit | 44596aa8f2cc908c73bc149fccbcc0f8458a330a (patch) | |
tree | efdb578e20becb8601132f3f8a9ca1d351ebcfb3 /src/ajax | |
parent | 48e13e9a5f9ab1ec0ce576d35271c8f2c7c7554d (diff) | |
download | jquery-44596aa8f2cc908c73bc149fccbcc0f8458a330a.tar.gz jquery-44596aa8f2cc908c73bc149fccbcc0f8458a330a.zip |
Move parsing methods to their own files (separates manipulation dependency from core)
Conflicts:
src/core.js
Diffstat (limited to 'src/ajax')
-rw-r--r-- | src/ajax/load.js | 1 | ||||
-rw-r--r-- | src/ajax/parseJSON.js | 41 | ||||
-rw-r--r-- | src/ajax/parseXML.js | 29 |
3 files changed, 71 insertions, 0 deletions
diff --git a/src/ajax/load.js b/src/ajax/load.js index d8e7e57c4..cc2a71958 100644 --- a/src/ajax/load.js +++ b/src/ajax/load.js @@ -1,5 +1,6 @@ define([ "../core", + "../core/parseHTML", "../ajax", "../traversing", "../manipulation", diff --git a/src/ajax/parseJSON.js b/src/ajax/parseJSON.js new file mode 100644 index 000000000..ff678623e --- /dev/null +++ b/src/ajax/parseJSON.js @@ -0,0 +1,41 @@ +define([ + "../core" +], function( jQuery ) { + + var rvalidchars = /^[\],:{}\s]*$/, + rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g, + rvalidescape = /\\(?:["\\\/bfnrt]|u[\da-fA-F]{4})/g, + rvalidtokens = /"[^"\\\r\n]*"|true|false|null|-?(?:\d+\.|)\d+(?:[eE][+-]?\d+|)/g; + + jQuery.parseJSON = function( data ) { + // Attempt to parse using the native JSON parser first + if ( window.JSON && window.JSON.parse ) { + return window.JSON.parse( data ); + } + + if ( data === null ) { + return data; + } + + if ( typeof data === "string" ) { + + // Make sure leading/trailing whitespace is removed (IE can't handle it) + data = jQuery.trim( data ); + + if ( data ) { + // Make sure the incoming data is actual JSON + // Logic borrowed from http://json.org/json2.js + if ( rvalidchars.test( data.replace( rvalidescape, "@" ) + .replace( rvalidtokens, "]" ) + .replace( rvalidbraces, "")) ) { + + return ( new Function( "return " + data ) )(); + } + } + } + + jQuery.error( "Invalid JSON: " + data ); + }; + + return jQuery.parseJSON; +}); diff --git a/src/ajax/parseXML.js b/src/ajax/parseXML.js new file mode 100644 index 000000000..ab2969992 --- /dev/null +++ b/src/ajax/parseXML.js @@ -0,0 +1,29 @@ +define([ + "../core" +], function( jQuery ) { + // Cross-browser xml parsing + jQuery.parseXML = function( data ) { + var xml, tmp; + if ( !data || typeof data !== "string" ) { + return null; + } + try { + if ( window.DOMParser ) { // Standard + tmp = new DOMParser(); + xml = tmp.parseFromString( data , "text/xml" ); + } else { // IE + xml = new ActiveXObject( "Microsoft.XMLDOM" ); + xml.async = "false"; + xml.loadXML( data ); + } + } catch( e ) { + xml = undefined; + } + if ( !xml || !xml.documentElement || xml.getElementsByTagName( "parsererror" ).length ) { + jQuery.error( "Invalid XML: " + data ); + } + return xml; + }; + + return jQuery.parseXML; +}); |