From 8fa0873a632f828ed1ad85aa30dee97bc585d32f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ulrich-Matthias=20Sch=C3=A4fer?= Date: Sat, 2 Sep 2023 21:23:53 +0200 Subject: [PATCH] skip descriptive elements on rebuild and toParent --- .gitignore | 2 ++ spec/helpers.js | 8 ++++++-- spec/spec/elements/Text.js | 5 ++++- src/elements/Text.js | 7 +++++-- src/modules/optional/transform.js | 5 ++++- src/utils/utils.js | 4 ++++ 6 files changed, 25 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 8a91f66..680a751 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ coverage/ spec/es5TestBundle.js .env dist +index.html +index.js \ No newline at end of file diff --git a/spec/helpers.js b/spec/helpers.js index b5b29c3..5a2568a 100644 --- a/spec/helpers.js +++ b/spec/helpers.js @@ -154,7 +154,9 @@ export function buildFixtures() { div.style.position = 'absolute' div.style.top = 0 div.style.left = 0 - } catch (e) {} + } catch (e) { + // + } div.appendChild(fixtures()) body.appendChild(div) @@ -172,7 +174,9 @@ export function buildCanvas() { div.style.position = 'absolute' div.style.top = 0 div.style.left = 0 - } catch (e) {} + } catch (e) { + // + } body.appendChild(div) } diff --git a/spec/spec/elements/Text.js b/spec/spec/elements/Text.js index b0c5a9a..f088618 100644 --- a/spec/spec/elements/Text.js +++ b/spec/spec/elements/Text.js @@ -53,12 +53,14 @@ describe('Text.js', () => { expect(text.text()).toBe('Hello World\nHow is it\ngoing') }) - it('returns the correct text with newlines and skips textPaths', () => { + it('returns the correct text with newlines and skips textPaths and descriptive elements', () => { const path = new Path() const text = new Text() const textPath = text.text('Hello World\nHow is it\ngoing').path(path) textPath.children().addTo(text) text.add(new TextPath(), 3) + text.add(SVG('MyText')) + expect(text.text()).toBe('Hello World\nHow is it\ngoing') }) @@ -113,6 +115,7 @@ describe('Text.js', () => { t.tspan('Hello World').newLine() t.tspan('How is it').newLine() t.tspan('going').newLine() + t.add('My Text') }) const dy = text.get(1).dy() diff --git a/src/elements/Text.js b/src/elements/Text.js index 39371f6..0c5815d 100644 --- a/src/elements/Text.js +++ b/src/elements/Text.js @@ -10,6 +10,7 @@ import SVGNumber from '../types/SVGNumber.js' import Shape from './Shape.js' import { globals } from '../utils/window.js' import * as textable from '../modules/core/textable.js' +import { isDescriptive } from '../utils/utils.js' export default class Text extends Shape { // Initialize node @@ -48,6 +49,8 @@ export default class Text extends Shape { const leading = this.dom.leading this.each(function (i) { + if (isDescriptive(this.node)) return + const fontSize = globals.window .getComputedStyle(this.node) .getPropertyValue('font-size') @@ -89,8 +92,8 @@ export default class Text extends Shape { for (let i = 0, len = children.length; i < len; ++i) { // skip textPaths - they are no lines - if (children[i].nodeName === 'textPath') { - if (i === 0) firstLine = 1 + if (children[i].nodeName === 'textPath' || isDescriptive(children[i])) { + if (i === 0) firstLine = i + 1 continue } diff --git a/src/modules/optional/transform.js b/src/modules/optional/transform.js index 7f950b3..b8ba46a 100644 --- a/src/modules/optional/transform.js +++ b/src/modules/optional/transform.js @@ -1,4 +1,4 @@ -import { getOrigin } from '../../utils/utils.js' +import { getOrigin, isDescriptive } from '../../utils/utils.js' import { delimiter, transforms } from '../core/regex.js' import { registerMethods } from '../../utils/methods.js' import Matrix from '../../types/Matrix.js' @@ -39,6 +39,9 @@ export function matrixify() { // add an element to another parent without changing the visual representation on the screen export function toParent(parent, i) { if (this === parent) return this + + if (isDescriptive(this.node)) return this.addTo(parent, i) + const ctm = this.screenCTM() const pCtm = parent.screenCTM().inverse() diff --git a/src/utils/utils.js b/src/utils/utils.js index c6e6d3b..0d297ec 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -120,3 +120,7 @@ export function getOrigin(o, element) { // Return the origin as it is if it wasn't a string return [ox, oy] } + +const descriptiveElements = new Set(['desc', 'metadata', 'title']) +export const isDescriptive = (element) => + descriptiveElements.has(element.nodeName) -- 2.39.5