aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2019-01-14 11:01:25 +0100
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2019-01-14 11:01:25 +0100
commitaec779cb019b5ad1c2ea709d9bf8e93d3d1e0c87 (patch)
tree41ec3b5c369374c33750091d291320aa3de8bc7c
parent1388b1f67b18cb2bc561840079f981253fa1643e (diff)
downloadsvg.js-aec779cb019b5ad1c2ea709d9bf8e93d3d1e0c87.tar.gz
svg.js-aec779cb019b5ad1c2ea709d9bf8e93d3d1e0c87.zip
fixed `textPath()` and `path().text()`
-rw-r--r--CHANGELOG.md8
-rw-r--r--package.json2
-rw-r--r--spec/spec/textpath.js35
-rw-r--r--src/elements/TextPath.js32
4 files changed, 68 insertions, 9 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index af23509..145ebf5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,11 @@ The document follows the conventions described in [“Keep a CHANGELOG”](http:
====
+## [3.0.10] - 2018-01-14
+
+### Fixed
+ - fixed `textPath()` and `path().text()`
+
## [3.0.9] - 2018-01-14
### Fixed
@@ -791,6 +796,9 @@ The document follows the conventions described in [“Keep a CHANGELOG”](http:
<!-- Headings above link to the releases listed here -->
+[3.0.10]: https://github.com/svgdotjs/svg.js/releases/tag/3.0.10
+[3.0.9]: https://github.com/svgdotjs/svg.js/releases/tag/3.0.9
+[3.0.8]: https://github.com/svgdotjs/svg.js/releases/tag/3.0.8
[3.0.7]: https://github.com/svgdotjs/svg.js/releases/tag/3.0.7
[3.0.6]: https://github.com/svgdotjs/svg.js/releases/tag/3.0.6
[3.0.5]: https://github.com/svgdotjs/svg.js/releases/tag/3.0.5
diff --git a/package.json b/package.json
index 8c582d1..5bacee2 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "@svgdotjs/svg.js",
- "version": "3.0.9",
+ "version": "3.0.10",
"description": "A lightweight library for manipulating and animating SVG.",
"url": "https://svgdotjs.github.io/",
"homepage": "https://svgdotjs.github.io/",
diff --git a/spec/spec/textpath.js b/spec/spec/textpath.js
index 8a3d4c2..9d4577f 100644
--- a/spec/spec/textpath.js
+++ b/spec/spec/textpath.js
@@ -13,6 +13,25 @@ describe('TextPath', function() {
draw.clear()
})
+ describe('textPath()', function () {
+ it ('creates a new textPath and uses text and path', function () {
+ expect(draw.textPath(txt, data)).toEqual(jasmine.any(SVG.TextPath))
+ })
+
+ it ('reuses text and path instances if possible', function () {
+ const textPath = draw.textPath(text, path)
+ expect(text.find('textPath')[0]).toBe(textPath)
+ expect(textPath.reference('href')).toBe(path)
+ })
+
+ it ('passes the text into textPath and not text', function () {
+ const tspan = text.first()
+ const textPath = draw.textPath(text, path)
+ expect(textPath.first()).toBe(tspan)
+ expect(text.first()).toBe(textPath)
+ })
+ })
+
describe('text().path()', function() {
it('returns an instance of TextPath', function() {
expect(text.path(data) instanceof SVG.TextPath).toBe(true)
@@ -21,6 +40,10 @@ describe('TextPath', function() {
text.path(data)
expect(text.node.querySelector('textPath')).not.toBe(null)
})
+ it('references the passed path', function () {
+ const textPath = text.path(path)
+ expect(textPath.reference('href')).toBe(path)
+ })
})
describe('path().text()', function() {
@@ -28,13 +51,19 @@ describe('TextPath', function() {
expect(path.text(txt) instanceof SVG.TextPath).toBe(true)
})
it('creates a text with textPath node and inserts it after the path', function() {
- var instance = path.text(txt)
- expect(instance.parent() instanceof SVG.Text).toBe(true)
+ var textPath = path.text(txt)
+ expect(textPath.parent() instanceof SVG.Text).toBe(true)
expect(SVG.adopt(path.node.nextSibling) instanceof SVG.Text).toBe(true)
})
+ it('transplants the node from text to textPath', function () {
+ let nodesInText = [].slice.call(text.node.childNodes)
+ var textPath = path.text(txt)
+ let nodesInTextPath = [].slice.call(textPath.node.childNodes)
+ expect(nodesInText).toEqual(nodesInTextPath)
+ })
})
- describe('textPath()', function() {
+ describe('text.textPath()', function() {
it('returns only the first textPath element in a text', function() {
text.path(data)
expect(text.textPath() instanceof SVG.TextPath).toBe(true)
diff --git a/src/elements/TextPath.js b/src/elements/TextPath.js
index 91c48ae..4d07b5d 100644
--- a/src/elements/TextPath.js
+++ b/src/elements/TextPath.js
@@ -40,7 +40,18 @@ export default class TextPath extends Text {
registerMethods({
Container: {
textPath: wrapWithAttrCheck(function (text, path) {
- return this.defs().path(path).text(text).addTo(this)
+ // Convert to instance if needed
+ if (!(path instanceof Path)) {
+ path = this.defs().path(path)
+ }
+
+ // Create textPath
+ const textPath = path.text(text)
+
+ // Move text to correct container
+ textPath.parent().addTo(this)
+
+ return textPath
})
},
Text: {
@@ -69,11 +80,22 @@ registerMethods({
Path: {
// creates a textPath from this path
text: wrapWithAttrCheck(function (text) {
- if (text instanceof Text) {
- var txt = text.text()
- return text.clear().path(this).text(txt)
+ // Convert text to instance if needed
+ if (!(text instanceof Text)) {
+ text = new Text().addTo(this.parent()).text(text)
}
- return this.parent().put(new Text()).path(this).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)
}),
targets () {