1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
;(function () {
try {
if (SVGElement.prototype.innerHTML) return
} catch (e) {
return
}
const serializeXML = function (node, output) {
const nodeType = node.nodeType
if (nodeType === 3) {
output.push(
node.textContent
.replace(/&/, '&')
.replace(/</, '<')
.replace('>', '>')
)
} else if (nodeType === 1) {
output.push('<', node.tagName)
if (node.hasAttributes()) {
;[].forEach.call(node.attributes, function (attrNode) {
output.push(' ', attrNode.name, '="', attrNode.value, '"')
})
}
output.push('>')
if (node.hasChildNodes()) {
;[].forEach.call(node.childNodes, function (childNode) {
serializeXML(childNode, output)
})
} else {
// output.push('/>')
}
output.push('</', node.tagName, '>')
} else if (nodeType === 8) {
output.push('<!--', node.nodeValue, '-->')
}
}
Object.defineProperty(SVGElement.prototype, 'innerHTML', {
get: function () {
const output = []
let childNode = this.firstChild
while (childNode) {
serializeXML(childNode, output)
childNode = childNode.nextSibling
}
return output.join('')
},
set: function (markupText) {
while (this.firstChild) {
this.removeChild(this.firstChild)
}
try {
const dXML = new DOMParser()
dXML.async = false
const sXML =
"<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'>" +
markupText +
'</svg>'
const svgDocElement = dXML.parseFromString(
sXML,
'text/xml'
).documentElement
let childNode = svgDocElement.firstChild
while (childNode) {
this.appendChild(this.ownerDocument.importNode(childNode, true))
childNode = childNode.nextSibling
}
} catch (e) {
throw new Error('Can not set innerHTML on node')
}
}
})
Object.defineProperty(SVGElement.prototype, 'outerHTML', {
get: function () {
const output = []
serializeXML(this, output)
return output.join('')
},
set: function (markupText) {
while (this.firstChild) {
this.removeChild(this.firstChild)
}
try {
const dXML = new DOMParser()
dXML.async = false
const sXML =
"<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'>" +
markupText +
'</svg>'
const svgDocElement = dXML.parseFromString(
sXML,
'text/xml'
).documentElement
let childNode = svgDocElement.firstChild
while (childNode) {
this.parentNode.insertBefore(
this.ownerDocument.importNode(childNode, true),
this
)
// this.appendChild(this.ownerDocument.importNode(childNode, true));
childNode = childNode.nextSibling
}
} catch (e) {
throw new Error('Can not set outerHTML on node')
}
}
})
})()
|