Skip to content

Commit 88b24c5

Browse files
sudkumargregberge
authored andcommitted
fix(babel-plugin-svg-dynamic-title): dont render empty title (#341)
Currently when no title is passed and titleProp is set to `true`, we are rendering an empty title element which is causing some issues. So we have added a conditional statement for rendering the title element only if the title props is not falsy. If we have an existing title, that will rendered by default. Passing the `null` values as the title will cause also cause the title element for not render even if a default title exists on the svg Fixes #333
1 parent 4b4bd2c commit 88b24c5

File tree

5 files changed

+40
-21
lines changed

5 files changed

+40
-21
lines changed

packages/babel-plugin-svg-dynamic-title/src/index.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,31 @@ const plugin = ({ types: t }) => ({
1111
return
1212
}
1313

14-
function createTitle(children = []) {
14+
function createTitle(children = [], attributes = []) {
1515
return t.jsxElement(
16-
t.jsxOpeningElement(t.jsxIdentifier('title'), []),
16+
t.jsxOpeningElement(t.jsxIdentifier('title'), attributes),
1717
t.jsxClosingElement(t.jsxIdentifier('title')),
1818
children,
1919
)
2020
}
21-
function getTitleElement(existingTitleChildren = []) {
21+
function getTitleElement(existingTitle) {
2222
const titleExpression = t.identifier('title')
23-
let titleElement = createTitle([
24-
t.jsxExpressionContainer(titleExpression),
25-
])
26-
if (existingTitleChildren && existingTitleChildren.length) {
23+
let titleElement = t.conditionalExpression(
24+
titleExpression,
25+
createTitle(
26+
[t.jsxExpressionContainer(titleExpression)],
27+
existingTitle ? existingTitle.openingElement.attributes : [],
28+
),
29+
t.nullLiteral(),
30+
)
31+
if (
32+
existingTitle &&
33+
existingTitle.children &&
34+
existingTitle.children.length
35+
) {
2736
// if title already exists
2837
// render as follows
29-
const fallbackTitleElement = createTitle(existingTitleChildren)
38+
const fallbackTitleElement = existingTitle
3039
// {title === undefined ? fallbackTitleElement : titleElement}
3140
const conditionalExpressionForTitle = t.conditionalExpression(
3241
t.binaryExpression(
@@ -38,6 +47,8 @@ const plugin = ({ types: t }) => ({
3847
titleElement,
3948
)
4049
titleElement = t.jsxExpressionContainer(conditionalExpressionForTitle)
50+
} else {
51+
titleElement = t.jsxExpressionContainer(titleElement)
4152
}
4253
return titleElement
4354
}
@@ -49,7 +60,7 @@ const plugin = ({ types: t }) => ({
4960
if (!childPath.isJSXElement()) return false
5061
if (childPath.node === titleElement) return false
5162
if (childPath.node.openingElement.name.name !== 'title') return false
52-
titleElement = getTitleElement(childPath.node.children)
63+
titleElement = getTitleElement(childPath.node)
5364
childPath.replaceWith(titleElement)
5465
return true
5566
})

packages/babel-plugin-svg-dynamic-title/src/index.test.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,44 @@ const testPlugin = (code, options) => {
1313
describe('plugin', () => {
1414
it('should add title attribute if not present', () => {
1515
expect(testPlugin('<svg></svg>')).toMatchInlineSnapshot(
16-
`"<svg><title>{title}</title></svg>;"`,
16+
`"<svg>{title ? <title>{title}</title> : null}</svg>;"`,
1717
)
1818
})
1919

2020
it('should add title element and fallback to existing title', () => {
2121
// testing when the existing title contains a simple string
2222
expect(testPlugin(`<svg><title>Hello</title></svg>`)).toMatchInlineSnapshot(
23-
`"<svg>{title === undefined ? <title>Hello</title> : <title>{title}</title>}</svg>;"`,
23+
`"<svg>{title === undefined ? <title>Hello</title> : title ? <title>{title}</title> : null}</svg>;"`,
2424
)
2525
// testing when the existing title contains an JSXExpression
2626
expect(
2727
testPlugin(`<svg><title>{"Hello"}</title></svg>`),
2828
).toMatchInlineSnapshot(
29-
`"<svg>{title === undefined ? <title>{\\"Hello\\"}</title> : <title>{title}</title>}</svg>;"`,
29+
`"<svg>{title === undefined ? <title>{\\"Hello\\"}</title> : title ? <title>{title}</title> : null}</svg>;"`,
30+
)
31+
})
32+
it('should preserve any existing title attributes', () => {
33+
// testing when the existing title contains a simple string
34+
expect(
35+
testPlugin(`<svg><title attr='a'>Hello</title></svg>`),
36+
).toMatchInlineSnapshot(
37+
`"<svg>{title === undefined ? <title attr='a'>Hello</title> : title ? <title attr='a'>{title}</title> : null}</svg>;"`,
3038
)
3139
})
3240
it('should support empty title', () => {
3341
expect(testPlugin('<svg><title></title></svg>')).toMatchInlineSnapshot(
34-
`"<svg><title>{title}</title></svg>;"`,
42+
`"<svg>{title ? <title>{title}</title> : null}</svg>;"`,
3543
)
3644
})
3745
it('should support self closing title', () => {
3846
expect(testPlugin('<svg><title /></svg>')).toMatchInlineSnapshot(
39-
`"<svg><title>{title}</title></svg>;"`,
47+
`"<svg>{title ? <title>{title}</title> : null}</svg>;"`,
4048
)
4149
})
4250

4351
it('should work if an attribute is already present', () => {
4452
expect(testPlugin('<svg><foo /></svg>')).toMatchInlineSnapshot(
45-
`"<svg><title>{title}</title><foo /></svg>;"`,
53+
`"<svg>{title ? <title>{title}</title> : null}<foo /></svg>;"`,
4654
)
4755
})
4856
})

packages/babel-preset/src/index.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe('preset', () => {
6363
6464
const SvgComponent = ({
6565
title
66-
}) => <svg><title>{title}</title></svg>;
66+
}) => <svg>{title ? <title>{title}</title> : null}</svg>;
6767
6868
export default SvgComponent;"
6969
`)
@@ -82,7 +82,7 @@ describe('preset', () => {
8282
8383
const SvgComponent = ({
8484
title
85-
}) => <svg>{title === undefined ? <title>Hello</title> : <title>{title}</title>}</svg>;
85+
}) => <svg>{title === undefined ? <title>Hello</title> : title ? <title>{title}</title> : null}</svg>;
8686
8787
export default SvgComponent;"
8888
`)
@@ -99,7 +99,7 @@ describe('preset', () => {
9999
100100
const SvgComponent = ({
101101
title
102-
}) => <svg>{title === undefined ? <title>{\\"Hello\\"}</title> : <title>{title}</title>}</svg>;
102+
}) => <svg>{title === undefined ? <title>{\\"Hello\\"}</title> : title ? <title>{title}</title> : null}</svg>;
103103
104104
export default SvgComponent;"
105105
`)

packages/cli/src/__snapshots__/index.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ exports[`cli should support various args: --title-prop 1`] = `
320320
321321
const SvgFile = ({ title, ...props }) => (
322322
<svg width={48} height={1} {...props}>
323-
<title>{title}</title>
323+
{title ? <title>{title}</title> : null}
324324
<path d=\\"M0 0h48v1H0z\\" fill=\\"#063855\\" fillRule=\\"evenodd\\" />
325325
</svg>
326326
)

packages/core/src/__snapshots__/convert.test.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ exports[`convert config should support options { titleProp: true } 1`] = `
332332
333333
const SvgComponent = ({ title, ...props }) => (
334334
<svg width={88} height={88} {...props}>
335-
<title>{title}</title>
335+
{title ? <title>{title}</title> : null}
336336
<g
337337
stroke=\\"#063855\\"
338338
strokeWidth={2}
@@ -361,7 +361,7 @@ const SvgComponent = ({ title, ...props }) => (
361361
}}
362362
{...props}
363363
>
364-
<title>{title}</title>
364+
{title ? <title>{title}</title> : null}
365365
<path d=\\"M0 0h24v24H0z\\" fill=\\"none\\" />
366366
</svg>
367367
)

0 commit comments

Comments
 (0)