forked from ferdinandtorggler/remove-svg-properties
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
193 lines (159 loc) · 5.24 KB
/
index.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
'use strict';
var commonProperties = require('./lib/properties');
var cheerio = require('cheerio');
var juice = require('juice');
var fs = require('graceful-fs');
var vfs = require('vinyl-fs');
var mkdirp = require('mkdirp');
var through2 = require('through2');
var path = require('path');
var css = require('css');
var consume = require('stream-consume');
var _ = require('lodash');
var colors = require('colors');
var cheerioOpts = {
xmlMode: true,
lowerCaseTags: false,
lowerCaseAttributeNames: false,
recognizeCDATA: true
};
var opt;
var defaults = {
src: null,
out: null,
stylesheets: true,
attributes: true,
inline: true,
properties: [],
namespaces: [],
stylesToInline: false,
log: true
};
var counters = {
stylesheetRules: 0,
attributes: 0,
inlineRules: 0,
reset: function () {
this.stylesheetRules = 0;
this.attributes = 0;
this.inlineRules = 0;
}
};
function noop () {
}
function writeFile (name, contents, cb) {
mkdirp(path.dirname(name), function () {
fs.writeFile(name, contents, cb);
});
}
function removeInline (cssProperty, element) {
if (element.css(cssProperty)) counters.inlineRules += 1; // count
element.css(cssProperty, '');
}
function removeAttribute (attribute, element) {
if (element.attr(attribute)) counters.attributes += 1; //count
element.removeAttr(attribute);
}
function attrSelector (propertiesArray) {
var selectors = propertiesArray.map(function (item) {
return '[' + item + ']';
});
return selectors.join(',');
}
function error (condition, message) {
if (condition) {
throw new Error(message);
}
}
function remove (file, enc, cb) {
var svgMarkup = opt.stylesheets && opt.stylesToInline ? juice(file.contents) : file.contents;
var $ = cheerio.load(svgMarkup, cheerioOpts);
_.forEach(opt.namespaces, function (namespace) {
$('*').each(function (i, elem) {
_.forEach(elem.attribs, function (value, attr) {
if (attr.match(new RegExp('^' + namespace + ':'))) {
removeAttribute(attr, $(elem));
}
});
});
});
var removeProperties = function (element, inline) {
opt.properties.forEach(function (item) {
if (inline) {
removeInline(item, element);
} else {
removeAttribute(item, element);
}
});
};
if (opt.attributes) $(attrSelector(opt.properties)).each(function (i, element) {
removeProperties($(element));
});
if (opt.inline) $('[style]').each(function (i, element) {
removeProperties($(element), true);
});
if (opt.stylesheets && !opt.stylesToInline) {
var styleDefs = $('style');
_.forEach(styleDefs, function (styleDef) {
styleDef = $(styleDef);
var parsed = css.parse(styleDef.text());
_.forEach(parsed.stylesheet.rules, function (ruleSet) {
var totalDeclarations = ruleSet.declarations.length;
ruleSet.declarations = _.filter(ruleSet.declarations, function (rule) {
return !_.contains(opt.properties, rule.property);
});
counters.stylesheetRules = totalDeclarations - ruleSet.declarations.length;
});
styleDef.text('\n' + css.stringify(parsed) + '\n');
});
};
if (opt.removeTitle) {
$('title').each(function() {
$(this).remove();
});
}
// In non-streaming mode: Write file to specified output directory (grunt)
// In streaming mode: Overwrite file contents and push it furhter (gulp)
if (opt.out) {
writeFile(path.join(path.resolve(opt.out), path.basename(file.path)), $.xml(), cb);
} else {
file.contents = new Buffer($.xml());
cb();
}
this.push(file);
var inlineCount = (counters.inlineRules + ' inline styles, ')[counters.inlineRules > 0 ? 'yellow': 'reset'];
var attrCount = (counters.attributes + ' attributes, ')[counters.attributes > 0 ? 'yellow': 'reset'];
var styleCount = (counters.stylesheetRules + ' stylesheet rules ')[counters.stylesheetRules > 0 ? 'yellow': 'reset'];
if (opt.log) {
console.log(path.basename(file.path).bold + ': ' +
inlineCount + attrCount + styleCount + 'removed.');
}
counters.reset();
}
// merge with defaults and flatten arrays
function prepareOptions (options) {
var prepared = _.assign(defaults, options);
prepared.properties = _.flatten(prepared.properties);
prepared.namespaces = _.flatten(prepared.namespaces);
return prepared;
}
function run (options, done) {
opt = prepareOptions(options);
// done function is used in grunt
if (!done) {
done = noop;
}
error(opt.src === null, 'source glob missing');
error(opt.out === null, 'output dir missing');
var stream = vfs.src(opt.src)
.pipe(through2.obj(remove, undefined, done));
consume(stream);
}
var stream = {
remove: function (options) {
opt = prepareOptions(options);
return through2.obj(remove);
}
};
stream = _.assign(stream, commonProperties);
module.exports = _.assign({remove: run, stream: stream}, commonProperties);