-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ππ Annotate JSX SVG namespace during build-time (#36722)
SVG Elements must be especially created with `createElementNS`. This transforms SVG-related JSX elements in order to always include an `xmlns` prop that's used to create the DOM nodes correctly. This prop is consumed by `core/dom/jsx`. We ignore files that do not import this module. Fixes #36713
- Loading branch information
1 parent
3126c9c
commit e70d7a6
Showing
14 changed files
with
233 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
113 changes: 113 additions & 0 deletions
113
build-system/babel-plugins/babel-plugin-dom-jsx-svg-namespace/index.js
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,113 @@ | ||
/** | ||
* @fileoverview | ||
* SVG Elements must be especially created with createElementNS. | ||
* | ||
* This transforms SVG-related JSX elements in order to always include an | ||
* 'xmlns' prop that's used to create the DOM nodes correctly. | ||
* | ||
* This prop is consumed by core/dom/jsx. We ignore files that do not import | ||
* this module. | ||
*/ | ||
|
||
const svgTags = new Set([ | ||
'animate', | ||
'circle', | ||
'clipPath', | ||
'defs', | ||
'desc', | ||
'ellipse', | ||
'feBlend', | ||
'feColorMatrix', | ||
'feComponentTransfer', | ||
'feComposite', | ||
'feConvolveMatrix', | ||
'feDiffuseLighting', | ||
'feDisplacementMap', | ||
'feDistantLight', | ||
'feFlood', | ||
'feFuncA', | ||
'feFuncB', | ||
'feFuncG', | ||
'feFuncR', | ||
'feGaussianBlur', | ||
'feImage', | ||
'feMerge', | ||
'feMergeNode', | ||
'feMorphology', | ||
'feOffset', | ||
'fePointLight', | ||
'feSpecularLighting', | ||
'feSpotLight', | ||
'feTile', | ||
'feTurbulence', | ||
'filter', | ||
'g', | ||
'image', | ||
'line', | ||
'linearGradient', | ||
'marker', | ||
'mask', | ||
'metadata', | ||
'path', | ||
'pattern', | ||
'polygon', | ||
'polyline', | ||
'radialGradient', | ||
'rect', | ||
'stop', | ||
'svg', | ||
'switch', | ||
'symbol', | ||
'text', | ||
'textPath', | ||
'tspan', | ||
'use', | ||
'view', | ||
]); | ||
|
||
module.exports = function (babel) { | ||
const {types: t} = babel; | ||
|
||
let isCoreDomJsx = false; | ||
return { | ||
name: 'dom-jsx-svg-namespace', | ||
visitor: { | ||
Program: { | ||
enter(path) { | ||
isCoreDomJsx = false; | ||
path.traverse({ | ||
ImportDeclaration(path) { | ||
if (path.node.source.value.endsWith('/core/dom/jsx')) { | ||
isCoreDomJsx = true; | ||
path.stop(); | ||
} | ||
}, | ||
}); | ||
}, | ||
}, | ||
JSXOpeningElement(path) { | ||
if (!isCoreDomJsx) { | ||
return; | ||
} | ||
const {name} = path.node.name; | ||
if (name === 'foreignObject') { | ||
throw path.buildCodeFrameError( | ||
'<foreignObject> is not supported. See src/core/dom/jsx.js' | ||
); | ||
} | ||
if (!svgTags.has(name)) { | ||
return; | ||
} | ||
if (path.node.attributes.find((attr) => attr.name.name === 'xmlns')) { | ||
return; | ||
} | ||
path.node.attributes.push( | ||
t.jsxAttribute( | ||
t.jsxIdentifier('xmlns'), | ||
t.stringLiteral('http://www.w3.org/2000/svg') | ||
) | ||
); | ||
}, | ||
}, | ||
}; | ||
}; |
11 changes: 11 additions & 0 deletions
11
...abel-plugins/babel-plugin-dom-jsx-svg-namespace/test/fixtures/transform/ignored/input.mjs
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,11 @@ | ||
// Import does not end with core/dom/jsx so we ignore this file. | ||
/** @jsx Preact.createElement */ | ||
import * as Preact from 'preact'; | ||
|
||
() => <svg />; | ||
() => <svg xmlns="http://www.w3.org/2000/svg" />; | ||
() => <path foo="bar" />; | ||
() => <circle foo="bar" />; | ||
|
||
() => <div />; | ||
() => <span class="whatever" />; |
6 changes: 6 additions & 0 deletions
6
...l-plugins/babel-plugin-dom-jsx-svg-namespace/test/fixtures/transform/ignored/options.json
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,6 @@ | ||
{ | ||
"plugins": [ | ||
"../../../..", | ||
"@babel/plugin-transform-react-jsx" | ||
] | ||
} |
24 changes: 24 additions & 0 deletions
24
...bel-plugins/babel-plugin-dom-jsx-svg-namespace/test/fixtures/transform/ignored/output.mjs
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,24 @@ | ||
// Import does not end with core/dom/jsx so we ignore this file. | ||
|
||
/** @jsx Preact.createElement */ | ||
import * as Preact from 'preact'; | ||
|
||
() => Preact.createElement("svg", null); | ||
|
||
() => Preact.createElement("svg", { | ||
xmlns: "http://www.w3.org/2000/svg" | ||
}); | ||
|
||
() => Preact.createElement("path", { | ||
foo: "bar" | ||
}); | ||
|
||
() => Preact.createElement("circle", { | ||
foo: "bar" | ||
}); | ||
|
||
() => Preact.createElement("div", null); | ||
|
||
() => Preact.createElement("span", { | ||
class: "whatever" | ||
}); |
10 changes: 10 additions & 0 deletions
10
...el-plugins/babel-plugin-dom-jsx-svg-namespace/test/fixtures/transform/sets-prop/input.mjs
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,10 @@ | ||
/** @jsx jsx.createElement */ | ||
import * as jsx from '../ANYWHERE_THAT_LEADS_TO/core/dom/jsx'; | ||
|
||
() => <svg />; | ||
() => <svg xmlns="http://www.w3.org/2000/svg" />; | ||
() => <path foo="bar" />; | ||
() => <circle foo="bar" />; | ||
|
||
() => <div />; | ||
() => <span class="whatever" />; |
6 changes: 6 additions & 0 deletions
6
...plugins/babel-plugin-dom-jsx-svg-namespace/test/fixtures/transform/sets-prop/options.json
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,6 @@ | ||
{ | ||
"plugins": [ | ||
"../../../..", | ||
"@babel/plugin-transform-react-jsx" | ||
] | ||
} |
26 changes: 26 additions & 0 deletions
26
...l-plugins/babel-plugin-dom-jsx-svg-namespace/test/fixtures/transform/sets-prop/output.mjs
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,26 @@ | ||
/** @jsx jsx.createElement */ | ||
import * as jsx from '../ANYWHERE_THAT_LEADS_TO/core/dom/jsx'; | ||
|
||
() => jsx.createElement("svg", { | ||
xmlns: "http://www.w3.org/2000/svg" | ||
}); | ||
|
||
() => jsx.createElement("svg", { | ||
xmlns: "http://www.w3.org/2000/svg" | ||
}); | ||
|
||
() => jsx.createElement("path", { | ||
foo: "bar", | ||
xmlns: "http://www.w3.org/2000/svg" | ||
}); | ||
|
||
() => jsx.createElement("circle", { | ||
foo: "bar", | ||
xmlns: "http://www.w3.org/2000/svg" | ||
}); | ||
|
||
() => jsx.createElement("div", null); | ||
|
||
() => jsx.createElement("span", { | ||
class: "whatever" | ||
}); |
3 changes: 3 additions & 0 deletions
3
build-system/babel-plugins/babel-plugin-dom-jsx-svg-namespace/test/index.js
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,3 @@ | ||
const runner = require('@babel/helper-plugin-test-runner').default; | ||
|
||
runner(__dirname); |
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