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 /dist/svg.js | |
parent | 81917ee7c714d0d7e3db4ec6471b734b62c63bfe (diff) | |
download | svg.js-48537ddd925fb28d4bc4b4188b14d316e51ce29f.tar.gz svg.js-48537ddd925fb28d4bc4b4188b14d316e51ce29f.zip |
add `parents()` method, added specs, fixed specs
Diffstat (limited to 'dist/svg.js')
-rw-r--r-- | dist/svg.js | 40 |
1 files changed, 30 insertions, 10 deletions
diff --git a/dist/svg.js b/dist/svg.js index fd5cd58..133b160 100644 --- a/dist/svg.js +++ b/dist/svg.js @@ -6,7 +6,7 @@ * @copyright Wout Fierens <wout@impinc.co.uk> * @license MIT * -* BUILT: Sun Oct 25 2015 22:38:57 GMT+0100 (Mitteleuropäische Zeit) +* BUILT: Mon Oct 26 2015 22:54:39 GMT+0100 (Mitteleuropäische Zeit) */; (function(root, factory) { if (typeof define === 'function' && define.amd) { @@ -1069,22 +1069,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 @@ -4175,7 +4195,7 @@ SVG.extend(SVG.Element, { // 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 |