Skip to content

Commit 8b6f1f1

Browse files
committed
Escape < in strings
1 parent 4738b26 commit 8b6f1f1

4 files changed

Lines changed: 7 additions & 4 deletions

File tree

src/function.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { quoteKey, isValidVariableName } from "./quote";
44
/**
55
* Used in function stringification.
66
*/
7-
/* istanbul ignore next */
87
const METHOD_NAMES_ARE_QUOTED =
98
{
109
" "() {

src/index.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,16 @@ describe("javascript-stringify", () => {
8282
"should escape certain unicode sequences",
8383
test("\u0602", "'\\u0602'"),
8484
);
85+
86+
it("should escape < for safety", test("</script>", "'\\u003c/script>'"));
8587
});
8688

8789
describe("numbers", () => {
8890
it("should stringify integers", test(10, "10"));
8991

9092
it("should stringify floats", test(10.5, "10.5"));
9193

92-
it('should stringify "NaN"', test(10.5, "10.5"));
94+
it('should stringify "NaN"', test(NaN, "NaN"));
9395

9496
it('should stringify "Infinity"', test(Infinity, "Infinity"));
9597

@@ -220,6 +222,8 @@ describe("javascript-stringify", () => {
220222

221223
describe("RegExp", () => {
222224
it("should stringify as shorthand", test(/[abc]/gi, "/[abc]/gi"));
225+
226+
it("should escape slashes", test(new RegExp("a/b"), "/a\\/b/"));
223227
});
224228

225229
describe("Number", () => {

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function stringify(
6666
// Track nodes to restore later.
6767
if (tracking.has(value)) {
6868
unpack.set(path.slice(1), tracking.get(value)!);
69-
// Use `undefined` as temporaray stand-in for referenced nodes
69+
// Use `undefined` as temporary stand-in for referenced nodes.
7070
return valueToString(undefined, space, onNext, key);
7171
}
7272

src/quote.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Next } from "./types";
77
* Source: https://github.com/douglascrockford/JSON-js/blob/master/json2.js
88
*/
99
const ESCAPABLE =
10-
/[\\\'\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
10+
/[\\'<\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
1111

1212
/**
1313
* Map of characters to escape characters.

0 commit comments

Comments
 (0)