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

Fix ampersand in jsx href and src #1056

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/printer.js
Expand Up @@ -1200,7 +1200,10 @@ function genericPrintNoParens(path, options, print) {
(n.value.type === "StringLiteral" || n.value.type === "Literal") &&
typeof n.value.value === "string"
) {
res = '"' + util.htmlEscapeInsideDoubleQuote(n.value.value) + '"';
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you delete that function from util since it's no longer being used?

const value = n.value.extra ? n.value.extra.raw : n.value.raw;
res = '"' +
value.slice(1, value.length - 1).replace(/"/g, """) +
Copy link
Contributor

Choose a reason for hiding this comment

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

fyi, you can do .slice(1, -1)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, yeah you're right!

'"';
} else {
res = path.call(print, "value");
}
Expand Down
11 changes: 0 additions & 11 deletions src/util.js
Expand Up @@ -265,16 +265,6 @@ function setLocEnd(node, index) {
}
}

// http://stackoverflow.com/a/7124052
function htmlEscapeInsideDoubleQuote(str) {
return str.replace(/&/g, "&").replace(/"/g, """);
// Intentionally disable the following since it is safe inside of a
// double quote context
// .replace(/'/g, ''')
// .replace(/</g, '&lt;')
// .replace(/>/g, '&gt;');
}

// http://stackoverflow.com/a/7124052
function htmlEscapeInsideAngleBracket(str) {
return str.replace(/</g, "&lt;").replace(/>/g, "&gt;");
Expand Down Expand Up @@ -328,6 +318,5 @@ module.exports = {
locEnd,
setLocStart,
setLocEnd,
htmlEscapeInsideDoubleQuote,
htmlEscapeInsideAngleBracket
};
15 changes: 13 additions & 2 deletions tests/jsx/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -139,6 +139,17 @@ exports[`expression.js 1`] = `
"
`;

exports[`html_escape.js 1`] = `
"export default () => <a href=\\"https://foo.bar?q1=foo&q2=bar\\" />;

() => <img src=\\"https://bar.foo?param1=1&param2=2\\" />;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
export default () => <a href=\\"https://foo.bar?q1=foo&q2=bar\\" />;

() => <img src=\\"https://bar.foo?param1=1&param2=2\\" />;
"
`;

exports[`hug.js 1`] = `
"<div>
{__DEV__
Expand Down Expand Up @@ -279,7 +290,7 @@ exports[`parens.js 1`] = `
<path
key='1'
d='M13.6,10.6l,4-2.8L9.5,M13.6,10.6l,4-2.8L9.5,M13.6,10.6l,4-2.8L9.5,M13.6,10.6l,4-2.8L9.5,M13.6,10.6l,4-2.8L9.5,M13.6,10.6l,4-2.8L9.5,'
/>,
/>,
];

<div {...((foo || foo === null) ? {foo} : null)} />
Expand All @@ -305,7 +316,7 @@ exports[`quotes.js 1`] = `
<div id={'\\\\'\\"&quot;<>&amp;quot;'} />;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<div id=\\"&quot;'<>&amp;quot;\\" />;
<div id=\\"&quot;'<>&amp;quot;\\" />;
<div id=\\"&quot;&#39;<>&amp;quot;\\" />;
<div id={\\"'\\\\\\"&quot;<>&amp;quot;\\"} />;
"
`;
3 changes: 3 additions & 0 deletions tests/jsx/html_escape.js
@@ -0,0 +1,3 @@
export default () => <a href="https://foo.bar?q1=foo&q2=bar" />;

() => <img src="https://bar.foo?param1=1&param2=2" />;
2 changes: 1 addition & 1 deletion tests/jsx/jsfmt.spec.js
@@ -1 +1 @@
run_spec(__dirname, null, ["typescript"]);
run_spec(__dirname, null, ["babylon", "typescript"]);
2 changes: 1 addition & 1 deletion tests/jsx/parens.js
Expand Up @@ -6,7 +6,7 @@ a = [
<path
key='1'
d='M13.6,10.6l,4-2.8L9.5,M13.6,10.6l,4-2.8L9.5,M13.6,10.6l,4-2.8L9.5,M13.6,10.6l,4-2.8L9.5,M13.6,10.6l,4-2.8L9.5,M13.6,10.6l,4-2.8L9.5,'
/>,
/>,
];

<div {...((foo || foo === null) ? {foo} : null)} />