jss 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +20 -0
- data/lib/jss.rb +7 -0
- data/lib/jss/engine.rb +6 -0
- data/lib/jss/version.rb +5 -0
- data/vendor/assets/javascripts/html5shiv.js +299 -0
- data/vendor/assets/javascripts/json.js +529 -0
- data/vendor/assets/javascripts/json2.js +486 -0
- metadata +54 -0
data/README.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# Rails JavaScriptS collection
|
2
|
+
json2, html5shiv and other fundamental javascripts libraries with Rails asset pipeline
|
3
|
+
|
4
|
+
|
5
|
+
## Install
|
6
|
+
|
7
|
+
gem "jss"
|
8
|
+
|
9
|
+
## Require
|
10
|
+
|
11
|
+
Add the following to your `app/assets/javascripts/application.js`:
|
12
|
+
|
13
|
+
[json2](https://github.com/douglascrockford/JSON-js)
|
14
|
+
|
15
|
+
//= require json
|
16
|
+
//= require json2
|
17
|
+
|
18
|
+
[html5shiv](https://github.com/aFarkas/html5shiv)
|
19
|
+
|
20
|
+
//= require html5shiv
|
data/lib/jss.rb
ADDED
data/lib/jss/engine.rb
ADDED
data/lib/jss/version.rb
ADDED
@@ -0,0 +1,299 @@
|
|
1
|
+
/**
|
2
|
+
* @preserve HTML5 Shiv v3.6.2pre | @afarkas @jdalton @jon_neal @rem | MIT/GPL2 Licensed
|
3
|
+
*/
|
4
|
+
;(function(window, document) {
|
5
|
+
/*jshint evil:true */
|
6
|
+
/** version */
|
7
|
+
var version = '3.6.2pre';
|
8
|
+
|
9
|
+
/** Preset options */
|
10
|
+
var options = window.html5 || {};
|
11
|
+
|
12
|
+
/** Used to skip problem elements */
|
13
|
+
var reSkip = /^<|^(?:button|map|select|textarea|object|iframe|option|optgroup)$/i;
|
14
|
+
|
15
|
+
/** Not all elements can be cloned in IE **/
|
16
|
+
var saveClones = /^(?:a|b|code|div|fieldset|h1|h2|h3|h4|h5|h6|i|label|li|ol|p|q|span|strong|style|table|tbody|td|th|tr|ul)$/i;
|
17
|
+
|
18
|
+
/** Detect whether the browser supports default html5 styles */
|
19
|
+
var supportsHtml5Styles;
|
20
|
+
|
21
|
+
/** Name of the expando, to work with multiple documents or to re-shiv one document */
|
22
|
+
var expando = '_html5shiv';
|
23
|
+
|
24
|
+
/** The id for the the documents expando */
|
25
|
+
var expanID = 0;
|
26
|
+
|
27
|
+
/** Cached data for each document */
|
28
|
+
var expandoData = {};
|
29
|
+
|
30
|
+
/** Detect whether the browser supports unknown elements */
|
31
|
+
var supportsUnknownElements;
|
32
|
+
|
33
|
+
(function() {
|
34
|
+
try {
|
35
|
+
var a = document.createElement('a');
|
36
|
+
a.innerHTML = '<xyz></xyz>';
|
37
|
+
//if the hidden property is implemented we can assume, that the browser supports basic HTML5 Styles
|
38
|
+
supportsHtml5Styles = ('hidden' in a);
|
39
|
+
|
40
|
+
supportsUnknownElements = a.childNodes.length == 1 || (function() {
|
41
|
+
// assign a false positive if unable to shiv
|
42
|
+
(document.createElement)('a');
|
43
|
+
var frag = document.createDocumentFragment();
|
44
|
+
return (
|
45
|
+
typeof frag.cloneNode == 'undefined' ||
|
46
|
+
typeof frag.createDocumentFragment == 'undefined' ||
|
47
|
+
typeof frag.createElement == 'undefined'
|
48
|
+
);
|
49
|
+
}());
|
50
|
+
} catch(e) {
|
51
|
+
// assign a false positive if detection fails => unable to shiv
|
52
|
+
supportsHtml5Styles = true;
|
53
|
+
supportsUnknownElements = true;
|
54
|
+
}
|
55
|
+
|
56
|
+
}());
|
57
|
+
|
58
|
+
/*--------------------------------------------------------------------------*/
|
59
|
+
|
60
|
+
/**
|
61
|
+
* Creates a style sheet with the given CSS text and adds it to the document.
|
62
|
+
* @private
|
63
|
+
* @param {Document} ownerDocument The document.
|
64
|
+
* @param {String} cssText The CSS text.
|
65
|
+
* @returns {StyleSheet} The style element.
|
66
|
+
*/
|
67
|
+
function addStyleSheet(ownerDocument, cssText) {
|
68
|
+
var p = ownerDocument.createElement('p'),
|
69
|
+
parent = ownerDocument.getElementsByTagName('head')[0] || ownerDocument.documentElement;
|
70
|
+
|
71
|
+
p.innerHTML = 'x<style>' + cssText + '</style>';
|
72
|
+
return parent.insertBefore(p.lastChild, parent.firstChild);
|
73
|
+
}
|
74
|
+
|
75
|
+
/**
|
76
|
+
* Returns the value of `html5.elements` as an array.
|
77
|
+
* @private
|
78
|
+
* @returns {Array} An array of shived element node names.
|
79
|
+
*/
|
80
|
+
function getElements() {
|
81
|
+
var elements = html5.elements;
|
82
|
+
return typeof elements == 'string' ? elements.split(' ') : elements;
|
83
|
+
}
|
84
|
+
|
85
|
+
/**
|
86
|
+
* Returns the data associated to the given document
|
87
|
+
* @private
|
88
|
+
* @param {Document} ownerDocument The document.
|
89
|
+
* @returns {Object} An object of data.
|
90
|
+
*/
|
91
|
+
function getExpandoData(ownerDocument) {
|
92
|
+
var data = expandoData[ownerDocument[expando]];
|
93
|
+
if (!data) {
|
94
|
+
data = {};
|
95
|
+
expanID++;
|
96
|
+
ownerDocument[expando] = expanID;
|
97
|
+
expandoData[expanID] = data;
|
98
|
+
}
|
99
|
+
return data;
|
100
|
+
}
|
101
|
+
|
102
|
+
/**
|
103
|
+
* returns a shived element for the given nodeName and document
|
104
|
+
* @memberOf html5
|
105
|
+
* @param {String} nodeName name of the element
|
106
|
+
* @param {Document} ownerDocument The context document.
|
107
|
+
* @returns {Object} The shived element.
|
108
|
+
*/
|
109
|
+
function createElement(nodeName, ownerDocument, data){
|
110
|
+
if (!ownerDocument) {
|
111
|
+
ownerDocument = document;
|
112
|
+
}
|
113
|
+
if(supportsUnknownElements){
|
114
|
+
return ownerDocument.createElement(nodeName);
|
115
|
+
}
|
116
|
+
if (!data) {
|
117
|
+
data = getExpandoData(ownerDocument);
|
118
|
+
}
|
119
|
+
var node;
|
120
|
+
|
121
|
+
if (data.cache[nodeName]) {
|
122
|
+
node = data.cache[nodeName].cloneNode();
|
123
|
+
} else if (saveClones.test(nodeName)) {
|
124
|
+
node = (data.cache[nodeName] = data.createElem(nodeName)).cloneNode();
|
125
|
+
} else {
|
126
|
+
node = data.createElem(nodeName);
|
127
|
+
}
|
128
|
+
|
129
|
+
// Avoid adding some elements to fragments in IE < 9 because
|
130
|
+
// * Attributes like `name` or `type` cannot be set/changed once an element
|
131
|
+
// is inserted into a document/fragment
|
132
|
+
// * Link elements with `src` attributes that are inaccessible, as with
|
133
|
+
// a 403 response, will cause the tab/window to crash
|
134
|
+
// * Script elements appended to fragments will execute when their `src`
|
135
|
+
// or `text` property is set
|
136
|
+
return node.canHaveChildren && !reSkip.test(nodeName) ? data.frag.appendChild(node) : node;
|
137
|
+
}
|
138
|
+
|
139
|
+
/**
|
140
|
+
* returns a shived DocumentFragment for the given document
|
141
|
+
* @memberOf html5
|
142
|
+
* @param {Document} ownerDocument The context document.
|
143
|
+
* @returns {Object} The shived DocumentFragment.
|
144
|
+
*/
|
145
|
+
function createDocumentFragment(ownerDocument, data){
|
146
|
+
if (!ownerDocument) {
|
147
|
+
ownerDocument = document;
|
148
|
+
}
|
149
|
+
if(supportsUnknownElements){
|
150
|
+
return ownerDocument.createDocumentFragment();
|
151
|
+
}
|
152
|
+
data = data || getExpandoData(ownerDocument);
|
153
|
+
var clone = data.frag.cloneNode(),
|
154
|
+
i = 0,
|
155
|
+
elems = getElements(),
|
156
|
+
l = elems.length;
|
157
|
+
for(;i<l;i++){
|
158
|
+
clone.createElement(elems[i]);
|
159
|
+
}
|
160
|
+
return clone;
|
161
|
+
}
|
162
|
+
|
163
|
+
/**
|
164
|
+
* Shivs the `createElement` and `createDocumentFragment` methods of the document.
|
165
|
+
* @private
|
166
|
+
* @param {Document|DocumentFragment} ownerDocument The document.
|
167
|
+
* @param {Object} data of the document.
|
168
|
+
*/
|
169
|
+
function shivMethods(ownerDocument, data) {
|
170
|
+
if (!data.cache) {
|
171
|
+
data.cache = {};
|
172
|
+
data.createElem = ownerDocument.createElement;
|
173
|
+
data.createFrag = ownerDocument.createDocumentFragment;
|
174
|
+
data.frag = data.createFrag();
|
175
|
+
}
|
176
|
+
|
177
|
+
|
178
|
+
ownerDocument.createElement = function(nodeName) {
|
179
|
+
//abort shiv
|
180
|
+
if (!html5.shivMethods) {
|
181
|
+
return data.createElem(nodeName);
|
182
|
+
}
|
183
|
+
return createElement(nodeName, ownerDocument, data);
|
184
|
+
};
|
185
|
+
|
186
|
+
ownerDocument.createDocumentFragment = Function('h,f', 'return function(){' +
|
187
|
+
'var n=f.cloneNode(),c=n.createElement;' +
|
188
|
+
'h.shivMethods&&(' +
|
189
|
+
// unroll the `createElement` calls
|
190
|
+
getElements().join().replace(/\w+/g, function(nodeName) {
|
191
|
+
data.createElem(nodeName);
|
192
|
+
data.frag.createElement(nodeName);
|
193
|
+
return 'c("' + nodeName + '")';
|
194
|
+
}) +
|
195
|
+
');return n}'
|
196
|
+
)(html5, data.frag);
|
197
|
+
}
|
198
|
+
|
199
|
+
/*--------------------------------------------------------------------------*/
|
200
|
+
|
201
|
+
/**
|
202
|
+
* Shivs the given document.
|
203
|
+
* @memberOf html5
|
204
|
+
* @param {Document} ownerDocument The document to shiv.
|
205
|
+
* @returns {Document} The shived document.
|
206
|
+
*/
|
207
|
+
function shivDocument(ownerDocument) {
|
208
|
+
if (!ownerDocument) {
|
209
|
+
ownerDocument = document;
|
210
|
+
}
|
211
|
+
var data = getExpandoData(ownerDocument);
|
212
|
+
|
213
|
+
if (html5.shivCSS && !supportsHtml5Styles && !data.hasCSS) {
|
214
|
+
data.hasCSS = !!addStyleSheet(ownerDocument,
|
215
|
+
// corrects block display not defined in IE6/7/8/9
|
216
|
+
'article,aside,figcaption,figure,footer,header,hgroup,main,nav,section{display:block}' +
|
217
|
+
// adds styling not present in IE6/7/8/9
|
218
|
+
'mark{background:#FF0;color:#000}'
|
219
|
+
);
|
220
|
+
}
|
221
|
+
if (!supportsUnknownElements) {
|
222
|
+
shivMethods(ownerDocument, data);
|
223
|
+
}
|
224
|
+
return ownerDocument;
|
225
|
+
}
|
226
|
+
|
227
|
+
/*--------------------------------------------------------------------------*/
|
228
|
+
|
229
|
+
/**
|
230
|
+
* The `html5` object is exposed so that more elements can be shived and
|
231
|
+
* existing shiving can be detected on iframes.
|
232
|
+
* @type Object
|
233
|
+
* @example
|
234
|
+
*
|
235
|
+
* // options can be changed before the script is included
|
236
|
+
* html5 = { 'elements': 'mark section', 'shivCSS': false, 'shivMethods': false };
|
237
|
+
*/
|
238
|
+
var html5 = {
|
239
|
+
|
240
|
+
/**
|
241
|
+
* An array or space separated string of node names of the elements to shiv.
|
242
|
+
* @memberOf html5
|
243
|
+
* @type Array|String
|
244
|
+
*/
|
245
|
+
'elements': options.elements || 'abbr article aside audio bdi canvas data datalist details figcaption figure footer header hgroup main mark meter nav output progress section summary time video',
|
246
|
+
|
247
|
+
/**
|
248
|
+
* current version of html5shiv
|
249
|
+
*/
|
250
|
+
'version': version,
|
251
|
+
|
252
|
+
/**
|
253
|
+
* A flag to indicate that the HTML5 style sheet should be inserted.
|
254
|
+
* @memberOf html5
|
255
|
+
* @type Boolean
|
256
|
+
*/
|
257
|
+
'shivCSS': (options.shivCSS !== false),
|
258
|
+
|
259
|
+
/**
|
260
|
+
* Is equal to true if a browser supports creating unknown/HTML5 elements
|
261
|
+
* @memberOf html5
|
262
|
+
* @type boolean
|
263
|
+
*/
|
264
|
+
'supportsUnknownElements': supportsUnknownElements,
|
265
|
+
|
266
|
+
/**
|
267
|
+
* A flag to indicate that the document's `createElement` and `createDocumentFragment`
|
268
|
+
* methods should be overwritten.
|
269
|
+
* @memberOf html5
|
270
|
+
* @type Boolean
|
271
|
+
*/
|
272
|
+
'shivMethods': (options.shivMethods !== false),
|
273
|
+
|
274
|
+
/**
|
275
|
+
* A string to describe the type of `html5` object ("default" or "default print").
|
276
|
+
* @memberOf html5
|
277
|
+
* @type String
|
278
|
+
*/
|
279
|
+
'type': 'default',
|
280
|
+
|
281
|
+
// shivs the document according to the specified `html5` object options
|
282
|
+
'shivDocument': shivDocument,
|
283
|
+
|
284
|
+
//creates a shived element
|
285
|
+
createElement: createElement,
|
286
|
+
|
287
|
+
//creates a shived documentFragment
|
288
|
+
createDocumentFragment: createDocumentFragment
|
289
|
+
};
|
290
|
+
|
291
|
+
/*--------------------------------------------------------------------------*/
|
292
|
+
|
293
|
+
// expose html5
|
294
|
+
window.html5 = html5;
|
295
|
+
|
296
|
+
// shiv the document
|
297
|
+
shivDocument(document);
|
298
|
+
|
299
|
+
}(this, document));
|
@@ -0,0 +1,529 @@
|
|
1
|
+
/*
|
2
|
+
json.js
|
3
|
+
2012-10-08
|
4
|
+
|
5
|
+
Public Domain
|
6
|
+
|
7
|
+
No warranty expressed or implied. Use at your own risk.
|
8
|
+
|
9
|
+
This file has been superceded by http://www.JSON.org/json2.js
|
10
|
+
|
11
|
+
See http://www.JSON.org/js.html
|
12
|
+
|
13
|
+
This code should be minified before deployment.
|
14
|
+
See http://javascript.crockford.com/jsmin.html
|
15
|
+
|
16
|
+
USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
|
17
|
+
NOT CONTROL.
|
18
|
+
|
19
|
+
This file adds these methods to JavaScript:
|
20
|
+
|
21
|
+
object.toJSONString(whitelist)
|
22
|
+
This method produce a JSON text from a JavaScript value.
|
23
|
+
It must not contain any cyclical references. Illegal values
|
24
|
+
will be excluded.
|
25
|
+
|
26
|
+
The default conversion for dates is to an ISO string. You can
|
27
|
+
add a toJSONString method to any date object to get a different
|
28
|
+
representation.
|
29
|
+
|
30
|
+
The object and array methods can take an optional whitelist
|
31
|
+
argument. A whitelist is an array of strings. If it is provided,
|
32
|
+
keys in objects not found in the whitelist are excluded.
|
33
|
+
|
34
|
+
string.parseJSON(filter)
|
35
|
+
This method parses a JSON text to produce an object or
|
36
|
+
array. It can throw a SyntaxError exception.
|
37
|
+
|
38
|
+
The optional filter parameter is a function which can filter and
|
39
|
+
transform the results. It receives each of the keys and values, and
|
40
|
+
its return value is used instead of the original value. If it
|
41
|
+
returns what it received, then structure is not modified. If it
|
42
|
+
returns undefined then the member is deleted.
|
43
|
+
|
44
|
+
Example:
|
45
|
+
|
46
|
+
// Parse the text. If a key contains the string 'date' then
|
47
|
+
// convert the value to a date.
|
48
|
+
|
49
|
+
myData = text.parseJSON(function (key, value) {
|
50
|
+
return key.indexOf('date') >= 0 ? new Date(value) : value;
|
51
|
+
});
|
52
|
+
|
53
|
+
This file will break programs with improper for..in loops. See
|
54
|
+
http://yuiblog.com/blog/2006/09/26/for-in-intrigue/
|
55
|
+
|
56
|
+
This file creates a global JSON object containing two methods: stringify
|
57
|
+
and parse.
|
58
|
+
|
59
|
+
JSON.stringify(value, replacer, space)
|
60
|
+
value any JavaScript value, usually an object or array.
|
61
|
+
|
62
|
+
replacer an optional parameter that determines how object
|
63
|
+
values are stringified for objects. It can be a
|
64
|
+
function or an array of strings.
|
65
|
+
|
66
|
+
space an optional parameter that specifies the indentation
|
67
|
+
of nested structures. If it is omitted, the text will
|
68
|
+
be packed without extra whitespace. If it is a number,
|
69
|
+
it will specify the number of spaces to indent at each
|
70
|
+
level. If it is a string (such as '\t' or ' '),
|
71
|
+
it contains the characters used to indent at each level.
|
72
|
+
|
73
|
+
This method produces a JSON text from a JavaScript value.
|
74
|
+
|
75
|
+
When an object value is found, if the object contains a toJSON
|
76
|
+
method, its toJSON method will be called and the result will be
|
77
|
+
stringified. A toJSON method does not serialize: it returns the
|
78
|
+
value represented by the name/value pair that should be serialized,
|
79
|
+
or undefined if nothing should be serialized. The toJSON method
|
80
|
+
will be passed the key associated with the value, and this will be
|
81
|
+
bound to the object holding the key.
|
82
|
+
|
83
|
+
For example, this would serialize Dates as ISO strings.
|
84
|
+
|
85
|
+
Date.prototype.toJSON = function (key) {
|
86
|
+
function f(n) {
|
87
|
+
// Format integers to have at least two digits.
|
88
|
+
return n < 10 ? '0' + n : n;
|
89
|
+
}
|
90
|
+
|
91
|
+
return this.getUTCFullYear() + '-' +
|
92
|
+
f(this.getUTCMonth() + 1) + '-' +
|
93
|
+
f(this.getUTCDate()) + 'T' +
|
94
|
+
f(this.getUTCHours()) + ':' +
|
95
|
+
f(this.getUTCMinutes()) + ':' +
|
96
|
+
f(this.getUTCSeconds()) + 'Z';
|
97
|
+
};
|
98
|
+
|
99
|
+
You can provide an optional replacer method. It will be passed the
|
100
|
+
key and value of each member, with this bound to the containing
|
101
|
+
object. The value that is returned from your method will be
|
102
|
+
serialized. If your method returns undefined, then the member will
|
103
|
+
be excluded from the serialization.
|
104
|
+
|
105
|
+
If the replacer parameter is an array of strings, then it will be
|
106
|
+
used to select the members to be serialized. It filters the results
|
107
|
+
such that only members with keys listed in the replacer array are
|
108
|
+
stringified.
|
109
|
+
|
110
|
+
Values that do not have JSON representations, such as undefined or
|
111
|
+
functions, will not be serialized. Such values in objects will be
|
112
|
+
dropped; in arrays they will be replaced with null. You can use
|
113
|
+
a replacer function to replace those with JSON values.
|
114
|
+
JSON.stringify(undefined) returns undefined.
|
115
|
+
|
116
|
+
The optional space parameter produces a stringification of the
|
117
|
+
value that is filled with line breaks and indentation to make it
|
118
|
+
easier to read.
|
119
|
+
|
120
|
+
If the space parameter is a non-empty string, then that string will
|
121
|
+
be used for indentation. If the space parameter is a number, then
|
122
|
+
the indentation will be that many spaces.
|
123
|
+
|
124
|
+
Example:
|
125
|
+
|
126
|
+
text = JSON.stringify(['e', {pluribus: 'unum'}]);
|
127
|
+
// text is '["e",{"pluribus":"unum"}]'
|
128
|
+
|
129
|
+
|
130
|
+
text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t');
|
131
|
+
// text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
|
132
|
+
|
133
|
+
text = JSON.stringify([new Date()], function (key, value) {
|
134
|
+
return this[key] instanceof Date ?
|
135
|
+
'Date(' + this[key] + ')' : value;
|
136
|
+
});
|
137
|
+
// text is '["Date(---current time---)"]'
|
138
|
+
|
139
|
+
|
140
|
+
JSON.parse(text, reviver)
|
141
|
+
This method parses a JSON text to produce an object or array.
|
142
|
+
It can throw a SyntaxError exception.
|
143
|
+
|
144
|
+
The optional reviver parameter is a function that can filter and
|
145
|
+
transform the results. It receives each of the keys and values,
|
146
|
+
and its return value is used instead of the original value.
|
147
|
+
If it returns what it received, then the structure is not modified.
|
148
|
+
If it returns undefined then the member is deleted.
|
149
|
+
|
150
|
+
Example:
|
151
|
+
|
152
|
+
// Parse the text. Values that look like ISO date strings will
|
153
|
+
// be converted to Date objects.
|
154
|
+
|
155
|
+
myData = JSON.parse(text, function (key, value) {
|
156
|
+
var a;
|
157
|
+
if (typeof value === 'string') {
|
158
|
+
a =
|
159
|
+
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
|
160
|
+
if (a) {
|
161
|
+
return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
|
162
|
+
+a[5], +a[6]));
|
163
|
+
}
|
164
|
+
}
|
165
|
+
return value;
|
166
|
+
});
|
167
|
+
|
168
|
+
myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) {
|
169
|
+
var d;
|
170
|
+
if (typeof value === 'string' &&
|
171
|
+
value.slice(0, 5) === 'Date(' &&
|
172
|
+
value.slice(-1) === ')') {
|
173
|
+
d = new Date(value.slice(5, -1));
|
174
|
+
if (d) {
|
175
|
+
return d;
|
176
|
+
}
|
177
|
+
}
|
178
|
+
return value;
|
179
|
+
});
|
180
|
+
|
181
|
+
|
182
|
+
This is a reference implementation. You are free to copy, modify, or
|
183
|
+
redistribute.
|
184
|
+
*/
|
185
|
+
|
186
|
+
/*jslint evil: true, regexp: true, unparam: true */
|
187
|
+
|
188
|
+
/*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply,
|
189
|
+
call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
|
190
|
+
getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
|
191
|
+
lastIndex, length, parse, parseJSON, prototype, push, replace, slice,
|
192
|
+
stringify, test, toJSON, toJSONString, toString, valueOf
|
193
|
+
*/
|
194
|
+
|
195
|
+
|
196
|
+
// Create a JSON object only if one does not already exist. We create the
|
197
|
+
// methods in a closure to avoid creating global variables.
|
198
|
+
|
199
|
+
if (typeof JSON !== 'object') {
|
200
|
+
JSON = {};
|
201
|
+
}
|
202
|
+
|
203
|
+
(function () {
|
204
|
+
'use strict';
|
205
|
+
|
206
|
+
function f(n) {
|
207
|
+
// Format integers to have at least two digits.
|
208
|
+
return n < 10 ? '0' + n : n;
|
209
|
+
}
|
210
|
+
|
211
|
+
if (typeof Date.prototype.toJSON !== 'function') {
|
212
|
+
|
213
|
+
Date.prototype.toJSON = function (key) {
|
214
|
+
|
215
|
+
return isFinite(this.valueOf()) ?
|
216
|
+
this.getUTCFullYear() + '-' +
|
217
|
+
f(this.getUTCMonth() + 1) + '-' +
|
218
|
+
f(this.getUTCDate()) + 'T' +
|
219
|
+
f(this.getUTCHours()) + ':' +
|
220
|
+
f(this.getUTCMinutes()) + ':' +
|
221
|
+
f(this.getUTCSeconds()) + 'Z' : null;
|
222
|
+
};
|
223
|
+
|
224
|
+
String.prototype.toJSON =
|
225
|
+
Number.prototype.toJSON =
|
226
|
+
Boolean.prototype.toJSON = function (key) {
|
227
|
+
return this.valueOf();
|
228
|
+
};
|
229
|
+
}
|
230
|
+
|
231
|
+
var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
|
232
|
+
escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
|
233
|
+
gap,
|
234
|
+
indent,
|
235
|
+
meta = { // table of character substitutions
|
236
|
+
'\b': '\\b',
|
237
|
+
'\t': '\\t',
|
238
|
+
'\n': '\\n',
|
239
|
+
'\f': '\\f',
|
240
|
+
'\r': '\\r',
|
241
|
+
'"' : '\\"',
|
242
|
+
'\\': '\\\\'
|
243
|
+
},
|
244
|
+
rep;
|
245
|
+
|
246
|
+
|
247
|
+
function quote(string) {
|
248
|
+
|
249
|
+
// If the string contains no control characters, no quote characters, and no
|
250
|
+
// backslash characters, then we can safely slap some quotes around it.
|
251
|
+
// Otherwise we must also replace the offending characters with safe escape
|
252
|
+
// sequences.
|
253
|
+
|
254
|
+
escapable.lastIndex = 0;
|
255
|
+
return escapable.test(string) ? '"' + string.replace(escapable, function (a) {
|
256
|
+
var c = meta[a];
|
257
|
+
return typeof c === 'string' ? c :
|
258
|
+
'\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
|
259
|
+
}) + '"' : '"' + string + '"';
|
260
|
+
}
|
261
|
+
|
262
|
+
|
263
|
+
function str(key, holder) {
|
264
|
+
|
265
|
+
// Produce a string from holder[key].
|
266
|
+
|
267
|
+
var i, // The loop counter.
|
268
|
+
k, // The member key.
|
269
|
+
v, // The member value.
|
270
|
+
length,
|
271
|
+
mind = gap,
|
272
|
+
partial,
|
273
|
+
value = holder[key];
|
274
|
+
|
275
|
+
// If the value has a toJSON method, call it to obtain a replacement value.
|
276
|
+
|
277
|
+
if (value && typeof value === 'object' &&
|
278
|
+
typeof value.toJSON === 'function') {
|
279
|
+
value = value.toJSON(key);
|
280
|
+
}
|
281
|
+
|
282
|
+
// If we were called with a replacer function, then call the replacer to
|
283
|
+
// obtain a replacement value.
|
284
|
+
|
285
|
+
if (typeof rep === 'function') {
|
286
|
+
value = rep.call(holder, key, value);
|
287
|
+
}
|
288
|
+
|
289
|
+
// What happens next depends on the value's type.
|
290
|
+
|
291
|
+
switch (typeof value) {
|
292
|
+
case 'string':
|
293
|
+
return quote(value);
|
294
|
+
|
295
|
+
case 'number':
|
296
|
+
|
297
|
+
// JSON numbers must be finite. Encode non-finite numbers as null.
|
298
|
+
|
299
|
+
return isFinite(value) ? String(value) : 'null';
|
300
|
+
|
301
|
+
case 'boolean':
|
302
|
+
case 'null':
|
303
|
+
|
304
|
+
// If the value is a boolean or null, convert it to a string. Note:
|
305
|
+
// typeof null does not produce 'null'. The case is included here in
|
306
|
+
// the remote chance that this gets fixed someday.
|
307
|
+
|
308
|
+
return String(value);
|
309
|
+
|
310
|
+
// If the type is 'object', we might be dealing with an object or an array or
|
311
|
+
// null.
|
312
|
+
|
313
|
+
case 'object':
|
314
|
+
|
315
|
+
// Due to a specification blunder in ECMAScript, typeof null is 'object',
|
316
|
+
// so watch out for that case.
|
317
|
+
|
318
|
+
if (!value) {
|
319
|
+
return 'null';
|
320
|
+
}
|
321
|
+
|
322
|
+
// Make an array to hold the partial results of stringifying this object value.
|
323
|
+
|
324
|
+
gap += indent;
|
325
|
+
partial = [];
|
326
|
+
|
327
|
+
// Is the value an array?
|
328
|
+
|
329
|
+
if (Object.prototype.toString.apply(value) === '[object Array]') {
|
330
|
+
|
331
|
+
// The value is an array. Stringify every element. Use null as a placeholder
|
332
|
+
// for non-JSON values.
|
333
|
+
|
334
|
+
length = value.length;
|
335
|
+
for (i = 0; i < length; i += 1) {
|
336
|
+
partial[i] = str(i, value) || 'null';
|
337
|
+
}
|
338
|
+
|
339
|
+
// Join all of the elements together, separated with commas, and wrap them in
|
340
|
+
// brackets.
|
341
|
+
|
342
|
+
v = partial.length === 0 ? '[]' : gap ?
|
343
|
+
'[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' :
|
344
|
+
'[' + partial.join(',') + ']';
|
345
|
+
gap = mind;
|
346
|
+
return v;
|
347
|
+
}
|
348
|
+
|
349
|
+
// If the replacer is an array, use it to select the members to be stringified.
|
350
|
+
|
351
|
+
if (rep && typeof rep === 'object') {
|
352
|
+
length = rep.length;
|
353
|
+
for (i = 0; i < length; i += 1) {
|
354
|
+
k = rep[i];
|
355
|
+
if (typeof k === 'string') {
|
356
|
+
v = str(k, value);
|
357
|
+
if (v) {
|
358
|
+
partial.push(quote(k) + (gap ? ': ' : ':') + v);
|
359
|
+
}
|
360
|
+
}
|
361
|
+
}
|
362
|
+
} else {
|
363
|
+
|
364
|
+
// Otherwise, iterate through all of the keys in the object.
|
365
|
+
|
366
|
+
for (k in value) {
|
367
|
+
if (Object.prototype.hasOwnProperty.call(value, k)) {
|
368
|
+
v = str(k, value);
|
369
|
+
if (v) {
|
370
|
+
partial.push(quote(k) + (gap ? ': ' : ':') + v);
|
371
|
+
}
|
372
|
+
}
|
373
|
+
}
|
374
|
+
}
|
375
|
+
|
376
|
+
// Join all of the member texts together, separated with commas,
|
377
|
+
// and wrap them in braces.
|
378
|
+
|
379
|
+
v = partial.length === 0 ? '{}' : gap ?
|
380
|
+
'{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' :
|
381
|
+
'{' + partial.join(',') + '}';
|
382
|
+
gap = mind;
|
383
|
+
return v;
|
384
|
+
}
|
385
|
+
}
|
386
|
+
|
387
|
+
// If the JSON object does not yet have a stringify method, give it one.
|
388
|
+
|
389
|
+
if (typeof JSON.stringify !== 'function') {
|
390
|
+
JSON.stringify = function (value, replacer, space) {
|
391
|
+
|
392
|
+
// The stringify method takes a value and an optional replacer, and an optional
|
393
|
+
// space parameter, and returns a JSON text. The replacer can be a function
|
394
|
+
// that can replace values, or an array of strings that will select the keys.
|
395
|
+
// A default replacer method can be provided. Use of the space parameter can
|
396
|
+
// produce text that is more easily readable.
|
397
|
+
|
398
|
+
var i;
|
399
|
+
gap = '';
|
400
|
+
indent = '';
|
401
|
+
|
402
|
+
// If the space parameter is a number, make an indent string containing that
|
403
|
+
// many spaces.
|
404
|
+
|
405
|
+
if (typeof space === 'number') {
|
406
|
+
for (i = 0; i < space; i += 1) {
|
407
|
+
indent += ' ';
|
408
|
+
}
|
409
|
+
|
410
|
+
// If the space parameter is a string, it will be used as the indent string.
|
411
|
+
|
412
|
+
} else if (typeof space === 'string') {
|
413
|
+
indent = space;
|
414
|
+
}
|
415
|
+
|
416
|
+
// If there is a replacer, it must be a function or an array.
|
417
|
+
// Otherwise, throw an error.
|
418
|
+
|
419
|
+
rep = replacer;
|
420
|
+
if (replacer && typeof replacer !== 'function' &&
|
421
|
+
(typeof replacer !== 'object' ||
|
422
|
+
typeof replacer.length !== 'number')) {
|
423
|
+
throw new Error('JSON.stringify');
|
424
|
+
}
|
425
|
+
|
426
|
+
// Make a fake root object containing our value under the key of ''.
|
427
|
+
// Return the result of stringifying the value.
|
428
|
+
|
429
|
+
return str('', {'': value});
|
430
|
+
};
|
431
|
+
}
|
432
|
+
|
433
|
+
|
434
|
+
// If the JSON object does not yet have a parse method, give it one.
|
435
|
+
|
436
|
+
if (typeof JSON.parse !== 'function') {
|
437
|
+
JSON.parse = function (text, reviver) {
|
438
|
+
|
439
|
+
// The parse method takes a text and an optional reviver function, and returns
|
440
|
+
// a JavaScript value if the text is a valid JSON text.
|
441
|
+
|
442
|
+
var j;
|
443
|
+
|
444
|
+
function walk(holder, key) {
|
445
|
+
|
446
|
+
// The walk method is used to recursively walk the resulting structure so
|
447
|
+
// that modifications can be made.
|
448
|
+
|
449
|
+
var k, v, value = holder[key];
|
450
|
+
if (value && typeof value === 'object') {
|
451
|
+
for (k in value) {
|
452
|
+
if (Object.prototype.hasOwnProperty.call(value, k)) {
|
453
|
+
v = walk(value, k);
|
454
|
+
if (v !== undefined) {
|
455
|
+
value[k] = v;
|
456
|
+
} else {
|
457
|
+
delete value[k];
|
458
|
+
}
|
459
|
+
}
|
460
|
+
}
|
461
|
+
}
|
462
|
+
return reviver.call(holder, key, value);
|
463
|
+
}
|
464
|
+
|
465
|
+
|
466
|
+
// Parsing happens in four stages. In the first stage, we replace certain
|
467
|
+
// Unicode characters with escape sequences. JavaScript handles many characters
|
468
|
+
// incorrectly, either silently deleting them, or treating them as line endings.
|
469
|
+
|
470
|
+
text = String(text);
|
471
|
+
cx.lastIndex = 0;
|
472
|
+
if (cx.test(text)) {
|
473
|
+
text = text.replace(cx, function (a) {
|
474
|
+
return '\\u' +
|
475
|
+
('0000' + a.charCodeAt(0).toString(16)).slice(-4);
|
476
|
+
});
|
477
|
+
}
|
478
|
+
|
479
|
+
// In the second stage, we run the text against regular expressions that look
|
480
|
+
// for non-JSON patterns. We are especially concerned with '()' and 'new'
|
481
|
+
// because they can cause invocation, and '=' because it can cause mutation.
|
482
|
+
// But just to be safe, we want to reject all unexpected forms.
|
483
|
+
|
484
|
+
// We split the second stage into 4 regexp operations in order to work around
|
485
|
+
// crippling inefficiencies in IE's and Safari's regexp engines. First we
|
486
|
+
// replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
|
487
|
+
// replace all simple value tokens with ']' characters. Third, we delete all
|
488
|
+
// open brackets that follow a colon or comma or that begin the text. Finally,
|
489
|
+
// we look to see that the remaining characters are only whitespace or ']' or
|
490
|
+
// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
|
491
|
+
|
492
|
+
if (/^[\],:{}\s]*$/
|
493
|
+
.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@')
|
494
|
+
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
|
495
|
+
.replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
|
496
|
+
|
497
|
+
// In the third stage we use the eval function to compile the text into a
|
498
|
+
// JavaScript structure. The '{' operator is subject to a syntactic ambiguity
|
499
|
+
// in JavaScript: it can begin a block or an object literal. We wrap the text
|
500
|
+
// in parens to eliminate the ambiguity.
|
501
|
+
|
502
|
+
j = eval('(' + text + ')');
|
503
|
+
|
504
|
+
// In the optional fourth stage, we recursively walk the new structure, passing
|
505
|
+
// each name/value pair to a reviver function for possible transformation.
|
506
|
+
|
507
|
+
return typeof reviver === 'function' ?
|
508
|
+
walk({'': j}, '') : j;
|
509
|
+
}
|
510
|
+
|
511
|
+
// If the text is not JSON parseable, then a SyntaxError is thrown.
|
512
|
+
|
513
|
+
throw new SyntaxError('JSON.parse');
|
514
|
+
};
|
515
|
+
}
|
516
|
+
|
517
|
+
// Augment the basic prototypes if they have not already been augmented.
|
518
|
+
// These forms are obsolete. It is recommended that JSON.stringify and
|
519
|
+
// JSON.parse be used instead.
|
520
|
+
|
521
|
+
if (!Object.prototype.toJSONString) {
|
522
|
+
Object.prototype.toJSONString = function (filter) {
|
523
|
+
return JSON.stringify(this, filter);
|
524
|
+
};
|
525
|
+
Object.prototype.parseJSON = function (filter) {
|
526
|
+
return JSON.parse(this, filter);
|
527
|
+
};
|
528
|
+
}
|
529
|
+
}());
|