123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>语法插件单元测试</title>
- <link rel="stylesheet" href="./js/qunit/qunit.css">
- <script src="./js/qunit/qunit.js"></script>
- <script src="../src/template.js"></script>
- <script src="../src/template-syntax.js"></script>
- <script id="default" type="text/html">{{value}}</script>
- <script id="noEscape" type="text/html">{{#value}}</script>
- <script id="include" type="text/html">{{include 'include-content'}}</script>
- <script id="include-content" type="text/html">{{#value}}</script>
- <script id="sandbox" type="text/html">{{typeof document}}</script>
- <script id="sandbox2" type="text/html">{{window.$sandbox = true}}</script>
- <script id="prototype" type="text/html">{{toString}}</script>
- <script id="html" type="text/html">""''\\</script>
- <script id="debug-render" type="text/html">{{a.b.c.d.e.f}}</script>
- <script id="debug-syntax" type="text/html">{{a b c d e f}}</script>
- <script id="helper" type="text/html">{{test value}}</script>
- <script>
- if (!window.console) {
- window.console = {
- log: function () {}
- }
- }
- test('基本', function () {
- var data = {value: 'hello <em>world</em>'};
- equal(typeof template.compile('{{value}}'), 'function', '编译成函数');
- equal(template('default', data), 'hello <em>world</em>', '编码输出');
- equal(template('noEscape', data), 'hello <em>world</em>', '原文输出');
- });
- test('特殊类型变量输出', function () {
- var data = {value: 'hello <em>world</em>'};
- equal(template('default', {value:function(){return 'hello world'}}), 'hello world', '函数类型运算后输出');
- equal(template('default', {value:0}), '0', 'Number类型输出');
- equal(template('default', {value:false}), '', 'Boolean(false)类型输出空值');
- equal(template('default', {value:true}), '', 'Boolean(true)类型输出空值');
- equal(template('default', {value:{}}), '', 'Object类型输出空值');
- });
- test('特殊字符输出', function () {
- equal(template('html', {}), '""\'\'\\\\', '编码输出js特殊字符');
- });
- test('特殊字段名输出', function () {
- equal(template('prototype', {toString: 'hello world'}), 'hello world', '避免输出出错的值');
- });
- test('XSS防范测试', function () {
- equal(template('default', {value:'<>"\'&'}), '<>"'&', 'HTML字符转义');
- });
- test('空值处理', function () {
- equal(template('default', {value:''}), '', '空字符串输出');
- equal(template('default', {}), '', 'undefined转成空值');
- equal(template('default', {value:null}), '', 'null转成空值');
- });
- test('内置方法', function () {
- var data = {value: 'hello <em>world</em>'};
- equal(template('include', data), 'hello <em>world</em>', 'include');
- });
- test('辅助方法', function () {
- var data = {value: 'hello world'};
- template.helper('test', function (content) {
- return '<em>' + content + '</em>';
- });
- equal(template('helper', data), '<em>hello world</em>', 'helper');
- });
- test('沙箱', function () {
- equal(template('sandbox', {}), 'undefined', '拒绝读取外部对象');
- template('sandbox2', {});
- equal(window.$sandbox, undefined, '防范污染外部对象');
- });
- test('调试', function () {
- var onerror = template.onerror;
- var error = null;
- template.onerror = function (e) {
- console.log(e)
- error = e;
- };
- template('debug-render-xxxxxxxxx', {});
- deepEqual({
- name: error.name,
- message: error.message
- }, {
- name: 'Render Error',
- message: 'No Template'
- }, '没有找到模板');
- error = null;
- template.onerror = function (e) {
- console.log(e)
- error = e;
- };
- template('debug-render', {});
- deepEqual({
- name: error.name,
- line: error.line
- }, {
- name: 'Render Error',
- line: 1
- }, '渲染错误调试');
- error = null;
- template.onerror = function (e) {
- console.log(e)
- error = e;
- };
- template('debug-syntax', {});
- deepEqual({
- name: error.name
- }, {
- name: 'Syntax Error'
- }, '语法错误调试');
- template.onerror = onerror;
- });
- test('配置功能', function () {
- template.isEscape = false;
- template.openTag = '<<';
- template.closeTag = '>>';
- var data = {value: 'hello <em>world</em>'};
- equal(template.compile('<<value>>')(data), 'hello <em>world</em>', '自定义界定符');
- equal(template.compile('<<#value>>')(data), 'hello <em>world</em>', '关闭默认编码输出');
- template.isEscape = true;
- template.openTag = '{{';
- template.closeTag = '}}';
- });
- </script>
- </head>
- <body>
- <div id="qunit"></div>
- <div id="qunit-fixture">test markup</div>
- </body>
- </html>
|