Hi! JSX attribute parsing does not produce the expected result to me and is escaped too early (before being handled aas a regular var by react). Attributes like title or value are not properly rendered as a result.
Input Code
var src = "<div title='café'></div>";
var result = require("babel-core").transform(src, {
plugins: ["syntax-jsx"]
// or: plugins: ["transform-react-jsx"]
})
console.log(result.code)
Expected Behavior
<div title="café"></div>;
Current Behavior
<div title="caf\xE9"></div>;
Details
Relevant and stripped down AST produced by babylon:
"openingElement": {
"type": "JSXOpeningElement",
"attributes": [
{ "type": "JSXAttribute"
"name": {
"type": "JSXIdentifier",
"name": "title"
},
"value": {
"type": "StringLiteral",
"extra": null,
"value": "café"
}
}]
}
The value being a StringLiteral with no extra, it is handled in babel-generator (lib/generators/types.js) as a StringLiteral and fully escaped in jsesc (a regular var assignment - eg: var x = 'café' - has a node.extra element : {value: .., rawValue: ...} that prevents it from being escaped inside jsesc).
I'm not sure what the fix is but I think we should treat JSX attributes as regular assignment.
A simple fix would be to remove the "node.extra = null;" in jsxParseAttributeValue (it does work, verified).
But I guess it is here for a reason?
I can make a pull request if you see the proper way to fix it.
Thank you!
Your Environment
| software |
version |
| Babylon |
6.14.1 (but tested w/ the most recent one) |