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()', () => {
// return array of all ancestors of given type up to the root svg
parents (until = this.root()) {
- let selector = null
- if (typeof until === 'string') {
- selector = until
- until = this.root()
- } else {
+ const isSelector = typeof until === 'string'
+ if (!isSelector) {
until = makeInstance(until)
}
const parents = new List()
parents.push(parent)
- if (parent.node === until.node) {
+ if (!isSelector && (parent.node === until.node)) {
break
}
- if (selector && parent.matches(selector)) {
+ 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