From e058f51bf16cd908a266391f7d657f35e501bb17 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ulrich-Matthias=20Sch=C3=A4fer?= Date: Thu, 23 Apr 2020 08:04:58 +1000 Subject: [PATCH] renamed svg namespace to svg --- spec/SpecRunner.html | 2 - spec/spec/elements/Svg.js | 2 +- src/elements/Dom.js | 103 ++++++++++++++++++--------------- src/elements/Svg.js | 4 +- src/modules/core/namespaces.js | 3 +- src/utils/adopter.js | 4 +- 6 files changed, 63 insertions(+), 55 deletions(-) diff --git a/spec/SpecRunner.html b/spec/SpecRunner.html index 946c970..d5b20af 100644 --- a/spec/SpecRunner.html +++ b/spec/SpecRunner.html @@ -56,8 +56,6 @@ - - diff --git a/spec/spec/elements/Svg.js b/spec/spec/elements/Svg.js index f561366..1fa6fd5 100644 --- a/spec/spec/elements/Svg.js +++ b/spec/spec/elements/Svg.js @@ -1,7 +1,7 @@ /* globals describe, expect, it, jasmine, container */ import { Svg, SVG, Defs } from '../../../src/main.js' -import { ns, xlink, svgjs } from '../../../src/modules/core/namespaces.js' +import { svg as ns, xlink, svgjs } from '../../../src/modules/core/namespaces.js' import { getWindow } from '../../../src/utils/window.js' const { any } = jasmine diff --git a/src/elements/Dom.js b/src/elements/Dom.js index c67dd18..1f25c0b 100644 --- a/src/elements/Dom.js +++ b/src/elements/Dom.js @@ -10,7 +10,7 @@ import { 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' +import { svg, html } from '../modules/core/namespaces.js' import EventTarget from '../types/EventTarget.js' import List from '../types/List.js' import attr from '../modules/core/attr.js' @@ -118,6 +118,10 @@ export default class Dom extends EventTarget { return this.index(element) >= 0 } + html (htmlOrFn, outerHTML) { + return this.xml(htmlOrFn, outerHTML, html) + } + // Get / set id id (id) { // generate new id if no id set @@ -220,30 +224,67 @@ export default class Dom extends EventTarget { } // Import / Export raw svg - svg (svgOrFn, outerHTML) { + svg (svgOrFn, outerSVG) { + return this.xml(svgOrFn, outerSVG, svg) + } + + // Return id on string conversion + toString () { + return this.id() + } + + words (text) { + // This is faster than removing all children and adding a new one + this.node.textContent = text + return this + } + + wrap (node) { + const parent = this.parent() + + if (!parent) { + return this.addTo(node) + } + + const position = parent.index(this) + return parent.put(node, position).put(this) + } + + // write svgjs data to the dom + writeDataToDom () { + // dump variables recursively + this.each(function () { + this.writeDataToDom() + }) + + return this + } + + // Import / Export raw svg + xml (xmlOrFn, outerXML, ns) { var well, len, fragment - if (svgOrFn === false) { - outerHTML = false - svgOrFn = null + if (xmlOrFn === false) { + outerXML = false + xmlOrFn = null } // act as getter if no svg string is given - if (svgOrFn == null || typeof svgOrFn === 'function') { + if (xmlOrFn == null || typeof xmlOrFn === 'function') { // The default for exports is, that the outerNode is included - outerHTML = outerHTML == null ? true : outerHTML + outerXML = outerXML == null ? true : outerXML // write svgjs data to the dom this.writeDataToDom() let current = this // An export modifier was passed - if (svgOrFn != null) { + if (xmlOrFn != null) { current = adopt(current.node.cloneNode(true)) // If the user wants outerHTML we need to process this node, too - if (outerHTML) { - const result = svgOrFn(current) + if (outerXML) { + const result = xmlOrFn(current) current = result || current // The user does not want this node? Well, then he gets nothing @@ -252,7 +293,7 @@ export default class Dom extends EventTarget { // Deep loop through all children and apply modifier current.each(function () { - const result = svgOrFn(this) + const result = xmlOrFn(this) const _this = result || this // If modifier returns false, discard node @@ -267,7 +308,7 @@ export default class Dom extends EventTarget { } // Return outer or inner content - return outerHTML + return outerXML ? current.node.outerHTML : current.node.innerHTML } @@ -275,14 +316,14 @@ export default class Dom extends EventTarget { // Act as setter if we got a string // The default for import is, that the current node is not replaced - outerHTML = outerHTML == null ? false : outerHTML + outerXML = outerXML == null ? false : outerXML // Create temporary holder well = globals.document.createElementNS(ns, 'svg') fragment = globals.document.createDocumentFragment() // Dump raw svg - well.innerHTML = svgOrFn + well.innerHTML = xmlOrFn // Transplant nodes into the fragment for (len = well.children.length; len--;) { @@ -292,42 +333,10 @@ export default class Dom extends EventTarget { const parent = this.parent() // Add the whole fragment at once - return outerHTML + return outerXML ? this.replace(fragment) && parent : this.add(fragment) } - - // Return id on string conversion - toString () { - return this.id() - } - - words (text) { - // This is faster than removing all children and adding a new one - this.node.textContent = text - return this - } - - wrap (node) { - const parent = this.parent() - - if (!parent) { - return this.addTo(node) - } - - const position = parent.index(this) - return parent.put(node, position).put(this) - } - - // write svgjs data to the dom - writeDataToDom () { - // dump variables recursively - this.each(function () { - this.writeDataToDom() - }) - - return this - } } extend(Dom, { attr, find, findOne }) diff --git a/src/elements/Svg.js b/src/elements/Svg.js index 51f4202..acddf34 100644 --- a/src/elements/Svg.js +++ b/src/elements/Svg.js @@ -4,7 +4,7 @@ import { register, wrapWithAttrCheck } from '../utils/adopter.js' -import { ns, svgjs, xlink, xmlns } from '../modules/core/namespaces.js' +import { svg, svgjs, xlink, xmlns } from '../modules/core/namespaces.js' import { registerMethods } from '../utils/methods.js' import Container from './Container.js' import Defs from './Defs.js' @@ -33,7 +33,7 @@ export default class Svg extends Container { namespace () { if (!this.isRoot()) return this.root().namespace() return this - .attr({ xmlns: ns, version: '1.1' }) + .attr({ xmlns: svg, version: '1.1' }) .attr('xmlns:xlink', xlink, xmlns) .attr('xmlns:svgjs', svgjs, xmlns) } diff --git a/src/modules/core/namespaces.js b/src/modules/core/namespaces.js index 086e0e9..3968e63 100644 --- a/src/modules/core/namespaces.js +++ b/src/modules/core/namespaces.js @@ -1,5 +1,6 @@ // Default namespaces -export const ns = 'http://www.w3.org/2000/svg' +export const svg = 'http://www.w3.org/2000/svg' +export const html = 'http://www.w3.org/1999/xhtml' export const xmlns = 'http://www.w3.org/2000/xmlns/' export const xlink = 'http://www.w3.org/1999/xlink' export const svgjs = 'http://svgjs.com/svgjs' diff --git a/src/utils/adopter.js b/src/utils/adopter.js index 217aafb..a136655 100644 --- a/src/utils/adopter.js +++ b/src/utils/adopter.js @@ -1,6 +1,6 @@ import { addMethodNames } from './methods.js' import { capitalize } from './utils.js' -import { ns } from '../modules/core/namespaces.js' +import { svg } from '../modules/core/namespaces.js' import { globals } from '../utils/window.js' import Base from '../types/Base.js' @@ -10,7 +10,7 @@ export const root = '___SYMBOL___ROOT___' // Method for element creation export function create (name) { // create element - return globals.document.createElementNS(ns, name) + return globals.document.createElementNS(svg, name) } export function makeInstance (element, isHTML = false) { -- 2.39.5