Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace the implementation of escapeTextContentForBrowser with escape-html #6862

Merged
merged 4 commits into from
Jun 2, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ var paths = {
};

var moduleMap = Object.assign(
{'object-assign': 'object-assign'},
{
'escape-html': 'escape-html',
'object-assign': 'object-assign',
},
require('fbjs/module-map'),
{
deepDiffer: 'react-native/lib/deepDiffer',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"coveralls": "^2.11.6",
"del": "^2.0.2",
"derequire": "^2.0.3",
"escape-html": "1.0.3",
"eslint": "1.10.3",
"eslint-plugin-react": "4.1.0",
"eslint-plugin-react-internal": "file:eslint-rules",
Expand Down
1 change: 1 addition & 0 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"node": ">=0.10.0"
},
"dependencies": {
"escape-html": "1.0.3",
"fbjs": "^0.8.1",
"loose-envify": "^1.1.0",
"object-assign": "^4.1.0"
Expand Down
1 change: 1 addition & 0 deletions scripts/jest/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var babelOptions = {
{},
moduleMap,
{
'escape-html': 'escape-html',
'object-assign': 'object-assign',
}
),
Expand Down
4 changes: 2 additions & 2 deletions src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1024,8 +1024,8 @@ describe('ReactDOMComponent', function() {
}, '\'"<>&')
)
).toBe(
'<div title="&#x27;&quot;&lt;&gt;&amp;" style="text-align:&#x27;&quot;&lt;&gt;&amp;;">' +
'&#x27;&quot;&lt;&gt;&amp;' +
'<div title="&#39;&quot;&lt;&gt;&amp;" style="text-align:&#39;&quot;&lt;&gt;&amp;;">' +
'&#39;&quot;&lt;&gt;&amp;' +
Copy link
Contributor Author

Choose a reason for hiding this comment

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

escape-html uses &#39 instead of &#x27, which should be a completely equivalent HTML entity for single quote (and it's one byte less, too).

'</div>'
);
});
Expand Down
25 changes: 11 additions & 14 deletions src/renderers/dom/shared/escapeTextContentForBrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,7 @@

'use strict';

var ESCAPE_LOOKUP = {
'&': '&amp;',
'>': '&gt;',
'<': '&lt;',
'"': '&quot;',
'\'': '&#x27;',
};

var ESCAPE_REGEX = /[&><"']/g;

function escaper(match) {
return ESCAPE_LOOKUP[match];
}
var escapeHtml = require('escape-html');

/**
* Escapes text to prevent scripting attacks.
Expand All @@ -32,7 +20,16 @@ function escaper(match) {
* @return {string} An escaped string.
*/
function escapeTextContentForBrowser(text) {
return ('' + text).replace(ESCAPE_REGEX, escaper);
switch (typeof text) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually, can we inline if (typeof text === 'boolean' || typeof text === 'number')? V8, at least, inlines typeof foo === <string> in the bytecode.

case 'boolean':
case 'number':
// this shortcircuit helps perf for types that we know will never have
// special characters, especially given that this function is used often
// for numeric dom ids.
return '' + text;
default:
return escapeHtml(text);
}
}

module.exports = escapeTextContentForBrowser;