Skip to content

Commit

Permalink
Merge pull request #110 from tomasklaen/presentation-g-wrap
Browse files Browse the repository at this point in the history
Transfer svg presentation attributes to a wrapping g element
  • Loading branch information
w0rm authored Nov 21, 2021
2 parents 6d489e4 + 2ca0510 commit 3edf62a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
32 changes: 31 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ const fancyLog = require('fancy-log')
const PluginError = require('plugin-error')
const Vinyl = require('vinyl')

const presentationAttributes = new Set([
'style', 'alignment-baseline', 'baseline-shift', 'clip', 'clip-path',
'clip-rule', 'color', 'color-interpolation', 'color-interpolation-filters',
'color-profile', 'color-rendering', 'cursor', 'd', 'direction', 'display',
'dominant-baseline', 'enable-background', 'fill', 'fill-opacity', 'fill-rule',
'filter', 'flood-color', 'flood-opacity', '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', 'lighting-color',
'marker-end', 'marker-mid', 'marker-start', 'mask', 'opacity', 'overflow',
'pointer-events', 'shape-rendering', 'solid-color', 'solid-opacity',
'stop-color', 'stop-opacity', 'stroke', 'stroke-dasharray',
'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit',
'stroke-opacity', 'stroke-width', 'text-anchor', 'text-decoration',
'text-rendering', 'transform', 'unicode-bidi', 'vector-effect', 'visibility',
'word-spacing', 'writing-mode'
]);

module.exports = function (config) {

config = config || {}
Expand Down Expand Up @@ -111,7 +129,19 @@ module.exports = function (config) {
$defs.remove()
}

$symbol.append($svg.contents())
let $groupWrap = null
for (let [name, value] of Object.entries($svg.attr())) {
if (!presentationAttributes.has(name)) continue;
if (!$groupWrap) $groupWrap = $('<g/>')
$groupWrap.attr(name, value)
}

if ($groupWrap) {
$groupWrap.append($svg.contents())
$symbol.append($groupWrap)
} else {
$symbol.append($svg.contents())
}
$combinedSvg.append($symbol)
cb()
}
Expand Down
24 changes: 24 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,30 @@ describe('gulp-svgstore unit test', () => {
stream.end()
})

it('should transfer svg presentation attributes to a wrapping g element', (done) => {
const stream = svgstore({ inlineSvg: true })
const attrs = 'stroke="currentColor" stroke-width="2" stroke-linecap="round" style="fill:#0000"';

stream.on('data', (file) => {
assert.strictEqual(
'<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">' +
`<symbol id="rect"><g ${attrs}><rect width="1" height="1"/></g></symbol></svg>`,
file.contents.toString()
)
done()
})

stream.write(new Vinyl({
contents: Buffer.from(
`<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" ${attrs}>` +
'<rect width="1" height="1"/></svg>'
)
, path: 'rect.svg'
}))

stream.end()
})

it('Warn about duplicate namespace value under different name', (done) => {
const stream = svgstore()

Expand Down

0 comments on commit 3edf62a

Please sign in to comment.