-
-
Notifications
You must be signed in to change notification settings - Fork 425
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(hast-util-to-babel-ast): correctly transforms data & aria attributes
Closes #221
- Loading branch information
Showing
6 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/hast-util-to-babel-ast/src/__snapshots__/index.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`hast-util-to-babel-ast should correctly transform svg 1`] = ` | ||
"<svg width=\\"88px\\" height=\\"88px\\" viewBox=\\"0 0 88 88\\" version={1.1} xmlns=\\"http://www.w3.org/2000/svg\\" xmlnsXlink=\\"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 /><g id=\\"Blocks\\" stroke=\\"none\\" strokeWidth={1} fill=\\"none\\" fillRule=\\"evenodd\\" strokeLinecap=\\"square\\"><g id=\\"Dismiss\\" stroke=\\"#063855\\" strokeWidth={2}><path d=\\"M51,37 L37,51\\" id=\\"Shape\\" /><path d=\\"M51,51 L37,37\\" id=\\"Shape\\" /></g></g></svg>;" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import unified from 'unified' | ||
import parse from 'rehype-parse' | ||
import vfile from 'vfile' | ||
import generate from '@babel/generator' | ||
import hastToBabelAst from './index' | ||
|
||
function transform(code) { | ||
const hastTree = unified() | ||
.use(parse, { | ||
fragment: true, | ||
space: 'svg', | ||
emitParseErrors: true, | ||
duplicateAttribute: false, | ||
}) | ||
.parse(vfile({ path: 'test.svg', contents: code })) | ||
|
||
const babelTree = hastToBabelAst(hastTree) | ||
|
||
const { code: generatedCode } = generate(babelTree) | ||
|
||
return generatedCode | ||
} | ||
|
||
describe('hast-util-to-babel-ast', () => { | ||
it('should correctly transform svg', () => { | ||
const code = ` | ||
<?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> | ||
<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> | ||
</g> | ||
</svg> | ||
` | ||
expect(transform(code)).toMatchSnapshot() | ||
}) | ||
|
||
it('should correctly transform aria-x', () => { | ||
const code = `<svg aria-hidden="true"></svg>` | ||
expect(transform(code)).toMatchInlineSnapshot( | ||
`"<svg aria-hidden=\\"true\\" />;"`, | ||
) | ||
}) | ||
|
||
it('should correctly transform data-x', () => { | ||
const code = `<svg data-hidden="true"></svg>` | ||
expect(transform(code)).toMatchInlineSnapshot( | ||
`"<svg data-hidden=\\"true\\" />;"`, | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters