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'
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
}
// 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
// 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
}
// Return outer or inner content
- return outerHTML
+ return outerXML
? current.node.outerHTML
: current.node.innerHTML
}
// 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--;) {
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 })