-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pa.html
166 lines (137 loc) · 4.65 KB
/
test_pa.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
<!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">
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;
};
// #######################
function reindr(){
var r = {};
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 tag_function_generator = function(tag){
return function(){
var args = Array.prototype.slice.apply(arguments);
var tag_attributes = args[0];
if(typeof(tag_attributes) == 'object'){
return function(){
return tag_function(tag, tag_attributes,
Array.prototype.join.apply(arguments, ["\n"]));
}
}else{
return tag_function(tag, {}, args.join("\n"));
}
}
};
var supported_tags = ['div', 'p', 'span', 'a', 'ul', 'li', 'table', 'tr', 'td'];
supported_tags.each(function(tag){ r[tag] = tag_function_generator(tag)});
return r;
}
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 r = reindr();
var example1 = (r.span)("lol");
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>