External Libraries: update `json2.js` to the `2015-05-03` version. Crockford does not tag releases: https://github.com/douglascrockford/JSON-js/commits/master

Props mgibbs189, chriscct7.
Fixes #26913.


git-svn-id: https://develop.svn.wordpress.org/trunk@34863 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2015-10-06 14:01:52 +00:00
parent 79a907edf3
commit d257aeb9a3
1 changed files with 103 additions and 64 deletions

View File

@ -1,6 +1,6 @@
/* /*
http://www.JSON.org/json2.js json2.js
2011-02-23 2015-05-03
Public Domain. Public Domain.
@ -17,7 +17,9 @@
This file creates a global JSON object containing two methods: stringify This file creates a global JSON object containing two methods: stringify
and parse. and parse. This file is provides the ES5 JSON capability to ES3 systems.
If a project might run on IE8 or earlier, then this file should be included.
This file does nothing on ES5 systems.
JSON.stringify(value, replacer, space) JSON.stringify(value, replacer, space)
value any JavaScript value, usually an object or array. value any JavaScript value, usually an object or array.
@ -48,7 +50,9 @@
Date.prototype.toJSON = function (key) { Date.prototype.toJSON = function (key) {
function f(n) { function f(n) {
// Format integers to have at least two digits. // Format integers to have at least two digits.
return n < 10 ? '0' + n : n; return n < 10
? '0' + n
: n;
} }
return this.getUTCFullYear() + '-' + return this.getUTCFullYear() + '-' +
@ -94,8 +98,9 @@
// text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]' // text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
text = JSON.stringify([new Date()], function (key, value) { text = JSON.stringify([new Date()], function (key, value) {
return this[key] instanceof Date ? return this[key] instanceof Date
'Date(' + this[key] + ')' : value; ? 'Date(' + this[key] + ')'
: value;
}); });
// text is '["Date(---current time---)"]' // text is '["Date(---current time---)"]'
@ -146,10 +151,12 @@
redistribute. redistribute.
*/ */
/*jslint evil: true, strict: false, regexp: false */ /*jslint
eval, for, this
*/
/*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply, /*property
call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours, JSON, apply, call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join, getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
lastIndex, length, parse, prototype, push, replace, slice, stringify, lastIndex, length, parse, prototype, push, replace, slice, stringify,
test, toJSON, toString, valueOf test, toJSON, toString, valueOf
@ -159,52 +166,53 @@
// Create a JSON object only if one does not already exist. We create the // Create a JSON object only if one does not already exist. We create the
// methods in a closure to avoid creating global variables. // methods in a closure to avoid creating global variables.
var JSON; if (typeof JSON !== 'object') {
if (!JSON) {
JSON = {}; JSON = {};
} }
(function () { (function () {
"use strict"; 'use strict';
var rx_one = /^[\],:{}\s]*$/,
rx_two = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
rx_three = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
rx_four = /(?:^|:|,)(?:\s*\[)+/g,
rx_escapable = /[\\\"\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
rx_dangerous = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
function f(n) { function f(n) {
// Format integers to have at least two digits. // Format integers to have at least two digits.
return n < 10 ? '0' + n : n; return n < 10
? '0' + n
: n;
}
function this_value() {
return this.valueOf();
} }
if (typeof Date.prototype.toJSON !== 'function') { if (typeof Date.prototype.toJSON !== 'function') {
Date.prototype.toJSON = function (key) { Date.prototype.toJSON = function () {
return isFinite(this.valueOf()) ? return isFinite(this.valueOf())
this.getUTCFullYear() + '-' + ? this.getUTCFullYear() + '-' +
f(this.getUTCMonth() + 1) + '-' + f(this.getUTCMonth() + 1) + '-' +
f(this.getUTCDate()) + 'T' + f(this.getUTCDate()) + 'T' +
f(this.getUTCHours()) + ':' + f(this.getUTCHours()) + ':' +
f(this.getUTCMinutes()) + ':' + f(this.getUTCMinutes()) + ':' +
f(this.getUTCSeconds()) + 'Z' : null; f(this.getUTCSeconds()) + 'Z'
: null;
}; };
String.prototype.toJSON = Boolean.prototype.toJSON = this_value;
Number.prototype.toJSON = Number.prototype.toJSON = this_value;
Boolean.prototype.toJSON = function (key) { String.prototype.toJSON = this_value;
return this.valueOf();
};
} }
var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, var gap,
escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
gap,
indent, indent,
meta = { // table of character substitutions meta,
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
},
rep; rep;
@ -215,12 +223,15 @@ if (!JSON) {
// Otherwise we must also replace the offending characters with safe escape // Otherwise we must also replace the offending characters with safe escape
// sequences. // sequences.
escapable.lastIndex = 0; rx_escapable.lastIndex = 0;
return escapable.test(string) ? '"' + string.replace(escapable, function (a) { return rx_escapable.test(string)
var c = meta[a]; ? '"' + string.replace(rx_escapable, function (a) {
return typeof c === 'string' ? c : var c = meta[a];
'\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); return typeof c === 'string'
}) + '"' : '"' + string + '"'; ? c
: '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
}) + '"'
: '"' + string + '"';
} }
@ -260,7 +271,9 @@ if (!JSON) {
// JSON numbers must be finite. Encode non-finite numbers as null. // JSON numbers must be finite. Encode non-finite numbers as null.
return isFinite(value) ? String(value) : 'null'; return isFinite(value)
? String(value)
: 'null';
case 'boolean': case 'boolean':
case 'null': case 'null':
@ -303,9 +316,11 @@ if (!JSON) {
// Join all of the elements together, separated with commas, and wrap them in // Join all of the elements together, separated with commas, and wrap them in
// brackets. // brackets.
v = partial.length === 0 ? '[]' : gap ? v = partial.length === 0
'[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' : ? '[]'
'[' + partial.join(',') + ']'; : gap
? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']'
: '[' + partial.join(',') + ']';
gap = mind; gap = mind;
return v; return v;
} }
@ -319,7 +334,11 @@ if (!JSON) {
k = rep[i]; k = rep[i];
v = str(k, value); v = str(k, value);
if (v) { if (v) {
partial.push(quote(k) + (gap ? ': ' : ':') + v); partial.push(quote(k) + (
gap
? ': '
: ':'
) + v);
} }
} }
} }
@ -331,7 +350,11 @@ if (!JSON) {
if (Object.prototype.hasOwnProperty.call(value, k)) { if (Object.prototype.hasOwnProperty.call(value, k)) {
v = str(k, value); v = str(k, value);
if (v) { if (v) {
partial.push(quote(k) + (gap ? ': ' : ':') + v); partial.push(quote(k) + (
gap
? ': '
: ':'
) + v);
} }
} }
} }
@ -340,9 +363,11 @@ if (!JSON) {
// Join all of the member texts together, separated with commas, // Join all of the member texts together, separated with commas,
// and wrap them in braces. // and wrap them in braces.
v = partial.length === 0 ? '{}' : gap ? v = partial.length === 0
'{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' : ? '{}'
'{' + partial.join(',') + '}'; : gap
? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}'
: '{' + partial.join(',') + '}';
gap = mind; gap = mind;
return v; return v;
} }
@ -351,6 +376,15 @@ if (!JSON) {
// If the JSON object does not yet have a stringify method, give it one. // If the JSON object does not yet have a stringify method, give it one.
if (typeof JSON.stringify !== 'function') { if (typeof JSON.stringify !== 'function') {
meta = { // table of character substitutions
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"': '\\"',
'\\': '\\\\'
};
JSON.stringify = function (value, replacer, space) { JSON.stringify = function (value, replacer, space) {
// The stringify method takes a value and an optional replacer, and an optional // The stringify method takes a value and an optional replacer, and an optional
@ -432,11 +466,11 @@ if (!JSON) {
// incorrectly, either silently deleting them, or treating them as line endings. // incorrectly, either silently deleting them, or treating them as line endings.
text = String(text); text = String(text);
cx.lastIndex = 0; rx_dangerous.lastIndex = 0;
if (cx.test(text)) { if (rx_dangerous.test(text)) {
text = text.replace(cx, function (a) { text = text.replace(rx_dangerous, function (a) {
return '\\u' + return '\\u' +
('0000' + a.charCodeAt(0).toString(16)).slice(-4); ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
}); });
} }
@ -453,10 +487,14 @@ if (!JSON) {
// we look to see that the remaining characters are only whitespace or ']' or // we look to see that the remaining characters are only whitespace or ']' or
// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval. // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
if (/^[\],:{}\s]*$/ if (
.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@') rx_one.test(
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']') text
.replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { .replace(rx_two, '@')
.replace(rx_three, ']')
.replace(rx_four, '')
)
) {
// In the third stage we use the eval function to compile the text into a // In the third stage we use the eval function to compile the text into a
// JavaScript structure. The '{' operator is subject to a syntactic ambiguity // JavaScript structure. The '{' operator is subject to a syntactic ambiguity
@ -468,8 +506,9 @@ if (!JSON) {
// In the optional fourth stage, we recursively walk the new structure, passing // In the optional fourth stage, we recursively walk the new structure, passing
// each name/value pair to a reviver function for possible transformation. // each name/value pair to a reviver function for possible transformation.
return typeof reviver === 'function' ? return typeof reviver === 'function'
walk({'': j}, '') : j; ? walk({'': j}, '')
: j;
} }
// If the text is not JSON parseable, then a SyntaxError is thrown. // If the text is not JSON parseable, then a SyntaxError is thrown.
@ -477,4 +516,4 @@ if (!JSON) {
throw new SyntaxError('JSON.parse'); throw new SyntaxError('JSON.parse');
}; };
} }
}()); }());