forked from tildeio/htmlbars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom-helper.js
338 lines (297 loc) · 9.97 KB
/
dom-helper.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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import Morph from "../morph/morph";
import AttrMorph from "../morph/attr-morph";
import {
buildHTMLDOM,
svgNamespace,
svgHTMLIntegrationPoints
} from "./dom-helper/build-html-dom";
import {
addClasses,
removeClasses
} from "./dom-helper/classes";
import {
normalizeProperty
} from "./dom-helper/prop";
import { isAttrRemovalValue } from "./dom-helper/prop";
var doc = typeof document === 'undefined' ? false : document;
var deletesBlankTextNodes = doc && (function(document){
var element = document.createElement('div');
element.appendChild( document.createTextNode('') );
var clonedElement = element.cloneNode(true);
return clonedElement.childNodes.length === 0;
})(doc);
var ignoresCheckedAttribute = doc && (function(document){
var element = document.createElement('input');
element.setAttribute('checked', 'checked');
var clonedElement = element.cloneNode(false);
return !clonedElement.checked;
})(doc);
// This is not the namespace of the element, but of
// the elements inside that elements.
function interiorNamespace(element){
if (
element &&
element.namespaceURI === svgNamespace &&
!svgHTMLIntegrationPoints[element.tagName]
) {
return svgNamespace;
} else {
return null;
}
}
// The HTML spec allows for "omitted start tags". These tags are optional
// when their intended child is the first thing in the parent tag. For
// example, this is a tbody start tag:
//
// <table>
// <tbody>
// <tr>
//
// The tbody may be omitted, and the browser will accept and render:
//
// <table>
// <tr>
//
// However, the omitted start tag will still be added to the DOM. Here
// we test the string and context to see if the browser is about to
// perform this cleanup.
//
// http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html#optional-tags
// describes which tags are omittable. The spec for tbody and colgroup
// explains this behavior:
//
// http://www.whatwg.org/specs/web-apps/current-work/multipage/tables.html#the-tbody-element
// http://www.whatwg.org/specs/web-apps/current-work/multipage/tables.html#the-colgroup-element
//
var omittedStartTagChildTest = /<([\w:]+)/;
function detectOmittedStartTag(string, contextualElement){
// Omitted start tags are only inside table tags.
if (contextualElement.tagName === 'TABLE') {
var omittedStartTagChildMatch = omittedStartTagChildTest.exec(string);
if (omittedStartTagChildMatch) {
var omittedStartTagChild = omittedStartTagChildMatch[1];
// It is already asserted that the contextual element is a table
// and not the proper start tag. Just see if a tag was omitted.
return omittedStartTagChild === 'tr' ||
omittedStartTagChild === 'col';
}
}
}
function buildSVGDOM(html, dom){
var div = dom.document.createElement('div');
div.innerHTML = '<svg>'+html+'</svg>';
return div.firstChild.childNodes;
}
/*
* A class wrapping DOM functions to address environment compatibility,
* namespaces, contextual elements for morph un-escaped content
* insertion.
*
* When entering a template, a DOMHelper should be passed:
*
* template(context, { hooks: hooks, dom: new DOMHelper() });
*
* TODO: support foreignObject as a passed contextual element. It has
* a namespace (svg) that does not match its internal namespace
* (xhtml).
*
* @class DOMHelper
* @constructor
* @param {HTMLDocument} _document The document DOM methods are proxied to
*/
function DOMHelper(_document){
this.document = _document || document;
if (!this.document) {
throw new Error("A document object must be passed to the DOMHelper, or available on the global scope");
}
this.namespace = null;
}
var prototype = DOMHelper.prototype;
prototype.constructor = DOMHelper;
prototype.getElementById = function(id, rootNode) {
rootNode = rootNode || this.document;
return rootNode.getElementById(id);
};
prototype.insertBefore = function(element, childElement, referenceChild) {
return element.insertBefore(childElement, referenceChild);
};
prototype.appendChild = function(element, childElement) {
return element.appendChild(childElement);
};
prototype.childAt = function(element, indices) {
var child = element;
for (var i = 0; i < indices.length; i++) {
child = child.childNodes.item(indices[i]);
}
return child;
};
prototype.childAtIndex = function(element, index) {
return element.childNodes.item(index);
};
prototype.appendText = function(element, text) {
return element.appendChild(this.document.createTextNode(text));
};
prototype.setAttribute = function(element, name, value) {
element.setAttribute(name, String(value));
};
prototype.setAttributeNS = function(element, namespace, name, value) {
element.setAttributeNS(namespace, name, String(value));
};
prototype.removeAttribute = function(element, name) {
element.removeAttribute(name);
};
prototype.setPropertyStrict = function(element, name, value) {
element[name] = value;
};
prototype.setProperty = function(element, name, value, namespace) {
var lowercaseName = name.toLowerCase();
if (element.namespaceURI === svgNamespace || lowercaseName === 'style') {
if (isAttrRemovalValue(value)) {
element.removeAttribute(name);
} else {
if (namespace) {
element.setAttributeNS(namespace, name, value);
} else {
element.setAttribute(name, value);
}
}
} else {
var normalized = normalizeProperty(element, name);
if (normalized) {
element[normalized] = value;
} else {
if (isAttrRemovalValue(value)) {
element.removeAttribute(name);
} else {
if (namespace) {
element.setAttributeNS(namespace, name, value);
} else {
element.setAttribute(name, value);
}
}
}
}
};
if (doc && doc.createElementNS) {
// Only opt into namespace detection if a contextualElement
// is passed.
prototype.createElement = function(tagName, contextualElement) {
var namespace = this.namespace;
if (contextualElement) {
if (tagName === 'svg') {
namespace = svgNamespace;
} else {
namespace = interiorNamespace(contextualElement);
}
}
if (namespace) {
return this.document.createElementNS(namespace, tagName);
} else {
return this.document.createElement(tagName);
}
};
} else {
prototype.createElement = function(tagName) {
return this.document.createElement(tagName);
};
}
prototype.addClasses = addClasses;
prototype.removeClasses = removeClasses;
prototype.setNamespace = function(ns) {
this.namespace = ns;
};
prototype.detectNamespace = function(element) {
this.namespace = interiorNamespace(element);
};
prototype.createDocumentFragment = function(){
return this.document.createDocumentFragment();
};
prototype.createTextNode = function(text){
return this.document.createTextNode(text);
};
prototype.createComment = function(text){
return this.document.createComment(text);
};
prototype.repairClonedNode = function(element, blankChildTextNodes, isChecked){
if (deletesBlankTextNodes && blankChildTextNodes.length > 0) {
for (var i=0, len=blankChildTextNodes.length;i<len;i++){
var textNode = this.document.createTextNode(''),
offset = blankChildTextNodes[i],
before = this.childAtIndex(element, offset);
if (before) {
element.insertBefore(textNode, before);
} else {
element.appendChild(textNode);
}
}
}
if (ignoresCheckedAttribute && isChecked) {
element.setAttribute('checked', 'checked');
}
};
prototype.cloneNode = function(element, deep){
var clone = element.cloneNode(!!deep);
return clone;
};
prototype.createAttrMorph = function(element, attrName, namespace){
return new AttrMorph(element, attrName, this, namespace);
};
prototype.createUnsafeAttrMorph = function(element, attrName, namespace){
var morph = this.createAttrMorph(element, attrName, namespace);
morph.escaped = false;
return morph;
};
prototype.createMorph = function(parent, start, end, contextualElement){
if (!contextualElement && parent.nodeType === 1) {
contextualElement = parent;
}
return new Morph(parent, start, end, this, contextualElement);
};
prototype.createUnsafeMorph = function(parent, start, end, contextualElement){
var morph = this.createMorph(parent, start, end, contextualElement);
morph.escaped = false;
return morph;
};
// This helper is just to keep the templates good looking,
// passing integers instead of element references.
prototype.createMorphAt = function(parent, startIndex, endIndex, contextualElement){
var start = startIndex === -1 ? null : this.childAtIndex(parent, startIndex),
end = endIndex === -1 ? null : this.childAtIndex(parent, endIndex);
return this.createMorph(parent, start, end, contextualElement);
};
prototype.createUnsafeMorphAt = function(parent, startIndex, endIndex, contextualElement) {
var morph = this.createMorphAt(parent, startIndex, endIndex, contextualElement);
morph.escaped = false;
return morph;
};
prototype.insertMorphBefore = function(element, referenceChild, contextualElement) {
var start = this.document.createTextNode('');
var end = this.document.createTextNode('');
element.insertBefore(start, referenceChild);
element.insertBefore(end, referenceChild);
return this.createMorph(element, start, end, contextualElement);
};
prototype.appendMorph = function(element, contextualElement) {
var start = this.document.createTextNode('');
var end = this.document.createTextNode('');
element.appendChild(start);
element.appendChild(end);
return this.createMorph(element, start, end, contextualElement);
};
prototype.parseHTML = function(html, contextualElement) {
if (interiorNamespace(contextualElement) === svgNamespace) {
return buildSVGDOM(html, this);
} else {
var nodes = buildHTMLDOM(html, contextualElement, this);
if (detectOmittedStartTag(html, contextualElement)) {
var node = nodes[0];
while (node && node.nodeType !== 1) {
node = node.nextSibling;
}
return node.childNodes;
} else {
return nodes;
}
}
};
export default DOMHelper;