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

Escape "</script" when inlining strings #384

Merged
merged 3 commits into from Jan 24, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -60,4 +60,15 @@ describe("constant-folding-plugin", () => {
`);
expect(transform(source)).toBe(source);
});

it("should handle script escape", () => {
const source = unpad(`
"</" + "script"
`);

const expected = unpad(`
"<\\\\/script";
`);
expect(transform(source)).toBe(expected);
});
});
6 changes: 6 additions & 0 deletions packages/babel-plugin-minify-constant-folding/src/index.js
Expand Up @@ -112,6 +112,12 @@ module.exports = ({ types: t, traverse }) => {
}
}

// https://github.com/babel/babili/issues/382
if (typeof res.value === "string" &&
res.value.indexOf("</script") > -1) {
Copy link
Member

Choose a reason for hiding this comment

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

Can we use includes instead of indexof since we are already targeting node 4?

Copy link

Choose a reason for hiding this comment

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

includes was introduced in node 6

Copy link
Member

Choose a reason for hiding this comment

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

Yeah...I know that..But we are transpiling the code to node 4.. So it shouldnt be a problem

res.value = res.value.replace(/<\/script/g, "<\\/script");
}

const node = t.valueToNode(res.value);
node[seen] = true;
path.replaceWith(node);
Expand Down