Skip to content

Commit

Permalink
feat(stringify): Escape unsafe characters
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed Oct 1, 2020
1 parent 8c763a5 commit 138b101
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/stringify.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { readFileSync } from "fs";
import { parse, stringify } from ".";
import { tests } from "./__fixtures__/tests";

describe("Stringify & re-parse", () => {
describe("Own tests", () => {
for (const [selector, expected, message] of tests) {
test(`${message} (${selector})`, () => {
expect(parse(stringify(expected))).toStrictEqual(expected);
});
}
});

it("Collected Selectors (qwery, sizzle, nwmatcher)", () => {
const out = JSON.parse(
readFileSync(`${__dirname}/__fixtures__/out.json`, "utf8")
);
for (const s of Object.keys(out)) {
expect(parse(s)).toStrictEqual(out[s]);
}
});
});
14 changes: 12 additions & 2 deletions src/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ const actionTypes: { [key: string]: string } = {
hyphen: "|",
};

const charsToEscape = new Set([
...Object.values(actionTypes).filter(Boolean),
":",
"[",
"]",
" ",
]);

export default function stringify(token: Selector[][]): string {
return token.map(stringifySubselector).join(", ");
}
Expand Down Expand Up @@ -73,6 +81,8 @@ function stringifyToken(token: Selector): string {
}

function escapeName(str: string): string {
// TODO
return str;
return str
.split("")
.map((c) => (charsToEscape.has(c) ? `\\${c}` : c))
.join("");
}

0 comments on commit 138b101

Please sign in to comment.