Skip to content

Handle the existing title case by using element instead of value (children)#315

Merged
gregberge merged 5 commits into
gregberge:masterfrom
sudkumar:fix/svg_title
Jun 10, 2019
Merged

Handle the existing title case by using element instead of value (children)#315
gregberge merged 5 commits into
gregberge:masterfrom
sudkumar:fix/svg_title

Conversation

@sudkumar

Copy link
Copy Markdown
Contributor

Summary

Fixes #314 titleProps=true not working as expected for existing title

We need to handle existing title element's children with a different strategy. Instead of looping through children and creating a children, we can instead, use all the children and push them into a title element. And from the conditional statement for fallback title, instead of returning a title children, we return the title.

- {title === undefined ? "Existing_Children" : title}
+ {title === undefined ? <title>{Existing_Children}</title> : <title>{title}</title>}

Test plan

I have updated the test case for the same with different type of existing title elements.

// pass
expect(testPlugin(`<svg><title>Hi</title></svg`)). toMatchInlineSnapshot(
`<svg>{title === undefined ? <title>Hi</title> : <title>{title}</title>}</svg>`
)
// pass
expect(testPlugin(`<svg><title>{"Hi"}</title></svg`)). toMatchInlineSnapshot(
`<svg>{title === undefined ? <title>{"Hi"}</title> : <title>{title}</title>}</svg>`
)

sudkumar added 4 commits May 24, 2019 17:47
… is not provided and titleProps is set to true
children

Instead of return the children of title from conditional expression, we
can return the title element itself e.g.
if title is undefined then retun the existing title else return
<title>{title}</title>

fixes #314
@vercel

vercel Bot commented May 29, 2019

Copy link
Copy Markdown

This pull request is automatically deployed with Now.
To access deployments, click Details below or on the icon next to each push.

@gregberge gregberge left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update, can you avoid using forEach in tests, it is kind of messy. I would rather prefer just isolated if with duplication. Is it OK for you?

@sudkumar

sudkumar commented Jun 5, 2019

Copy link
Copy Markdown
Contributor Author

Yes. I realised the same. Test should be testable while reading :). Will update them.

@sudkumar

sudkumar commented Jun 5, 2019

Copy link
Copy Markdown
Contributor Author

@neoziro I updated the tests.

There is one more thing I want to do here. I want to preserve the attributes of the existing title.

I just need to update the createTitle function to accept the attributes and we should be good to go.

- function createTitle (children = []) {
+ function createTitle (children = [], attributes = [])
     return t.jsxElement(
-       t.jsxOpeningElement(t.jsxIdentifier('title')),
+       t.jsxOpeningElement(t.jsxIdentifier('title'), attributes),
        t.jsxClosingElement(t.jsxIdentifier('title')),
        children,
     )
}

And in the getTitleElement

- function getTitleElement(existingTitleChlidren) {
+ function getTitleElement(existingTitle) {
        const titleExpression = t.identifier('title')
        let titleElement = createTitle(
          [t.jsxExpressionContainer(titleExpression)],
+         existingTitle ? existingTitle.openingElement.attributes : [],
        )
-      if (existingTitleChildren && existingTitleChildren.length)
+      if (existingTitle && existingTitle.children &&  existingTitle.children.length) {

-        const fallbackTitleElement = createTitle(existingTitle.children)
+        const fallbackTitleElement = existingTitle

And in our loop over path.get('children'), we can

-       titleElement = getTitleElement(childPath.node.children)
+       titleElement = getTitleElement(childPath.node)

@sudkumar sudkumar marked this pull request as ready for review June 5, 2019 11:23
@codecov

codecov Bot commented Jun 5, 2019

Copy link
Copy Markdown

Codecov Report

Merging #315 into master will increase coverage by 0.05%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #315      +/-   ##
==========================================
+ Coverage   86.58%   86.63%   +0.05%     
==========================================
  Files          30       30              
  Lines         477      479       +2     
  Branches      135      134       -1     
==========================================
+ Hits          413      415       +2     
  Misses         53       53              
  Partials       11       11
Impacted Files Coverage Δ
...ckages/babel-plugin-svg-dynamic-title/src/index.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 79069ed...7172429. Read the comment docs.

1 similar comment
@codecov

codecov Bot commented Jun 5, 2019

Copy link
Copy Markdown

Codecov Report

Merging #315 into master will increase coverage by 0.05%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #315      +/-   ##
==========================================
+ Coverage   86.58%   86.63%   +0.05%     
==========================================
  Files          30       30              
  Lines         477      479       +2     
  Branches      135      134       -1     
==========================================
+ Hits          413      415       +2     
  Misses         53       53              
  Partials       11       11
Impacted Files Coverage Δ
...ckages/babel-plugin-svg-dynamic-title/src/index.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 79069ed...7172429. Read the comment docs.

@gregberge

Copy link
Copy Markdown
Owner

It looks good to me. I merge it.

@gregberge gregberge merged commit 065e7a9 into gregberge:master Jun 10, 2019
@sudkumar

Copy link
Copy Markdown
Contributor Author

Hi @neoziro . Any updates on release that includes this PR ?

@gregberge

Copy link
Copy Markdown
Owner

Sorry I have just published it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

titleProp with existing title not working properly

2 participants