Skip to content

Commit

Permalink
修改this为window
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxiao committed Oct 9, 2012
1 parent 2b58f00 commit 2526efd
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 6 deletions.
9 changes: 5 additions & 4 deletions baiduTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
* @email [email protected]
*/

;(function(){
;(function(window){

//取得浏览器环境的baidu命名空间,非浏览器环境符合commonjs规范exports出去
var baidu = typeof module === 'undefined' ? (this.baidu = this.baidu || {}) : module.exports;
//修正在nodejs环境下,采用baiduTemplate变量名
var baidu = typeof module === 'undefined' ? (window.baidu = window.baidu || {}) : module.exports;

//模板函数(放置于baidu.template命名空间下)
baidu.template = function(str, data){
Expand All @@ -22,7 +23,7 @@
var fn = (function(){

//判断如果没有document,则为非浏览器环境
if(!this.document){
if(!window.document){
return bt._compile(str);
};

Expand Down Expand Up @@ -206,4 +207,4 @@
return str;
};

})();
})(window);
10 changes: 8 additions & 2 deletions test.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
</head>
<body>
<h3>BaiduTemplate单元测试用例</h3>

<!-- 测试模板1开始 -->
<script id='t:_1234-abcd-1' type="text/template">
<br>
Expand Down Expand Up @@ -76,7 +75,14 @@ <h3>BaiduTemplate单元测试用例</h3>
16、a标签 <br>
单引问题:<a target='_blank' href='http://www.baidu.com ' onclick='alert("test");'>test</a><br>
双引问题:<a target="_blank" href="http://www.baidu.com" onclick="alert('test');">test</a><br>

17、定义变量:
<%var varTest1 = value1%>
varTest1: <%=varTest1;%>
<br>
18、定义变量容错:
<%var varTest2 = value2;%>
varTest2: <%=varTest2%>
<br>
</script>
<!-- 测试模板1结束 -->

Expand Down
83 changes: 83 additions & 0 deletions test2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="keywords" content="baidu template" />
<meta name="description" content="javascript前端模板" />
<title>test</title>
<script type="text/javascript" src="./baiduTemplate.js"></script>
</head>
<body>
<h3>BaiduTemplate单元测试用例</h3>
<!-- 测试模板1开始 -->
<script id='t:_1234-abcd-1' type="text/template">
17、定义变量容错:<br>
<%var tttt = value1%>
1111111<%=tttt%>
<br>

18、定义变量容错:<br>
<%var wwww = value2;%>
1111111<%=wwww%>
<br>
</script>
<!-- 测试模板1结束 -->

<div id="results"></div>

<script type="text/javascript">
window.onload=function(){
//测试数据,对应每个用例
var data={
value1:'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span>',
value2:'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span>',
value3:'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span>',
value4:'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span>',
value5:'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span>',
value6:'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span>',
value7:'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span><span style="color:red;">\\\'\"</span>',
value8:'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span><span style="color:red;">\\\'\"</span>',
value9:'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span>',
value10:'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span>',
value14:'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span>',
value15:'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span>',
value16:['<span style="color:red;">这是value</span>',123,'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span>'],
value17:['<span style="color:red;">这是value</span>',123,'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span>']
};

//使用
var bat=baidu.template;

//设置分隔符
//bat.LEFT_DELIMITER='<!';
//bat.RIGHT_DELIMITER='!>';

//设置默认是否转义
//bat.ESCAPE = false;

var timestart = new Date().getTime();

//输出函数
var fun=bat('t:_1234-abcd-1',data);


var timeend = new Date().getTime();

//alert('最大话编译一次时间:'+(timeend-timestart)+'毫秒');

timestart = new Date().getTime();

//输出HTML
//var html=bat('t:_1234-abcd-1',data);

timeend = new Date().getTime();

//alert('运行时间:'+(timeend-timestart)+'毫秒');

//显示结果
document.getElementById('results').innerHTML=fun;
}
</script>

</body>
</html>

0 comments on commit 2526efd

Please sign in to comment.