Skip to content

Commit

Permalink
Merge pull request facebook#23 from RSO/fix-lt-gt-entities
Browse files Browse the repository at this point in the history
Make sure less-than and great-than characters are properly encoded.
  • Loading branch information
cpojer committed Jan 12, 2016
2 parents d3abe36 + 1cb1027 commit 166713e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions test/__tests__/create-element-to-jsx-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ describe('create-element-to-jsx', () => {
test('create-element-to-jsx', 'create-element-to-jsx-call-expression-as-prop');

test('create-element-to-jsx', 'create-element-to-jsx-allow-member-expression');

test('create-element-to-jsx', 'create-element-to-jsx-gt-lt-entities');
});

it('raises when it does not recognize a property type', () => {
Expand Down
3 changes: 3 additions & 0 deletions test/create-element-to-jsx-gt-lt-entities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var React = require('React');

React.createElement('div', null, '\x3C\x3E');
3 changes: 3 additions & 0 deletions test/create-element-to-jsx-gt-lt-entities.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var React = require('React');

<div>&lt;&gt;</div>;
6 changes: 5 additions & 1 deletion transforms/create-element-to-jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ module.exports = function(file, api, options) {
const j = api.jscodeshift;
const root = j(file.source);
const ReactUtils = require('./utils/ReactUtils')(j);
const encodeJSXTextValue = value =>
value
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');

const convertExpressionToJSXAttributes = (expression) => {
const isReactSpread = expression.type === 'CallExpression' &&
Expand Down Expand Up @@ -89,7 +93,7 @@ module.exports = function(file, api, options) {

const children = node.value.arguments.slice(2).map((child, index) => {
if (child.type === 'Literal' && typeof child.value === 'string') {
return j.jsxText(child.value);
return j.jsxText(encodeJSXTextValue(child.value));
} else if (child.type === 'CallExpression' &&
child.callee.object &&
child.callee.object.name === 'React' &&
Expand Down

0 comments on commit 166713e

Please sign in to comment.