src/cm/media/js/lib/yui/yui3-3.15.0/build/json-parse-shim/json-parse-shim.js
changeset 602 e16a97fb364a
equal deleted inserted replaced
601:d334a616c023 602:e16a97fb364a
       
     1 YUI.add('json-parse-shim', function (Y, NAME) {
       
     2 
       
     3 /**
       
     4  * <p>The JSON module adds support for serializing JavaScript objects into
       
     5  * JSON strings and parsing JavaScript objects from strings in JSON format.</p>
       
     6  *
       
     7  * <p>The JSON namespace is added to your YUI instance including static methods
       
     8  * Y.JSON.parse(..) and Y.JSON.stringify(..).</p>
       
     9  *
       
    10  * <p>The functionality and method signatures follow the ECMAScript 5
       
    11  * specification.  In browsers with native JSON support, the native
       
    12  * implementation is used.</p>
       
    13  *
       
    14  * <p>The <code>json</code> module is a rollup of <code>json-parse</code> and
       
    15  * <code>json-stringify</code>.</p>
       
    16  *
       
    17  * <p>As their names suggest, <code>json-parse</code> adds support for parsing
       
    18  * JSON data (Y.JSON.parse) and <code>json-stringify</code> for serializing
       
    19  * JavaScript data into JSON strings (Y.JSON.stringify).  You may choose to
       
    20  * include either of the submodules individually if you don't need the
       
    21  * complementary functionality, or include the rollup for both.</p>
       
    22  *
       
    23  * @module json
       
    24  * @main json
       
    25  * @class JSON
       
    26  * @static
       
    27  */
       
    28 
       
    29 /**
       
    30  * Provides Y.JSON.parse method to accept JSON strings and return native
       
    31  * JavaScript objects.
       
    32  *
       
    33  * @module json
       
    34  * @submodule json-parse
       
    35  * @for JSON
       
    36  * @static
       
    37  */
       
    38 
       
    39 
       
    40     /**
       
    41      * Replace certain Unicode characters that JavaScript may handle incorrectly
       
    42      * during eval--either by deleting them or treating them as line
       
    43      * endings--with escape sequences.
       
    44      * IMPORTANT NOTE: This regex will be used to modify the input if a match is
       
    45      * found.
       
    46      *
       
    47      * @property _UNICODE_EXCEPTIONS
       
    48      * @type {RegExp}
       
    49      * @private
       
    50      */
       
    51 var _UNICODE_EXCEPTIONS = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
       
    52 
       
    53 
       
    54     /**
       
    55      * First step in the safety evaluation.  Regex used to replace all escape
       
    56      * sequences (i.e. "\\", etc) with '@' characters (a non-JSON character).
       
    57      *
       
    58      * @property _ESCAPES
       
    59      * @type {RegExp}
       
    60      * @private
       
    61      */
       
    62     _ESCAPES = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
       
    63 
       
    64     /**
       
    65      * Second step in the safety evaluation.  Regex used to replace all simple
       
    66      * values with ']' characters.
       
    67      *
       
    68      * @property _VALUES
       
    69      * @type {RegExp}
       
    70      * @private
       
    71      */
       
    72     _VALUES  = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
       
    73 
       
    74     /**
       
    75      * Third step in the safety evaluation.  Regex used to remove all open
       
    76      * square brackets following a colon, comma, or at the beginning of the
       
    77      * string.
       
    78      *
       
    79      * @property _BRACKETS
       
    80      * @type {RegExp}
       
    81      * @private
       
    82      */
       
    83     _BRACKETS = /(?:^|:|,)(?:\s*\[)+/g,
       
    84 
       
    85     /**
       
    86      * Final step in the safety evaluation.  Regex used to test the string left
       
    87      * after all previous replacements for invalid characters.
       
    88      *
       
    89      * @property _UNSAFE
       
    90      * @type {RegExp}
       
    91      * @private
       
    92      */
       
    93     _UNSAFE = /[^\],:{}\s]/,
       
    94 
       
    95     /**
       
    96      * Replaces specific unicode characters with their appropriate \unnnn
       
    97      * format. Some browsers ignore certain characters during eval.
       
    98      *
       
    99      * @method escapeException
       
   100      * @param c {String} Unicode character
       
   101      * @return {String} the \unnnn escapement of the character
       
   102      * @private
       
   103      */
       
   104     _escapeException = function (c) {
       
   105         return '\\u'+('0000'+(+(c.charCodeAt(0))).toString(16)).slice(-4);
       
   106     },
       
   107 
       
   108     /**
       
   109      * Traverses nested objects, applying a reviver function to each (key,value)
       
   110      * from the scope if the key:value's containing object.  The value returned
       
   111      * from the function will replace the original value in the key:value pair.
       
   112      * If the value returned is undefined, the key will be omitted from the
       
   113      * returned object.
       
   114      *
       
   115      * @method _revive
       
   116      * @param data {MIXED} Any JavaScript data
       
   117      * @param reviver {Function} filter or mutation function
       
   118      * @return {MIXED} The results of the filtered data
       
   119      * @private
       
   120      */
       
   121     _revive = function (data, reviver) {
       
   122         var walk = function (o,key) {
       
   123             var k,v,value = o[key];
       
   124             if (value && typeof value === 'object') {
       
   125                 for (k in value) {
       
   126                     if (value.hasOwnProperty(k)) {
       
   127                         v = walk(value, k);
       
   128                         if (v === undefined) {
       
   129                             delete value[k];
       
   130                         } else {
       
   131                             value[k] = v;
       
   132                         }
       
   133                     }
       
   134                 }
       
   135             }
       
   136             return reviver.call(o,key,value);
       
   137         };
       
   138 
       
   139         return typeof reviver === 'function' ? walk({'':data},'') : data;
       
   140     };
       
   141 
       
   142 /**
       
   143  * Parse a JSON string, returning the native JavaScript representation.
       
   144  *
       
   145  * @param s {string} JSON string data
       
   146  * @param reviver {function} (optional) function(k,v) passed each key value
       
   147  *          pair of object literals, allowing pruning or altering values
       
   148  * @return {MIXED} the native JavaScript representation of the JSON string
       
   149  * @throws SyntaxError
       
   150  * @method parse
       
   151  * @static
       
   152  */
       
   153 // JavaScript implementation in lieu of native browser support.  Based on
       
   154 // the json2.js library from http://json.org
       
   155 Y.JSON.parse = function (s,reviver) {
       
   156     if (typeof s !== 'string') {
       
   157         s += '';
       
   158     }
       
   159 
       
   160     // Replace certain Unicode characters that are otherwise handled
       
   161     // incorrectly by some browser implementations.
       
   162     // NOTE: This modifies the input if such characters are found!
       
   163     s = s.replace(_UNICODE_EXCEPTIONS, _escapeException);
       
   164 
       
   165     // Test for any remaining invalid characters
       
   166     if (!_UNSAFE.test(s.replace(_ESCAPES,'@').
       
   167                         replace(_VALUES,']').
       
   168                         replace(_BRACKETS,''))) {
       
   169 
       
   170         // Eval the text into a JavaScript data structure, apply any
       
   171         // reviver function, and return
       
   172         return _revive(eval('(' + s + ')'), reviver);
       
   173     }
       
   174 
       
   175     throw new SyntaxError('JSON.parse');
       
   176 };
       
   177 
       
   178 // Property available for testing if the implementation being used
       
   179 // is native or a shim
       
   180 Y.JSON.parse.isShim = true;
       
   181 
       
   182 
       
   183 }, '@VERSION@', {"requires": ["json-parse"]});