This repository has been archived by the owner on Apr 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay-table.htc
272 lines (215 loc) · 6.31 KB
/
display-table.htc
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<!DOCTYPE html><!-- DOCTYPE is needed to make IE version detection possible. -->
<public:component lightweight="true"><public:attach event="ondocumentready" onevent="_(element)" />
<script>
/**
* display-table.htc by Marat Tanalin
* http://tanalin.com/en/projects/display-table-htc/
* @version 2011-11-25
*/
function _(element) {
var d = element.document;
// Exit if {display: table} is natively supported (IE8+).
// See http://tanalin.com/en/articles/ie-version-js/
if (d.querySelector) {
return;
}
var rspace = /\s+/g,
prefix = 'dt-';
// If IE7+. See http://tanalin.com/en/articles/ie-version-js/
if (window.XMLHttpRequest) {
// IE6 can't read properties with leading dash, but can without,
// so using full prefix in IE7, and no-leading-dash one in IE6.
prefix = '-' + prefix;
}
var getCssValue = function(el, property) {
return el.currentStyle.getAttribute(prefix + property);
};
var inArray = function(needle, haystack) {
var i = haystack.length;
while (i--) {
if (needle === haystack[i]) {
return true;
}
}
return false;
};
var trim = function(str) {
return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
};
var hasClass = function(elem, className) {
return (' ' + elem.className.replace(rspace, ' ') + ' ').indexOf(' ' + className + ' ') != -1;
};
var addClass = function(elem, className) {
className = trim(className);
if (!className.length) {
return;
}
if (rspace.test(className)) {
var classes = className.split(rspace),
i = classes.length;
while (i--) {
addClass(elem, classes[i]);
}
return;
}
if (elem.className.length) {
if (!hasClass(elem, className)) {
elem.className += ' ' + className;
}
}
else {
elem.className = className;
}
};
var getChildren = function(elem) {
var node = elem.firstChild,
elems = [];
while (node) {
if (1 === node.nodeType) {
elems.push(node);
}
node = node.nextSibling;
}
return elems;
};
var getFirstChildElement = function(elem) {
var node = elem.firstChild;
while (node) {
if (1 === node.nodeType) {
return node;
}
node = node.nextSibling;
}
return null;
};
var moveChildNodes = function(src, dst) {
while (src.firstChild) {
dst.appendChild(src.firstChild);
}
};
var addFirstLastClass = function(outElem, inElem, className) {
addClass(outElem, inElem.nodeName + '-' + className + ' ' + className);
};
var copyClasses = function(src, dst) {
addClass(dst, src.nodeName + ' ' + src.className);
};
// Filters elements that can be converted to table elements.
var filterElements = function(elems) {
var filtered = [],
count = elems.length;
for (var i = 0; i < count; i++) {
var elem = elems[i];
if ('HR' !== elem.nodeName) {
filtered.push(elem);
}
}
return filtered;
};
var generateElements = function(inContainer, outElemNodeName, action, outContainer) {
var inElems = filterElements(getChildren(inContainer)),
count = inElems.length;
if (!count) {
return;
}
var outElems = [],
lastIndex = count - 1;
for (var i = 0; i < count; i++) {
var inElem = inElems[i],
outElem = d.createElement(outElemNodeName);
outElem.id = inElem.id;
copyClasses(inElem, outElem);
action(inElem, outElem);
outContainer.appendChild(outElem);
outElems.push(outElem);
}
addFirstLastClass(outElems[0], inElems[0], 'first');
addFirstLastClass(outElems[lastIndex], inElems[lastIndex], 'last');
if (!lastIndex) {
addClass(outElems[0], inElems[0].nodeName + '-first-last');
}
};
var generateRowCells = function(inRow, outRow) {
generateElements(inRow, 'td', moveChildNodes, outRow);
};
var generateRows = function(inTable, outTbody) {
var firstChildEl = getFirstChildElement(inTable);
// If cells are direct children of table.
if ( firstChildEl && ('table-cell' === getCssValue(firstChildEl, 'display')) ) {
var outRow = d.createElement('tr');
outTbody.appendChild(outRow);
generateRowCells(inTable, outRow);
}
else {
generateElements(inTable, 'tr', generateRowCells, outTbody);
}
};
var getTablyElements = function() {
var elems = d.body.getElementsByTagName('*'),
i = elems.length,
tables = [];
while (i--) {
var elem = elems[i];
if ('table' === getCssValue(elem, 'display')) {
tables.push(elem);
}
}
return tables;
};
var processTable = function(inTable) {
var inNodeName = inTable.nodeName;
// Exit if element to process is already a table element.
if (inArray(inNodeName, ['TABLE', 'TR', 'TD', 'TH', 'TBODY', 'THEAD', 'TFOOT'])) {
return;
}
var outTable = d.createElement('table'),
outTbody = d.createElement('tbody');
var borderSpacing = getCssValue(inTable, 'border-spacing');
outTable.cellSpacing = null === borderSpacing
? 0
: parseInt(borderSpacing, 10);
outTable.cellPadding = 0;
// Unlike HTML-tables, default vertical align for CSS-table cells
// is 'baseline' (as for normal text elements).
outTbody.vAlign = 'baseline';
copyClasses(inTable, outTable);
generateRows(inTable, outTbody);
outTable.appendChild(outTbody);
// If element cannot be replaced with table due to its semantics/functioning,
// then we insert table inside the element, replacing its contents.
if (inArray(inNodeName, ['DT', 'DD', 'LI', 'FORM', 'A'])) {
inTable.innerHTML = '';
inTable.appendChild(outTable);
if ('A' === inNodeName) {
// Default link cursor is displayed unstably for link that contains
// table, so we set it explicitly.
if ('auto' === inTable.currentStyle.cursor) {
inTable.style.cursor = 'pointer';
}
// Table inside link is unclickable in IE, so we generate click
// event for link manually when generated table iself is clicked.
outTable.onclick = function() {
this.parentNode.click();
}
}
}
else {
outTable.id = inTable.id;
inTable.replaceNode(outTable);
}
};
// If display-table.htc is attached to HTML or BODY element,
// process all elements that have {-dt-display: table}.
if (inArray(element.nodeName, ['HTML', 'BODY'])) {
var tables = getTablyElements(),
i = tables.length;
while (i--) {
processTable(tables[i]);
}
}
// Process single element that display-table.htc is attached to.
else {
processTable(element);
}
}
</script>
</public:component>