Skip to content

Commit

Permalink
Add a small JSON Parser to October framework lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayumi Hamasaki committed Aug 6, 2019
1 parent 4df3149 commit 46bba7d
Show file tree
Hide file tree
Showing 11 changed files with 720 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
if (typeof value == 'object') return value

try {
return JSON.parse(JSON.stringify(eval("({" + value + "})")))
return octoberJSON.parse("{" + value + "}")
}
catch (e) {
throw new Error('Error parsing the '+name+' attribute value. '+e)
Expand Down
2 changes: 1 addition & 1 deletion modules/backend/widgets/form/assets/js/october.form.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@
if (typeof value == 'object') return value

try {
return JSON.parse(JSON.stringify(eval("({" + value + "})")))
return octoberJSON.parse("{" + value + "}")
}
catch (e) {
throw new Error('Error parsing the '+name+' attribute value. '+e)
Expand Down
2 changes: 1 addition & 1 deletion modules/system/assets/js/framework-min.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

"use strict";!function(){function a(e,r,n){for(var t="",o=r;o<e.length;o++){if(n&&n===e[o])return t;if(!n&&(" "===e[o]||":"===e[o]))return t;t+=e[o],"\\"===e[o]&&o+1<e.length&&(t+=e[o+1],o++)}throw new Error("Broken JSON syntax near "+t)}function u(e,r){if('"'===e[r]||"'"===e[r]){for(var n=e[r],t=r+1;t<e.length;t++)if("\\"===e[t])n+=e[t],t+1<e.length&&(n+=e[t+1]),t++;else{if(e[t]===e[r])return{originLength:(n+=e[r]).length,body:n};n+=e[t]}throw new Error("Broken JSON string body near "+n)}if("t"===e[r]){if(e.indexOf("true",r)===r)return{originLength:"true".length,body:"true"};throw new Error("Broken JSON boolean body near "+e.substr(0,r+10))}if("f"===e[r]){if(e.indexOf("f",r)===r)return{originLength:"false".length,body:"false"};throw new Error("Broken JSON boolean body near "+e.substr(0,r+10))}if("n"===e[r]){if(e.indexOf("null",r)===r)return{originLength:"null".length,body:"null"};throw new Error("Broken JSON boolean body near "+e.substr(0,r+10))}if("-"===e[r]||"+"===e[r]||"."===e[r]||"0"<=e[r]&&e[r]<="9"){for(n="",t=r;t<e.length;t++){if(!("-"===e[t]||"+"===e[t]||"."===e[t]||"0"<=e[t]&&e[t]<="9"))return{originLength:n.length,body:n};n+=e[t]}throw new Error("Broken JSON number body near "+n)}if("{"!==e[r]&&"["!==e[r])throw new Error("Broken JSON body near "+e.substr(0<=r-5?r-5:0,50));var o=[e[r]];for(n=e[r],t=r+1;t<e.length;t++){if(n+=e[t],"\\"===e[t])t+1<e.length&&(n+=e[t+1]),t++;else if('"'===e[t])'"'===o[o.length-1]?o.pop():"'"!==o[o.length-1]&&o.push(e[t]);else if("'"===e[t])"'"===o[o.length-1]?o.pop():'"'!==o[o.length-1]&&o.push(e[t]);else if('"'!==o[o.length-1]&&"'"!==o[o.length-1])if("{"===e[t])o.push("{");else if("}"===e[t]){if("{"!==o[o.length-1])throw new Error("Broken JSON "+("{"===e[r]?"object":"array")+" body near "+n);o.pop()}else if("["===e[t])o.push("[");else if("]"===e[t]){if("["!==o[o.length-1])throw new Error("Broken JSON "+("{"===e[r]?"object":"array")+" body near "+n);o.pop()}if(!o.length)return{originLength:t-r,body:n}}throw new Error("Broken JSON "+("{"===e[r]?"object":"array")+" body near "+n)}function s(e){return" "===e||"\n"===e||"\t"===e}("undefined"==typeof exports?window.octoberJSON={}:exports).parse=function(e){var r=function e(r){if(!(r=r.trim()).length)throw new Error("Broken JSON object.");for(var n="";r&&","===r[0];)r=r.substr(1);if('"'===r[0]||"'"===r[0]){if(r[r.length-1]!==r[0])throw new Error("Invalid string JSON object.");for(var t='"',o=1;o<r.length;o++)if("\\"===r[o])"'"===r[o+1]||(t+=r[o]),t+=r[o+1],o++;else{if(r[o]===r[0])return t+='"';'"'===r[o]?t+='\\"':t+=r[o]}throw new Error("Invalid string JSON object.")}if("true"===r||"false"===r)return r;if("null"===r)return"null";var i,f=parseFloat(r);if(!isNaN(f))return f.toString();if("{"===r[0]){var l="needKey";for(n="{",o=1;o<r.length;o++)if(!s(r[o]))if("needKey"!==l||'"'!==r[o]&&"'"!==r[o]){if("needKey"===l&&"\\"!==(i=r[o])[0]&&("a"<=i[0]&&i[0]<="z"||"A"<=i[0]&&i[0]<="Z"||"_"===i[0]||"0"<=i[0]&&i[0]<="9"||"$"===i[0]||255<i.charCodeAt(0))){var h;n+='"',n+=h=a(r,o),n+='"',o+=h.length-1,l="afterKey"}else if("afterKey"===l&&":"===r[o])n+=":",l=":";else if(":"===l)o=o+(t=u(r,o)).originLength-1,n+=e(t.body),l="afterBody";else if("afterBody"===l||"needKey"===l){for(var g=o;","===r[g]||s(r[g]);)g++;if("}"===r[g]&&g===r.length-1){for(;","===n[n.length-1];)n=n.substr(0,n.length-1);return n+="}"}g!==o&&"{"!==n&&(n+=",",l="needKey",o=g-1)}}else n+='"'+(h=a(r,o+1,r[o]))+'"',o+=h.length,o+=1,l="afterKey";throw new Error("Broken JSON object near "+n)}if("["===r[0]){for(n="[",l="needBody",o=1;o<r.length;o++)if(" "!==r[o]&&"\n"!==r[o]&&"\t"!==r[o])if("needBody"===l){if(","===r[o]){n+="null,";continue}if("]"===r[o]&&o===r.length-1)return","===n[n.length-1]&&(n=n.substr(0,n.length-1)),n+="]";o=o+(t=u(r,o)).originLength-1,n+=e(t.body),l="afterBody"}else if("afterBody"===l)if(","===r[o])for(n+=",",l="needBody";","===r[o+1]||s(r[o+1]);)","===r[o+1]&&(n+="null,"),o++;else if("]"===r[o]&&o===r.length-1)return n+="]";throw new Error("Broken JSON array near "+n)}}(e);return JSON.parse(r)}}();
if(window.jQuery===undefined){throw new Error('The jQuery library is not loaded. The OctoberCMS framework cannot be initialized.');}
if(window.jQuery.request!==undefined){throw new Error('The OctoberCMS framework is already loaded.');}
+function($){"use strict";var Request=function(element,handler,options){var $el=this.$el=$(element);this.options=options||{};if(handler===undefined){throw new Error('The request handler name is not specified.')}
Expand Down
4 changes: 2 additions & 2 deletions modules/system/assets/js/framework.combined-min.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

"use strict";!function(){function a(e,r,n){for(var t="",o=r;o<e.length;o++){if(n&&n===e[o])return t;if(!n&&(" "===e[o]||":"===e[o]))return t;t+=e[o],"\\"===e[o]&&o+1<e.length&&(t+=e[o+1],o++)}throw new Error("Broken JSON syntax near "+t)}function u(e,r){if('"'===e[r]||"'"===e[r]){for(var n=e[r],t=r+1;t<e.length;t++)if("\\"===e[t])n+=e[t],t+1<e.length&&(n+=e[t+1]),t++;else{if(e[t]===e[r])return{originLength:(n+=e[r]).length,body:n};n+=e[t]}throw new Error("Broken JSON string body near "+n)}if("t"===e[r]){if(e.indexOf("true",r)===r)return{originLength:"true".length,body:"true"};throw new Error("Broken JSON boolean body near "+e.substr(0,r+10))}if("f"===e[r]){if(e.indexOf("f",r)===r)return{originLength:"false".length,body:"false"};throw new Error("Broken JSON boolean body near "+e.substr(0,r+10))}if("n"===e[r]){if(e.indexOf("null",r)===r)return{originLength:"null".length,body:"null"};throw new Error("Broken JSON boolean body near "+e.substr(0,r+10))}if("-"===e[r]||"+"===e[r]||"."===e[r]||"0"<=e[r]&&e[r]<="9"){for(n="",t=r;t<e.length;t++){if(!("-"===e[t]||"+"===e[t]||"."===e[t]||"0"<=e[t]&&e[t]<="9"))return{originLength:n.length,body:n};n+=e[t]}throw new Error("Broken JSON number body near "+n)}if("{"!==e[r]&&"["!==e[r])throw new Error("Broken JSON body near "+e.substr(0<=r-5?r-5:0,50));var o=[e[r]];for(n=e[r],t=r+1;t<e.length;t++){if(n+=e[t],"\\"===e[t])t+1<e.length&&(n+=e[t+1]),t++;else if('"'===e[t])'"'===o[o.length-1]?o.pop():"'"!==o[o.length-1]&&o.push(e[t]);else if("'"===e[t])"'"===o[o.length-1]?o.pop():'"'!==o[o.length-1]&&o.push(e[t]);else if('"'!==o[o.length-1]&&"'"!==o[o.length-1])if("{"===e[t])o.push("{");else if("}"===e[t]){if("{"!==o[o.length-1])throw new Error("Broken JSON "+("{"===e[r]?"object":"array")+" body near "+n);o.pop()}else if("["===e[t])o.push("[");else if("]"===e[t]){if("["!==o[o.length-1])throw new Error("Broken JSON "+("{"===e[r]?"object":"array")+" body near "+n);o.pop()}if(!o.length)return{originLength:t-r,body:n}}throw new Error("Broken JSON "+("{"===e[r]?"object":"array")+" body near "+n)}function s(e){return" "===e||"\n"===e||"\t"===e}("undefined"==typeof exports?window.octoberJSON={}:exports).parse=function(e){var r=function e(r){if(!(r=r.trim()).length)throw new Error("Broken JSON object.");for(var n="";r&&","===r[0];)r=r.substr(1);if('"'===r[0]||"'"===r[0]){if(r[r.length-1]!==r[0])throw new Error("Invalid string JSON object.");for(var t='"',o=1;o<r.length;o++)if("\\"===r[o])"'"===r[o+1]||(t+=r[o]),t+=r[o+1],o++;else{if(r[o]===r[0])return t+='"';'"'===r[o]?t+='\\"':t+=r[o]}throw new Error("Invalid string JSON object.")}if("true"===r||"false"===r)return r;if("null"===r)return"null";var i,f=parseFloat(r);if(!isNaN(f))return f.toString();if("{"===r[0]){var l="needKey";for(n="{",o=1;o<r.length;o++)if(!s(r[o]))if("needKey"!==l||'"'!==r[o]&&"'"!==r[o]){if("needKey"===l&&"\\"!==(i=r[o])[0]&&("a"<=i[0]&&i[0]<="z"||"A"<=i[0]&&i[0]<="Z"||"_"===i[0]||"0"<=i[0]&&i[0]<="9"||"$"===i[0]||255<i.charCodeAt(0))){var h;n+='"',n+=h=a(r,o),n+='"',o+=h.length-1,l="afterKey"}else if("afterKey"===l&&":"===r[o])n+=":",l=":";else if(":"===l)o=o+(t=u(r,o)).originLength-1,n+=e(t.body),l="afterBody";else if("afterBody"===l||"needKey"===l){for(var g=o;","===r[g]||s(r[g]);)g++;if("}"===r[g]&&g===r.length-1){for(;","===n[n.length-1];)n=n.substr(0,n.length-1);return n+="}"}g!==o&&"{"!==n&&(n+=",",l="needKey",o=g-1)}}else n+='"'+(h=a(r,o+1,r[o]))+'"',o+=h.length,o+=1,l="afterKey";throw new Error("Broken JSON object near "+n)}if("["===r[0]){for(n="[",l="needBody",o=1;o<r.length;o++)if(" "!==r[o]&&"\n"!==r[o]&&"\t"!==r[o])if("needBody"===l){if(","===r[o]){n+="null,";continue}if("]"===r[o]&&o===r.length-1)return","===n[n.length-1]&&(n=n.substr(0,n.length-1)),n+="]";o=o+(t=u(r,o)).originLength-1,n+=e(t.body),l="afterBody"}else if("afterBody"===l)if(","===r[o])for(n+=",",l="needBody";","===r[o+1]||s(r[o+1]);)","===r[o+1]&&(n+="null,"),o++;else if("]"===r[o]&&o===r.length-1)return n+="]";throw new Error("Broken JSON array near "+n)}}(e);return JSON.parse(r)}}();
if(window.jQuery===undefined){throw new Error('The jQuery library is not loaded. The OctoberCMS framework cannot be initialized.');}
if(window.jQuery.request!==undefined){throw new Error('The OctoberCMS framework is already loaded.');}
+function($){"use strict";var Request=function(element,handler,options){var $el=this.$el=$(element);this.options=options||{};if(handler===undefined){throw new Error('The request handler name is not specified.')}
Expand Down Expand Up @@ -110,7 +110,7 @@ $.fn.request.noConflict=function(){$.fn.request=old
return this}
function paramToObj(name,value){if(value===undefined)value=''
if(typeof value=='object')return value
try{return JSON.parse(JSON.stringify(eval("({"+value+"})")))}
try{return octoberJSON.parse("{" + value + "}")}
catch(e){throw new Error('Error parsing the '+name+' attribute value. '+e)}}
$(document).on('change','select[data-request], input[type=radio][data-request], input[type=checkbox][data-request], input[type=file][data-request]',function documentOnChange(){$(this).request()})
$(document).on('click','a[data-request], button[data-request], input[type=button][data-request], input[type=submit][data-request]',function documentOnClick(e){e.preventDefault()
Expand Down
1 change: 1 addition & 0 deletions modules/system/assets/js/framework.combined.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
=require october.parser.js
=require framework.js
=require framework.extras.js
*/
Loading

0 comments on commit 46bba7d

Please sign in to comment.