-
Notifications
You must be signed in to change notification settings - Fork 0
/
fnd.js
132 lines (114 loc) · 3.85 KB
/
fnd.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
(function (root, factory) {
'use strict';
/* global define, module */
if (typeof define === 'function' && define.amd) { // AMD
define([], factory);
} else if (typeof exports === 'object') { // Node, browserify and alike
module.exports = factory();
} else {
// Browser globals (root is window)
root.fnd = factory();
}
}(this, function () {
'use strict';
/* global NodeList, HTMLCollection */
var toArray = Array.from || (function () {
var slice = Array.prototype.slice;
return function (arrayLike) {
return slice.call(arrayLike, 0);
}
}());
var isArray = Array.isArray || function (arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
var isQSARe = /(.+[ ,>+~#:\[\.]|[\[:])/;
function isQSASelector (selector) {
return isQSARe.test(selector);
}
var strategies = {
'#': function (selector) {
return document.getElementById(selector.substr(1));
},
'.': function (selector, parent) {
return parent.getElementsByClassName(selector.substr(1));
},
'tagName': function (selector, parent) {
return parent.getElementsByTagName(selector);
},
'qSA': function (selector, parent) {
return parent.querySelectorAll(selector);
}
};
function getStrategy (selector, parent) {
var firstChar = selector.substr(0, 1);
if (isQSASelector(selector) || (firstChar === '#' && parent !== document)) { // searching by id in some context falls back to qSA
return strategies.qSA;
} else {
return strategies[firstChar] || strategies.tagName;
}
}
function seekIn (selector, parent) {
parent = (parent || document);
var searcher = getStrategy(selector, parent);
var result = searcher(selector, parent);
if (result === null) {
return null;
} else {
if (result instanceof NodeList || result instanceof HTMLCollection) {
if (result.length) {
return toArray(result);
} else {
return null;
}
} else {
return [result];
}
}
}
function fnd (selector, parent) {
if (parent === null) {
throw new Error('Got null instead of element(s) when searching for "' + selector + '"');
}
if (!isArray(parent)) {
return seekIn(selector, parent);
} else {
if (parent.length === 1) {
return seekIn(selector, parent[0]);
} else {
var results = [];
var push = results.push.apply;
var found;
for (var i = 0, len = parent.length; i < len; i++) {
found = seekIn(selector, parent[i]);
if (found !== null) {
push(found);
}
}
if (results.length) {
return results;
} else {
return null;
}
}
}
}
// determine which matches method is available
var matchesMethod = '';
(function () {
var docEl = document.documentElement;
var methodNames = ['matches', 'msMatchesSelector', 'mozMatchesSelector', 'webkitMatchesSelector', 'matchesSelector'];
for (var i = 0, len = methodNames.length; i < len; i++) {
if (docEl[methodNames[i]]) {
matchesMethod = methodNames[i];
break;
}
}
}());
function isFactory (selector) {
return function (element) {
return element[matchesMethod](selector);
};
}
fnd.is = isFactory;
return fnd;
}));