## [3.0.10] - 2018-01-14
### Fixed
- - fixed `textPath()` and `path().text()`
+ - fixed `textPath()`, `path().text()` and `text().path()`
+ - fixed `root()` method
+ - fixed default values returned by `attr`. Can be missleading if present.
+
+### Added
+ - added `findOne()` for better performance
## [3.0.9] - 2018-01-14
, e4 = draw.rect(100, 100).addClass('unselectable-element')
, e5 = group.rect(100, 100).addClass('selectable-element')
- expect(group.find('rect.selectable-element').valueOf()).toEqual([e3, e5])
+ expect(group.find('rect.selectable-element')).toEqual([e3, e5])
+ })
+ })
+
+ describe('Parent#findOne()', function() {
+ it('gets all elements with a given class name inside a given element', function() {
+ var group = draw.group()
+ , e1 = draw.rect(100, 100).addClass('selectable-element')
+ , e2 = draw.rect(100, 100).addClass('unselectable-element')
+ , e3 = group.rect(100, 100).addClass('selectable-element')
+ , e4 = draw.rect(100, 100).addClass('unselectable-element')
+ , e5 = group.rect(100, 100).addClass('selectable-element')
+
+ expect(group.findOne('rect.selectable-element')).toBe(e3)
})
})
create,
register
} from '../utils/adopter.js'
-import { find } from '../modules/core/selector.js'
+import { find, findOne } from '../modules/core/selector.js'
import { globals } from '../utils/window.js'
import { map } from '../utils/utils.js'
import { ns } from '../modules/core/namespaces.js'
}
}
-extend(Dom, { attr, find })
+extend(Dom, { attr, find, findOne })
register(Dom)
import List from '../types/List.js'
import SVGNumber from '../types/SVGNumber.js'
-const Svg = getClass(root)
-
export default class Element extends Dom {
constructor (node, attrs) {
super(node, attrs)
// Get parent document
root () {
- let p = this.parent(Svg)
+ let p = this.parent(getClass(root))
return p && p.root()
}
registerMethods({
Container: {
textPath: wrapWithAttrCheck(function (text, path) {
- // Convert to instance if needed
- if (!(path instanceof Path)) {
- path = this.defs().path(path)
+ // Convert text to instance if needed
+ if (!(text instanceof Text)) {
+ text = this.text(text)
}
- // Create textPath
- const textPath = path.text(text)
-
- // Move text to correct container
- textPath.parent().addTo(this)
-
- return textPath
+ return text.path(path)
})
},
Text: {
// Create path for text to run on
- path: wrapWithAttrCheck(function (track) {
- var path = new TextPath()
+ path: wrapWithAttrCheck(function (track, importNodes = true) {
+ var textPath = new TextPath()
// if track is a path, reuse it
if (!(track instanceof Path)) {
// create path element
- track = this.root().defs().path(track)
+ track = this.defs().path(track)
}
// link textPath to path and add content
- path.attr('href', '#' + track, xlink)
+ textPath.attr('href', '#' + track, xlink)
+
+ // Transplant all nodes from text to textPath
+ let node
+ if (importNodes) {
+ while ((node = this.node.firstChild)) {
+ textPath.node.appendChild(node)
+ }
+ }
// add textPath element as child node and return textPath
- return this.put(path)
+ return this.put(textPath)
}),
// Get the textPath children
textPath () {
- return this.find('textPath')[0]
+ return this.findOne('textPath')
}
},
Path: {
text = new Text().addTo(this.parent()).text(text)
}
- // Create textPath from text and path
- const textPath = text.path(this)
- textPath.remove()
-
- // Transplant all nodes from text to textPath
- let node
- while ((node = text.node.firstChild)) {
- textPath.node.appendChild(node)
- }
-
- return textPath.addTo(text)
+ // Create textPath from text and path and return
+ return text.path(this)
}),
targets () {
'stop-color': '#000000',
// text
- 'font-size': 16,
- 'font-family': 'Helvetica, Arial, sans-serif',
'text-anchor': 'start'
}
export function find (query) {
return baseFind(query, this.node)
}
+
+export function findOne (query) {
+ return adopt(this.node.querySelector(query))
+}