-
Notifications
You must be signed in to change notification settings - Fork 399
/
dom.js
executable file
·161 lines (128 loc) · 3.78 KB
/
dom.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
import { isClient, svg, vendors, win, doc } from 'config/environment';
import { html } from 'config/namespaces';
import { isString, isNumber } from 'utils/is';
let createElement, matches, div, methodNames, unprefixed, prefixed, i, j, makeFunction;
// Test for SVG support
if (!svg) {
/* istanbul ignore next */
createElement = (type, ns, extend) => {
if (ns && ns !== html) {
throw "This browser does not support namespaces other than http://www.w3.org/1999/xhtml. The most likely cause of this error is that you're trying to render SVG in an older browser. See http://ractive.js.org/support/#svgs for more information";
}
return extend ? doc.createElement(type, extend) : doc.createElement(type);
};
} else {
createElement = (type, ns, extend) => {
if (!ns || ns === html) {
return extend ? doc.createElement(type, extend) : doc.createElement(type);
}
return extend ? doc.createElementNS(ns, type, extend) : doc.createElementNS(ns, type);
};
}
export function createDocumentFragment() {
return doc.createDocumentFragment();
}
function getElement(input) {
let output;
if (!input || typeof input === 'boolean') {
return;
}
/* istanbul ignore next */
if (!win || !doc || !input) {
return null;
}
// We already have a DOM node - no work to do. (Duck typing alert!)
if (input.nodeType) {
return input;
}
// Get node from string
if (isString(input)) {
// try ID first
output = doc.getElementById(input);
// then as selector, if possible
if (!output && doc.querySelector) {
try {
output = doc.querySelector(input);
} catch (e) {
/* this space intentionally left blank */
}
}
// did it work?
if (output && output.nodeType) {
return output;
}
}
// If we've been given a collection (jQuery, Zepto etc), extract the first item
if (input[0] && input[0].nodeType) {
return input[0];
}
return null;
}
if (!isClient) {
matches = null;
} else {
div = createElement('div');
methodNames = ['matches', 'matchesSelector'];
makeFunction = function(methodName) {
return function(node, selector) {
return node[methodName](selector);
};
};
i = methodNames.length;
while (i-- && !matches) {
unprefixed = methodNames[i];
if (div[unprefixed]) {
matches = makeFunction(unprefixed);
} else {
j = vendors.length;
while (j--) {
prefixed = vendors[i] + unprefixed.substr(0, 1).toUpperCase() + unprefixed.substring(1);
if (div[prefixed]) {
matches = makeFunction(prefixed);
break;
}
}
}
}
// IE8... and apparently phantom some?
/* istanbul ignore next */
if (!matches) {
matches = function(node, selector) {
let parentNode, i;
parentNode = node.parentNode;
if (!parentNode) {
// empty dummy <div>
div.innerHTML = '';
parentNode = div;
node = node.cloneNode();
div.appendChild(node);
}
const nodes = parentNode.querySelectorAll(selector);
i = nodes.length;
while (i--) {
if (nodes[i] === node) {
return true;
}
}
return false;
};
}
}
function detachNode(node) {
// stupid ie
// eslint-disable-next-line valid-typeof
if (node && typeof node.parentNode !== 'unknown' && node.parentNode) {
node.parentNode.removeChild(node);
}
return node;
}
function safeToStringValue(value) {
return value == null || (isNumber(value) && isNaN(value)) || !value.toString ? '' : '' + value;
}
function safeAttributeString(string) {
return safeToStringValue(string)
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
export { createElement, detachNode, getElement, matches, safeToStringValue, safeAttributeString };