Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

support variable name like @rid_child

  • Loading branch information...
commit 99e8740d41c4eae8a5839b816b339145ebb3e5ae 1 parent 6fe7cb8
@kinogam authored
View
2  lib/kino.razor.js
@@ -58,7 +58,7 @@
//正则集合
var RegexCollection = {
VariableFirstChar: /^[\(_a-zA-Z]/,
- Variable: /^(?:(?:\()(?:new\s+)?[a-zA-Z0-9]+(?:\.|[-\+*\/^=<>?:]|[\[\(][^\]\)]*[\]\)]|[a-zA-Z0-9]+)*(?:\))|(?:new\s+)?[a-zA-Z0-9]+(?:\.|[\[\(][^\]\)]*[\]\)]|[a-zA-Z0-9]+)*)/,
+ Variable: /^(?:(?:\()(?:new\s+)?[_a-zA-Z0-9]+(?:\.|[-\+*\/^=<>?:]|[\[\(][^\]\)]*[\]\)]|[_a-zA-Z0-9]+)*(?:\))|(?:new\s+)?[_a-zA-Z0-9]+(?:\.|[\[\(][^\]\)]*[\]\)]|[_a-zA-Z0-9]+)*)/,
ConditionAndLoop: /^(?:if|for|while)\s*\(/,
ElseCondition: /^[\s\r\n\t]*else(?:\s*{|[\s\t]+if\()?/
};
View
2  lib/kino.razor.min.js
@@ -1 +1 @@
-;(function(e){"use strict";var t="@",n=function(e,t){if(arguments.length==1)return r(e);var i;return typeof e=="function"?i=e:i=r(e),i.call(null,n.HtmlHelper,t)},r=function(e,t){var n={segments:[],segmentIndex:0,conditionOpeningBraceCount:0},r=o.parse(n,e),i=u.toFunctionContent(r);return new Function("Html","m",i)};n.HtmlHelper={escape:function(e){return(""+e).replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#x27;").replace(/\//g,"&#x2F;")}};var i={String:0,Variable:1,ScriptBlock:2},s={VariableFirstChar:/^[\(_a-zA-Z]/,Variable:/^(?:(?:\()(?:new\s+)?[a-zA-Z0-9]+(?:\.|[-\+*\/^=<>?:]|[\[\(][^\]\)]*[\]\)]|[a-zA-Z0-9]+)*(?:\))|(?:new\s+)?[a-zA-Z0-9]+(?:\.|[\[\(][^\]\)]*[\]\)]|[a-zA-Z0-9]+)*)/,ConditionAndLoop:/^(?:if|for|while)\s*\(/,ElseCondition:/^[\s\r\n\t]*else(?:\s*{|[\s\t]+if\()?/},o={parse:function(e,n){var r=n.length;for(var i=0;i<r;i++){var o=n.substr(i,1);if(o===t){this._handleString(e,n,e.segmentIndex,i-e.segmentIndex);var u=n.substr(i+1,1);u===t?(this._handleEscape(e,t,e.segmentIndex),i=e.segmentIndex-1):u==="}"?(this._handleEscape(e,u,e.segmentIndex),i=e.segmentIndex-1):u==="{"?(this._handleScriptBlock(e,n,i+1),i=e.segmentIndex-1):s.VariableFirstChar.test(u)&&(s.ConditionAndLoop.test(n.substr(i+1))?this._handleCondition(e,n,i+1):this._handleVariable(e,n,i+1),i=e.segmentIndex-1)}else o==="}"&&e.conditionOpeningBraceCount>0&&(this._handleCloseBrace(e,n,i),s.ElseCondition.test(n.substr(e.segmentIndex))&&this._handleCondition(e,n,i+1),i=e.segmentIndex-1)}return e.segmentIndex<r&&this._handleString(e,n,e.segmentIndex,r-e.segmentIndex),e.segments},_handleString:function(e,t,n,r){if(r==0)return;e.segments[e.segments.length]={segmentType:i.String,content:t.substr(n,r)},e.segmentIndex=n+r},_handleEscape:function(e,t,n){e.segmentIndex=n+2,e.segments[e.segments.length]={segmentType:i.String,content:t}},_handleVariable:function(e,t,n){var r=t.substr(n),o=s.Variable.exec(r)[0];e.segmentIndex=n+o.length,e.segments[e.segments.length]={segmentType:i.Variable,content:o}},_handleScriptBlock:function(e,t,n){var r=this._getScriptBlockLength(t,n);e.segmentIndex=n+r,e.segments[e.segments.length]={segmentType:i.ScriptBlock,content:t.substr(n+1,r-2)},e.conditionOpeningBraceCount++},_getScriptBlockLength:function(e,t){var n=0;for(var r=t;r<e.length;r++){var i=e.substr(r,1);if(i==="{")n++;else if(i==="}"){if(--n===0)return r-t+1;n--}}throw"no matches found }"},_handleCondition:function(e,t,n){var r=t.substr(n),s=r.indexOf("{");e.segments[e.segments.length]={segmentType:i.ScriptBlock,content:r.substr(0,s+1)},e.segmentIndex=n+s+1,e.conditionOpeningBraceCount++},_handleCloseBrace:function(e,t,n){this._handleString(e,t,e.segmentIndex,n-e.segmentIndex),e.segments[e.segments.length]={segmentType:i.ScriptBlock,content:t.substr(n,1)},e.segmentIndex=n+1,e.conditionOpeningBraceCount--}},u={escape:function(e){return e.replace(/\\/g,"\\\\").replace(/'/g,"\\'").replace(/[\n\r]/g,"")},toFunctionContent:function(e){var t="var __c='';with(m||{}){";for(var n=0;n<e.length;n++)if(e[n].segmentType===i.String)t+="__c = __c+'"+this.escape(e[n].content)+"';";else if(e[n].segmentType===i.Variable){var r=e[n].content;t+="if(typeof "+r+" !== 'undefined'){",t+="__c = __c+"+r+";}"}else e[n].segmentType===i.ScriptBlock&&(t+=e[n].content);return t+="};return __c;",t}};n.use=function(e){t=e},typeof module!="undefined"&&module.exports?module.exports=n:(e.kino=e.kino?e.kino:{},e.kino.razor=n)})(window)
+;(function(e){"use strict";var t="@",n=function(e,t){if(arguments.length==1)return r(e);var i;return typeof e=="function"?i=e:i=r(e),i.call(null,n.HtmlHelper,t)},r=function(e,t){var n={segments:[],segmentIndex:0,conditionOpeningBraceCount:0},r=o.parse(n,e),i=u.toFunctionContent(r);return new Function("Html","m",i)};n.HtmlHelper={escape:function(e){return(""+e).replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#x27;").replace(/\//g,"&#x2F;")}};var i={String:0,Variable:1,ScriptBlock:2},s={VariableFirstChar:/^[\(_a-zA-Z]/,Variable:/^(?:(?:\()(?:new\s+)?[_a-zA-Z0-9]+(?:\.|[-\+*\/^=<>?:]|[\[\(][^\]\)]*[\]\)]|[_a-zA-Z0-9]+)*(?:\))|(?:new\s+)?[_a-zA-Z0-9]+(?:\.|[\[\(][^\]\)]*[\]\)]|[_a-zA-Z0-9]+)*)/,ConditionAndLoop:/^(?:if|for|while)\s*\(/,ElseCondition:/^[\s\r\n\t]*else(?:\s*{|[\s\t]+if\()?/},o={parse:function(e,n){var r=n.length;for(var i=0;i<r;i++){var o=n.substr(i,1);if(o===t){this._handleString(e,n,e.segmentIndex,i-e.segmentIndex);var u=n.substr(i+1,1);u===t?(this._handleEscape(e,t,e.segmentIndex),i=e.segmentIndex-1):u==="}"?(this._handleEscape(e,u,e.segmentIndex),i=e.segmentIndex-1):u==="{"?(this._handleScriptBlock(e,n,i+1),i=e.segmentIndex-1):s.VariableFirstChar.test(u)&&(s.ConditionAndLoop.test(n.substr(i+1))?this._handleCondition(e,n,i+1):this._handleVariable(e,n,i+1),i=e.segmentIndex-1)}else o==="}"&&e.conditionOpeningBraceCount>0&&(this._handleCloseBrace(e,n,i),s.ElseCondition.test(n.substr(e.segmentIndex))&&this._handleCondition(e,n,i+1),i=e.segmentIndex-1)}return e.segmentIndex<r&&this._handleString(e,n,e.segmentIndex,r-e.segmentIndex),e.segments},_handleString:function(e,t,n,r){if(r==0)return;e.segments[e.segments.length]={segmentType:i.String,content:t.substr(n,r)},e.segmentIndex=n+r},_handleEscape:function(e,t,n){e.segmentIndex=n+2,e.segments[e.segments.length]={segmentType:i.String,content:t}},_handleVariable:function(e,t,n){var r=t.substr(n),o=s.Variable.exec(r)[0];e.segmentIndex=n+o.length,e.segments[e.segments.length]={segmentType:i.Variable,content:o}},_handleScriptBlock:function(e,t,n){var r=this._getScriptBlockLength(t,n);e.segmentIndex=n+r,e.segments[e.segments.length]={segmentType:i.ScriptBlock,content:t.substr(n+1,r-2)},e.conditionOpeningBraceCount++},_getScriptBlockLength:function(e,t){var n=0;for(var r=t;r<e.length;r++){var i=e.substr(r,1);if(i==="{")n++;else if(i==="}"){if(--n===0)return r-t+1;n--}}throw"no matches found }"},_handleCondition:function(e,t,n){var r=t.substr(n),s=r.indexOf("{");e.segments[e.segments.length]={segmentType:i.ScriptBlock,content:r.substr(0,s+1)},e.segmentIndex=n+s+1,e.conditionOpeningBraceCount++},_handleCloseBrace:function(e,t,n){this._handleString(e,t,e.segmentIndex,n-e.segmentIndex),e.segments[e.segments.length]={segmentType:i.ScriptBlock,content:t.substr(n,1)},e.segmentIndex=n+1,e.conditionOpeningBraceCount--}},u={escape:function(e){return e.replace(/\\/g,"\\\\").replace(/'/g,"\\'").replace(/[\n\r]/g,"")},toFunctionContent:function(e){var t="var __c='';with(m||{}){";for(var n=0;n<e.length;n++)if(e[n].segmentType===i.String)t+="__c = __c+'"+this.escape(e[n].content)+"';";else if(e[n].segmentType===i.Variable){var r=e[n].content;t+="if(typeof "+r+" !== 'undefined'){",t+="__c = __c+"+r+";}"}else e[n].segmentType===i.ScriptBlock&&(t+=e[n].content);return t+="};return __c;",t}};n.use=function(e){t=e},typeof module!="undefined"&&module.exports?module.exports=n:(e.kino=e.kino?e.kino:{},e.kino.razor=n)})(window)
View
24 test/kino.razor.test.js
@@ -118,10 +118,7 @@ test("It should use new XXX() as variable", function () {
equal(/^now\sis\s\d+$/.test(str), true);
});
-test("fix bug with ')'", function () {
- var str = kino.razor("Address:(@Address)", { Address: "test load" });
- equal(str, "Address:(test load)");
-});
+
test("use custom symbol instead of '@'", function () {
kino.razor.use("&");
@@ -144,3 +141,22 @@ test("@(i) style should support operation", function () {
var str = kino.razor("@(i+1)", { i: 1 });
equal(str, "2");
});
+
+
+module("bug fix", {
+ setup: function () {
+ kino.razor.use("@");
+ }
+});
+
+
+test("fix bug with ')'", function () {
+ var str = kino.razor("Address:(@Address)", { Address: "test load" });
+ equal(str, "Address:(test load)");
+});
+
+test("support variable name like @rid_child", function () {
+ var templateStr = "@{var r_child_id = 'rid_1';}<hello>@r_child_id</hello>";
+ var result = kino.razor(templateStr, {});
+ equal(result, "<hello>rid_1</hello>");
+});
Please sign in to comment.
Something went wrong with that request. Please try again.