svgdom
Version:
Straightforward DOM implementation for SVG, HTML and XML
44 lines (33 loc) • 1.42 kB
JavaScript
const htmlEntities = function (str) {
return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"')
}
var emptyElements = {
br: true,
hr: true,
img: true,
link: true
}
export const tag = function (node) {
const attrs = [ ...node.attrs ].map(function (node) {
return (node.prefix ? node.prefix + ':' : '') + node.localName + '="' + htmlEntities(node.value) + '"'
})
const { prefix, localName } = node
const qualifiedName = (prefix ? prefix + ':' : '') + localName
return '<' + [].concat(qualifiedName, attrs).join(' ') + '>' + (emptyElements[qualifiedName.toLowerCase()] ? '' : node.innerHTML + '</' + qualifiedName + '>')
}
export const cloneNode = function (node) {
const { prefix, localName, namespaceURI: ns, nodeValue, ownerDocument } = node
// Build up the correctly cased qualified name
const qualifiedName = (prefix ? prefix + ':' : '') + localName
// Check if node was created using non-namespace function which can lead to : in the localName.
// This check allows false negatives because `local` only matters IF there are : in the localName
// and we dont care about it when there are non
const local = localName.includes(':')
var clone = new node.constructor(qualifiedName, {
attrs: new Set([ ...node.attrs ].map(node => node.cloneNode())),
nodeValue,
ownerDocument,
local
}, ns)
return clone
}