Make WordPress Core

Ticket #26913: 26913-json2-update.patch

File 26913-json2-update.patch, 11.0 KB (added by chriscct7, 10 years ago)
  • src/wp-includes/js/json2.js

     
    11/*
    2     http://www.JSON.org/json2.js
    3     2011-02-23
     2    json2.js
     3    2015-05-03
    44
    55    Public Domain.
    66
     
    1717
    1818
    1919    This file creates a global JSON object containing two methods: stringify
    20     and parse.
     20    and parse. This file is provides the ES5 JSON capability to ES3 systems.
     21    If a project might run on IE8 or earlier, then this file should be included.
     22    This file does nothing on ES5 systems.
    2123
    2224        JSON.stringify(value, replacer, space)
    2325            value       any JavaScript value, usually an object or array.
     
    4850                Date.prototype.toJSON = function (key) {
    4951                    function f(n) {
    5052                        // Format integers to have at least two digits.
    51                         return n < 10 ? '0' + n : n;
     53                        return n < 10
     54                            ? '0' + n
     55                            : n;
    5256                    }
    5357
    5458                    return this.getUTCFullYear()   + '-' +
     
    9498            // text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
    9599
    96100            text = JSON.stringify([new Date()], function (key, value) {
    97                 return this[key] instanceof Date ?
    98                     'Date(' + this[key] + ')' : value;
     101                return this[key] instanceof Date
     102                    ? 'Date(' + this[key] + ')'
     103                    : value;
    99104            });
    100105            // text is '["Date(---current time---)"]'
    101106
     
    146151    redistribute.
    147152*/
    148153
    149 /*jslint evil: true, strict: false, regexp: false */
     154/*jslint
     155    eval, for, this
     156*/
    150157
    151 /*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply,
    152     call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
     158/*property
     159    JSON, apply, call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
    153160    getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
    154161    lastIndex, length, parse, prototype, push, replace, slice, stringify,
    155162    test, toJSON, toString, valueOf
     
    159166// Create a JSON object only if one does not already exist. We create the
    160167// methods in a closure to avoid creating global variables.
    161168
    162 var JSON;
    163 if (!JSON) {
     169if (typeof JSON !== 'object') {
    164170    JSON = {};
    165171}
    166172
    167173(function () {
    168     "use strict";
     174    'use strict';
     175   
     176    var rx_one = /^[\],:{}\s]*$/,
     177        rx_two = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
     178        rx_three = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
     179        rx_four = /(?:^|:|,)(?:\s*\[)+/g,
     180        rx_escapable = /[\\\"\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
     181        rx_dangerous = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
    169182
    170183    function f(n) {
    171184        // Format integers to have at least two digits.
    172         return n < 10 ? '0' + n : n;
     185        return n < 10
     186            ? '0' + n
     187            : n;
    173188    }
     189   
     190    function this_value() {
     191        return this.valueOf();
     192    }
    174193
    175194    if (typeof Date.prototype.toJSON !== 'function') {
    176195
    177         Date.prototype.toJSON = function (key) {
     196        Date.prototype.toJSON = function () {
    178197
    179             return isFinite(this.valueOf()) ?
    180                 this.getUTCFullYear()     + '-' +
    181                 f(this.getUTCMonth() + 1) + '-' +
    182                 f(this.getUTCDate())      + 'T' +
    183                 f(this.getUTCHours())     + ':' +
    184                 f(this.getUTCMinutes())   + ':' +
    185                 f(this.getUTCSeconds())   + 'Z' : null;
     198            return isFinite(this.valueOf())
     199                ? this.getUTCFullYear() + '-' +
     200                        f(this.getUTCMonth() + 1) + '-' +
     201                        f(this.getUTCDate()) + 'T' +
     202                        f(this.getUTCHours()) + ':' +
     203                        f(this.getUTCMinutes()) + ':' +
     204                        f(this.getUTCSeconds()) + 'Z'
     205                : null;
    186206        };
    187207
    188         String.prototype.toJSON      =
    189             Number.prototype.toJSON  =
    190             Boolean.prototype.toJSON = function (key) {
    191                 return this.valueOf();
    192             };
     208        Boolean.prototype.toJSON = this_value;
     209        Number.prototype.toJSON = this_value;
     210        String.prototype.toJSON = this_value;
    193211    }
    194212
    195     var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
    196         escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
    197         gap,
     213    var gap,
    198214        indent,
    199         meta = {    // table of character substitutions
    200             '\b': '\\b',
    201             '\t': '\\t',
    202             '\n': '\\n',
    203             '\f': '\\f',
    204             '\r': '\\r',
    205             '"' : '\\"',
    206             '\\': '\\\\'
    207         },
     215        meta,
    208216        rep;
    209217
    210218
     
    215223// Otherwise we must also replace the offending characters with safe escape
    216224// sequences.
    217225
    218         escapable.lastIndex = 0;
    219         return escapable.test(string) ? '"' + string.replace(escapable, function (a) {
    220             var c = meta[a];
    221             return typeof c === 'string' ? c :
    222                 '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
    223         }) + '"' : '"' + string + '"';
     226        rx_escapable.lastIndex = 0;
     227        return rx_escapable.test(string)
     228            ? '"' + string.replace(rx_escapable, function (a) {
     229                var c = meta[a];
     230                return typeof c === 'string'
     231                    ? c
     232                    : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
     233            }) + '"'
     234            : '"' + string + '"';
    224235    }
    225236
    226237
     
    260271
    261272// JSON numbers must be finite. Encode non-finite numbers as null.
    262273
    263             return isFinite(value) ? String(value) : 'null';
     274            return isFinite(value)
     275                ? String(value)
     276                : 'null';
    264277
    265278        case 'boolean':
    266279        case 'null':
     
    303316// Join all of the elements together, separated with commas, and wrap them in
    304317// brackets.
    305318
    306                 v = partial.length === 0 ? '[]' : gap ?
    307                     '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' :
    308                     '[' + partial.join(',') + ']';
     319                v = partial.length === 0
     320                    ? '[]'
     321                    : gap
     322                        ? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']'
     323                        : '[' + partial.join(',') + ']';
    309324                gap = mind;
    310325                return v;
    311326            }
     
    319334                        k = rep[i];
    320335                        v = str(k, value);
    321336                        if (v) {
    322                             partial.push(quote(k) + (gap ? ': ' : ':') + v);
     337                            partial.push(quote(k) + (
     338                                gap
     339                                    ? ': '
     340                                    : ':'
     341                            ) + v);
    323342                        }
    324343                    }
    325344                }
     
    331350                    if (Object.prototype.hasOwnProperty.call(value, k)) {
    332351                        v = str(k, value);
    333352                        if (v) {
    334                             partial.push(quote(k) + (gap ? ': ' : ':') + v);
     353                            partial.push(quote(k) + (
     354                                gap
     355                                    ? ': '
     356                                    : ':'
     357                            ) + v);
    335358                        }
    336359                    }
    337360                }
     
    340363// Join all of the member texts together, separated with commas,
    341364// and wrap them in braces.
    342365
    343             v = partial.length === 0 ? '{}' : gap ?
    344                 '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' :
    345                 '{' + partial.join(',') + '}';
     366            v = partial.length === 0
     367                ? '{}'
     368                : gap
     369                    ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}'
     370                    : '{' + partial.join(',') + '}';
    346371            gap = mind;
    347372            return v;
    348373        }
     
    351376// If the JSON object does not yet have a stringify method, give it one.
    352377
    353378    if (typeof JSON.stringify !== 'function') {
     379        meta = {    // table of character substitutions
     380            '\b': '\\b',
     381            '\t': '\\t',
     382            '\n': '\\n',
     383            '\f': '\\f',
     384            '\r': '\\r',
     385            '"': '\\"',
     386            '\\': '\\\\'
     387        };
    354388        JSON.stringify = function (value, replacer, space) {
    355389
    356390// The stringify method takes a value and an optional replacer, and an optional
     
    432466// incorrectly, either silently deleting them, or treating them as line endings.
    433467
    434468            text = String(text);
    435             cx.lastIndex = 0;
    436             if (cx.test(text)) {
    437                 text = text.replace(cx, function (a) {
     469            rx_dangerous.lastIndex = 0;
     470            if (rx_dangerous.test(text)) {
     471                text = text.replace(rx_dangerous, function (a) {
    438472                    return '\\u' +
    439                         ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
     473                            ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
    440474                });
    441475            }
    442476
     
    453487// we look to see that the remaining characters are only whitespace or ']' or
    454488// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
    455489
    456             if (/^[\],:{}\s]*$/
    457                     .test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@')
    458                         .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
    459                         .replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
     490            if (
     491                rx_one.test(
     492                    text
     493                        .replace(rx_two, '@')
     494                        .replace(rx_three, ']')
     495                        .replace(rx_four, '')
     496                )
     497            ) {
    460498
    461499// In the third stage we use the eval function to compile the text into a
    462500// JavaScript structure. The '{' operator is subject to a syntactic ambiguity
     
    468506// In the optional fourth stage, we recursively walk the new structure, passing
    469507// each name/value pair to a reviver function for possible transformation.
    470508
    471                 return typeof reviver === 'function' ?
    472                     walk({'': j}, '') : j;
     509                return typeof reviver === 'function'
     510                    ? walk({'': j}, '')
     511                    : j;
    473512            }
    474513
    475514// If the text is not JSON parseable, then a SyntaxError is thrown.
     
    477516            throw new SyntaxError('JSON.parse');
    478517        };
    479518    }
    480 }());
     519}());
     520 No newline at end of file