diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2015-10-25 22:50:52 +0100 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2015-10-25 22:50:52 +0100 |
commit | f72c867e7e3da9d8939ab67afaddee0755678ba4 (patch) | |
tree | a2a0eb636ef9543334f2d7cfb6cec9ac0cce4622 /src | |
parent | 78e492ca461ccba1ca57eb2362be02280ef232ac (diff) | |
download | svg.js-f72c867e7e3da9d8939ab67afaddee0755678ba4.tar.gz svg.js-f72c867e7e3da9d8939ab67afaddee0755678ba4.zip |
added support for css selectors with the `parent()` method
Diffstat (limited to 'src')
-rw-r--r-- | src/element.js | 6 | ||||
-rw-r--r-- | src/helpers.js | 5 |
2 files changed, 8 insertions, 3 deletions
diff --git a/src/element.js b/src/element.js index 01ab8b0..180fbfd 100644 --- a/src/element.js +++ b/src/element.js @@ -161,16 +161,16 @@ SVG.Element = SVG.invent({ // get parent element var parent = SVG.adopt(this.node.parentNode) - // if a specific type is given, find a parent with that class + // if a specific type or selector is given, find a parent with that class if (type) - while (!(parent instanceof type) && parent.node.parentNode instanceof SVGElement) + while (parent.node.parentNode instanceof SVGElement && !(typeof type === 'string' ? matches(parent.node, type) : parent instanceof type)) parent = SVG.adopt(parent.node.parentNode) return parent } } // Get parent document - , doc: function(type) { + , doc: function() { return this instanceof SVG.Doc ? this : this.parent(SVG.Doc) } // Returns the svg node to call native svg methods on it diff --git a/src/helpers.js b/src/helpers.js index f3a36b9..ee86dc7 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -1,3 +1,8 @@ +// tests if a given selector matches an element +function matches(el, selector) { + return (el.matches || el.matchesSelector || el.msMatchesSelector || el.mozMatchesSelector || el.webkitMatchesSelector || el.oMatchesSelector).call(el, selector); +} + // Convert dash-separated-string to camelCase function camelCase(s) { return s.toLowerCase().replace(/-(.)/g, function(m, g) { |