@xylentis/testgen 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,241 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import * as css from './cssTokenizer.js';
17
+ export class InvalidSelectorError extends Error {
18
+ }
19
+ export function isInvalidSelectorError(error) {
20
+ return error instanceof InvalidSelectorError;
21
+ }
22
+ export function parseCSS(selector, customNames) {
23
+ let tokens;
24
+ try {
25
+ tokens = css.tokenize(selector);
26
+ if (!(tokens[tokens.length - 1] instanceof css.EOFToken))
27
+ tokens.push(new css.EOFToken());
28
+ }
29
+ catch (e) {
30
+ const newMessage = e.message + ` while parsing css selector "${selector}". Did you mean to CSS.escape it?`;
31
+ const index = (e.stack || '').indexOf(e.message);
32
+ if (index !== -1)
33
+ e.stack = e.stack.substring(0, index) + newMessage + e.stack.substring(index + e.message.length);
34
+ e.message = newMessage;
35
+ throw e;
36
+ }
37
+ const unsupportedToken = tokens.find(token => {
38
+ return (token instanceof css.AtKeywordToken) ||
39
+ (token instanceof css.BadStringToken) ||
40
+ (token instanceof css.BadURLToken) ||
41
+ (token instanceof css.ColumnToken) ||
42
+ (token instanceof css.CDOToken) ||
43
+ (token instanceof css.CDCToken) ||
44
+ (token instanceof css.SemicolonToken) ||
45
+ // TODO: Consider using these for something, e.g. to escape complex strings.
46
+ // For example :xpath{ (//div/bar[@attr="foo"])[2]/baz }
47
+ // Or this way :xpath( {complex-xpath-goes-here("hello")} )
48
+ (token instanceof css.OpenCurlyToken) ||
49
+ (token instanceof css.CloseCurlyToken) ||
50
+ // TODO: Consider treating these as strings?
51
+ (token instanceof css.URLToken) ||
52
+ (token instanceof css.PercentageToken);
53
+ });
54
+ if (unsupportedToken)
55
+ throw new InvalidSelectorError(`Unsupported token "${unsupportedToken.toSource()}" while parsing css selector "${selector}". Did you mean to CSS.escape it?`);
56
+ let pos = 0;
57
+ const names = new Set();
58
+ function unexpected() {
59
+ return new InvalidSelectorError(`Unexpected token "${tokens[pos].toSource()}" while parsing css selector "${selector}". Did you mean to CSS.escape it?`);
60
+ }
61
+ function skipWhitespace() {
62
+ while (tokens[pos] instanceof css.WhitespaceToken)
63
+ pos++;
64
+ }
65
+ function isIdent(p = pos) {
66
+ return tokens[p] instanceof css.IdentToken;
67
+ }
68
+ function isString(p = pos) {
69
+ return tokens[p] instanceof css.StringToken;
70
+ }
71
+ function isNumber(p = pos) {
72
+ return tokens[p] instanceof css.NumberToken;
73
+ }
74
+ function isComma(p = pos) {
75
+ return tokens[p] instanceof css.CommaToken;
76
+ }
77
+ function isOpenParen(p = pos) {
78
+ return tokens[p] instanceof css.OpenParenToken;
79
+ }
80
+ function isCloseParen(p = pos) {
81
+ return tokens[p] instanceof css.CloseParenToken;
82
+ }
83
+ function isFunction(p = pos) {
84
+ return tokens[p] instanceof css.FunctionToken;
85
+ }
86
+ function isStar(p = pos) {
87
+ return (tokens[p] instanceof css.DelimToken) && tokens[p].value === '*';
88
+ }
89
+ function isEOF(p = pos) {
90
+ return tokens[p] instanceof css.EOFToken;
91
+ }
92
+ function isClauseCombinator(p = pos) {
93
+ return (tokens[p] instanceof css.DelimToken) && (['>', '+', '~'].includes(tokens[p].value));
94
+ }
95
+ function isSelectorClauseEnd(p = pos) {
96
+ return isComma(p) || isCloseParen(p) || isEOF(p) || isClauseCombinator(p) || (tokens[p] instanceof css.WhitespaceToken);
97
+ }
98
+ function consumeFunctionArguments() {
99
+ const result = [consumeArgument()];
100
+ while (true) {
101
+ skipWhitespace();
102
+ if (!isComma())
103
+ break;
104
+ pos++;
105
+ result.push(consumeArgument());
106
+ }
107
+ return result;
108
+ }
109
+ function consumeArgument() {
110
+ skipWhitespace();
111
+ if (isNumber())
112
+ return tokens[pos++].value;
113
+ if (isString())
114
+ return tokens[pos++].value;
115
+ return consumeComplexSelector();
116
+ }
117
+ function consumeComplexSelector() {
118
+ const result = { simples: [] };
119
+ skipWhitespace();
120
+ if (isClauseCombinator()) {
121
+ // Put implicit ":scope" at the start. https://drafts.csswg.org/selectors-4/#relative
122
+ result.simples.push({ selector: { functions: [{ name: 'scope', args: [] }] }, combinator: '' });
123
+ }
124
+ else {
125
+ result.simples.push({ selector: consumeSimpleSelector(), combinator: '' });
126
+ }
127
+ while (true) {
128
+ skipWhitespace();
129
+ if (isClauseCombinator()) {
130
+ result.simples[result.simples.length - 1].combinator = tokens[pos++].value;
131
+ skipWhitespace();
132
+ }
133
+ else if (isSelectorClauseEnd()) {
134
+ break;
135
+ }
136
+ result.simples.push({ combinator: '', selector: consumeSimpleSelector() });
137
+ }
138
+ return result;
139
+ }
140
+ function consumeSimpleSelector() {
141
+ let rawCSSString = '';
142
+ const functions = [];
143
+ while (!isSelectorClauseEnd()) {
144
+ if (isIdent() || isStar()) {
145
+ rawCSSString += tokens[pos++].toSource();
146
+ }
147
+ else if (tokens[pos] instanceof css.HashToken) {
148
+ rawCSSString += tokens[pos++].toSource();
149
+ }
150
+ else if ((tokens[pos] instanceof css.DelimToken) && tokens[pos].value === '.') {
151
+ pos++;
152
+ if (isIdent())
153
+ rawCSSString += '.' + tokens[pos++].toSource();
154
+ else
155
+ throw unexpected();
156
+ }
157
+ else if (tokens[pos] instanceof css.ColonToken) {
158
+ pos++;
159
+ if (isIdent()) {
160
+ if (!customNames.has(tokens[pos].value.toLowerCase())) {
161
+ rawCSSString += ':' + tokens[pos++].toSource();
162
+ }
163
+ else {
164
+ const name = tokens[pos++].value.toLowerCase();
165
+ functions.push({ name, args: [] });
166
+ names.add(name);
167
+ }
168
+ }
169
+ else if (isFunction()) {
170
+ const name = tokens[pos++].value.toLowerCase();
171
+ if (!customNames.has(name)) {
172
+ rawCSSString += `:${name}(${consumeBuiltinFunctionArguments()})`;
173
+ }
174
+ else {
175
+ functions.push({ name, args: consumeFunctionArguments() });
176
+ names.add(name);
177
+ }
178
+ skipWhitespace();
179
+ if (!isCloseParen())
180
+ throw unexpected();
181
+ pos++;
182
+ }
183
+ else {
184
+ throw unexpected();
185
+ }
186
+ }
187
+ else if (tokens[pos] instanceof css.OpenSquareToken) {
188
+ rawCSSString += '[';
189
+ pos++;
190
+ while (!(tokens[pos] instanceof css.CloseSquareToken) && !isEOF())
191
+ rawCSSString += tokens[pos++].toSource();
192
+ if (!(tokens[pos] instanceof css.CloseSquareToken))
193
+ throw unexpected();
194
+ rawCSSString += ']';
195
+ pos++;
196
+ }
197
+ else {
198
+ throw unexpected();
199
+ }
200
+ }
201
+ if (!rawCSSString && !functions.length)
202
+ throw unexpected();
203
+ return { css: rawCSSString || undefined, functions };
204
+ }
205
+ function consumeBuiltinFunctionArguments() {
206
+ let s = '';
207
+ let balance = 1; // First open paren is a part of a function token.
208
+ while (!isEOF()) {
209
+ if (isOpenParen() || isFunction())
210
+ balance++;
211
+ if (isCloseParen())
212
+ balance--;
213
+ if (!balance)
214
+ break;
215
+ s += tokens[pos++].toSource();
216
+ }
217
+ return s;
218
+ }
219
+ const result = consumeFunctionArguments();
220
+ if (!isEOF())
221
+ throw unexpected();
222
+ if (result.some(arg => typeof arg !== 'object' || !('simples' in arg)))
223
+ throw new InvalidSelectorError(`Error while parsing css selector "${selector}". Did you mean to CSS.escape it?`);
224
+ return { selector: result, names: Array.from(names) };
225
+ }
226
+ export function serializeSelector(args) {
227
+ return args.map(arg => {
228
+ if (typeof arg === 'string')
229
+ return `"${arg}"`;
230
+ if (typeof arg === 'number')
231
+ return String(arg);
232
+ return arg.simples.map(({ selector, combinator }) => {
233
+ let s = selector.css || '';
234
+ s = s + selector.functions.map(func => `:${func.name}(${serializeSelector(func.args)})`).join('');
235
+ if (combinator)
236
+ s += ' ' + combinator;
237
+ return s;
238
+ }).join(' ');
239
+ }).join(', ');
240
+ }
241
+ //# sourceMappingURL=cssParser.js.map