forked from Hypercubed/svgsaver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.js
More file actions
443 lines (386 loc) · 14.7 KB
/
Copy pathbrowser.js
File metadata and controls
443 lines (386 loc) · 14.7 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.SvgSaver = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var svgStyles = { // Whitelist of CSS styles and default values
'alignment-baseline': 'auto',
'baseline-shift': 'baseline',
'clip': 'auto',
'clip-path': 'none',
'clip-rule': 'nonzero',
'color': 'rgb(51, 51, 51)',
'color-interpolation': 'srgb',
'color-interpolation-filters': 'linearrgb',
'color-profile': 'auto',
'color-rendering': 'auto',
'cursor': 'auto',
'direction': 'ltr',
'display': 'inline',
'dominant-baseline': 'auto',
'enable-background': '',
'fill': 'rgb(0, 0, 0)',
'fill-opacity': '1',
'fill-rule': 'nonzero',
'filter': 'none',
'flood-color': 'rgb(0, 0, 0)',
'flood-opacity': '1',
'font': '',
'font-family': 'normal',
'font-size': 'medium',
'font-size-adjust': 'auto',
'font-stretch': 'normal',
'font-style': 'normal',
'font-variant': 'normal',
'font-weight': '400',
'glyph-orientation-horizontal': '0deg',
'glyph-orientation-vertical': 'auto',
'image-rendering': 'auto',
'kerning': 'auto',
'letter-spacing': '0',
'lighting-color': 'rgb(255, 255, 255)',
'marker': '',
'marker-end': 'none',
'marker-mid': 'none',
'marker-start': 'none',
'mask': 'none',
'opacity': '1',
'overflow': 'visible',
'paint-order': 'fill',
'pointer-events': 'auto',
'shape-rendering': 'auto',
'stop-color': 'rgb(0, 0, 0)',
'stop-opacity': '1',
'stroke': 'none',
'stroke-dasharray': 'none',
'stroke-dashoffset': '0',
'stroke-linecap': 'butt',
'stroke-linejoin': 'miter',
'stroke-miterlimit': '4',
'stroke-opacity': '1',
'stroke-width': '1',
'text-anchor': 'start',
'text-decoration': 'none',
'text-rendering': 'auto',
'unicode-bidi': 'normal',
'visibility': 'visible',
'word-spacing': '0px',
'writing-mode': 'lr-tb'
};
var svgAttrs = [// white list of attributes
'id', 'xml: base', 'xml: lang', 'xml: space', // Core
'height', 'result', 'width', 'x', 'y', // Primitive
'xlink: href', // Xlink attribute
'style', 'class', 'd', 'pathLength', // Path
'x', 'y', 'dx', 'dy', 'glyphRef', 'format', 'x1', 'y1', 'x2', 'y2', 'rotate', 'textLength', 'cx', 'cy', 'r', 'rx', 'ry', 'fx', 'fy', 'width', 'height', 'refX', 'refY', 'orient', 'markerUnits', 'markerWidth', 'markerHeight', 'maskUnits', 'transform', 'viewBox', 'version', // Container
'preserveAspectRatio', 'xmlns', 'points', // Polygons
'offset'];
// http://www.w3.org/TR/SVG/propidx.html
// via https://github.com/svg/svgo/blob/master/plugins/_collections.js
var inheritableAttrs = ['clip-rule', 'color', 'color-interpolation', 'color-interpolation-filters', 'color-profile', 'color-rendering', 'cursor', 'direction', 'fill', 'fill-opacity', 'fill-rule', 'font', 'font-family', 'font-size', 'font-size-adjust', 'font-stretch', 'font-style', 'font-variant', 'font-weight', 'glyph-orientation-horizontal', 'glyph-orientation-vertical', 'image-rendering', 'kerning', 'letter-spacing', 'marker', 'marker-end', 'marker-mid', 'marker-start', 'pointer-events', 'shape-rendering', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'text-anchor', 'text-rendering', 'transform', 'visibility', 'white-space', 'word-spacing', 'writing-mode'];
/* Some simple utilities */
var isFunction = function isFunction(a) {
return typeof a === 'function';
};
var isDefined = function isDefined(a) {
return typeof a !== 'undefined';
};
var isUndefined = function isUndefined(a) {
return typeof a === 'undefined';
};
var isObject = function isObject(a) {
return a !== null && typeof a === 'object';
};
// from https://github.com/npm-dom/is-dom/blob/master/index.js
function isNode(val) {
if (!isObject(val)) {
return false;
}
if (isDefined(window) && isObject(window.Node)) {
return val instanceof window.Node;
}
return typeof val.nodeType === 'number' && typeof val.nodeName === 'string';
}
// detection
var DownloadAttributeSupport = typeof document !== 'undefined' && 'download' in document.createElement('a');
function saveUri(uri, name) {
if (DownloadAttributeSupport) {
var dl = document.createElement('a');
dl.setAttribute('href', uri);
dl.setAttribute('download', name);
// firefox doesn't support `.click()`...
// from https://github.com/sindresorhus/multi-download/blob/gh-pages/index.js
dl.dispatchEvent(new MouseEvent('click'));
return true;
} else if (typeof window !== 'undefined') {
window.open(uri, '_blank', '');
return true;
}
return false;
}
function savePng(uri, name) {
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
var image = new Image();
image.onload = function () {
canvas.width = image.width;
canvas.height = image.height;
context.drawImage(image, 0, 0);
if (isDefined(window.saveAs) && isDefined(canvas.toBlob)) {
canvas.toBlob(function (blob) {
saveAs(blob, name);
});
} else {
saveUri(canvas.toDataURL('image/png'), name);
}
};
image.src = uri;
return true;
}
var _isDefined = function _isDefined(a) {
return typeof a !== 'undefined';
};
var _isUndefined = function _isUndefined(a) {
return typeof a === 'undefined';
};
var _isObject = function _isObject(a) {
return a !== null && typeof a === 'object';
};
// from https://github.com/npm-dom/is-dom/blob/master/index.js
function _isNode(val) {
if (!_isObject(val)) return false;
if (_isDefined(window) && _isObject(window.Node)) return val instanceof window.Node;
return 'number' == typeof val.nodeType && 'string' == typeof val.nodeName;
}
var useComputedStyles = _isDefined(window) && _isDefined(window.getComputedStyle);
// Gets computed styles for an element
// from https://github.com/jquery/jquery/blob/master/src/css/var/getStyles.js
function getComputedStyles(node) {
if (useComputedStyles) {
var view = node.ownerDocument.defaultView;
if (!view.opener) view = window;
return view.getComputedStyle(node, null);
} else {
return node.currentStyle || node.style;
}
}
/**
* Returns a collection of CSS property-value pairs
* @param {Element} node A DOM element to copy styles from
* @param {Object} [target] An optional object to copy styles to
* @param {(Object|Boolean)} [default=true] A collection of CSS property-value pairs, false: copy none, true: copy all
* @return {object} collection of CSS property-value pairs
* @api public
*/
function computedStyles(node) {
var target = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var styleList = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2];
if (!_isNode(node)) {
throw new Error('parameter 1 is not of type \'Element\'');
}
if (styleList === false) return target;
var computed = getComputedStyles(node);
if (styleList === true) {
var keysArray = useComputedStyles ? computed : Object.keys(computed);
} else {
var keysArray = Object.keys(styleList);
}
for (var i = 0, l = keysArray.length; i < l; i++) {
var key = keysArray[i];
var def = styleList === true || styleList[key];
if (def === false || _isUndefined(def)) continue; // copy never
var value = /* computed.getPropertyValue(key) || */computed[key]; // using getPropertyValue causes error in IE11
if (typeof value !== 'string' || value === '') continue; // invalid value
if (def === true || value !== def) {
// styleList === true || styleList[key] === true || styleList[key] !== value
target[key] = value;
}
}
return target;
}
// Removes attributes that are not valid for SVGs
function cleanAttrs(el, attrs, styles) {
// attrs === false - remove all, attrs === true - allow all
if (attrs === true) {
return;
}
Array.prototype.slice.call(el.attributes).forEach(function (attr) {
// remove if it is not style nor on attrs whitelist
// keeping attributes that are also styles because attributes override
if (attr.specified) {
if (attrs === '' || attrs === false || isUndefined(styles[attr.name]) && attrs.indexOf(attr.name) < 0) {
el.removeAttribute(attr.name);
}
}
});
}
function cleanStyle(tgt, parentStyles) {
parentStyles = parentStyles || tgt.parentNode.style;
inheritableAttrs.forEach(function (key) {
if (tgt.style[key] === parentStyles[key]) {
tgt.style.removeProperty(key);
}
});
}
function domWalk(src, tgt, down, up) {
down(src, tgt);
var children = src.childNodes;
for (var i = 0; i < children.length; i++) {
domWalk(children[i], tgt.childNodes[i], down, up);
}
up(src, tgt);
}
// Clones an SVGElement, copies approprate atttributes and styles.
function cloneSvg(src, attrs, styles) {
var clonedSvg = src.cloneNode(true);
domWalk(src, clonedSvg, function (src, tgt) {
if (tgt.style) {
computedStyles(src, tgt.style, styles);
}
}, function (src, tgt) {
if (tgt.style && tgt.parentNode) {
cleanStyle(tgt);
}
if (tgt.attributes) {
cleanAttrs(tgt, attrs, styles);
}
});
return clonedSvg;
}
// inheritable styles may be overridden by parent, always copy for now
inheritableAttrs.forEach(function (k) {
if (k in svgStyles) {
svgStyles[k] = true;
}
});
function getSvg(el) {
if (isUndefined(el) || el === '') {
el = document.body.querySelector('svg');
} else if (typeof el === 'string') {
el = document.body.querySelector(el);
}
if (el && el.tagName !== 'svg') {
el = el.querySelector('svg');
}
if (!isNode(el)) {
throw new Error('svgsaver: Can\'t find an svg element');
}
return el;
}
function getFilename(el, filename, ext) {
if (!filename || filename === '') {
filename = (el.getAttribute('title') || 'untitled') + '.' + ext;
}
return encodeURI(filename);
}
var SvgSaver = (function () {
/**
* SvgSaver constructor.
* @constructs SvgSaver
* @api public
*
* @example
* var svgsaver = new SvgSaver(); // creates a new instance
* var svg = document.querySelector('#mysvg'); // find the SVG element
* svgsaver.asSvg(svg); // save as SVG
*/
function SvgSaver() {
var _ref = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var attrs = _ref.attrs;
var styles = _ref.styles;
_classCallCheck(this, SvgSaver);
this.attrs = attrs === undefined ? svgAttrs : attrs;
this.styles = styles === undefined ? svgStyles : styles;
}
/**
* Return the SVG HTML text after cleaning
*
* @param {SVGElement} el The element to copy.
* @returns {String} SVG text after cleaning
* @api public
*/
_createClass(SvgSaver, [{
key: 'getHTML',
value: function getHTML(el) {
el = getSvg(el);
var svg = cloneSvg(el, this.attrs, this.styles);
svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
svg.setAttribute('version', 1.1);
// height and width needed to download in FireFox
svg.setAttribute('width', svg.getAttribute('width') || '500');
svg.setAttribute('height', svg.getAttribute('height') || '900');
return svg.outerHTML || new window.XMLSerializer().serializeToString(svg);
}
/**
* Return the SVG, after cleaning, as a text/xml Blob
*
* @param {SVGElement} el The element to copy.
* @returns {Blog} SVG as a text/xml Blob
* @api public
*/
}, {
key: 'getBlob',
value: function getBlob(el) {
var html = this.getHTML(el);
return new Blob([html], { type: 'text/xml' });
}
/**
* Return the SVG, after cleaning, as a image/svg+xml;base64 URI encoded string
*
* @param {SVGElement} el The element to copy.
* @returns {String} SVG as image/svg+xml;base64 URI encoded string
* @api public
*/
}, {
key: 'getUri',
value: function getUri(el) {
var html = encodeURIComponent(this.getHTML(el));
if (isDefined(window.btoa)) {
// see http://stackoverflow.com/questions/23223718/failed-to-execute-btoa-on-window-the-string-to-be-encoded-contains-characte
return 'data:image/svg+xml;base64,' + window.btoa(unescape(html));
}
return 'data:image/svg+xml,' + html;
}
/**
* Saves the SVG as a SVG file using method compatible with the browser
*
* @param {SVGElement} el The element to copy.
* @param {string} [filename] The filename to save, defaults to the SVG title or 'untitled.svg'
* @returns {SvgSaver} The SvgSaver instance
* @api public
*/
}, {
key: 'asSvg',
value: function asSvg(el, filename) {
el = getSvg(el);
filename = getFilename(el, filename, 'svg');
if (isDefined(window.saveAs) && isFunction(Blob)) {
return saveAs(this.getBlob(el), filename);
} else {
return saveUri(this.getUri(el), filename);
}
}
/**
* Saves the SVG as a PNG file using method compatible with the browser
*
* @param {SVGElement} el The element to copy.
* @param {string} [filename] The filename to save, defaults to the SVG title or 'untitled.png'
* @returns {SvgSaver} The SvgSaver instance
* @api public
*/
}, {
key: 'asPng',
value: function asPng(el, filename) {
el = getSvg(el);
filename = getFilename(el, filename, 'png');
return savePng(this.getUri(el), filename);
}
}]);
return SvgSaver;
})();
exports['default'] = SvgSaver;
module.exports = exports['default'];
},{}]},{},[1])(1)
});