Index: trunk/extensions/InlineEditor/jquery.json.js |
— | — | @@ -0,0 +1,181 @@ |
| 2 | +/* |
| 3 | + * jQuery JSON Plugin |
| 4 | + * version: 2.1 (2009-08-14) |
| 5 | + * |
| 6 | + * This document is licensed as free software under the terms of the |
| 7 | + * MIT License: http://www.opensource.org/licenses/mit-license.php |
| 8 | + * |
| 9 | + * Brantley Harris wrote this plugin. It is based somewhat on the JSON.org |
| 10 | + * website's http://www.json.org/json2.js, which proclaims: |
| 11 | + * "NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.", a sentiment that |
| 12 | + * I uphold. |
| 13 | + * |
| 14 | + * It is also influenced heavily by MochiKit's serializeJSON, which is |
| 15 | + * copyrighted 2005 by Bob Ippolito. |
| 16 | + * |
| 17 | + * The script has been modified by Jan Paul Posma to always use $.secureEvalJSON. |
| 18 | + */ |
| 19 | + |
| 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 | + return $.secureEvalJSON(src); |
| 121 | + //if (typeof(JSON) == 'object' && JSON.parse) |
| 122 | + // return JSON.parse(src); |
| 123 | + //return eval("(" + src + ")"); |
| 124 | + }; |
| 125 | + |
| 126 | + /** jQuery.secureEvalJSON(src) |
| 127 | + Evals JSON in a way that is *more* secure. |
| 128 | + **/ |
| 129 | + $.secureEvalJSON = function(src) |
| 130 | + { |
| 131 | + if (typeof(JSON) == 'object' && JSON.parse) |
| 132 | + return JSON.parse(src); |
| 133 | + |
| 134 | + var filtered = src; |
| 135 | + filtered = filtered.replace(/\\["\\\/bfnrtu]/g, '@'); |
| 136 | + filtered = filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']'); |
| 137 | + filtered = filtered.replace(/(?:^|:|,)(?:\s*\[)+/g, ''); |
| 138 | + |
| 139 | + if (/^[\],:{}\s]*$/.test(filtered)) |
| 140 | + return eval("(" + src + ")"); |
| 141 | + else |
| 142 | + throw new SyntaxError("Error parsing JSON, source is not valid."); |
| 143 | + }; |
| 144 | + |
| 145 | + /** jQuery.quoteString(string) |
| 146 | + Returns a string-repr of a string, escaping quotes intelligently. |
| 147 | + Mostly a support function for toJSON. |
| 148 | + |
| 149 | + Examples: |
| 150 | + >>> jQuery.quoteString("apple") |
| 151 | + "apple" |
| 152 | + |
| 153 | + >>> jQuery.quoteString('"Where are we going?", she asked.') |
| 154 | + "\"Where are we going?\", she asked." |
| 155 | + **/ |
| 156 | + $.quoteString = function(string) |
| 157 | + { |
| 158 | + if (string.match(_escapeable)) |
| 159 | + { |
| 160 | + return '"' + string.replace(_escapeable, function (a) |
| 161 | + { |
| 162 | + var c = _meta[a]; |
| 163 | + if (typeof c === 'string') return c; |
| 164 | + c = a.charCodeAt(); |
| 165 | + return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16); |
| 166 | + }) + '"'; |
| 167 | + } |
| 168 | + return '"' + string + '"'; |
| 169 | + }; |
| 170 | + |
| 171 | + var _escapeable = /["\\\x00-\x1f\x7f-\x9f]/g; |
| 172 | + |
| 173 | + var _meta = { |
| 174 | + '\b': '\\b', |
| 175 | + '\t': '\\t', |
| 176 | + '\n': '\\n', |
| 177 | + '\f': '\\f', |
| 178 | + '\r': '\\r', |
| 179 | + '"' : '\\"', |
| 180 | + '\\': '\\\\' |
| 181 | + }; |
| 182 | +})(jQuery); |
Property changes on: trunk/extensions/InlineEditor/jquery.json.js |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 183 | + native |
Index: trunk/extensions/InlineEditor/InlineEditor.php |
— | — | @@ -77,6 +77,7 @@ |
78 | 78 | 'dependencies' => array( |
79 | 79 | 'jquery.color', |
80 | 80 | 'mediawiki.util', |
| 81 | + 'jquery.json', |
81 | 82 | ), |
82 | 83 | ), |
83 | 84 | 'jquery.inlineEditor.editors.basic' => $inlineEditorTpl + array( |
— | — | @@ -99,4 +100,7 @@ |
100 | 101 | 'jquery.elastic' => $inlineEditorTpl + array( |
101 | 102 | 'scripts' => 'jquery.elastic.js', |
102 | 103 | ), |
| 104 | + 'jquery.json' => $inlineEditorTpl + array( |
| 105 | + 'scripts' => 'jquery.json.js', |
| 106 | + ), |
103 | 107 | ); |
Index: trunk/extensions/InlineEditor/jquery.inlineEditor.js |
— | — | @@ -39,7 +39,7 @@ |
40 | 40 | 'lastEdit': { 'id': id, 'text': text } |
41 | 41 | }; |
42 | 42 | |
43 | | - var args = [ JSON.stringify( data ), wgPageName ]; |
| 43 | + var args = [ $.toJSON( data ), wgPageName ]; |
44 | 44 | sajax_request_type = 'POST'; |
45 | 45 | sajax_do_call( 'InlineEditor::ajaxPreview', args, $.inlineEditor.addNewState ); |
46 | 46 | }, |
— | — | @@ -48,7 +48,7 @@ |
49 | 49 | * Adds a new state from an AJAX request. |
50 | 50 | */ |
51 | 51 | addNewState: function( request ) { |
52 | | - var state = JSON.parse( request.responseText ); |
| 52 | + var state = $.parseJSON( request.responseText ); |
53 | 53 | |
54 | 54 | // restore the html to the current state, instantly remove the lastEdit, |
55 | 55 | // and then add the new html |
— | — | @@ -180,7 +180,7 @@ |
181 | 181 | var data = { |
182 | 182 | 'object': $.inlineEditor.states[$.inlineEditor.currentState].object |
183 | 183 | }; |
184 | | - var json = JSON.stringify( data ); |
| 184 | + var json = $.toJSON( data ); |
185 | 185 | |
186 | 186 | // set and send the form |
187 | 187 | $( '#json' ).val( json ); |