-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.js
177 lines (149 loc) · 3.69 KB
/
document.js
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
/**
* documentの再現モジュール
* 文字列化する能力しか持っていません
*/
//-- utility -------------------------
var create = Object.create;
function object(o) {
var n=create(o), i=1, l=arguments.length, p;
for (; i<l; ++i)
for (p in arguments[i])
n[p] = arguments[i][p];
return n;
}
function mix(proto) {
var i=1, l=arguments.length, p, mixin;
for (; i<l; ++i) {
mixin = arguments[i];
for (p in mixin) {
proto[p] = mixin[p];
}
}
}
//-- classes --------------------------
/**
* class Element
*
*/
function Element(name, children) {
this._name = name;
this._children = children || [];
}
Element.prototype = {
constructor: Element,
nodeType: 1,
appendChild: function(node){
this._children.push(node);
},
cloneNode: function(){
var i,l,cur;
var elem = new Element(this._name, this._children.concat());
for (i=0,l=elem._children.length; i<l; i++) {
cur = elem._children[i];
if (cur instanceof Element) {
elem._children[i] = cur.cloneNode();
}
}
return elem;
},
toString: function(){
var children;
if (this._children.length) {
children = this._children.map(function(v){return ""+v});
return '<'+this._name + this._attributes() + '>'
+ children.join('')
+ '</'+this._name+'>';
} else {
return '<'+this._name+' />';
}
},
_attributes: function(){
var attrs = [''], p;
for (p in this) switch (p) {
case 'nodeType': case 'cloneNode':
case 'toString': case 'appendChild':
case '_name': case '_attributes':
case 'constructor':
case '_children':
break;
case 'className':
attrs.push('class="' + escapeAttr(this[p]) + '"');
break;
case 'htmlFor':
attrs.push('for="' + escapeAttr(this[p]) + '"');
break;
default:
attrs.push(p + '="' + escapeAttr(this[p]) + '"');
}
return attrs.join(' ');
}
};
/**
* class DocumentFragment
*
*/
function DocumentFragment() {
Element.call(this, null);
}
DocumentFragment.prototype = object(Element.prototype, {
constructor: DocumentFragment,
nodeType: 11,
toString: function() {
if (this._children.length) {
return this._children.map(function(v){
return ""+v;
}).join('');
} else {
return '';
}
}
});
/**
* class Comment
*
*/
function Comment(text) {
this._text = text;
}
Comment.prototype = {
constructor: Comment,
nodeType: 8,
toString: function(){
return '<!--' + this._text + '-->';
}
};
//-- escaper ---------------------------------
function escapeElement(str) {
return (""+str).replace(/[&<>]/g, function($0){
return escapeElement.dic[$0];
});
}
escapeElement.dic = {
"&": "&",
"<": "<",
">": ">"
};
function escapeAttr(str) {
return (""+str).replace(/[&<>"]/g, function($0){
return escapeAttr.dic[$0];
});
}
escapeAttr.dic = {
"&": "&",
"<": "<",
">": ">",
'"': """
};
//-- exports ---------------------------------
this.createElement = function(name){
var el = new Element(name);
// console.log(el.constructor.name);
return el;
};
this.createTextNode = escapeElement;
this.createDocumentFragment = function(){
return new DocumentFragment;
};
this.createComment = function(str){
return new Comment(str);
};