Skip to content

Commit

Permalink
fix(jsx): remove style tag
Browse files Browse the repository at this point in the history
Fix #9
  • Loading branch information
gregberge committed Oct 30, 2017
1 parent cc3d80d commit f10e703
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ export default SvgComponent;
"
`;

exports[`convert should remove style tags 1`] = `
"import React from \\"react\\";
const SvgComponent = props => (
<svg width={88} height={88} viewBox=\\"0 0 88 88\\" {...props}>
<title>Dismiss</title>
<g id=\\"Blocks\\" fill=\\"none\\" fillRule=\\"evenodd\\" strokeLinecap=\\"square\\">
<g id=\\"Dismiss\\" stroke=\\"#063855\\" strokeWidth={2}>
<path d=\\"M51 37L37 51M51 51L37 37\\" id=\\"Shape\\" />
</g>
</g>
</svg>
);
export default SvgComponent;
"
`;

exports[`rawConvert should convert using specific options 1`] = `
"import React from 'react'
Expand Down
3 changes: 2 additions & 1 deletion src/configToOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import emSize from './h2x/emSize'
import expandProps from './h2x/expandProps'
import replaceAttrValue from './h2x/replaceAttrValue'
import removeComments from './h2x/removeComments'
import removeStyle from './h2x/removeStyle'

const defaultConfig = {
svgo: true,
Expand All @@ -28,7 +29,7 @@ function configToOptions(config = {}) {
config = { ...defaultConfig, ...config }

function getH2xPlugins() {
const plugins = [jsx, stripAttribute('xmlns'), removeComments]
const plugins = [jsx, stripAttribute('xmlns'), removeComments, removeStyle]
if (config.icon) plugins.push(emSize)
config.replaceAttrValues.forEach(([oldValue, newValue]) => {
plugins.push(replaceAttrValue(oldValue, newValue))
Expand Down
13 changes: 13 additions & 0 deletions src/h2x/removeStyle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const removeStyle = () => ({
visitor: {
JSXElement: {
enter(path) {
if (path.node.name === 'style') {
path.remove()
}
},
},
},
})

export default removeStyle
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import emSize from './h2x/emSize'
import expandProps from './h2x/expandProps'
import replaceAttrValue from './h2x/replaceAttrValue'
import removeComments from './h2x/removeComments'
import removeStyle from './h2x/removeStyle'
import configToOptions from './configToOptions'

export {
Expand All @@ -21,6 +22,7 @@ export {
replaceAttrValue,
wrapIntoComponent,
removeComments,
removeStyle,
}

function expandState(state) {
Expand Down
30 changes: 30 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,34 @@ describe('convert', () => {

expect(result).toMatchSnapshot()
})

it('should remove style tags', async () => {
const result = await convert(
`
<?xml version="1.0" encoding="UTF-8"?>
<svg width="88px" height="88px" viewBox="0 0 88 88" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- Generator: Sketch 46.2 (44496) - http://www.bohemiancoding.com/sketch -->
<title>Dismiss</title>
<desc>Created with Sketch.</desc>
<defs></defs>
<style>
#Blocks {
fill: red;
}
</style>
<g id="Blocks" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" stroke-linecap="square">
<g id="Dismiss" stroke="#063855" stroke-width="2">
<path d="M51,37 L37,51" id="Shape"></path>
<path d="M51,51 L37,37" id="Shape"></path>
</g>
<style>
#Shape {}
</style>
</g>
</svg>
`,
)

expect(result).toMatchSnapshot()
})
})

0 comments on commit f10e703

Please sign in to comment.