From 92b48d2da14a4870c348c50443a2e22d015c3828 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ulrich-Matthias=20Sch=C3=A4fer?= Date: Mon, 14 Jan 2019 13:10:38 +0100 Subject: [PATCH] fixed `root()`, `textPath()`, `text.path()` and `path.text()` and removed font-family and size from the defaults list --- CHANGELOG.md | 7 +++++- spec/spec/selector.js | 15 +++++++++++- src/elements/Dom.js | 4 +-- src/elements/Element.js | 4 +-- src/elements/TextPath.js | 47 +++++++++++++++--------------------- src/modules/core/defaults.js | 2 -- src/modules/core/selector.js | 4 +++ 7 files changed, 47 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 145ebf5..dc49acc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,12 @@ The document follows the conventions described in [“Keep a CHANGELOG”](http: ## [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 diff --git a/spec/spec/selector.js b/spec/spec/selector.js index 99823cf..470eaef 100644 --- a/spec/spec/selector.js +++ b/spec/spec/selector.js @@ -44,7 +44,20 @@ describe('Selector', function() { , 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) }) }) diff --git a/src/elements/Dom.js b/src/elements/Dom.js index 458ebbc..a0afa6d 100644 --- a/src/elements/Dom.js +++ b/src/elements/Dom.js @@ -7,7 +7,7 @@ import { 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' @@ -313,5 +313,5 @@ export default class Dom extends EventTarget { } } -extend(Dom, { attr, find }) +extend(Dom, { attr, find, findOne }) register(Dom) diff --git a/src/elements/Element.js b/src/elements/Element.js index ba15f52..b13ddd5 100644 --- a/src/elements/Element.js +++ b/src/elements/Element.js @@ -15,8 +15,6 @@ import Dom from './Dom.js' 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) @@ -67,7 +65,7 @@ export default class Element extends Dom { // Get parent document root () { - let p = this.parent(Svg) + let p = this.parent(getClass(root)) return p && p.root() } diff --git a/src/elements/TextPath.js b/src/elements/TextPath.js index 4d07b5d..d8ab125 100644 --- a/src/elements/TextPath.js +++ b/src/elements/TextPath.js @@ -40,41 +40,43 @@ export default class TextPath extends Text { 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: { @@ -85,17 +87,8 @@ registerMethods({ 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 () { diff --git a/src/modules/core/defaults.js b/src/modules/core/defaults.js index 0d496bc..499d1b4 100644 --- a/src/modules/core/defaults.js +++ b/src/modules/core/defaults.js @@ -42,7 +42,5 @@ export let attrs = { 'stop-color': '#000000', // text - 'font-size': 16, - 'font-family': 'Helvetica, Arial, sans-serif', 'text-anchor': 'start' } diff --git a/src/modules/core/selector.js b/src/modules/core/selector.js index 24841c5..1667d4d 100644 --- a/src/modules/core/selector.js +++ b/src/modules/core/selector.js @@ -13,3 +13,7 @@ export default function baseFind (query, parent) { export function find (query) { return baseFind(query, this.node) } + +export function findOne (query) { + return adopt(this.node.querySelector(query)) +} -- 2.39.5