-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_pa_ch.html
187 lines (155 loc) · 5.22 KB
/
test_pa_ch.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<!--<script src="src/reindr.js" type="text/javascript"></script>-->
<style type="text/css">
#results div {
padding: 12px;
background: #ccc;
border: 1px solid black;
margin-bottom: 8px;
}
#results div.pass {
background: #C4FFA0;
border-color: #4ECB00;
color: #4ECB00;
font-size: 14px;
font-weight: bold;
}
#results div.fail {
background: #FFADAD;
border-color: #FF0000;
color: #FF0000;
font-size: 14px;
font-weight: bold;
}
</style>
<script type="text/javascript">
function isArray(obj) {
if (obj.constructor.toString().indexOf("Array") == -1){
return false;
}else{
return true;
}
}
Array.prototype.each = function(func){
for(var i=0; i < this.length; i++){
func(this[i]);
}
return this;
};
Object.prototype.forEach = function(func){
if(typeof(func) != 'function'){ return }
for(var prop in this){
if(this.hasOwnProperty(prop)){
func(prop, this[prop]);
}
}
}
Object.prototype.length = function(){
var count = 0;
this.forEach(function(){ count++ });
return count;
};
// #######################
var reindr = function(){
var supported_tags = ['div', 'p', 'span', 'a', 'ul', 'li', 'table', 'tr', 'td'];
var tag_function = function(tag, attrs, content){
var tag_attributes = [];
if(typeof(attrs) == 'object'){
for(var prop in attrs){
if(attrs.hasOwnProperty(prop)){
tag_attributes.push(prop + '="' + attrs[prop] + '"');
}
}
}
html = '';
html += '<' + tag;
if(tag_attributes.length > 0){
html += ' ' + tag_attributes.join(' ');
}
if(typeof(content) == 'string' && content.length > 0){
html += '>' + content + '</' + tag + '>';
}else{
html += '/>'
}
return html;
};
var render_stack = function(todo, saved, target){
saved = saved || [];
if(todo.length == 0){ return saved.join("\n"); }
var current = todo.shift();
if(typeof(target) == 'undefined' && supported_tags.indexOf(current) != -1){
saved.push([]);
return render_stack(todo, saved, current);
}else if(current == '/' + target){
saved = [tag_function(target, {}, saved.join("\n"))];
return render_stack(todo, saved);
}else{
saved.push(current);
return render_stack(todo, saved, target);
}
};
// ##########################################
var stack;
if(isArray(arguments[0])){
stack = Array.prototype.shift.apply(arguments);
}else{
stack = [];
}
if(arguments[0] == reindr){
return render_stack(stack);
} else {
stack.push(arguments[0]);
return function(){
return reindr(stack, arguments[0]);
}
}
}
var pass = function(desc) {
var result = "<div class=\"pass\">";
result += "<span>Pass: " + desc + "</span>";
result += "</div>";
$("#results").append(result);
};
var fail = function(desc, msg) {
var result = $("<div>").addClass("fail");
var desc = $("<span>").text("Failure: " + desc);
var msg = $("<span>").text(msg);
$("#results").append(result.append(msg).append(msg));
};
var assert_renders = function(rd, markup, desc){
//same markup without whitespaces
if(rd.replace(/\s/g,'') == markup.replace(/\s/g,'')){
pass(desc);
}else{
fail(desc, "expected: " + rd + " to be: " + markup);
}
};
$(function(){
var example1 = (reindr)('span')("lol")('/span')(reindr);
assert_renders(example1, '<span>lol</span>', "tag with content");
//var example2 = (r.span)((r.a)("myhouse"));
//// r.span(r.a("myhouse"));
//assert_renders(example2, "<span><a>myhouse</a></span>", "nested tag with content");
//var example3 = (r.span({class: 'droid'}))
// ("Robots");
//assert_renders(example3, '<span class="droid">Robots</span>', "tag with attributes");
// var example4 = (r.ul)
// ((r.li)("ichi"),
// (r.li)("ni"),
// (r.li)("san"));
//assert_renders(example4, '<ul><li>ichi</li><li>ni</li><li>san</li></ul>', "tag with multiple children with respective content");
});
</script>
</head>
<body>
<h1>Reindr tests</h1>
<div id="results">
</div>
<div id="target1" style="visibility: hidden"></div>
</body>
</html>