Skip to content

Commit

Permalink
Merge pull request #92 from DirectorRen-TV/main
Browse files Browse the repository at this point in the history
preventing vue's compileTemplate from removing style tag
  • Loading branch information
gkatsanos committed May 8, 2023
2 parents 43e1d25 + 6a416f2 commit 8a2e17d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cypress/integration/spec.js
Expand Up @@ -36,6 +36,10 @@ describe('Vite SVG Loader', () => {
expect($svg[0].width.baseVal.value).to.equal(400)
})
})
it('keeps style tag in components', () => {
cy.get('#style-tag svg style')
.should('exist')
})

it('supports ?url param', () => {
cy.get('#url').contains(/^\/assets\/test\..+\.svg/)
Expand Down
6 changes: 6 additions & 0 deletions examples/vue/src/App.vue
Expand Up @@ -4,6 +4,8 @@ import { defineAsyncComponent } from 'vue'
import HelloWorld from './components/HelloWorld.vue'
import Test from './assets/test.svg'
import TestSkipSvgo from './assets/test.svg?skipsvgo'
//skipsvgo because svgo with some params will inline styles
import StyleTagTest from './assets/style.svg?skipsvgo'
import testUrl from './assets/test.svg?url'
import testRaw from './assets/test.svg?raw'
Expand All @@ -28,6 +30,10 @@ const Async = defineAsyncComponent(() => import(`./assets/${name}.svg`))
<TestSkipSvgo />
</div>

<div id="style-tag">
<StyleTagTest />
</div>

<div id="url">
{{ testUrl }}
</div>
Expand Down
16 changes: 16 additions & 0 deletions examples/vue/src/assets/style.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions index.js
Expand Up @@ -44,6 +44,9 @@ module.exports = function svgLoader (options = {}) {
}).data
}

// To prevent compileTemplate from removing the style tag
svg = svg.replace(/<style/g, '<component is="style"').replace(/<\/style/g, '</component')

const { code } = compileTemplate({
id: JSON.stringify(id),
source: svg,
Expand Down

0 comments on commit 8a2e17d

Please sign in to comment.