1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.render = void 0;
|
4 | var utils_js_1 = require("./utils.js");
|
5 | var errorPrefix = "css-selector-parser render error: ";
|
6 | function renderNamespace(namespace) {
|
7 | if (namespace.type === 'WildcardNamespace') {
|
8 | return '*|';
|
9 | }
|
10 | else if (namespace.type === 'NamespaceName') {
|
11 | return "".concat((0, utils_js_1.escapeIdentifier)(namespace.name), "|");
|
12 | }
|
13 | else if (namespace.type === 'NoNamespace') {
|
14 | return '|';
|
15 | }
|
16 | throw new Error("".concat(errorPrefix, "Unknown namespace type: ").concat(namespace.type, "."));
|
17 | }
|
18 | function renderSubstitution(sub) {
|
19 | return "$".concat((0, utils_js_1.escapeIdentifier)(sub.name));
|
20 | }
|
21 | function renderFormula(a, b) {
|
22 | if (a) {
|
23 | var result = "".concat(a === 1 ? '' : a === -1 ? '-' : a, "n");
|
24 | if (b) {
|
25 | result += "".concat(b > 0 ? '+' : '').concat(b);
|
26 | }
|
27 | return result;
|
28 | }
|
29 | else {
|
30 | return String(b);
|
31 | }
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | function render(entity) {
|
57 | if (entity.type === 'Selector') {
|
58 | return entity.rules.map(render).join(', ');
|
59 | }
|
60 | if (entity.type === 'Rule') {
|
61 | var result = '';
|
62 | var items = entity.items, combinator = entity.combinator, nestedRule = entity.nestedRule;
|
63 | if (combinator) {
|
64 | result += "".concat(combinator, " ");
|
65 | }
|
66 | for (var _i = 0, items_1 = items; _i < items_1.length; _i++) {
|
67 | var item = items_1[_i];
|
68 | result += render(item);
|
69 | }
|
70 | if (nestedRule) {
|
71 | result += " ".concat(render(nestedRule));
|
72 | }
|
73 | return result;
|
74 | }
|
75 | else if (entity.type === 'TagName' || entity.type === 'WildcardTag') {
|
76 | var result = '';
|
77 | var namespace = entity.namespace;
|
78 | if (namespace) {
|
79 | result += renderNamespace(namespace);
|
80 | }
|
81 | if (entity.type === 'TagName') {
|
82 | result += (0, utils_js_1.escapeIdentifier)(entity.name);
|
83 | }
|
84 | else if (entity.type === 'WildcardTag') {
|
85 | result += '*';
|
86 | }
|
87 | return result;
|
88 | }
|
89 | else if (entity.type === 'Id') {
|
90 | return "#".concat((0, utils_js_1.escapeIdentifier)(entity.name));
|
91 | }
|
92 | else if (entity.type === 'ClassName') {
|
93 | return ".".concat((0, utils_js_1.escapeIdentifier)(entity.name));
|
94 | }
|
95 | else if (entity.type === 'Attribute') {
|
96 | var name_1 = entity.name, namespace = entity.namespace, operator = entity.operator, value = entity.value, caseSensitivityModifier = entity.caseSensitivityModifier;
|
97 | var result = '[';
|
98 | if (namespace) {
|
99 | result += renderNamespace(namespace);
|
100 | }
|
101 | result += (0, utils_js_1.escapeIdentifier)(name_1);
|
102 | if (operator && value) {
|
103 | result += operator;
|
104 | if (value.type === 'String') {
|
105 | result += (0, utils_js_1.escapeString)(value.value);
|
106 | }
|
107 | else if (value.type === 'Substitution') {
|
108 | result += renderSubstitution(value);
|
109 | }
|
110 | else {
|
111 | throw new Error("Unknown attribute value type: ".concat(value.type, "."));
|
112 | }
|
113 | if (caseSensitivityModifier) {
|
114 | result += " ".concat((0, utils_js_1.escapeIdentifier)(caseSensitivityModifier));
|
115 | }
|
116 | }
|
117 | result += ']';
|
118 | return result;
|
119 | }
|
120 | else if (entity.type === 'PseudoClass') {
|
121 | var name_2 = entity.name, argument = entity.argument;
|
122 | var result = ":".concat((0, utils_js_1.escapeIdentifier)(name_2));
|
123 | if (argument) {
|
124 | result += "(".concat(argument.type === 'String' ? (0, utils_js_1.escapeIdentifier)(argument.value) : render(argument), ")");
|
125 | }
|
126 | return result;
|
127 | }
|
128 | else if (entity.type === 'PseudoElement') {
|
129 | var name_3 = entity.name, argument = entity.argument;
|
130 | var result = "::".concat((0, utils_js_1.escapeIdentifier)(name_3));
|
131 | if (argument) {
|
132 | result += "(".concat(argument.type === 'String' ? (0, utils_js_1.escapeIdentifier)(argument.value) : render(argument), ")");
|
133 | }
|
134 | return result;
|
135 | }
|
136 | else if (entity.type === 'String') {
|
137 | throw new Error("".concat(errorPrefix, "String cannot be rendered outside of context."));
|
138 | }
|
139 | else if (entity.type === 'Formula') {
|
140 | return renderFormula(entity.a, entity.b);
|
141 | }
|
142 | else if (entity.type === 'FormulaOfSelector') {
|
143 | return renderFormula(entity.a, entity.b) + ' of ' + render(entity.selector);
|
144 | }
|
145 | else if (entity.type === 'Substitution') {
|
146 | return "$".concat((0, utils_js_1.escapeIdentifier)(entity.name));
|
147 | }
|
148 | throw new Error("Unknown type specified to render method: ".concat(entity.type, "."));
|
149 | }
|
150 | exports.render = render;
|