forked from oreillymedia/svg-essentials-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg_utils_webkit.js
218 lines (194 loc) · 4.87 KB
/
svg_utils_webkit.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
var svgOriginal;
var svgMarkup;
var svgSource;
var svgError;
var svgShowError;
var svgZoom;
function setValue(id, value)
{
document.getElementById(id).value = value;
}
function setAttr(id, attr, value)
{
document.getElementById(id).setAttribute(attr, value);
}
function setHTML(id, html)
{
document.getElementById(id).innerHTML = html;
}
function setText(id, text)
{
var node = document.createTextNode(text);
var obj = document.getElementById(id);
obj.replaceChild(node, obj.firstChild);
}
function getFloat(id)
{
var n = document.getElementById(id).value;
return parseFloat(n);
}
function getInt(id)
{
var n = document.getElementById(id).value;
return parseInt(n, 10);
}
function getValue(id)
{
return document.getElementById(id).value;
}
function initElements()
{
svgOriginal = document.getElementById("svgOriginal");
// svgMarkup = document.getElementById("svgMarkup");
svgSource = document.getElementById("svgSource");
svgError = document.getElementById("svgError");
svgShowError = document.getElementById("svgShowError");
svgZoom = document.getElementById('svgZoom');
if (svgZoom)
{
svgZoom.checked = false;
}
}
function init()
{
initElements();
reset();
alert(document.getElementById("c1"));
}
function reset()
{
if (svgOriginal && svgSource)
{
var original = svgOriginal.innerHTML;
svgSource.innerHTML = original;
refresh();
}
}
function zoom()
{
var svgElement = document.getElementById("svgOutput").
getElementsByTagName("svg")[0];
var w = svgElement.getAttribute("width");
var h = svgElement.getAttribute("height");
if (svgZoom && svgZoom.checked)
{
svgElement.setAttribute("width", w * 2);
svgElement.setAttribute("height", h * 2);
}
else
{
svgElement.setAttribute("width", w / 2);
svgElement.setAttribute("height", h / 2);
}
}
function showGrid()
{
var show = document.getElementById("showGrid").checked;
var grid = document.getElementById("svgGrid");
grid.style.display = (show) ? "block" : "none";
}
function refresh()
{
if (svgSource) {
var source = svgSource.innerHTML;
var parser = new DOMParser();
var doc;
var elements;
source = source.replace(/<[^>]+?>/g, "");
source = source.replace(/</g, "<");
source = source.replace(/>/g, ">");
source = source.replace(/&/g, "&");
// If showing errors, parse as image/svg+xml
// otherwise, set "parsererror" elements to empty
if (svgShowError && svgShowError.checked)
{
doc = parser.parseFromString(source, "image/svg+xml");
var ser = new XMLSerializer();
alert(ser.serializeToString(doc));
elements = doc.getElementsByTagName("parsererror");
alert(elements + " " + elements.length);
}
else
{
elements = [];
}
if (elements.length > 0)
{
var nodeType = elements[0].firstChild.nodeType;
if (nodeType == 1) // element node
{
svgError.innerHTML = elements[0].innerHTML;
}
else if (nodeType == 3) // text node
{
svgError.innerHTML = elements[0].firstChild.textContent;
}
svgError.style.display = "block";
}
else
{
// now parse again as text/html so it can be
// inserted into an HTML document.
doc = parser.parseFromString(source, "text/html");
elements = doc.getElementsByTagName("svg");
if (elements.length > 0)
{
svgError.style.display = "none";
if (svgOutput.firstChild)
{
svgOutput.replaceChild(elements[0], svgOutput.firstChild)
}
else
{
svgOutput.appendChild(elements[0]);
}
}
}
}
}
function reanimate()
{
var svg = document.getElementById("svgOutput").firstChild;
svg.setCurrentTime(0);
}
/*
* DOMParser HTML extension
* 2012-09-04
*
* By Eli Grey, http://eligrey.com
* Public domain.
* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
*/
/*! @source https://gist.github.com/1129031 */
/*global document, DOMParser*/
(function(DOMParser) {
"use strict";
var
DOMParser_proto = DOMParser.prototype
, real_parseFromString = DOMParser_proto.parseFromString
;
// Firefox/Opera/IE throw errors on unsupported types
try {
// WebKit returns null on unsupported types
if ((new DOMParser).parseFromString("", "text/html")) {
// text/html parsing is natively supported
return;
}
} catch (ex) {}
DOMParser_proto.parseFromString = function(markup, type) {
if (/^\s*text\/html\s*(?:;|$)/i.test(type)) {
var
doc = document.implementation.createHTMLDocument("")
;
if (markup.toLowerCase().indexOf('<!doctype') > -1) {
doc.documentElement.innerHTML = markup;
}
else {
doc.body.innerHTML = markup;
}
return doc;
} else {
return real_parseFromString.apply(this, arguments);
}
};
}(DOMParser));