r112908 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112907‎ | r112908 | r112909 >
Date:19:11, 2 March 2012
Author:reedy
Status:ok
Tags:
Comment:
Bug 34896 - Update jQuery.json

Has whitespace changes (bit of a null diff in many cases)
Modified paths:
  • /trunk/phase3/RELEASE-NOTES-1.20 (modified) (history)
  • /trunk/phase3/resources/jquery/jquery.json.js (modified) (history)

Diff [purge]

Index: trunk/phase3/RELEASE-NOTES-1.20
@@ -22,6 +22,7 @@
2323 * (bug 34475) Add support for IP/CIDR notation to tablesorter
2424 * (bug 27619) Remove preference option to display broken links as link?
2525 * (bug 15404) Add support for sorting fractions in jquery.tablesorter
 26+* jQuery.json updated 2.3 (2011-09-17).
2627
2728 === Bug fixes in 1.20 ===
2829 * (bug 30245) Use the correct way to construct a log page title.
Index: trunk/phase3/resources/jquery/jquery.json.js
@@ -1,180 +1,193 @@
2 -/*
 2+/**
33 * jQuery JSON Plugin
4 - * version: 2.1 (2009-08-14)
 4+ * version: 2.3 (2011-09-17)
55 *
66 * This document is licensed as free software under the terms of the
77 * MIT License: http://www.opensource.org/licenses/mit-license.php
88 *
9 - * Brantley Harris wrote this plugin. It is based somewhat on the JSON.org
 9+ * Brantley Harris wrote this plugin. It is based somewhat on the JSON.org
1010 * website's http://www.json.org/json2.js, which proclaims:
1111 * "NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.", a sentiment that
1212 * I uphold.
1313 *
14 - * It is also influenced heavily by MochiKit's serializeJSON, which is
 14+ * It is also influenced heavily by MochiKit's serializeJSON, which is
1515 * copyrighted 2005 by Bob Ippolito.
16 - *
17 - * @see http://code.google.com/p/jquery-json/
1816 */
1917
20 -(function($) {
21 - /** jQuery.toJSON( json-serializble )
22 - Converts the given argument into a JSON respresentation.
23 -
24 - If an object has a "toJSON" function, that will be used to get the representation.
25 - Non-integer/string keys are skipped in the object, as are keys that point to a function.
26 -
27 - json-serializble:
28 - The *thing* to be converted.
29 - **/
30 - $.toJSON = function(o)
31 - {
32 - if (typeof(JSON) == 'object' && JSON.stringify)
33 - return JSON.stringify(o);
34 -
35 - var type = typeof(o);
36 -
37 - if (o === null)
38 - return "null";
39 -
40 - if (type == "undefined")
41 - return undefined;
42 -
43 - if (type == "number" || type == "boolean")
44 - return o + "";
45 -
46 - if (type == "string")
47 - return $.quoteString(o);
48 -
49 - if (type == 'object')
50 - {
51 - if (typeof o.toJSON == "function")
52 - return $.toJSON( o.toJSON() );
53 -
54 - if (o.constructor === Date)
55 - {
56 - var month = o.getUTCMonth() + 1;
57 - if (month < 10) month = '0' + month;
58 -
59 - var day = o.getUTCDate();
60 - if (day < 10) day = '0' + day;
61 -
62 - var year = o.getUTCFullYear();
63 -
64 - var hours = o.getUTCHours();
65 - if (hours < 10) hours = '0' + hours;
66 -
67 - var minutes = o.getUTCMinutes();
68 - if (minutes < 10) minutes = '0' + minutes;
69 -
70 - var seconds = o.getUTCSeconds();
71 - if (seconds < 10) seconds = '0' + seconds;
72 -
73 - var milli = o.getUTCMilliseconds();
74 - if (milli < 100) milli = '0' + milli;
75 - if (milli < 10) milli = '0' + milli;
76 -
77 - return '"' + year + '-' + month + '-' + day + 'T' +
78 - hours + ':' + minutes + ':' + seconds +
79 - '.' + milli + 'Z"';
80 - }
81 -
82 - if (o.constructor === Array)
83 - {
84 - var ret = [];
85 - for (var i = 0; i < o.length; i++)
86 - ret.push( $.toJSON(o[i]) || "null" );
87 -
88 - return "[" + ret.join(",") + "]";
89 - }
90 -
91 - var pairs = [];
92 - for (var k in o) {
93 - var name;
94 - var type = typeof k;
95 -
96 - if (type == "number")
97 - name = '"' + k + '"';
98 - else if (type == "string")
99 - name = $.quoteString(k);
100 - else
101 - continue; //skip non-string or number keys
102 -
103 - if (typeof o[k] == "function")
104 - continue; //skip pairs where the value is a function.
105 -
106 - var val = $.toJSON(o[k]);
107 -
108 - pairs.push(name + ":" + val);
109 - }
110 -
111 - return "{" + pairs.join(", ") + "}";
112 - }
113 - };
114 -
115 - /** jQuery.evalJSON(src)
116 - Evaluates a given piece of json source.
117 - **/
118 - $.evalJSON = function(src)
119 - {
120 - if (typeof(JSON) == 'object' && JSON.parse)
121 - return JSON.parse(src);
122 - return eval("(" + src + ")");
123 - };
124 -
125 - /** jQuery.secureEvalJSON(src)
126 - Evals JSON in a way that is *more* secure.
127 - **/
128 - $.secureEvalJSON = function(src)
129 - {
130 - if (typeof(JSON) == 'object' && JSON.parse)
131 - return JSON.parse(src);
132 -
133 - var filtered = src;
134 - filtered = filtered.replace(/\\["\\\/bfnrtu]/g, '@');
135 - filtered = filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']');
136 - filtered = filtered.replace(/(?:^|:|,)(?:\s*\[)+/g, '');
137 -
138 - if (/^[\],:{}\s]*$/.test(filtered))
139 - return eval("(" + src + ")");
140 - else
141 - throw new SyntaxError("Error parsing JSON, source is not valid.");
142 - };
143 -
144 - /** jQuery.quoteString(string)
145 - Returns a string-repr of a string, escaping quotes intelligently.
146 - Mostly a support function for toJSON.
147 -
148 - Examples:
149 - >>> jQuery.quoteString("apple")
150 - "apple"
151 -
152 - >>> jQuery.quoteString('"Where are we going?", she asked.')
153 - "\"Where are we going?\", she asked."
154 - **/
155 - $.quoteString = function(string)
156 - {
157 - if (string.match(_escapeable))
158 - {
159 - return '"' + string.replace(_escapeable, function (a)
160 - {
161 - var c = _meta[a];
162 - if (typeof c === 'string') return c;
163 - c = a.charCodeAt();
164 - return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
165 - }) + '"';
166 - }
167 - return '"' + string + '"';
168 - };
169 -
170 - var _escapeable = /["\\\x00-\x1f\x7f-\x9f]/g;
171 -
172 - var _meta = {
173 - '\b': '\\b',
174 - '\t': '\\t',
175 - '\n': '\\n',
176 - '\f': '\\f',
177 - '\r': '\\r',
178 - '"' : '\\"',
179 - '\\': '\\\\'
180 - };
181 -})(jQuery);
 18+(function( $ ) {
 19+
 20+ var escapeable = /["\\\x00-\x1f\x7f-\x9f]/g,
 21+ meta = {
 22+ '\b': '\\b',
 23+ '\t': '\\t',
 24+ '\n': '\\n',
 25+ '\f': '\\f',
 26+ '\r': '\\r',
 27+ '"' : '\\"',
 28+ '\\': '\\\\'
 29+ };
 30+
 31+ /**
 32+ * jQuery.toJSON
 33+ * Converts the given argument into a JSON respresentation.
 34+ *
 35+ * @param o {Mixed} The json-serializble *thing* to be converted
 36+ *
 37+ * If an object has a toJSON prototype, that will be used to get the representation.
 38+ * Non-integer/string keys are skipped in the object, as are keys that point to a
 39+ * function.
 40+ *
 41+ */
 42+ $.toJSON = typeof JSON === 'object' && JSON.stringify
 43+ ? JSON.stringify
 44+ : function( o ) {
 45+
 46+ if ( o === null ) {
 47+ return 'null';
 48+ }
 49+
 50+ var type = typeof o;
 51+
 52+ if ( type === 'undefined' ) {
 53+ return undefined;
 54+ }
 55+ if ( type === 'number' || type === 'boolean' ) {
 56+ return '' + o;
 57+ }
 58+ if ( type === 'string') {
 59+ return $.quoteString( o );
 60+ }
 61+ if ( type === 'object' ) {
 62+ if ( typeof o.toJSON === 'function' ) {
 63+ return $.toJSON( o.toJSON() );
 64+ }
 65+ if ( o.constructor === Date ) {
 66+ var month = o.getUTCMonth() + 1,
 67+ day = o.getUTCDate(),
 68+ year = o.getUTCFullYear(),
 69+ hours = o.getUTCHours(),
 70+ minutes = o.getUTCMinutes(),
 71+ seconds = o.getUTCSeconds(),
 72+ milli = o.getUTCMilliseconds();
 73+
 74+ if ( month < 10 ) {
 75+ month = '0' + month;
 76+ }
 77+ if ( day < 10 ) {
 78+ day = '0' + day;
 79+ }
 80+ if ( hours < 10 ) {
 81+ hours = '0' + hours;
 82+ }
 83+ if ( minutes < 10 ) {
 84+ minutes = '0' + minutes;
 85+ }
 86+ if ( seconds < 10 ) {
 87+ seconds = '0' + seconds;
 88+ }
 89+ if ( milli < 100 ) {
 90+ milli = '0' + milli;
 91+ }
 92+ if ( milli < 10 ) {
 93+ milli = '0' + milli;
 94+ }
 95+ return '"' + year + '-' + month + '-' + day + 'T' +
 96+ hours + ':' + minutes + ':' + seconds +
 97+ '.' + milli + 'Z"';
 98+ }
 99+ if ( o.constructor === Array ) {
 100+ var ret = [];
 101+ for ( var i = 0; i < o.length; i++ ) {
 102+ ret.push( $.toJSON( o[i] ) || 'null' );
 103+ }
 104+ return '[' + ret.join(',') + ']';
 105+ }
 106+ var name,
 107+ val,
 108+ pairs = [];
 109+ for ( var k in o ) {
 110+ type = typeof k;
 111+ if ( type === 'number' ) {
 112+ name = '"' + k + '"';
 113+ } else if (type === 'string') {
 114+ name = $.quoteString(k);
 115+ } else {
 116+ // Keys must be numerical or string. Skip others
 117+ continue;
 118+ }
 119+ type = typeof o[k];
 120+
 121+ if ( type === 'function' || type === 'undefined' ) {
 122+ // Invalid values like these return undefined
 123+ // from toJSON, however those object members
 124+ // shouldn't be included in the JSON string at all.
 125+ continue;
 126+ }
 127+ val = $.toJSON( o[k] );
 128+ pairs.push( name + ':' + val );
 129+ }
 130+ return '{' + pairs.join( ',' ) + '}';
 131+ }
 132+ };
 133+
 134+ /**
 135+ * jQuery.evalJSON
 136+ * Evaluates a given piece of json source.
 137+ *
 138+ * @param src {String}
 139+ */
 140+ $.evalJSON = typeof JSON === 'object' && JSON.parse
 141+ ? JSON.parse
 142+ : function( src ) {
 143+ return eval('(' + src + ')');
 144+ };
 145+
 146+ /**
 147+ * jQuery.secureEvalJSON
 148+ * Evals JSON in a way that is *more* secure.
 149+ *
 150+ * @param src {String}
 151+ */
 152+ $.secureEvalJSON = typeof JSON === 'object' && JSON.parse
 153+ ? JSON.parse
 154+ : function( src ) {
 155+
 156+ var filtered =
 157+ src
 158+ .replace( /\\["\\\/bfnrtu]/g, '@' )
 159+ .replace( /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
 160+ .replace( /(?:^|:|,)(?:\s*\[)+/g, '');
 161+
 162+ if ( /^[\],:{}\s]*$/.test( filtered ) ) {
 163+ return eval( '(' + src + ')' );
 164+ } else {
 165+ throw new SyntaxError( 'Error parsing JSON, source is not valid.' );
 166+ }
 167+ };
 168+
 169+ /**
 170+ * jQuery.quoteString
 171+ * Returns a string-repr of a string, escaping quotes intelligently.
 172+ * Mostly a support function for toJSON.
 173+ * Examples:
 174+ * >>> jQuery.quoteString('apple')
 175+ * "apple"
 176+ *
 177+ * >>> jQuery.quoteString('"Where are we going?", she asked.')
 178+ * "\"Where are we going?\", she asked."
 179+ */
 180+ $.quoteString = function( string ) {
 181+ if ( string.match( escapeable ) ) {
 182+ return '"' + string.replace( escapeable, function( a ) {
 183+ var c = meta[a];
 184+ if ( typeof c === 'string' ) {
 185+ return c;
 186+ }
 187+ c = a.charCodeAt();
 188+ return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
 189+ }) + '"';
 190+ }
 191+ return '"' + string + '"';
 192+ };
 193+
 194+})( jQuery );

Follow-up revisions

RevisionCommit summaryAuthorDate
r112910release-noteskrinkle19:26, 2 March 2012

Status & tagging log