Skip to content

Commit

Permalink
Escape "</script" when inlining strings (#384)
Browse files Browse the repository at this point in the history
* Escape "</script" when inlining strings

* Fix lint

* Use jsesc
  • Loading branch information
kangax committed Jan 24, 2017
1 parent 7f60941 commit 17eba03
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
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

0 comments on commit 17eba03

Please sign in to comment.