Skip to content

Commit

Permalink
Merge branch 'v4.7.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
aui committed May 1, 2017
2 parents 142c0f6 + 4ccc51f commit 45ef017
Show file tree
Hide file tree
Showing 17 changed files with 335 additions and 235 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## v4.7.0

1. 增加 `ignore` 配置,可以让模板编译器忽略指定的变量初始化
2. 增加 `htmlMinifierOptions` 配置,可对 [htmlMinifie](https://github.com/kangax/html-minifier) 压缩器进行配置

## v4.6.0

1. `$escape()` 函数提高 4 倍性能
Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,15 @@ var html = template.render('hi, <%=value%>.', {value: 'aui'});
// HTML 压缩器。仅在 NodeJS 环境下有效
htmlMinifier: htmlMinifier,

// HTML 压缩器配置
htmlMinifierOptions: {
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true,
// 运行时自动合并:rules.map(rule => rule.test)
ignoreCustomFragments: []
},

// 错误事件。仅在 bail 为 false 时生效
onerror: onerror,

Expand All @@ -520,13 +529,16 @@ var html = template.render('hi, <%=value%>.', {value: 'aui'});
// 默认后缀名。如果没有后缀名,则会自动添加 extname
extname: '.art',

// 忽略的变量。指定模板编译器忽略对指定的变量预先声明
ignore: [],

// 导入的模板变量
imports: {
$each: each,
$escape: escape,
$include: include
}
};
}
```

## 兼容性
Expand Down
7 changes: 7 additions & 0 deletions example/web-ie-compatible/es5-sham.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions example/web-ie-compatible/es5-shim.min.js

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions example/web-ie-compatible/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
<meta charset="UTF-8">
<title>IE</title>

<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.7/es5-shim.min.js"></script>
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.7/es5-shim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.7/es5-sham.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/json3/3.3.2/json3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/json3/3.3.2/json3.min.js"></script>-->
<script src="es5-shim.min.js"></script>
<script src="es5-sham.min.js"></script>
<script src="json3.min.js"></script>

<script src="../../lib/template-web.js"></script>
</head>
Expand Down
17 changes: 17 additions & 0 deletions example/web-ie-compatible/json3.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

106 changes: 1 addition & 105 deletions lib/imports.js
Original file line number Diff line number Diff line change
@@ -1,105 +1 @@
/*! art-template@runtime | https://github.com/aui/art-template */


/**
* 迭代器,支持数组与对象
* @param {array|Object} data
* @param {function} callback
*/
var each = function each(data, callback) {
if (Array.isArray(data)) {
for (var i = 0, len = data.length; i < len; i++) {
callback(data[i], i, data);
}
} else {
for (var _i in data) {
callback(data[_i], _i);
}
}
};


// 将目标转成字符
var toString = function toString(value) {
if (typeof value !== 'string') {
if (value === undefined || value === null) {
value = '';
} else if (typeof value === 'function') {
value = toString(value.call(value));
} else {
value = JSON.stringify(value);
}
}

return value;
};


// 编码 HTML 内容
var ESCAPE_REG = /["&'<>]/;
var xmlEscape = function xmlEscape(content) {
var html = '' + content;
var regexResult = ESCAPE_REG.exec(html);
if (!regexResult) {
return content;
}

var result = '';
var i = void 0,
lastIndex = void 0,
char = void 0;
for (i = regexResult.index, lastIndex = 0; i < html.length; i++) {

switch (html.charCodeAt(i)) {
case 34:
char = '&#34;';
break;
case 38:
char = '&#38;';
break;
case 39:
char = '&#39;';
break;
case 60:
char = '&#60;';
break;
case 62:
char = '&#62;';
break;
default:
continue;
}

if (lastIndex !== i) {
result += html.substring(lastIndex, i);
}

lastIndex = i + 1;
result += char;
}

if (lastIndex !== i) {
return result + html.substring(lastIndex, i);
} else {
return result;
}
};


/**
* 编码模板输出的内容
* @param {any} content
* @return {string}
*/
var escape = function escape(content) {
return xmlEscape(toString(content));
};


var imports = {
$each: each,
$escape: escape
};


module.exports = imports;
module.exports = require('./runtime');
2 changes: 1 addition & 1 deletion lib/precompile.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const precompile = options => {

if (typeof imports !== 'string') {
throw Error('template.precompile(): "options.imports" is a file. Example:\n' +
'options: { imports: require.resolve("art-template/lib/imports") }\n');
'options: { imports: require.resolve("art-template/lib/runtime") }\n');
} else {
options.imports = require(imports);
}
Expand Down
Loading

0 comments on commit 45ef017

Please sign in to comment.