-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlmnt.js
77 lines (69 loc) · 2.8 KB
/
lmnt.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
// polyfill ElementTraversal interfaces in older browsers
//
// LMNT version 2.0 (AMD)
// author: Ryan Corradini
define(
function() {
// some browsers don't define this constant, which most of the widget bootstraps rely on
var ELEMENT_NODE = document.ELEMENT_NODE || 1;
function _nextElementSibling( el ) {
if ( el && el.nextElementSibling ) { return el.nextElementSibling; }
if ( !el ) { return null; }
do { el = el.nextSibling } while ( el && el.nodeType !== ELEMENT_NODE );
return el;
}
function _previousElementSibling( el ) {
if ( el && el.previousElementSibling ) { return el.previousElementSibling; }
if ( !el ) { return null; }
do { el = el.previousSibling } while ( el && el.nodeType !== ELEMENT_NODE );
return el;
}
function _firstElementChild( el ) {
if ( el && el.firstElementChild ) { return el.firstElementChild; }
el = el ? el.firstChild : null;
if ( el && el.nodeType == ELEMENT_NODE ) {
return el;
} else {
return el.nextElementSibling || _nextElementSibling(el);
}
}
function _lastElementChild( el ) {
if ( el && el.lastElementChild ) { return el.lastElementChild; }
el = el ? el.lastChild : null;
if ( el && el.nodeType == ELEMENT_NODE ) {
return el;
} else {
return el.previousElementSibling || _previousElementSibling(el);
}
}
function _childElementCount( el ) {
// note that we can't use el.children because IE<9 lies, including comment nodes.
if ( el && el.childElementCount ) { return el.childElementCount; }
var count = 0;
el = el ? el.firstChild : null;
while ( el ) {
if ( el && el.nodeType == ELEMENT_NODE ) { count++; }
el = el.nextSibling;
};
return count;
}
function _childElements( el ) {
// see above; can't trust el.children, so we have to do it manually no matter what.
var stash = [];
el = el ? el.firstChild : null;
while ( el ) {
if ( el && el.nodeType == ELEMENT_NODE ) { stash.push( el ); }
el = el.nextSibling;
};
return stash;
}
return {
nextElementSibling : _nextElementSibling,
previousElementSibling : _previousElementSibling,
firstElementChild : _firstElementChild,
lastElementChild : _lastElementChild,
childElementCount : _childElementCount,
children : _childElements
}
}
);