-
Notifications
You must be signed in to change notification settings - Fork 0
/
svginnerhtml.js
99 lines (72 loc) · 2.79 KB
/
svginnerhtml.js
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
(function (view) {
var
constructors = ['SVGSVGElement', 'SVGGElement']
, dummy = document.createElement('dummy');
if (!constructors[0] in view) {
return false;
}
if (Object.defineProperty) {
var innerHTMLPropDesc = {
get : function () {
dummy.innerHTML = '';
Array.prototype.slice.call(this.childNodes)
.forEach(function (node, index) {
dummy.appendChild(node.cloneNode(true));
});
return dummy.innerHTML;
},
set : function (content) {
var
self = this
, parent = this
, allNodes = Array.prototype.slice.call(self.childNodes)
, fn = function (to, node) {
if (node.nodeType !== 1) {
return false;
}
var newNode = document.createElementNS('http://www.w3.org/2000/svg', node.nodeName);
Array.prototype.slice.call(node.attributes)
.forEach(function (attribute) {
newNode.setAttribute(attribute.name, attribute.value);
});
if (node.nodeName === 'TEXT') {
newNode.textContent = node.innerHTML;
}
to.appendChild(newNode);
if (node.childNodes.length) {
Array.prototype.slice.call(node.childNodes)
.forEach(function (node, index) {
fn(newNode, node);
});
}
};
// /> to </tag>
content = content.replace(/<(\w+)([^<]+?)\/>/, '<$1$2></$1>');
// Remove existing nodes
allNodes.forEach(function (node, index) {
node.parentNode.removeChild(node);
});
dummy.innerHTML = content;
Array.prototype.slice.call(dummy.childNodes)
.forEach(function (node) {
fn(self, node);
});
}
, enumerable : true
, configurable : true
};
try {
constructors.forEach(function (constructor, index) {
Object.defineProperty(window[constructor].prototype, 'innerHTML', innerHTMLPropDesc);
});
} catch (ex) {
// TODO: Do something meaningful here
}
} else if (Object['prototype'].__defineGetter__) {
constructors.forEach(function (constructor, index) {
window[constructor].prototype.__defineSetter__('innerHTML', innerHTMLPropDesc.set);
window[constructor].prototype.__defineGetter__('innerHTML', innerHTMLPropDesc.get);
});
}
/** https://github.com/phaistonian/SVGInnerHTML */
} (window));