-
Notifications
You must be signed in to change notification settings - Fork 49
/
dom-util.js
95 lines (83 loc) · 2.11 KB
/
dom-util.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
// Various DOM-related JS helper functions, client-side
var escape_HTML = (function() {
var text = txt('');
var div = elt('div');
div.appendChild(text);
return function(s) {
text.data = s;
return div.innerHTML;
};
})();
function elt_(d, n) {
return d.createElement(n);
}
function txt_(d, s) {
return d.createTextNode(s);
}
function elt(n) {
return elt_(document, n);
}
function txt(s) {
return txt_(document, s);
}
function elt_class(e, c) {
var s = elt(e);
s.className = c;
return s;
}
function elt_txt_class(e, t, c) {
var s = elt(e);
s.className = c;
s.appendChild(txt(t));
return s;
}
function a_txt_class_url(t, c, u) {
var a = elt_txt_class("a", t, c);
a.setAttribute("href", u);
return a;
}
function json_as_html_tree(j) {
return json_as_html_tree_(document, j);
}
function json_as_html_tree_(d, j) {
if (typeof(j) == 'object') {
if (j == null) {
return txt_(d, "null");
} else {
var table = elt_(d, 'table');
table.className = "json-msg";
var sk = sorted_keys(j);
for (var i in sk) {
var k = sk[i];
var row = elt_(d, 'tr');
var ke = elt_(d, 'td');
var ve = elt_(d, 'td');
row.className = "json-pair";
ke.className = "json-key";
ve.className = "json-val";
ke.appendChild(txt_(d, k));
ve.appendChild(json_as_html_tree_(d,j[k]));
row.appendChild(ke);
row.appendChild(ve);
table.appendChild(row);
}
return table;
}
} else {
return txt_(d, String(j));
}
}
function prepend_child(parent, node) {
var c = parent.firstChild;
if (c) {
parent.insertBefore(node, c);
} else {
parent.appendChild(node);
}
}
function prepend_child_and_expire(parent, node, max_children) {
prepend_child(parent, node);
if (parent.childNodes.length > max_children) {
parent.removeChild(parent.lastChild);
}
}