Skip to content

Commit

Permalink
反斜杠
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudcome committed Jul 2, 2016
1 parent 27d2770 commit ad876d9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
6 changes: 2 additions & 4 deletions example/slash.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
<body ontouchstart="">

<script type="text/html" id="demo">
<p>{{item.remark}}</p>
<p>\{{item.remark}}</p>
<p>\\{{item.remark}}</p>
<span a="冻结原因:{{item.remark}}" b="冻结原因:\{{item.remark}}" c="冻结原因:\\{{item.remark}}"></span>
<p>\\{{item.remark}}{{item.remark}}</p>
<p a="\" b="\\" c="\{{item.remark}}{{item.remark}}"></p>
</script>

<script src="/node_modules/coolie.js/coolie.js"
Expand Down
2 changes: 1 addition & 1 deletion example/slash.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ console.log(tpl.render({
remark: '【】'
}
}));
console.log('<p>【】</p><p>{{item.remark}}</p><p>\\【】</p><span a="冻结原因:【】" b="冻结原因:{{item.remark}}" c="冻结原因:\\【】"></span>');
console.log('<p>\\{{item.remark}}</p>');
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "blear.classes.template",
"version": "1.0.28",
"version": "1.0.29",
"description": "指令类型模板引擎",
"scripts": {
"live": "browser-sync start --config bs-config.js",
Expand Down
51 changes: 29 additions & 22 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var staticDirectives = {};
var staticStatements = {};
var reOriginal = /^=/;
var reSafeKey = /^[a-z_$][a-z\d$_]*$/i;
var reUnExp = /([^\\]|^)\\$/;
var reUnExp = /\\$/;
var reDoubleBackSlash = /\\\\$/;
var IGNORE_SEP = '•';
var reStatement = /^#/;
Expand Down Expand Up @@ -68,19 +68,17 @@ var generateVarName = (function () {
/**
* 字符化,双引号
* @param value
* @param noQuote
*/
var textify = (function () {
var reStart = /^.*?:/;
var reEnd = /}$/;
var textify = function (value, noQuote) {
var ret = string.textify(value);

return function textify(value) {
var o = {
p: String(value)
};
if (noQuote) {
return ret;
}

return json.stringify(o).replace(reStart, '').replace(reEnd, '');
};
}());
return '"' + ret + '"';
};

var STATIC_METHODS = {};

Expand Down Expand Up @@ -474,12 +472,15 @@ var Template = Events.extend({
break;

case 'TEXT':
if (inUnExp) {
inUnExp = false;
expList[expList.length - 1] += '\\';
}

inUnExp = reUnExp.test(value);

if (inUnExp) {
value = value.slice(0, -1);
} else {
value = value.replace(reDoubleBackSlash, '\\');
}

expList.push(textify(value));
Expand Down Expand Up @@ -532,8 +533,6 @@ var _TAG_OPEN = Template.sole();
var _EXPR_OPEN = Template.sole();
var _STATEMENT_OPEN = Template.sole();
var _COMMENT = Template.sole();
// \{{varible}}
var _inText = Template.sole();
var _compileAttrs = Template.sole();
var _compileDirectives = Template.sole();
var _compileStatement = Template.sole();
Expand Down Expand Up @@ -591,26 +590,30 @@ pro[_parse] = function () {
var the = this;
var slices = [];
var token = the[_next]();
var inUnExp = false;

while (token && token.type !== 'EOF' && token.type !== 'TAG_CLOSE') {
var slice = the[_program]();

if (slice.type === 'text') {
if (inUnExp) {
inUnExp = false;
slices[slices.length - 1].value += '\\';
}

// 反斜杆
if (reUnExp.test(slice.value)) {
slice.value = slice.value.slice(0, -1);
the[_inText] = true;
} else {
slice.value = slice.value.replace(reDoubleBackSlash, '\\');
inUnExp = true;
}

slices.push(slice);
token = the[_next]();
continue;
}

if (slice.type === 'exp' && the[_inText]) {
the[_inText] = false;
if (slice.type === 'exp' && inUnExp) {
inUnExp = false;
slice.type = 'text';
slice.value = '{{' + slice.value + '}}';
slices.push(slice);
Expand All @@ -622,6 +625,10 @@ pro[_parse] = function () {
token = the[_next]();
}

if (inUnExp) {
slices[slices.length - 1].value += '\\';
}

return slices;
};

Expand Down Expand Up @@ -924,13 +931,13 @@ pro[_compileAttrs] = function (vnode) {
if (booleanAttrMap[name]) {
arttsList.push(the[_outputName] + ' += " " + (Boolean(' + value + ') ? ' + textify(name) + ' : "");');
} else {
arttsList.push(the[_outputName] + ' += " " + ' + textify(name) + ' + "=\\"" + ' + value + ' +"\\"";');
arttsList.push(the[_outputName] + ' += " " + ' + textify(name) + ' + "=\\"" + ' + value + ' + "\\"";');
}
}
// 常量
else {
if (value && value !== true) {
arttsList.push(the[_outputName] + ' += " " + ' + textify(name) + ' + "=\\"' + value + '\\"";');
arttsList.push(the[_outputName] + ' += " " + ' + textify(name) + ' + "=\\"" + ' + textify(value) + ' + "\\"";');
} else {
arttsList.push(the[_outputName] + ' += " " + ' + textify(name) + ';');
}
Expand Down

0 comments on commit ad876d9

Please sign in to comment.