inmyhead (owner)

Revisions

gist: 41440 Download_button fork
public
Public Clone URL: git://gist.github.com/41440.git
Embed All Files: show embed
DOM-min.js #
1
DOM=function(){function b(c){if(c&&typeof c==="string"){c=document.getElementById(c)}return c||null}function a(e,c,d,i,g){var f=b(e)[i||d],h=g?[]:null;while(f){if(f.nodeType===1&&(!c||f.tagName.toLowerCase()===c)){if(!g){return f}h.push(f)}f=f[d]}return h}return{get:b,walk:a,getPrevious:function(d,c){return a(d,c,"previousSibling")},getAllPrevious:function(d,c){return a(d,c,"previousSibling",null,true)},getNext:function(d,c){return a(d,c,"nextSibling")},getAllNext:function(d,c){return a(d,c,"nextSibling",null,true)},getFirst:function(d,c){return a(d,c,"nextSibling","firstChild")},getLast:function(d,c){return a(d,c,"previousSibling","lastChild")},getParent:function(d,c){return a(d,c,"parentNode")},getParents:function(d,c){return a(d,c,"parentNode",null,true)},getChildren:function(d,c){return a(d,c,"nextSibling","firstChild",true)},dispose:function(c){c=b(c);return(c.parentNode)?c.parentNode.removeChild(c):c}}}();
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
/**
* Simple DOM traversing copied from MooTools.
*/
DOM = function () {
 
function get(id) {
if (id && typeof id === 'string') {
id = document.getElementById(id);
}
return id || null;
}
 
function walk(element, tag, walk, start, all) {
var el = get(element)[start || walk], elements = all ? [] : null;
while (el) {
if (el.nodeType === 1 && (!tag || el.tagName.toLowerCase() === tag)) {
if (!all) {
return el;
}
elements.push(el);
}
el = el[walk];
}
return elements;
}
 
return {
 
// Get the element by its id
get: get,
 
walk: walk,
 
// Returns the previousSibling of the Element (excluding text nodes).
getPrevious: function (el, tag) {
return walk(el, tag, 'previousSibling');
},
 
// Like getPrevious, but returns a collection of all the matched previousSiblings.
getAllPrevious: function (el, tag) {
return walk(el, tag, 'previousSibling', null, true);
},
 
// As getPrevious, but tries to find the nextSibling (excluding text nodes).
getNext: function (el, tag) {
return walk(el, tag, 'nextSibling');
},
 
// Like getNext, but returns a collection of all the matched nextSiblings.
getAllNext: function (el, tag) {
return walk(el, tag, 'nextSibling', null, true);
},
 
// Works as getPrevious, but tries to find the firstChild (excluding text nodes).
getFirst: function (el, tag) {
return walk(el, tag, 'nextSibling', 'firstChild');
},
 
// Works as getPrevious, but tries to find the lastChild.
getLast: function (el, tag) {
return walk(el, tag, 'previousSibling', 'lastChild');
},
 
// Works as getPrevious, but tries to find the parentNode.
getParent: function (el, tag) {
return walk(el, tag, 'parentNode');
},
 
// Like getParent, but returns a collection of all the matched parentNodes up the tree.
getParents: function (el, tag) {
return walk(el, tag, 'parentNode', null, true);
},
 
// Returns all the Element's children (excluding text nodes).
getChildren: function (el, tag) {
return walk(el, tag, 'nextSibling', 'firstChild', true);
},
 
// Removes the Element from the DOM.
dispose: function (el) {
el = get(el);
return (el.parentNode) ? el.parentNode.removeChild(el) : el;
}
 
};
}();