diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2015-10-26 22:56:32 +0100 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2015-10-26 22:56:32 +0100 |
commit | 48537ddd925fb28d4bc4b4188b14d316e51ce29f (patch) | |
tree | 7534cd120a278256960608a9e53b6cb77cf282ca /src | |
parent | 81917ee7c714d0d7e3db4ec6471b734b62c63bfe (diff) | |
download | svg.js-48537ddd925fb28d4bc4b4188b14d316e51ce29f.tar.gz svg.js-48537ddd925fb28d4bc4b4188b14d316e51ce29f.zip |
add `parents()` method, added specs, fixed specs
Diffstat (limited to 'src')
-rw-r--r-- | src/element.js | 36 | ||||
-rw-r--r-- | src/selector.js | 2 |
2 files changed, 29 insertions, 9 deletions
diff --git a/src/element.js b/src/element.js index 180fbfd..aa75235 100644 --- a/src/element.js +++ b/src/element.js @@ -157,22 +157,42 @@ SVG.Element = SVG.invent({ } // Returns the parent element instance , parent: function(type) { - if (this.node.parentNode) { - // get parent element - var parent = SVG.adopt(this.node.parentNode) + var parent = this - // if a specific type or selector is given, find a parent with that class - if (type) - while (parent.node.parentNode instanceof SVGElement && !(typeof type === 'string' ? matches(parent.node, type) : parent instanceof type)) - parent = SVG.adopt(parent.node.parentNode) + // check for parent + if(!parent.node.parentNode) return null - return parent + // get parent element + parent = SVG.adopt(parent.node.parentNode) + + if(!type) return parent + + // loop trough ancestors if type is given + while(parent.node instanceof SVGElement){ + if(typeof type === 'string' ? parent.matches(type) : parent instanceof type) return parent + parent = SVG.adopt(parent.node.parentNode) } } // Get parent document , doc: function() { return this instanceof SVG.Doc ? this : this.parent(SVG.Doc) } + , parents: function(type) { + var parents = [], parent = this + + do{ + parent = parent.parent(type) + if(!parent || !parent.node) break + + parents.push(parent) + } while(parent.parent) + + return parents + } + // matches the element vs a css selector + , matches: function(selector){ + return matches(this.node, selector) + } // Returns the svg node to call native svg methods on it , native: function() { return this.node diff --git a/src/selector.js b/src/selector.js index ec6298a..fe87e4e 100644 --- a/src/selector.js +++ b/src/selector.js @@ -1,7 +1,7 @@ // Method for getting an element by id SVG.get = function(id) { var node = document.getElementById(idFromReference(id) || id) - if (node) return SVG.adopt(node) + return SVG.adopt(node) } // Select elements by query string |