govuk_template 0.3.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/LICENCE.txt +22 -0
  2. data/README.md +61 -0
  3. data/app/assets/images/apple-touch-icon-114x114.png +0 -0
  4. data/app/assets/images/apple-touch-icon-144x144.png +0 -0
  5. data/app/assets/images/apple-touch-icon-57x57.png +0 -0
  6. data/app/assets/images/apple-touch-icon-72x72.png +0 -0
  7. data/app/assets/images/favicon.ico +0 -0
  8. data/app/assets/images/gov.uk_logotype-2x.png +0 -0
  9. data/app/assets/images/gov.uk_logotype_crown.png +0 -0
  10. data/app/assets/images/gov.uk_logotype_crown_invert.png +0 -0
  11. data/app/assets/images/gov.uk_logotype_crown_invert_trans.png +0 -0
  12. data/app/assets/images/open-government-licence_2x.png +0 -0
  13. data/app/assets/images/opengraph-image.png +0 -0
  14. data/app/assets/javascripts/govuk-template.js +97 -0
  15. data/app/assets/javascripts/ie.js +512 -0
  16. data/app/assets/javascripts/vendor/goog/webfont-debug.js +2126 -0
  17. data/app/assets/stylesheets/external-link-24x24.png +0 -0
  18. data/app/assets/stylesheets/external-link-black-12x12.png +0 -0
  19. data/app/assets/stylesheets/external-link-black-24x24.png +0 -0
  20. data/app/assets/stylesheets/external-link.png +0 -0
  21. data/app/assets/stylesheets/fonts-ie8.css.erb +30 -0
  22. data/app/assets/stylesheets/fonts.css.erb +30 -0
  23. data/app/assets/stylesheets/fonts/NTA-Bold-20130221.eot +0 -0
  24. data/app/assets/stylesheets/fonts/NTA-Bold-20130221.woff +0 -0
  25. data/app/assets/stylesheets/fonts/NTA-Light-20130221.eot +0 -0
  26. data/app/assets/stylesheets/fonts/NTA-Light-20130221.woff +0 -0
  27. data/app/assets/stylesheets/fonts/NTATabularNumbers-Bold.eot +0 -0
  28. data/app/assets/stylesheets/fonts/NTATabularNumbers-Bold.woff +0 -0
  29. data/app/assets/stylesheets/fonts/NTATabularNumbers-Light.eot +0 -0
  30. data/app/assets/stylesheets/fonts/NTATabularNumbers-Light.woff +0 -0
  31. data/app/assets/stylesheets/govuk-template-ie6.css.erb +548 -0
  32. data/app/assets/stylesheets/govuk-template-ie7.css.erb +544 -0
  33. data/app/assets/stylesheets/govuk-template-ie8.css.erb +527 -0
  34. data/app/assets/stylesheets/govuk-template-print.css.erb +78 -0
  35. data/app/assets/stylesheets/govuk-template.css.erb +575 -0
  36. data/app/assets/stylesheets/images/close.png +0 -0
  37. data/app/assets/stylesheets/images/govuk-crest-2x.png +0 -0
  38. data/app/assets/stylesheets/images/govuk-crest-ie.png +0 -0
  39. data/app/assets/stylesheets/images/govuk-crest.png +0 -0
  40. data/app/assets/stylesheets/images/separator-2x.png +0 -0
  41. data/app/assets/stylesheets/images/separator.png +0 -0
  42. data/app/views/layouts/govuk_template.html.erb +127 -0
  43. data/lib/govuk_template.rb +5 -0
  44. data/lib/govuk_template/engine.rb +13 -0
  45. data/lib/govuk_template/version.rb +3 -0
  46. metadata +209 -0
data/LICENCE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 GDS
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # GovukTemplate
2
+
3
+ This provides a template containing the GOV.UK header and footer, and associated assets.
4
+
5
+ This is versioned following [Semantic Versioning](http://semver.org).
6
+
7
+ ## Packaging
8
+
9
+ At present this generates 3 output formats, a gem containing a Rails engine, a tarball containing Play Framework templates and a tarball.
10
+
11
+ ### Gem version
12
+
13
+ This is available on rubygems.org. To use it, add this line to your application's Gemfile:
14
+
15
+ gem 'govuk_template'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ You can then use the `govuk_template` layout in your app. If you need to extend the layout you can use [nested layouts](http://guides.rubyonrails.org/layouts_and_rendering.html#using-nested-layouts).
22
+
23
+ ### Play version
24
+
25
+ To generate the tarball of Play Framework templates run `build:play` rake task. This will produce a tarball in the `pkg` directory.
26
+
27
+ ### Tarball version
28
+
29
+ To generate the tarball, run the `build:tar` rake task. This will produce a tarball in the `pkg` directory.
30
+
31
+ ## Development
32
+
33
+ The source files are in the `/source` directory. The `compile` rake task builds the `/app` contents from these sources. This process involves the following:
34
+
35
+ * compiling all stylesheets referenced in `/manifests.yml` to plain css (actually css.erb, so the Rails asset pipeline can work in the gem).
36
+ * combining all javascripts referenced in `/manifests.yml` (using Sprockets)
37
+ * copying the images across (including any needed images from the toolkit)
38
+
39
+ This resulting app directory is included in the gem and hooked in as a Rails engine
40
+
41
+ ### Extra details for the tarball build
42
+
43
+ The tarball build process takes the compiled template and assets from the `/app` directory, and performs some extra processing:
44
+
45
+ * it compiles the `*.css.erb` files to plain css, replacing all calls to `asset_path` with the relative path to the asset.
46
+ For this reason, all assets referenced in the stylesheets must be stored relative to the stylesheet.
47
+ * it compiles the erb layout to plain html.
48
+ * All `asset_path` calls are replaced by the the path to the assets, assuming the assets folder is served from /assets
49
+ * Any `content_for?` calls are assumed to return false
50
+ * yields in the template are removed except for the main layout one which is replaced with an HTML comment.
51
+
52
+ See the `TemplateProcessor` class for details of this implementation.
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
59
+ 4. Run the tests (`bundle exec rake`)
60
+ 5. Push to the branch (`git push origin my-new-feature`)
61
+ 6. Create new Pull Request
Binary file
@@ -0,0 +1,97 @@
1
+ (function () {
2
+ "use strict"
3
+ var root = this;
4
+ if(typeof root.GOVUK === 'undefined') { root.GOVUK = {}; }
5
+
6
+ /*
7
+ Cookie methods
8
+ ==============
9
+
10
+ Usage:
11
+
12
+ Setting a cookie:
13
+ GOVUK.cookie('hobnob', 'tasty', { days: 30 });
14
+
15
+ Reading a cookie:
16
+ GOVUK.cookie('hobnob');
17
+
18
+ Deleting a cookie:
19
+ GOVUK.cookie('hobnob', null);
20
+ */
21
+ GOVUK.cookie = function (name, value, options) {
22
+ if(typeof value !== 'undefined'){
23
+ if(value === false || value === null) {
24
+ return GOVUK.setCookie(name, '', { days: -1 });
25
+ } else {
26
+ return GOVUK.setCookie(name, value, options);
27
+ }
28
+ } else {
29
+ return GOVUK.getCookie(name);
30
+ }
31
+ };
32
+ GOVUK.setCookie = function (name, value, options) {
33
+ if(typeof options === 'undefined') {
34
+ options = {};
35
+ }
36
+ var cookieString = name + "=" + value + "; path=/";
37
+ if (options.days) {
38
+ var date = new Date();
39
+ date.setTime(date.getTime() + (options.days * 24 * 60 * 60 * 1000));
40
+ cookieString = cookieString + "; expires=" + date.toGMTString();
41
+ }
42
+ if (document.location.protocol == 'https:'){
43
+ cookieString = cookieString + "; Secure";
44
+ }
45
+ document.cookie = cookieString;
46
+ };
47
+ GOVUK.getCookie = function (name) {
48
+ var nameEQ = name + "=";
49
+ var cookies = document.cookie.split(';');
50
+ for(var i = 0, len = cookies.length; i < len; i++) {
51
+ var cookie = cookies[i];
52
+ while (cookie.charAt(0) == ' ') {
53
+ cookie = cookie.substring(1, cookie.length);
54
+ }
55
+ if (cookie.indexOf(nameEQ) === 0) {
56
+ return decodeURIComponent(cookie.substring(nameEQ.length));
57
+ }
58
+ }
59
+ return null;
60
+ };
61
+ }).call(this);
62
+ (function () {
63
+ "use strict"
64
+ var root = this;
65
+ if(typeof root.GOVUK === 'undefined') { root.GOVUK = {}; }
66
+
67
+ GOVUK.addCookieMessage = function () {
68
+ var message = document.getElementById('global-cookie-message'),
69
+ hasCookieMessage = (message && GOVUK.cookie('seen_cookie_message') === null);
70
+
71
+ if (hasCookieMessage) {
72
+ message.style.display = 'block';
73
+ GOVUK.cookie('seen_cookie_message', 'yes', { days: 28 });
74
+ }
75
+ };
76
+ }).call(this);
77
+ (function() {
78
+ // fix for printing bug in Windows Safari
79
+ var windowsSafari = (window.navigator.userAgent.match(/(\(Windows[\s\w\.]+\))[\/\(\s\w\.\,\)]+(Version\/[\d\.]+)\s(Safari\/[\d\.]+)/) !== null),
80
+ style;
81
+
82
+ if (windowsSafari) {
83
+ // set the New Transport font to Arial for printing
84
+ style = document.createElement('style');
85
+ style.setAttribute('type', 'text/css');
86
+ style.setAttribute('media', 'print');
87
+ style.innerHTML = '@font-face { font-family: nta !important; src: local("Arial") !important; }';
88
+ document.getElementsByTagName('head')[0].appendChild(style);
89
+ }
90
+
91
+ if (window.GOVUK && GOVUK.addCookieMessage) {
92
+ GOVUK.addCookieMessage();
93
+ }
94
+ }).call(this);
95
+
96
+
97
+
@@ -0,0 +1,512 @@
1
+ /*
2
+ HTML5 Shiv v3.6.2 | @afarkas @jdalton @jon_neal @rem | MIT/GPL2 Licensed
3
+ */
4
+
5
+ (function(l,f){function m(){var a=e.elements;return"string"==typeof a?a.split(" "):a}function i(a){var b=n[a[o]];b||(b={},h++,a[o]=h,n[h]=b);return b}function p(a,b,c){b||(b=f);if(g)return b.createElement(a);c||(c=i(b));b=c.cache[a]?c.cache[a].cloneNode():r.test(a)?(c.cache[a]=c.createElem(a)).cloneNode():c.createElem(a);return b.canHaveChildren&&!s.test(a)?c.frag.appendChild(b):b}function t(a,b){if(!b.cache)b.cache={},b.createElem=a.createElement,b.createFrag=a.createDocumentFragment,b.frag=b.createFrag();
6
+ a.createElement=function(c){return!e.shivMethods?b.createElem(c):p(c,a,b)};a.createDocumentFragment=Function("h,f","return function(){var n=f.cloneNode(),c=n.createElement;h.shivMethods&&("+m().join().replace(/\w+/g,function(a){b.createElem(a);b.frag.createElement(a);return'c("'+a+'")'})+");return n}")(e,b.frag)}function q(a){a||(a=f);var b=i(a);if(e.shivCSS&&!j&&!b.hasCSS){var c,d=a;c=d.createElement("p");d=d.getElementsByTagName("head")[0]||d.documentElement;c.innerHTML="x<style>article,aside,figcaption,figure,footer,header,hgroup,main,nav,section{display:block}mark{background:#FF0;color:#000}</style>";
7
+ c=d.insertBefore(c.lastChild,d.firstChild);b.hasCSS=!!c}g||t(a,b);return a}var k=l.html5||{},s=/^<|^(?:button|map|select|textarea|object|iframe|option|optgroup)$/i,r=/^(?: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,j,o="_html5shiv",h=0,n={},g;(function(){try{var a=f.createElement("a");a.innerHTML="<xyz></xyz>";j="hidden"in a;var b;if(!(b=1==a.childNodes.length)){f.createElement("a");var c=f.createDocumentFragment();b="undefined"==typeof c.cloneNode||
8
+ "undefined"==typeof c.createDocumentFragment||"undefined"==typeof c.createElement}g=b}catch(d){g=j=!0}})();var e={elements:k.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",version:"3.6.2",shivCSS:!1!==k.shivCSS,supportsUnknownElements:g,shivMethods:!1!==k.shivMethods,type:"default",shivDocument:q,createElement:p,createDocumentFragment:function(a,b){a||(a=f);if(g)return a.createDocumentFragment();
9
+ for(var b=b||i(a),c=b.frag.cloneNode(),d=0,e=m(),h=e.length;d<h;d++)c.createElement(e[d]);return c}};l.html5=e;q(f)})(this,document);
10
+ /*
11
+ HTML5 Shiv v3.6.2 | @afarkas @jdalton @jon_neal @rem | MIT/GPL2 Licensed
12
+ */
13
+
14
+ (function(j,f){function s(a,b){var c=a.createElement("p"),m=a.getElementsByTagName("head")[0]||a.documentElement;c.innerHTML="x<style>"+b+"</style>";return m.insertBefore(c.lastChild,m.firstChild)}function o(){var a=d.elements;return"string"==typeof a?a.split(" "):a}function n(a){var b=t[a[u]];b||(b={},p++,a[u]=p,t[p]=b);return b}function v(a,b,c){b||(b=f);if(e)return b.createElement(a);c||(c=n(b));b=c.cache[a]?c.cache[a].cloneNode():y.test(a)?(c.cache[a]=c.createElem(a)).cloneNode():c.createElem(a);
15
+ return b.canHaveChildren&&!z.test(a)?c.frag.appendChild(b):b}function A(a,b){if(!b.cache)b.cache={},b.createElem=a.createElement,b.createFrag=a.createDocumentFragment,b.frag=b.createFrag();a.createElement=function(c){return!d.shivMethods?b.createElem(c):v(c,a,b)};a.createDocumentFragment=Function("h,f","return function(){var n=f.cloneNode(),c=n.createElement;h.shivMethods&&("+o().join().replace(/\w+/g,function(a){b.createElem(a);b.frag.createElement(a);return'c("'+a+'")'})+");return n}")(d,b.frag)}
16
+ function w(a){a||(a=f);var b=n(a);if(d.shivCSS&&!q&&!b.hasCSS)b.hasCSS=!!s(a,"article,aside,figcaption,figure,footer,header,hgroup,main,nav,section{display:block}mark{background:#FF0;color:#000}");e||A(a,b);return a}function B(a){for(var b,c=a.attributes,m=c.length,f=a.ownerDocument.createElement(l+":"+a.nodeName);m--;)b=c[m],b.specified&&f.setAttribute(b.nodeName,b.nodeValue);f.style.cssText=a.style.cssText;return f}function x(a){function b(){clearTimeout(d._removeSheetTimer);c&&c.removeNode(!0);
17
+ c=null}var c,f,d=n(a),e=a.namespaces,j=a.parentWindow;if(!C||a.printShived)return a;"undefined"==typeof e[l]&&e.add(l);j.attachEvent("onbeforeprint",function(){b();var g,i,d;d=a.styleSheets;for(var e=[],h=d.length,k=Array(h);h--;)k[h]=d[h];for(;d=k.pop();)if(!d.disabled&&D.test(d.media)){try{g=d.imports,i=g.length}catch(j){i=0}for(h=0;h<i;h++)k.push(g[h]);try{e.push(d.cssText)}catch(n){}}g=e.reverse().join("").split("{");i=g.length;h=RegExp("(^|[\\s,>+~])("+o().join("|")+")(?=[[\\s,>+~#.:]|$)","gi");
18
+ for(k="$1"+l+"\\:$2";i--;)e=g[i]=g[i].split("}"),e[e.length-1]=e[e.length-1].replace(h,k),g[i]=e.join("}");e=g.join("{");i=a.getElementsByTagName("*");h=i.length;k=RegExp("^(?:"+o().join("|")+")$","i");for(d=[];h--;)g=i[h],k.test(g.nodeName)&&d.push(g.applyElement(B(g)));f=d;c=s(a,e)});j.attachEvent("onafterprint",function(){for(var a=f,c=a.length;c--;)a[c].removeNode();clearTimeout(d._removeSheetTimer);d._removeSheetTimer=setTimeout(b,500)});a.printShived=!0;return a}var r=j.html5||{},z=/^<|^(?:button|map|select|textarea|object|iframe|option|optgroup)$/i,
19
+ y=/^(?: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,q,u="_html5shiv",p=0,t={},e;(function(){try{var a=f.createElement("a");a.innerHTML="<xyz></xyz>";q="hidden"in a;var b;if(!(b=1==a.childNodes.length)){f.createElement("a");var c=f.createDocumentFragment();b="undefined"==typeof c.cloneNode||"undefined"==typeof c.createDocumentFragment||"undefined"==typeof c.createElement}e=b}catch(d){e=q=!0}})();var d={elements:r.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",
20
+ version:"3.6.2",shivCSS:!1!==r.shivCSS,supportsUnknownElements:e,shivMethods:!1!==r.shivMethods,type:"default",shivDocument:w,createElement:v,createDocumentFragment:function(a,b){a||(a=f);if(e)return a.createDocumentFragment();for(var b=b||n(a),c=b.frag.cloneNode(),d=0,j=o(),l=j.length;d<l;d++)c.createElement(j[d]);return c}};j.html5=d;w(f);var D=/^$|\b(?:all|print)\b/,l="html5shiv",C=!e&&function(){var a=f.documentElement;return!("undefined"==typeof f.namespaces||"undefined"==typeof f.parentWindow||
21
+ "undefined"==typeof a.applyElement||"undefined"==typeof a.removeNode||"undefined"==typeof j.attachEvent)}();d.type+=" print";d.shivPrint=x;x(f)})(this,document);
22
+
23
+
24
+ /*
25
+ json2.js
26
+ 2011-10-19
27
+
28
+ Public Domain.
29
+
30
+ NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
31
+
32
+ See http://www.JSON.org/js.html
33
+
34
+
35
+ This code should be minified before deployment.
36
+ See http://javascript.crockford.com/jsmin.html
37
+
38
+ USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
39
+ NOT CONTROL.
40
+
41
+
42
+ This file creates a global JSON object containing two methods: stringify
43
+ and parse.
44
+
45
+ JSON.stringify(value, replacer, space)
46
+ value any JavaScript value, usually an object or array.
47
+
48
+ replacer an optional parameter that determines how object
49
+ values are stringified for objects. It can be a
50
+ function or an array of strings.
51
+
52
+ space an optional parameter that specifies the indentation
53
+ of nested structures. If it is omitted, the text will
54
+ be packed without extra whitespace. If it is a number,
55
+ it will specify the number of spaces to indent at each
56
+ level. If it is a string (such as '\t' or '&nbsp;'),
57
+ it contains the characters used to indent at each level.
58
+
59
+ This method produces a JSON text from a JavaScript value.
60
+
61
+ When an object value is found, if the object contains a toJSON
62
+ method, its toJSON method will be called and the result will be
63
+ stringified. A toJSON method does not serialize: it returns the
64
+ value represented by the name/value pair that should be serialized,
65
+ or undefined if nothing should be serialized. The toJSON method
66
+ will be passed the key associated with the value, and this will be
67
+ bound to the value
68
+
69
+ For example, this would serialize Dates as ISO strings.
70
+
71
+ Date.prototype.toJSON = function (key) {
72
+ function f(n) {
73
+ // Format integers to have at least two digits.
74
+ return n < 10 ? '0' + n : n;
75
+ }
76
+
77
+ return this.getUTCFullYear() + '-' +
78
+ f(this.getUTCMonth() + 1) + '-' +
79
+ f(this.getUTCDate()) + 'T' +
80
+ f(this.getUTCHours()) + ':' +
81
+ f(this.getUTCMinutes()) + ':' +
82
+ f(this.getUTCSeconds()) + 'Z';
83
+ };
84
+
85
+ You can provide an optional replacer method. It will be passed the
86
+ key and value of each member, with this bound to the containing
87
+ object. The value that is returned from your method will be
88
+ serialized. If your method returns undefined, then the member will
89
+ be excluded from the serialization.
90
+
91
+ If the replacer parameter is an array of strings, then it will be
92
+ used to select the members to be serialized. It filters the results
93
+ such that only members with keys listed in the replacer array are
94
+ stringified.
95
+
96
+ Values that do not have JSON representations, such as undefined or
97
+ functions, will not be serialized. Such values in objects will be
98
+ dropped; in arrays they will be replaced with null. You can use
99
+ a replacer function to replace those with JSON values.
100
+ JSON.stringify(undefined) returns undefined.
101
+
102
+ The optional space parameter produces a stringification of the
103
+ value that is filled with line breaks and indentation to make it
104
+ easier to read.
105
+
106
+ If the space parameter is a non-empty string, then that string will
107
+ be used for indentation. If the space parameter is a number, then
108
+ the indentation will be that many spaces.
109
+
110
+ Example:
111
+
112
+ text = JSON.stringify(['e', {pluribus: 'unum'}]);
113
+ // text is '["e",{"pluribus":"unum"}]'
114
+
115
+
116
+ text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t');
117
+ // text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
118
+
119
+ text = JSON.stringify([new Date()], function (key, value) {
120
+ return this[key] instanceof Date ?
121
+ 'Date(' + this[key] + ')' : value;
122
+ });
123
+ // text is '["Date(---current time---)"]'
124
+
125
+
126
+ JSON.parse(text, reviver)
127
+ This method parses a JSON text to produce an object or array.
128
+ It can throw a SyntaxError exception.
129
+
130
+ The optional reviver parameter is a function that can filter and
131
+ transform the results. It receives each of the keys and values,
132
+ and its return value is used instead of the original value.
133
+ If it returns what it received, then the structure is not modified.
134
+ If it returns undefined then the member is deleted.
135
+
136
+ Example:
137
+
138
+ // Parse the text. Values that look like ISO date strings will
139
+ // be converted to Date objects.
140
+
141
+ myData = JSON.parse(text, function (key, value) {
142
+ var a;
143
+ if (typeof value === 'string') {
144
+ a =
145
+ /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
146
+ if (a) {
147
+ return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
148
+ +a[5], +a[6]));
149
+ }
150
+ }
151
+ return value;
152
+ });
153
+
154
+ myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) {
155
+ var d;
156
+ if (typeof value === 'string' &&
157
+ value.slice(0, 5) === 'Date(' &&
158
+ value.slice(-1) === ')') {
159
+ d = new Date(value.slice(5, -1));
160
+ if (d) {
161
+ return d;
162
+ }
163
+ }
164
+ return value;
165
+ });
166
+
167
+
168
+ This is a reference implementation. You are free to copy, modify, or
169
+ redistribute.
170
+ */
171
+
172
+ /*jslint evil: true, regexp: true */
173
+
174
+ /*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply,
175
+ call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
176
+ getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
177
+ lastIndex, length, parse, prototype, push, replace, slice, stringify,
178
+ test, toJSON, toString, valueOf
179
+ */
180
+
181
+
182
+ // Create a JSON object only if one does not already exist. We create the
183
+ // methods in a closure to avoid creating global variables.
184
+
185
+ var JSON;
186
+ if (!JSON) {
187
+ JSON = {};
188
+ }
189
+
190
+ (function () {
191
+ 'use strict';
192
+
193
+ function f(n) {
194
+ // Format integers to have at least two digits.
195
+ return n < 10 ? '0' + n : n;
196
+ }
197
+
198
+ if (typeof Date.prototype.toJSON !== 'function') {
199
+
200
+ Date.prototype.toJSON = function (key) {
201
+
202
+ return isFinite(this.valueOf())
203
+ ? this.getUTCFullYear() + '-' +
204
+ f(this.getUTCMonth() + 1) + '-' +
205
+ f(this.getUTCDate()) + 'T' +
206
+ f(this.getUTCHours()) + ':' +
207
+ f(this.getUTCMinutes()) + ':' +
208
+ f(this.getUTCSeconds()) + 'Z'
209
+ : null;
210
+ };
211
+
212
+ String.prototype.toJSON =
213
+ Number.prototype.toJSON =
214
+ Boolean.prototype.toJSON = function (key) {
215
+ return this.valueOf();
216
+ };
217
+ }
218
+
219
+ var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
220
+ escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
221
+ gap,
222
+ indent,
223
+ meta = { // table of character substitutions
224
+ '\b': '\\b',
225
+ '\t': '\\t',
226
+ '\n': '\\n',
227
+ '\f': '\\f',
228
+ '\r': '\\r',
229
+ '"' : '\\"',
230
+ '\\': '\\\\'
231
+ },
232
+ rep;
233
+
234
+
235
+ function quote(string) {
236
+
237
+ // If the string contains no control characters, no quote characters, and no
238
+ // backslash characters, then we can safely slap some quotes around it.
239
+ // Otherwise we must also replace the offending characters with safe escape
240
+ // sequences.
241
+
242
+ escapable.lastIndex = 0;
243
+ return escapable.test(string) ? '"' + string.replace(escapable, function (a) {
244
+ var c = meta[a];
245
+ return typeof c === 'string'
246
+ ? c
247
+ : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
248
+ }) + '"' : '"' + string + '"';
249
+ }
250
+
251
+
252
+ function str(key, holder) {
253
+
254
+ // Produce a string from holder[key].
255
+
256
+ var i, // The loop counter.
257
+ k, // The member key.
258
+ v, // The member value.
259
+ length,
260
+ mind = gap,
261
+ partial,
262
+ value = holder[key];
263
+
264
+ // If the value has a toJSON method, call it to obtain a replacement value.
265
+
266
+ if (value && typeof value === 'object' &&
267
+ typeof value.toJSON === 'function') {
268
+ value = value.toJSON(key);
269
+ }
270
+
271
+ // If we were called with a replacer function, then call the replacer to
272
+ // obtain a replacement value.
273
+
274
+ if (typeof rep === 'function') {
275
+ value = rep.call(holder, key, value);
276
+ }
277
+
278
+ // What happens next depends on the value's type.
279
+
280
+ switch (typeof value) {
281
+ case 'string':
282
+ return quote(value);
283
+
284
+ case 'number':
285
+
286
+ // JSON numbers must be finite. Encode non-finite numbers as null.
287
+
288
+ return isFinite(value) ? String(value) : 'null';
289
+
290
+ case 'boolean':
291
+ case 'null':
292
+
293
+ // If the value is a boolean or null, convert it to a string. Note:
294
+ // typeof null does not produce 'null'. The case is included here in
295
+ // the remote chance that this gets fixed someday.
296
+
297
+ return String(value);
298
+
299
+ // If the type is 'object', we might be dealing with an object or an array or
300
+ // null.
301
+
302
+ case 'object':
303
+
304
+ // Due to a specification blunder in ECMAScript, typeof null is 'object',
305
+ // so watch out for that case.
306
+
307
+ if (!value) {
308
+ return 'null';
309
+ }
310
+
311
+ // Make an array to hold the partial results of stringifying this object value.
312
+
313
+ gap += indent;
314
+ partial = [];
315
+
316
+ // Is the value an array?
317
+
318
+ if (Object.prototype.toString.apply(value) === '[object Array]') {
319
+
320
+ // The value is an array. Stringify every element. Use null as a placeholder
321
+ // for non-JSON values.
322
+
323
+ length = value.length;
324
+ for (i = 0; i < length; i += 1) {
325
+ partial[i] = str(i, value) || 'null';
326
+ }
327
+
328
+ // Join all of the elements together, separated with commas, and wrap them in
329
+ // brackets.
330
+
331
+ v = partial.length === 0
332
+ ? '[]'
333
+ : gap
334
+ ? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']'
335
+ : '[' + partial.join(',') + ']';
336
+ gap = mind;
337
+ return v;
338
+ }
339
+
340
+ // If the replacer is an array, use it to select the members to be stringified.
341
+
342
+ if (rep && typeof rep === 'object') {
343
+ length = rep.length;
344
+ for (i = 0; i < length; i += 1) {
345
+ if (typeof rep[i] === 'string') {
346
+ k = rep[i];
347
+ v = str(k, value);
348
+ if (v) {
349
+ partial.push(quote(k) + (gap ? ': ' : ':') + v);
350
+ }
351
+ }
352
+ }
353
+ } else {
354
+
355
+ // Otherwise, iterate through all of the keys in the object.
356
+
357
+ for (k in value) {
358
+ if (Object.prototype.hasOwnProperty.call(value, k)) {
359
+ v = str(k, value);
360
+ if (v) {
361
+ partial.push(quote(k) + (gap ? ': ' : ':') + v);
362
+ }
363
+ }
364
+ }
365
+ }
366
+
367
+ // Join all of the member texts together, separated with commas,
368
+ // and wrap them in braces.
369
+
370
+ v = partial.length === 0
371
+ ? '{}'
372
+ : gap
373
+ ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}'
374
+ : '{' + partial.join(',') + '}';
375
+ gap = mind;
376
+ return v;
377
+ }
378
+ }
379
+
380
+ // If the JSON object does not yet have a stringify method, give it one.
381
+
382
+ if (typeof JSON.stringify !== 'function') {
383
+ JSON.stringify = function (value, replacer, space) {
384
+
385
+ // The stringify method takes a value and an optional replacer, and an optional
386
+ // space parameter, and returns a JSON text. The replacer can be a function
387
+ // that can replace values, or an array of strings that will select the keys.
388
+ // A default replacer method can be provided. Use of the space parameter can
389
+ // produce text that is more easily readable.
390
+
391
+ var i;
392
+ gap = '';
393
+ indent = '';
394
+
395
+ // If the space parameter is a number, make an indent string containing that
396
+ // many spaces.
397
+
398
+ if (typeof space === 'number') {
399
+ for (i = 0; i < space; i += 1) {
400
+ indent += ' ';
401
+ }
402
+
403
+ // If the space parameter is a string, it will be used as the indent string.
404
+
405
+ } else if (typeof space === 'string') {
406
+ indent = space;
407
+ }
408
+
409
+ // If there is a replacer, it must be a function or an array.
410
+ // Otherwise, throw an error.
411
+
412
+ rep = replacer;
413
+ if (replacer && typeof replacer !== 'function' &&
414
+ (typeof replacer !== 'object' ||
415
+ typeof replacer.length !== 'number')) {
416
+ throw new Error('JSON.stringify');
417
+ }
418
+
419
+ // Make a fake root object containing our value under the key of ''.
420
+ // Return the result of stringifying the value.
421
+
422
+ return str('', {'': value});
423
+ };
424
+ }
425
+
426
+
427
+ // If the JSON object does not yet have a parse method, give it one.
428
+
429
+ if (typeof JSON.parse !== 'function') {
430
+ JSON.parse = function (text, reviver) {
431
+
432
+ // The parse method takes a text and an optional reviver function, and returns
433
+ // a JavaScript value if the text is a valid JSON text.
434
+
435
+ var j;
436
+
437
+ function walk(holder, key) {
438
+
439
+ // The walk method is used to recursively walk the resulting structure so
440
+ // that modifications can be made.
441
+
442
+ var k, v, value = holder[key];
443
+ if (value && typeof value === 'object') {
444
+ for (k in value) {
445
+ if (Object.prototype.hasOwnProperty.call(value, k)) {
446
+ v = walk(value, k);
447
+ if (v !== undefined) {
448
+ value[k] = v;
449
+ } else {
450
+ delete value[k];
451
+ }
452
+ }
453
+ }
454
+ }
455
+ return reviver.call(holder, key, value);
456
+ }
457
+
458
+
459
+ // Parsing happens in four stages. In the first stage, we replace certain
460
+ // Unicode characters with escape sequences. JavaScript handles many characters
461
+ // incorrectly, either silently deleting them, or treating them as line endings.
462
+
463
+ text = String(text);
464
+ cx.lastIndex = 0;
465
+ if (cx.test(text)) {
466
+ text = text.replace(cx, function (a) {
467
+ return '\\u' +
468
+ ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
469
+ });
470
+ }
471
+
472
+ // In the second stage, we run the text against regular expressions that look
473
+ // for non-JSON patterns. We are especially concerned with '()' and 'new'
474
+ // because they can cause invocation, and '=' because it can cause mutation.
475
+ // But just to be safe, we want to reject all unexpected forms.
476
+
477
+ // We split the second stage into 4 regexp operations in order to work around
478
+ // crippling inefficiencies in IE's and Safari's regexp engines. First we
479
+ // replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
480
+ // replace all simple value tokens with ']' characters. Third, we delete all
481
+ // open brackets that follow a colon or comma or that begin the text. Finally,
482
+ // we look to see that the remaining characters are only whitespace or ']' or
483
+ // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
484
+
485
+ if (/^[\],:{}\s]*$/
486
+ .test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@')
487
+ .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
488
+ .replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
489
+
490
+ // In the third stage we use the eval function to compile the text into a
491
+ // JavaScript structure. The '{' operator is subject to a syntactic ambiguity
492
+ // in JavaScript: it can begin a block or an object literal. We wrap the text
493
+ // in parens to eliminate the ambiguity.
494
+
495
+ j = eval('(' + text + ')');
496
+
497
+ // In the optional fourth stage, we recursively walk the new structure, passing
498
+ // each name/value pair to a reviver function for possible transformation.
499
+
500
+ return typeof reviver === 'function'
501
+ ? walk({'': j}, '')
502
+ : j;
503
+ }
504
+
505
+ // If the text is not JSON parseable, then a SyntaxError is thrown.
506
+
507
+ throw new SyntaxError('JSON.parse');
508
+ };
509
+ }
510
+ }());
511
+
512
+