-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjquery.inlinesvg.js
178 lines (149 loc) · 5.76 KB
/
jquery.inlinesvg.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
/*!
* InlineSVG
*
* This is a jQuery plugin that takes an image selector as an argument having a
* SVG as source. Then it inlines the SVG so that the SVG stroke and path can
* be manipulated using plain CSS.
*
* @license MIT
* @version 2.0.0
* @see {@link https://github.com/createlogic/InlineSVG|GitHub}
*
* The MIT License (MIT)
*
* Copyright (c) 2010-2015 Bilal Niaz Awan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
(function ($) {
'use strict';
/**
* @name jQuery
* @constructor
*/
/**
* @callback jQuery.inlineSVG~eachAfter
* @this DOM
*/
/**
* @callback jQuery.inlineSVG~allAfter
* @this null
*/
/**
* @callback jQuery.inlineSVG~beforeReplace
* @param {jQuery} $img - Source <img> element
* @param {jQuery} $svg - <svg> element to replace
* @param {jQuery.inlineSVG~beforeReplaceNext} next - Callback to next element
* @this null
*/
/**
* @callback jQuery.inlineSVG~beforeReplaceNext
* @param {Boolean} [replace=true] - Replace element otherwise just go to next
*/
/**
* @typedef {Object} jQuery.inlineSVG~options
* @property {?jQuery.inlineSVG~eachAfter} [eachAfter] - Callback for each replaced <img> element
* @property {?jQuery.inlineSVG~allAfter} [allAfter] - Callback after all <img> elements is replaced
* @property {?jQuery.inlineSVG~beforeReplaceCallback} [beforeReplace] - Callback for each item before replaced
* @property {?String} [replacedClass='replaced-svg'] - Class name to add to new <svg> DOM-element
* @property {Boolean} [keepSize=true] - Set "width" and "height" attributes from source <img> to new <svg>
* @property {Boolean} [keepStyle=true] - Set "style" attribute from source <img> to new <svg>
*/
/**
* @name inlineSVG
* @memberof jQuery
* @param {jQuery.inlineSVG~options} [options]
* @this jQuery
* @returns {jQuery} Source jQuery instance
* @public
* @static
*/
$.fn.inlineSVG = function (options) {
options = $.extend({
eachAfter: null,
allAfter: null,
beforeReplace: null,
replacedClass: 'replaced-svg',
keepSize: true,
keepStyle: true
}, (options || {}));
var $list = this;
var counter = 0;
return $list.each(function () {
var $img = $(this);
var imgID = $img.attr('id');
var imgClass = $img.attr('class');
var imgURL = $img.attr('src');
$.get(imgURL, function (data) {
// Get the SVG tag, ignore the rest
var $svg = $(data).find('svg');
var classes = [];
// Add replaced image's ID to the new SVG
if (imgID) {
$svg.attr('id', imgID);
}
// Add replaced image's classes to the new SVG
if (imgClass) {
classes.push(imgClass);
}
if (options.replacedClass) {
classes.push(options.replacedClass);
}
$svg.attr('class', classes.join(' '));
// Remove any invalid XML tags as per http://validator.w3.org
$svg.removeAttr('xmlns:a');
if (options.keepSize) {
var w = $img.attr('width');
var h = $img.attr('height');
if (w) {
$svg.attr('width', w);
}
if (h) {
$svg.attr('height', h);
}
}
if (options.keepStyle) {
var style = $img.attr('style');
if (style) {
$svg.attr('style', style);
}
}
function cb(replace) {
replace = ($.type(replace) === 'boolean') ? replace : true;
if (replace) {
// Replace image with new SVG
$img.replaceWith($svg);
// Callback for each element
options.eachAfter && options.eachAfter.call($svg.get(0));
} else {
$svg.remove();
}
// Check for all is completed
if (++counter === $list.length) {
options.allAfter && options.allAfter.call(null);
}
}
if (options.beforeReplace) {
options.beforeReplace.call(null, $img, $svg, cb);
} else {
cb();
}
}, 'xml');
});
};
})(jQuery);