Skip to content

Commit 3edf62a

Browse files
authored
Merge pull request #110 from tomasklaen/presentation-g-wrap
Transfer svg presentation attributes to a wrapping g element
2 parents 6d489e4 + 2ca0510 commit 3edf62a

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

index.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@ const fancyLog = require('fancy-log')
55
const PluginError = require('plugin-error')
66
const Vinyl = require('vinyl')
77

8+
const presentationAttributes = new Set([
9+
'style', 'alignment-baseline', 'baseline-shift', 'clip', 'clip-path',
10+
'clip-rule', 'color', 'color-interpolation', 'color-interpolation-filters',
11+
'color-profile', 'color-rendering', 'cursor', 'd', 'direction', 'display',
12+
'dominant-baseline', 'enable-background', 'fill', 'fill-opacity', 'fill-rule',
13+
'filter', 'flood-color', 'flood-opacity', 'font-family', 'font-size',
14+
'font-size-adjust', 'font-stretch', 'font-style', 'font-variant',
15+
'font-weight', 'glyph-orientation-horizontal', 'glyph-orientation-vertical',
16+
'image-rendering', 'kerning', 'letter-spacing', 'lighting-color',
17+
'marker-end', 'marker-mid', 'marker-start', 'mask', 'opacity', 'overflow',
18+
'pointer-events', 'shape-rendering', 'solid-color', 'solid-opacity',
19+
'stop-color', 'stop-opacity', 'stroke', 'stroke-dasharray',
20+
'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit',
21+
'stroke-opacity', 'stroke-width', 'text-anchor', 'text-decoration',
22+
'text-rendering', 'transform', 'unicode-bidi', 'vector-effect', 'visibility',
23+
'word-spacing', 'writing-mode'
24+
]);
25+
826
module.exports = function (config) {
927

1028
config = config || {}
@@ -111,7 +129,19 @@ module.exports = function (config) {
111129
$defs.remove()
112130
}
113131

114-
$symbol.append($svg.contents())
132+
let $groupWrap = null
133+
for (let [name, value] of Object.entries($svg.attr())) {
134+
if (!presentationAttributes.has(name)) continue;
135+
if (!$groupWrap) $groupWrap = $('<g/>')
136+
$groupWrap.attr(name, value)
137+
}
138+
139+
if ($groupWrap) {
140+
$groupWrap.append($svg.contents())
141+
$symbol.append($groupWrap)
142+
} else {
143+
$symbol.append($svg.contents())
144+
}
115145
$combinedSvg.append($symbol)
116146
cb()
117147
}

test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,30 @@ describe('gulp-svgstore unit test', () => {
287287
stream.end()
288288
})
289289

290+
it('should transfer svg presentation attributes to a wrapping g element', (done) => {
291+
const stream = svgstore({ inlineSvg: true })
292+
const attrs = 'stroke="currentColor" stroke-width="2" stroke-linecap="round" style="fill:#0000"';
293+
294+
stream.on('data', (file) => {
295+
assert.strictEqual(
296+
'<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">' +
297+
`<symbol id="rect"><g ${attrs}><rect width="1" height="1"/></g></symbol></svg>`,
298+
file.contents.toString()
299+
)
300+
done()
301+
})
302+
303+
stream.write(new Vinyl({
304+
contents: Buffer.from(
305+
`<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" ${attrs}>` +
306+
'<rect width="1" height="1"/></svg>'
307+
)
308+
, path: 'rect.svg'
309+
}))
310+
311+
stream.end()
312+
})
313+
290314
it('Warn about duplicate namespace value under different name', (done) => {
291315
const stream = svgstore()
292316

0 commit comments

Comments
 (0)