aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2021-11-23 09:58:37 +0100
committerGitHub <noreply@github.com>2021-11-23 09:58:37 +0100
commit480e5d77e2dcefd6bc4f95a053bb545d9916eb72 (patch)
treeb1e5f440460190c258e61628ab1baf3f247d53b8
parentb01c7ee0bc70b1221aa887be355634e1b4e0dd06 (diff)
parenta8a8c636b8ee3b536c05653decd71856777c7a5a (diff)
downloadsvg.js-480e5d77e2dcefd6bc4f95a053bb545d9916eb72.tar.gz
svg.js-480e5d77e2dcefd6bc4f95a053bb545d9916eb72.zip
Merge pull request #1236 from gormster/fix-1235
Fix #1235
-rw-r--r--spec/spec/elements/Element.js28
-rw-r--r--src/elements/Element.js14
2 files changed, 40 insertions, 2 deletions
diff --git a/spec/spec/elements/Element.js b/spec/spec/elements/Element.js
index 0d7fa55..70c3a52 100644
--- a/spec/spec/elements/Element.js
+++ b/spec/spec/elements/Element.js
@@ -165,6 +165,7 @@ describe('Element.js', function () {
describe('parents()', () => {
it('returns array of parents until the passed element or root svg', () => {
const canvas = SVG().addTo(container)
+ const groupA = canvas.group().addClass('test')
const group1 = canvas.group().addClass('test')
const group2 = group1.group()
const group3 = group2.group()
@@ -175,6 +176,33 @@ describe('Element.js', function () {
expect(rect.parents(group1).length).toBe(3)
expect(rect.parents()).toEqual([ group3, group2, group1, canvas ])
})
+
+ it('returns array of parents until the closest matching parent', () => {
+ const canvas = SVG().addTo(container)
+ const groupA = canvas.group().addClass('test')
+ const group1 = canvas.group().addClass('test')
+ const group2 = group1.group().addClass('test').addClass('foo')
+ const group3 = group2.group().addClass('foo')
+ const rect = group3.rect(100, 100)
+
+ expect(rect.parents('.test')).toEqual([ group3, group2 ])
+ expect(rect.parents('.foo')).toEqual([ group3 ])
+ expect(rect.parents('.test:not(.foo)')).toEqual([ group3, group2, group1 ])
+ })
+
+ it('returns null if the passed element is not an ancestor', () => {
+ const canvas = SVG().addTo(container)
+ const groupA = canvas.group().addClass('test')
+ const group1 = canvas.group()
+ const group2 = group1.group()
+ const group3 = group2.group()
+ const rect = group3.rect(100, 100)
+
+
+ expect(rect.parents('.does-not-exist')).toEqual(null)
+ expect(rect.parents('.test')).toEqual(null)
+ expect(rect.parents(groupA)).toEqual(null)
+ })
})
describe('reference()', () => {
diff --git a/src/elements/Element.js b/src/elements/Element.js
index f39f777..2aaeab5 100644
--- a/src/elements/Element.js
+++ b/src/elements/Element.js
@@ -87,7 +87,10 @@ export default class Element extends Dom {
// return array of all ancestors of given type up to the root svg
parents (until = this.root()) {
- until = makeInstance(until)
+ const isSelector = typeof until === 'string'
+ if (!isSelector) {
+ until = makeInstance(until)
+ }
const parents = new List()
let parent = this
@@ -98,9 +101,16 @@ export default class Element extends Dom {
parents.push(parent)
- if (parent.node === until.node) {
+ if (!isSelector && (parent.node === until.node)) {
+ break
+ }
+ if (isSelector && parent.matches(until)) {
break
}
+ if (parent.node === this.root().node) {
+ // We worked our way to the root and didn't match `until`
+ return null
+ }
}
return parents