Skip to content

Warn about HTML within SVG tags#13516

Closed
alex-saunders wants to merge 5 commits intofacebook:masterfrom
alex-saunders:no-html-in-svg
Closed

Warn about HTML within SVG tags#13516
alex-saunders wants to merge 5 commits intofacebook:masterfrom
alex-saunders:no-html-in-svg

Conversation

@alex-saunders
Copy link
Copy Markdown

This PR aims to warn about nesting non-SVG HTML tags within SVG (similar to how developers are warned against nesting <p> tags within <p> tags).

Example:

<svg>
  <path>
    <div>
      I'm a nested div!
    </div>
  </path>
</svg>

Produces the following warning:
screen shot 2018-08-30 at 09 57 07

However, including the <div> within a <foreignObject>

<svg>
  <path>
    <foreignObject>
      <div>
        I'm a nested div!
      </div>
    </foreignObject>
  </path>
</svg>

Produces no errors message.

Ideally, I think the warning should include some messaging about wrapping HTML inside a foreignObject, i.e. '<div> cannot appear as a descendant of <svg>, consider wrapping it inside a <foreignObject>', but I wasn't 100% sure on how to implement this with how validateDOMNesting.js currently works.

closes #11013

@pull-bot
Copy link
Copy Markdown

pull-bot commented Aug 30, 2018

Details of bundled changes.

Comparing: 1664b08...91ac38d

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.development.js +0.1% 0.0% 650.21 KB 650.66 KB 152.73 KB 152.79 KB UMD_DEV
react-dom.development.js +0.1% 0.0% 646.34 KB 646.8 KB 151.62 KB 151.69 KB NODE_DEV
ReactDOM-dev.js +0.1% +0.1% 653.44 KB 653.97 KB 149.62 KB 149.7 KB FB_WWW_DEV

Generated by 🚫 dangerJS

case 'h5':
case 'h6':
return ancestorInfo.pTagInButtonScope;
return (
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

These checks all look very similar, is there a refactoring you can do to make the logic clearer?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I've tried putting it into a variable to avoid the repeated code

return ancestorInfo.pTagInButtonScope || isValidHtmlInSvg;

case 'span':
return !ancestorInfo.foreignObjectInScope
Copy link
Copy Markdown
Contributor

@Simek Simek Sep 15, 2018

Choose a reason for hiding this comment

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

One more code piece (span case) which could be replaced with isValidHtmlInSvg.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Nice spot, cheers @Simek , added that now

@alex-saunders
Copy link
Copy Markdown
Author

CI is saying that this PR is failing tests but they all complete successfully on my machine, any ideas as to why this might be happening?

@sizebot
Copy link
Copy Markdown

sizebot commented Oct 17, 2019

Size changes (experimental)

Details of bundled changes.

Comparing: 4eeee35...e302cfa

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.profiling.min.js 0.0% -0.0% 122.6 KB 122.6 KB 38.58 KB 38.58 KB NODE_PROFILING
react-dom-server.browser.development.js 0.0% 0.0% 140.03 KB 140.03 KB 36.78 KB 36.79 KB UMD_DEV
ReactDOM-dev.js 0.0% 0.0% 968.79 KB 969.18 KB 215.32 KB 215.39 KB FB_WWW_DEV
react-dom-test-utils.production.min.js 0.0% -0.1% 11.18 KB 11.18 KB 4.15 KB 4.15 KB UMD_PROD
react-dom-unstable-fizz.browser.production.min.js 0.0% -0.1% 1.21 KB 1.21 KB 708 B 707 B UMD_PROD
react-dom-unstable-fizz.browser.development.js 0.0% +0.1% 3.62 KB 3.62 KB 1.49 KB 1.49 KB NODE_DEV
react-dom-test-utils.production.min.js 0.0% -0.1% 10.95 KB 10.95 KB 4.09 KB 4.09 KB NODE_PROD
react-dom-unstable-fizz.browser.production.min.js 0.0% -0.3% 1.05 KB 1.05 KB 640 B 638 B NODE_PROD
react-dom.development.js 0.0% 0.0% 946.7 KB 947.06 KB 214.48 KB 214.54 KB UMD_DEV
react-dom.production.min.js 0.0% -0.0% 118.7 KB 118.7 KB 38.12 KB 38.12 KB UMD_PROD
react-dom.profiling.min.js 0.0% -0.0% 122.33 KB 122.33 KB 39.23 KB 39.23 KB UMD_PROFILING
react-dom.development.js 0.0% 0.0% 940.77 KB 941.12 KB 212.89 KB 212.95 KB NODE_DEV
react-dom-server.node.production.min.js 0.0% -0.0% 20.62 KB 20.62 KB 7.58 KB 7.58 KB NODE_PROD
react-dom-unstable-native-dependencies.development.js 0.0% -0.0% 60.14 KB 60.14 KB 15.79 KB 15.79 KB UMD_DEV
react-dom-unstable-native-dependencies.production.min.js 0.0% -0.0% 10.73 KB 10.73 KB 3.67 KB 3.67 KB UMD_PROD
ReactDOMServer-prod.js 0.0% 0.0% 48.51 KB 48.51 KB 11.08 KB 11.08 KB FB_WWW_PROD
react-dom-unstable-native-dependencies.development.js 0.0% -0.0% 59.82 KB 59.82 KB 15.66 KB 15.66 KB NODE_DEV
react-dom-unstable-fizz.node.development.js 0.0% -0.1% 3.88 KB 3.88 KB 1.51 KB 1.51 KB NODE_DEV
react-dom-unstable-fizz.node.production.min.js 0.0% -0.1% 1.12 KB 1.12 KB 674 B 673 B NODE_PROD

Generated by 🚫 dangerJS against e302cfa

@sizebot
Copy link
Copy Markdown

sizebot commented Oct 17, 2019

Size changes (stable)

Details of bundled changes.

Comparing: 4eeee35...e302cfa

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.profiling.min.js 0.0% -0.0% 119.05 KB 119.05 KB 37.61 KB 37.61 KB NODE_PROFILING
react-dom-server.browser.development.js 0.0% 0.0% 140 KB 140 KB 36.78 KB 36.78 KB UMD_DEV
ReactDOM-dev.js 0.0% 0.0% 968.78 KB 969.17 KB 215.32 KB 215.38 KB FB_WWW_DEV
react-dom-test-utils.production.min.js 0.0% -0.1% 11.17 KB 11.17 KB 4.14 KB 4.14 KB UMD_PROD
react-dom-unstable-fizz.browser.production.min.js 0.0% -0.1% 1.2 KB 1.2 KB 700 B 699 B UMD_PROD
react-dom-unstable-fizz.browser.development.js 0.0% +0.1% 3.61 KB 3.61 KB 1.48 KB 1.48 KB NODE_DEV
react-dom-test-utils.production.min.js 0.0% -0.1% 10.94 KB 10.94 KB 4.08 KB 4.08 KB NODE_PROD
react-dom-unstable-fizz.browser.production.min.js 0.0% -0.3% 1.04 KB 1.04 KB 632 B 630 B NODE_PROD
react-dom.development.js 0.0% 0.0% 946.68 KB 947.03 KB 214.46 KB 214.52 KB UMD_DEV
react-dom.production.min.js 0.0% -0.0% 115.29 KB 115.29 KB 37.17 KB 37.17 KB UMD_PROD
react-dom.profiling.min.js 0.0% -0.0% 118.82 KB 118.82 KB 38.25 KB 38.25 KB UMD_PROFILING
react-dom.development.js 0.0% 0.0% 940.74 KB 941.1 KB 212.87 KB 212.93 KB NODE_DEV
react-dom-server.node.production.min.js 0.0% -0.0% 20.17 KB 20.17 KB 7.5 KB 7.5 KB NODE_PROD
ReactDOM-prod.js 0.0% -0.0% 396.66 KB 396.66 KB 72.19 KB 72.19 KB FB_WWW_PROD
ReactDOM-profiling.js 0.0% -0.0% 397.4 KB 397.4 KB 72.7 KB 72.7 KB FB_WWW_PROFILING
react-dom-unstable-native-dependencies.development.js 0.0% -0.0% 60.13 KB 60.13 KB 15.79 KB 15.79 KB UMD_DEV
ReactDOMServer-prod.js 0.0% 0.0% 48.49 KB 48.49 KB 11.07 KB 11.07 KB FB_WWW_PROD
react-dom-unstable-native-dependencies.development.js 0.0% -0.0% 59.8 KB 59.8 KB 15.65 KB 15.65 KB NODE_DEV
react-dom-unstable-native-dependencies.production.min.js 0.0% -0.0% 10.46 KB 10.46 KB 3.56 KB 3.56 KB NODE_PROD
react-dom-unstable-fizz.node.production.min.js 0.0% -0.2% 1.1 KB 1.1 KB 666 B 665 B NODE_PROD

Generated by 🚫 dangerJS against e302cfa

@alex-saunders
Copy link
Copy Markdown
Author

@gaearon I've revisited this PR & rebased to help all checks pass, do you think this is something we could look at again for merging?

@alex-saunders alex-saunders requested a review from gaearon October 17, 2019 23:59
@stale
Copy link
Copy Markdown

stale bot commented Jan 16, 2020

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution.

@stale stale bot added the Resolution: Stale Automatically closed due to inactivity label Jan 16, 2020
@stale
Copy link
Copy Markdown

stale bot commented Jan 24, 2020

Closing this pull request after a prolonged period of inactivity. If this issue is still present in the latest release, please ask for this pull request to be reopened. Thank you!

@stale stale bot closed this Jan 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Resolution: Stale Automatically closed due to inactivity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Warn about unexpected HTML inside SVG

7 participants