-
-
Notifications
You must be signed in to change notification settings - Fork 414
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(titleProp): add titleProp option (#103)
Closes #88
- Loading branch information
1 parent
ab6e0b6
commit be39fe4
Showing
11 changed files
with
161 additions
and
1 deletion.
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
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
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
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,21 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`titleProp should add title element to svg 1`] = ` | ||
"<svg> | ||
<title> | ||
{title} | ||
</title> | ||
<path d=\\"M51,37 L37,51\\" id=\\"Shape\\" /> | ||
</svg> | ||
" | ||
`; | ||
|
||
exports[`titleProp should remove old title and add the new title 1`] = ` | ||
"<svg> | ||
<title> | ||
{title} | ||
</title> | ||
<path d=\\"M51,37 L37,51\\" id=\\"Shape\\" /> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { JSXElement, JSXText } from 'h2x-plugin-jsx' | ||
|
||
/** | ||
* Check if a classical title exist | ||
* @param {*} node - NodeElement | ||
* @returns {boolean} | ||
*/ | ||
const hasTitle = node => | ||
node.children.reduce((accumulation, value) => { | ||
if (value.name !== 'title') return accumulation | ||
if (value.children.some((e) => e.text === '{title}')) return accumulation | ||
return true | ||
}, false) | ||
|
||
const titleProp = () => ({ | ||
visitor: { | ||
JSXElement: { | ||
enter(path) { | ||
if (path.node.name === 'svg') { | ||
if (hasTitle(path.node)) { | ||
path.node.children = path.node.children.filter(element => { | ||
if (element.name !== 'title') return true | ||
if (element.children.some(e => e.text === '{title}')) return true | ||
return false | ||
}) | ||
} | ||
|
||
if (!path.node.children.some(children => children.name === 'title')) { | ||
const element = new JSXElement() | ||
const text = new JSXText() | ||
|
||
text.text = '{title}' | ||
|
||
element.name = 'title' | ||
element.children.push(text) | ||
|
||
path.node.children.unshift(element) | ||
} | ||
} | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
export default titleProp |
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,30 @@ | ||
import jsx from 'h2x-plugin-jsx' | ||
import h2x from '../plugins/h2x' | ||
import titleProp from './titleProp' | ||
|
||
describe('titleProp', () => { | ||
it('should add title element to svg', () => { | ||
const result = h2x( | ||
`<svg> | ||
<path d="M51,37 L37,51" id="Shape"></path> | ||
</svg> | ||
`, | ||
{ plugins: [jsx, titleProp] }, | ||
) | ||
|
||
expect(result).toMatchSnapshot() | ||
}) | ||
|
||
it('should remove old title and add the new title', () => { | ||
const result = h2x( | ||
`<svg> | ||
<title>Rectangle 5</title> | ||
<path d="M51,37 L37,51" id="Shape"></path> | ||
</svg> | ||
`, | ||
{ plugins: [jsx, titleProp] }, | ||
) | ||
|
||
expect(result).toMatchSnapshot() | ||
}) | ||
}) |
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