test-simple.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>语法插件单元测试</title>
  6. <link rel="stylesheet" href="./js/qunit/qunit.css">
  7. <script src="./js/qunit/qunit.js"></script>
  8. <script src="../src/template.js"></script>
  9. <script src="../src/template-syntax.js"></script>
  10. <script id="default" type="text/html">{{value}}</script>
  11. <script id="noEscape" type="text/html">{{#value}}</script>
  12. <script id="include" type="text/html">{{include 'include-content'}}</script>
  13. <script id="include-content" type="text/html">{{#value}}</script>
  14. <script id="sandbox" type="text/html">{{typeof document}}</script>
  15. <script id="sandbox2" type="text/html">{{window.$sandbox = true}}</script>
  16. <script id="prototype" type="text/html">{{toString}}</script>
  17. <script id="html" type="text/html">""''\\</script>
  18. <script id="debug-render" type="text/html">{{a.b.c.d.e.f}}</script>
  19. <script id="debug-syntax" type="text/html">{{a b c d e f}}</script>
  20. <script id="helper" type="text/html">{{test value}}</script>
  21. <script>
  22. if (!window.console) {
  23. window.console = {
  24. log: function () {}
  25. }
  26. }
  27. test('基本', function () {
  28. var data = {value: 'hello <em>world</em>'};
  29. equal(typeof template.compile('{{value}}'), 'function', '编译成函数');
  30. equal(template('default', data), 'hello &#60;em&#62;world&#60;/em&#62;', '编码输出');
  31. equal(template('noEscape', data), 'hello <em>world</em>', '原文输出');
  32. });
  33. test('特殊类型变量输出', function () {
  34. var data = {value: 'hello <em>world</em>'};
  35. equal(template('default', {value:function(){return 'hello world'}}), 'hello world', '函数类型运算后输出');
  36. equal(template('default', {value:0}), '0', 'Number类型输出');
  37. equal(template('default', {value:false}), '', 'Boolean(false)类型输出空值');
  38. equal(template('default', {value:true}), '', 'Boolean(true)类型输出空值');
  39. equal(template('default', {value:{}}), '', 'Object类型输出空值');
  40. });
  41. test('特殊字符输出', function () {
  42. equal(template('html', {}), '""\'\'\\\\', '编码输出js特殊字符');
  43. });
  44. test('特殊字段名输出', function () {
  45. equal(template('prototype', {toString: 'hello world'}), 'hello world', '避免输出出错的值');
  46. });
  47. test('XSS防范测试', function () {
  48. equal(template('default', {value:'<>"\'&'}), '&#60;&#62;&#34;&#39;&#38;', 'HTML字符转义');
  49. });
  50. test('空值处理', function () {
  51. equal(template('default', {value:''}), '', '空字符串输出');
  52. equal(template('default', {}), '', 'undefined转成空值');
  53. equal(template('default', {value:null}), '', 'null转成空值');
  54. });
  55. test('内置方法', function () {
  56. var data = {value: 'hello <em>world</em>'};
  57. equal(template('include', data), 'hello <em>world</em>', 'include');
  58. });
  59. test('辅助方法', function () {
  60. var data = {value: 'hello world'};
  61. template.helper('test', function (content) {
  62. return '<em>' + content + '</em>';
  63. });
  64. equal(template('helper', data), '<em>hello world</em>', 'helper');
  65. });
  66. test('沙箱', function () {
  67. equal(template('sandbox', {}), 'undefined', '拒绝读取外部对象');
  68. template('sandbox2', {});
  69. equal(window.$sandbox, undefined, '防范污染外部对象');
  70. });
  71. test('调试', function () {
  72. var onerror = template.onerror;
  73. var error = null;
  74. template.onerror = function (e) {
  75. console.log(e)
  76. error = e;
  77. };
  78. template('debug-render-xxxxxxxxx', {});
  79. deepEqual({
  80. name: error.name,
  81. message: error.message
  82. }, {
  83. name: 'Render Error',
  84. message: 'No Template'
  85. }, '没有找到模板');
  86. error = null;
  87. template.onerror = function (e) {
  88. console.log(e)
  89. error = e;
  90. };
  91. template('debug-render', {});
  92. deepEqual({
  93. name: error.name,
  94. line: error.line
  95. }, {
  96. name: 'Render Error',
  97. line: 1
  98. }, '渲染错误调试');
  99. error = null;
  100. template.onerror = function (e) {
  101. console.log(e)
  102. error = e;
  103. };
  104. template('debug-syntax', {});
  105. deepEqual({
  106. name: error.name
  107. }, {
  108. name: 'Syntax Error'
  109. }, '语法错误调试');
  110. template.onerror = onerror;
  111. });
  112. test('配置功能', function () {
  113. template.isEscape = false;
  114. template.openTag = '<<';
  115. template.closeTag = '>>';
  116. var data = {value: 'hello <em>world</em>'};
  117. equal(template.compile('<<value>>')(data), 'hello <em>world</em>', '自定义界定符');
  118. equal(template.compile('<<#value>>')(data), 'hello <em>world</em>', '关闭默认编码输出');
  119. template.isEscape = true;
  120. template.openTag = '{{';
  121. template.closeTag = '}}';
  122. });
  123. </script>
  124. </head>
  125. <body>
  126. <div id="qunit"></div>
  127. <div id="qunit-fixture">test markup</div>
  128. </body>
  129. </html>