-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathsatisfy.js
204 lines (152 loc) · 5.41 KB
/
satisfy.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
/**
* @license Satisfy - made to satisfy CSS selectors
* Copyright (c) 2009 James Padolsey
* -------------------------------------------------------
* Dual licensed under the MIT and GPL licenses.
* - http://www.opensource.org/licenses/mit-license.php
* - http://www.gnu.org/copyleft/gpl.html
* -------------------------------------------------------
* Version: 0.3
* -------------------------------------------------------
* (most) Regular Expressions are Copyright (c) John Resig,
* from the Sizzle Selector Engine.
*/
(function (definition) {
var satisfy = definition();
// CommonJS
if (typeof exports === "object") {
module.exports = satisfy;
// RequireJS
} else if (typeof define === "function" && define.amd) {
define(definition);
// <script>
} else {
window['satisfy'] = satisfy;
}
if (window['jQuery'] !== undefined && jQuery['fn']) {
jQuery['satisfy'] = function(selector) {
return jQuery(satisfy(selector));
};
}
})(function(){
var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]*\]|['"][^'"]*['"]|[^[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?/g,
exprRegex = {
ID: /#((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,
CLASS: /\.((?:[\w\u00c0-\uFFFF_-]|\\.)+)(?![^[\]]+])/g,
NAME: /\[name=['"]*((?:[\w\u00c0-\uFFFF_-]|\\.)+)['"]*\]/,
ATTR: /\[\s*((?:[\w\u00c0-\uFFFF_-]|\\.)+)\s*(?:(\S?=)\s*(['"]*)(.*?)\3|)\s*\]/g,
TAG: /^((?:[\w\u00c0-\uFFFF\*_-]|\\.)+)/,
CLONE: /\:(\d+)(?=$|[:[])/,
COMBINATOR: /^[>~+]$/
},
doc = document,
cache = {},
attrMap = {
'for': 'htmlFor',
'class': 'className',
'html': 'innerHTML'
},
callbackTypes = ['ID','CLASS','NAME','ATTR'],
exprCallback = {
ID: function(match, node) {
node.id = match[1];
},
CLASS: function(match, node) {
var cls = node.className.replace(/^\s+$/,'');
node.className = cls ? cls + ' ' + match[1] : match[1];
},
NAME: function(match, node) {
node.name = match[1];
},
ATTR: function(match, node) {
var attr = match[1],
val = match[4] || true;
if ( val === true || attr === 'innerHTML' || attrMap.hasOwnProperty(attr) ) {
node[attrMap[attr] || attr] = val;
} else {
node.setAttribute( attr, val );
}
}
};
function create(part, n) {
var tag = exprRegex.TAG.exec(part),
node = doc.createElement( tag && tag[1] !== '*' ? tag[1] : 'div' ),
fragment = doc.createDocumentFragment(),
c = callbackTypes.length,
match, regex, callback;
while (c--) {
regex = exprRegex[callbackTypes[c]];
callback = exprCallback[callbackTypes[c]];
if (regex.global) {
while ( (match = regex.exec(part)) !== null ) {
callback( match, node );
}
continue;
}
if (match = regex.exec(part)) {
callback( match, node );
}
}
while (n--) {
fragment.appendChild( node.cloneNode(true) );
}
return fragment;
}
function multiAppend(parents, children) {
parents = parents.childNodes;
var i = parents.length,
parent;
while ( i-- ) {
parent = parents[i];
if (parent.nodeName.toLowerCase() === 'table') {
/* IE requires table to have tbody */
parent.appendChild(parent = doc.createElement('tbody'));
}
parent.appendChild(children.cloneNode(true));
}
}
function satisfy(selector) {
if (selector in cache) {
return cache[selector].cloneNode(true).childNodes;
}
var selectorParts = [],
fragment = doc.createDocumentFragment(),
children,
prevChildren,
curSelector,
nClones = 1,
nParts = 0,
isSibling = false,
cloneMatch,
m;
while ( (m = chunker.exec(selector)) !== null ) {
++nParts;
selectorParts.push(m[1]);
}
// We're going in reverse
while (nParts--) {
curSelector = selectorParts[nParts];
if (exprRegex.COMBINATOR.test(curSelector)) {
isSibling = curSelector === '~' || curSelector === '+';
continue;
}
// Number of clones must be an int >= 1
nClones = (cloneMatch = curSelector.match(exprRegex.CLONE)) ? ~~cloneMatch[1] : 1;
prevChildren = children;
children = create(curSelector, nClones);
if (prevChildren) {
if (isSibling) {
children.appendChild(prevChildren);
isSibling = false;
} else {
multiAppend(children, prevChildren);
}
}
}
fragment.appendChild(children);
cache[selector] = fragment.cloneNode(true);
return fragment.childNodes;
}
satisfy.cache = cache;
return satisfy;
});