1 | "use strict";
|
---|
2 | var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
---|
3 | if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
---|
4 | if (ar || !(i in from)) {
|
---|
5 | if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
---|
6 | ar[i] = from[i];
|
---|
7 | }
|
---|
8 | }
|
---|
9 | return to.concat(ar || Array.prototype.slice.call(from));
|
---|
10 | };
|
---|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
12 | var actionTypes = {
|
---|
13 | equals: "",
|
---|
14 | element: "~",
|
---|
15 | start: "^",
|
---|
16 | end: "$",
|
---|
17 | any: "*",
|
---|
18 | not: "!",
|
---|
19 | hyphen: "|",
|
---|
20 | };
|
---|
21 | var charsToEscape = new Set(__spreadArray(__spreadArray([], Object.keys(actionTypes)
|
---|
22 | .map(function (typeKey) { return actionTypes[typeKey]; })
|
---|
23 | .filter(Boolean), true), [
|
---|
24 | ":",
|
---|
25 | "[",
|
---|
26 | "]",
|
---|
27 | " ",
|
---|
28 | "\\",
|
---|
29 | "(",
|
---|
30 | ")",
|
---|
31 | "'",
|
---|
32 | ], false));
|
---|
33 | /**
|
---|
34 | * Turns `selector` back into a string.
|
---|
35 | *
|
---|
36 | * @param selector Selector to stringify.
|
---|
37 | */
|
---|
38 | function stringify(selector) {
|
---|
39 | return selector.map(stringifySubselector).join(", ");
|
---|
40 | }
|
---|
41 | exports.default = stringify;
|
---|
42 | function stringifySubselector(token) {
|
---|
43 | return token.map(stringifyToken).join("");
|
---|
44 | }
|
---|
45 | function stringifyToken(token) {
|
---|
46 | switch (token.type) {
|
---|
47 | // Simple types
|
---|
48 | case "child":
|
---|
49 | return " > ";
|
---|
50 | case "parent":
|
---|
51 | return " < ";
|
---|
52 | case "sibling":
|
---|
53 | return " ~ ";
|
---|
54 | case "adjacent":
|
---|
55 | return " + ";
|
---|
56 | case "descendant":
|
---|
57 | return " ";
|
---|
58 | case "universal":
|
---|
59 | return getNamespace(token.namespace) + "*";
|
---|
60 | case "tag":
|
---|
61 | return getNamespacedName(token);
|
---|
62 | case "pseudo-element":
|
---|
63 | return "::" + escapeName(token.name);
|
---|
64 | case "pseudo":
|
---|
65 | if (token.data === null)
|
---|
66 | return ":" + escapeName(token.name);
|
---|
67 | if (typeof token.data === "string") {
|
---|
68 | return ":" + escapeName(token.name) + "(" + escapeName(token.data) + ")";
|
---|
69 | }
|
---|
70 | return ":" + escapeName(token.name) + "(" + stringify(token.data) + ")";
|
---|
71 | case "attribute": {
|
---|
72 | if (token.name === "id" &&
|
---|
73 | token.action === "equals" &&
|
---|
74 | !token.ignoreCase &&
|
---|
75 | !token.namespace) {
|
---|
76 | return "#" + escapeName(token.value);
|
---|
77 | }
|
---|
78 | if (token.name === "class" &&
|
---|
79 | token.action === "element" &&
|
---|
80 | !token.ignoreCase &&
|
---|
81 | !token.namespace) {
|
---|
82 | return "." + escapeName(token.value);
|
---|
83 | }
|
---|
84 | var name_1 = getNamespacedName(token);
|
---|
85 | if (token.action === "exists") {
|
---|
86 | return "[" + name_1 + "]";
|
---|
87 | }
|
---|
88 | return "[" + name_1 + actionTypes[token.action] + "='" + escapeName(token.value) + "'" + (token.ignoreCase ? "i" : token.ignoreCase === false ? "s" : "") + "]";
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 | function getNamespacedName(token) {
|
---|
93 | return "" + getNamespace(token.namespace) + escapeName(token.name);
|
---|
94 | }
|
---|
95 | function getNamespace(namespace) {
|
---|
96 | return namespace !== null
|
---|
97 | ? (namespace === "*" ? "*" : escapeName(namespace)) + "|"
|
---|
98 | : "";
|
---|
99 | }
|
---|
100 | function escapeName(str) {
|
---|
101 | return str
|
---|
102 | .split("")
|
---|
103 | .map(function (c) { return (charsToEscape.has(c) ? "\\" + c : c); })
|
---|
104 | .join("");
|
---|
105 | }
|
---|