forked from w0rm/gulp-svgstore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
127 lines (104 loc) · 3.32 KB
/
Copy pathindex.js
File metadata and controls
127 lines (104 loc) · 3.32 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
var cheerio = require('cheerio')
var path = require('path')
var gutil = require('gulp-util')
var Stream = require('stream')
module.exports = function (config) {
config = config || {}
var namespaces = {}
var isEmpty = true
var fileName
var inlineSvg = config.inlineSvg || false
var ids = {}
var resultSvg = '<svg xmlns="http://www.w3.org/2000/svg"><defs/></svg>'
if (!inlineSvg) {
resultSvg =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" ' +
'"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' +
resultSvg
}
var $ = cheerio.load(resultSvg, { xmlMode: true })
var $combinedSvg = $('svg')
var $combinedDefs = $('defs')
var stream = new Stream.Transform({ objectMode: true })
stream._transform = function transform (file, encoding, cb) {
if (file.isStream()) {
return cb(new gutil.PluginError('gulp-svgstore', 'Streams are not supported!'))
}
if (file.isNull()) return cb()
var $svg = cheerio.load(file.contents.toString(), { xmlMode: true })('svg')
if ($svg.length === 0) return cb()
var idAttr = path.basename(file.relative, path.extname(file.relative))
var viewBoxAttr = $svg.attr('viewBox')
var $symbol = $('<symbol/>')
if (idAttr in ids) {
return cb(new gutil.PluginError('gulp-svgstore', 'File name should be unique: ' + idAttr))
}
ids[idAttr] = true
if (!fileName) {
fileName = path.basename(file.base)
if (fileName === '.' || !fileName) {
fileName = 'svgstore.svg'
} else {
fileName = fileName.split(path.sep).shift() + '.svg'
}
}
if (file && isEmpty) {
isEmpty = false
}
$symbol.attr('id', idAttr)
if (viewBoxAttr) {
$symbol.attr('viewBox', viewBoxAttr)
}
var attrs = $svg[0].attribs
for (var attrName in attrs) {
if (attrName.match(/xmlns:.+/)) {
var storedNs = namespaces[attrName]
var attrNs = attrs[attrName]
if (storedNs !== undefined) {
if (storedNs !== attrNs) {
gutil.log(gutil.colors.red(
attrName + ' namespace appeared multiple times with different value.' +
' Keeping the first one : "' + storedNs +
'".\nEach namespace must be unique across files.'
))
}
} else {
for (var nsName in namespaces) {
if (namespaces[nsName] === attrNs) {
gutil.log(gutil.colors.yellow(
'Same namespace value under different names : ' +
nsName +
' and ' +
attrName +
'.\nKeeping both.'
))
}
}
namespaces[attrName] = attrNs;
}
}
}
var $defs = $svg.find('defs')
if ($defs.length > 0) {
$combinedDefs.append($defs.contents())
$defs.remove()
}
$symbol.append($svg.contents())
$combinedSvg.append($symbol)
cb()
}
stream._flush = function flush (cb) {
if (isEmpty) return cb()
if ($combinedDefs.contents().length === 0) {
$combinedDefs.remove()
}
for (var nsName in namespaces) {
$combinedSvg.attr(nsName, namespaces[nsName])
}
var file = new gutil.File({ path: fileName, contents: new Buffer($.xml()) })
this.push(file)
cb()
}
return stream;
}