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 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
Expand Up @@ -60,4 +60,37 @@ 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);
});

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

const expected = unpad(`
"<\\\\/style";
`);
expect(transform(source)).toBe(expected);
});

it("should handle html comment escape", () => {
const source = unpad(`
"<!" + "--"
`);

const expected = unpad(`
"\\\\x3C!--";
`);
expect(transform(source)).toBe(expected);
});
});
8 changes: 8 additions & 0 deletions packages/babel-plugin-minify-constant-folding/src/index.js
@@ -1,6 +1,7 @@
"use strict";

const evaluate = require("babel-helper-evaluate-path");
const jsesc = require("jsesc");

module.exports = ({ types: t, traverse }) => {
const seen = Symbol("seen");
Expand Down Expand Up @@ -112,6 +113,13 @@ module.exports = ({ types: t, traverse }) => {
}
}

// https://github.com/babel/babili/issues/382
if (typeof res.value === "string") {
res.value = jsesc(res.value, {
isScriptContext: true
});
}

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