-
Notifications
You must be signed in to change notification settings - Fork 0
/
sillyctor.js
97 lines (86 loc) · 2.7 KB
/
sillyctor.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
/*
*
* It's a simple selector engine for educational purpose ONLY.
* It supports elements, classes, ids and conjunction of them.
* It DOES NOT support *, >, +, attributes and pseudo-selectors.
*
*/
"use strict";
var $ = function (selector) {
var elements = [],
el, tag, tmpEl, sLen, parentNode,
i = 0, j = 0;
/*
* Check if hash's location is other than the beginning of the selector
* and if it's true, get rid of the text (tag) part of the selector.
*/
if(selector.indexOf('#') > 0) {
selector = selector.split('#');
tag = selector[0];
selector = '#' + selector[selector.length - 1];
selector = selector.split('.')[0];
}
selector = selector.split(' ');
sLen = selector.length;
/* Set default context for getElementsByTag/ClassName methods */
elements = document;
/* Main match functions */
var matchSelector = {
id: function(selector) {
return document.getElementById(selector);
},
get: function(classOrElement,selector,parentNode) {
var k = 0, m = 0, arr = [], gw = (classOrElement === 'class') ?
'getElementsByClassName' : 'getElementsByTagName';
if(parentNode.length) {
while(parentNode[k]) {
/* We need to convert NodeList to Array */
Array.prototype.slice.call(parentNode[i][gw](selector));
m++;
}
}
/* If parentNode has no length, it's a single element then */
else { arr = parentNode[gw](selector); }
return (arr.length === 1) ? arr[0] : arr;
}
};
/* Main loop */
for (; i < sLen; i++) {
el = selector[i];
parentNode = elements;
/* If selector is an ID: */
if(el.indexOf('#') === 0){
if(tag) {
/* Remove some classes from the tag if there are any */
tag = tag.split('.')[0];
tmpEl = matchSelector.id(el.split('#')[1]);
tmpEl.tagName.toLowerCase() === tag ? elements = tmpEl : elements = [];
}
else {
elements = matchSelector.id(el.split('#')[1]);
}
}
/* If selector is/are class(es): */
else if(el.indexOf('.') > -1) {
el = el.split('.');
if(el[0]) {
parentNode = matchSelector.get('elements',el[0],parentNode);
if(parentNode.length) {
for (;parentNode[j];j++) {
if(parentNode[j].className.indexOf(el[1]) > -1) {
elements.push(parentNode[j]);
}
}
}
else {
elements = (parentNode.className.indexOf(el[1]) > -1) ?
parentNode : [];
}
}
else { elements = matchSelector.get('class',el[1],parentNode);}
}
/* Finally, if selector is a tag: */
else { elements = matchSelector.get('elements',el,parentNode); }
}
return elements;
}