How to use SubstringMatchToken method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

cssTokenizer.js

Source: cssTokenizer.js Github

copy
1/*
2 * Original at https://github.com/tabatkins/parse-css
3 * licensed under http://creativecommons.org/publicdomain/zero/1.0/
4 *
5 * Modifications copyright (c) Microsoft Corporation.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20// Changes from https://github.com/tabatkins/parse-css
21//   - Tabs are replaced with two spaces.
22//   - Everything not related to tokenizing - below the first exports block - is removed.
23
24// @ts-nocheck
25
26(function (root, factory) {
27    // Universal Module Definition (UMD) to support AMD, CommonJS/Node.js,
28    // Rhino, and plain browser loading.
29    if (typeof define === 'function' && define.amd) {
30        define(['exports'], factory);
31    } else if (typeof exports !== 'undefined') {
32        factory(exports);
33    } else {
34        factory(root);
35    }
36}(this, function (exports) {
37
38var between = function (num, first, last) { return num >= first && num <= last; }
39function digit(code) { return between(code, 0x30,0x39); }
40function hexdigit(code) { return digit(code) || between(code, 0x41,0x46) || between(code, 0x61,0x66); }
41function uppercaseletter(code) { return between(code, 0x41,0x5a); }
42function lowercaseletter(code) { return between(code, 0x61,0x7a); }
43function letter(code) { return uppercaseletter(code) || lowercaseletter(code); }
44function nonascii(code) { return code >= 0x80; }
45function namestartchar(code) { return letter(code) || nonascii(code) || code == 0x5f; }
46function namechar(code) { return namestartchar(code) || digit(code) || code == 0x2d; }
47function nonprintable(code) { return between(code, 0,8) || code == 0xb || between(code, 0xe,0x1f) || code == 0x7f; }
48function newline(code) { return code == 0xa; }
49function whitespace(code) { return newline(code) || code == 9 || code == 0x20; }
50function badescape(code) { return newline(code) || isNaN(code); }
51
52var maximumallowedcodepoint = 0x10ffff;
53
54var InvalidCharacterError = function(message) {
55  this.message = message;
56};
57InvalidCharacterError.prototype = new Error;
58InvalidCharacterError.prototype.name = 'InvalidCharacterError';
59
60function preprocess(str) {
61  // Turn a string into an array of code points,
62  // following the preprocessing cleanup rules.
63  var codepoints = [];
64  for(var i = 0; i < str.length; i++) {
65    var code = str.charCodeAt(i);
66    if(code == 0xd && str.charCodeAt(i+1) == 0xa) {
67      code = 0xa; i++;
68    }
69    if(code == 0xd || code == 0xc) code = 0xa;
70    if(code == 0x0) code = 0xfffd;
71    if(between(code, 0xd800, 0xdbff) && between(str.charCodeAt(i+1), 0xdc00, 0xdfff)) {
72      // Decode a surrogate pair into an astral codepoint.
73      var lead = code - 0xd800;
74      var trail = str.charCodeAt(i+1) - 0xdc00;
75      code = Math.pow(2, 16) + lead * Math.pow(2, 10) + trail;
76      i++;
77    }
78    codepoints.push(code);
79  }
80  return codepoints;
81}
82
83function stringFromCode(code) {
84  if(code <= 0xffff) return String.fromCharCode(code);
85  // Otherwise, encode astral char as surrogate pair.
86  code -= Math.pow(2, 16);
87  var lead = Math.floor(code/Math.pow(2, 10)) + 0xd800;
88  var trail = code % Math.pow(2, 10) + 0xdc00;
89  return String.fromCharCode(lead) + String.fromCharCode(trail);
90}
91
92function tokenize(str) {
93  str = preprocess(str);
94  var i = -1;
95  var tokens = [];
96  var code;
97
98  // Line number information.
99  var line = 0;
100  var column = 0;
101  // The only use of lastLineLength is in reconsume().
102  var lastLineLength = 0;
103  var incrLineno = function() {
104    line += 1;
105    lastLineLength = column;
106    column = 0;
107  };
108  var locStart = {line:line, column:column};
109
110  var codepoint = function(i) {
111    if(i >= str.length) {
112      return -1;
113    }
114    return str[i];
115  }
116  var next = function(num) {
117    if(num === undefined)
118      num = 1;
119    if(num > 3)
120      throw "Spec Error: no more than three codepoints of lookahead.";
121    return codepoint(i+num);
122  };
123  var consume = function(num) {
124    if(num === undefined)
125      num = 1;
126    i += num;
127    code = codepoint(i);
128    if(newline(code)) incrLineno();
129    else column += num;
130    //console.log('Consume '+i+' '+String.fromCharCode(code) + ' 0x' + code.toString(16));
131    return true;
132  };
133  var reconsume = function() {
134    i -= 1;
135    if (newline(code)) {
136      line -= 1;
137      column = lastLineLength;
138    } else {
139      column -= 1;
140    }
141    locStart.line = line;
142    locStart.column = column;
143    return true;
144  };
145  var eof = function(codepoint) {
146    if(codepoint === undefined) codepoint = code;
147    return codepoint == -1;
148  };
149  var donothing = function() {};
150  var parseerror = function() { console.log("Parse error at index " + i + ", processing codepoint 0x" + code.toString(16) + ".");return true; };
151
152  var consumeAToken = function() {
153    consumeComments();
154    consume();
155    if(whitespace(code)) {
156      while(whitespace(next())) consume();
157      return new WhitespaceToken;
158    }
159    else if(code == 0x22) return consumeAStringToken();
160    else if(code == 0x23) {
161      if(namechar(next()) || areAValidEscape(next(1), next(2))) {
162        var token = new HashToken();
163        if(wouldStartAnIdentifier(next(1), next(2), next(3))) token.type = "id";
164        token.value = consumeAName();
165        return token;
166      } else {
167        return new DelimToken(code);
168      }
169    }
170    else if(code == 0x24) {
171      if(next() == 0x3d) {
172        consume();
173        return new SuffixMatchToken();
174      } else {
175        return new DelimToken(code);
176      }
177    }
178    else if(code == 0x27) return consumeAStringToken();
179    else if(code == 0x28) return new OpenParenToken();
180    else if(code == 0x29) return new CloseParenToken();
181    else if(code == 0x2a) {
182      if(next() == 0x3d) {
183        consume();
184        return new SubstringMatchToken();
185      } else {
186        return new DelimToken(code);
187      }
188    }
189    else if(code == 0x2b) {
190      if(startsWithANumber()) {
191        reconsume();
192        return consumeANumericToken();
193      } else {
194        return new DelimToken(code);
195      }
196    }
197    else if(code == 0x2c) return new CommaToken();
198    else if(code == 0x2d) {
199      if(startsWithANumber()) {
200        reconsume();
201        return consumeANumericToken();
202      } else if(next(1) == 0x2d && next(2) == 0x3e) {
203        consume(2);
204        return new CDCToken();
205      } else if(startsWithAnIdentifier()) {
206        reconsume();
207        return consumeAnIdentlikeToken();
208      } else {
209        return new DelimToken(code);
210      }
211    }
212    else if(code == 0x2e) {
213      if(startsWithANumber()) {
214        reconsume();
215        return consumeANumericToken();
216      } else {
217        return new DelimToken(code);
218      }
219    }
220    else if(code == 0x3a) return new ColonToken;
221    else if(code == 0x3b) return new SemicolonToken;
222    else if(code == 0x3c) {
223      if(next(1) == 0x21 && next(2) == 0x2d && next(3) == 0x2d) {
224        consume(3);
225        return new CDOToken();
226      } else {
227        return new DelimToken(code);
228      }
229    }
230    else if(code == 0x40) {
231      if(wouldStartAnIdentifier(next(1), next(2), next(3))) {
232        return new AtKeywordToken(consumeAName());
233      } else {
234        return new DelimToken(code);
235      }
236    }
237    else if(code == 0x5b) return new OpenSquareToken();
238    else if(code == 0x5c) {
239      if(startsWithAValidEscape()) {
240        reconsume();
241        return consumeAnIdentlikeToken();
242      } else {
243        parseerror();
244        return new DelimToken(code);
245      }
246    }
247    else if(code == 0x5d) return new CloseSquareToken();
248    else if(code == 0x5e) {
249      if(next() == 0x3d) {
250        consume();
251        return new PrefixMatchToken();
252      } else {
253        return new DelimToken(code);
254      }
255    }
256    else if(code == 0x7b) return new OpenCurlyToken();
257    else if(code == 0x7c) {
258      if(next() == 0x3d) {
259        consume();
260        return new DashMatchToken();
261      } else if(next() == 0x7c) {
262        consume();
263        return new ColumnToken();
264      } else {
265        return new DelimToken(code);
266      }
267    }
268    else if(code == 0x7d) return new CloseCurlyToken();
269    else if(code == 0x7e) {
270      if(next() == 0x3d) {
271        consume();
272        return new IncludeMatchToken();
273      } else {
274        return new DelimToken(code);
275      }
276    }
277    else if(digit(code)) {
278      reconsume();
279      return consumeANumericToken();
280    }
281    else if(namestartchar(code)) {
282      reconsume();
283      return consumeAnIdentlikeToken();
284    }
285    else if(eof()) return new EOFToken();
286    else return new DelimToken(code);
287  };
288
289  var consumeComments = function() {
290    while(next(1) == 0x2f && next(2) == 0x2a) {
291      consume(2);
292      while(true) {
293        consume();
294        if(code == 0x2a && next() == 0x2f) {
295          consume();
296          break;
297        } else if(eof()) {
298          parseerror();
299          return;
300        }
301      }
302    }
303  };
304
305  var consumeANumericToken = function() {
306    var num = consumeANumber();
307    if(wouldStartAnIdentifier(next(1), next(2), next(3))) {
308      var token = new DimensionToken();
309      token.value = num.value;
310      token.repr = num.repr;
311      token.type = num.type;
312      token.unit = consumeAName();
313      return token;
314    } else if(next() == 0x25) {
315      consume();
316      var token = new PercentageToken();
317      token.value = num.value;
318      token.repr = num.repr;
319      return token;
320    } else {
321      var token = new NumberToken();
322      token.value = num.value;
323      token.repr = num.repr;
324      token.type = num.type;
325      return token;
326    }
327  };
328
329  var consumeAnIdentlikeToken = function() {
330    var str = consumeAName();
331    if(str.toLowerCase() == "url" && next() == 0x28) {
332      consume();
333      while(whitespace(next(1)) && whitespace(next(2))) consume();
334      if(next() == 0x22 || next() == 0x27) {
335        return new FunctionToken(str);
336      } else if(whitespace(next()) && (next(2) == 0x22 || next(2) == 0x27)) {
337        return new FunctionToken(str);
338      } else {
339        return consumeAURLToken();
340      }
341    } else if(next() == 0x28) {
342      consume();
343      return new FunctionToken(str);
344    } else {
345      return new IdentToken(str);
346    }
347  };
348
349  var consumeAStringToken = function(endingCodePoint) {
350    if(endingCodePoint === undefined) endingCodePoint = code;
351    var string = "";
352    while(consume()) {
353      if(code == endingCodePoint || eof()) {
354        return new StringToken(string);
355      } else if(newline(code)) {
356        parseerror();
357        reconsume();
358        return new BadStringToken();
359      } else if(code == 0x5c) {
360        if(eof(next())) {
361          donothing();
362        } else if(newline(next())) {
363          consume();
364        } else {
365          string += stringFromCode(consumeEscape())
366        }
367      } else {
368        string += stringFromCode(code);
369      }
370    }
371  };
372
373  var consumeAURLToken = function() {
374    var token = new URLToken("");
375    while(whitespace(next())) consume();
376    if(eof(next())) return token;
377    while(consume()) {
378      if(code == 0x29 || eof()) {
379        return token;
380      } else if(whitespace(code)) {
381        while(whitespace(next())) consume();
382        if(next() == 0x29 || eof(next())) {
383          consume();
384          return token;
385        } else {
386          consumeTheRemnantsOfABadURL();
387          return new BadURLToken();
388        }
389      } else if(code == 0x22 || code == 0x27 || code == 0x28 || nonprintable(code)) {
390        parseerror();
391        consumeTheRemnantsOfABadURL();
392        return new BadURLToken();
393      } else if(code == 0x5c) {
394        if(startsWithAValidEscape()) {
395          token.value += stringFromCode(consumeEscape());
396        } else {
397          parseerror();
398          consumeTheRemnantsOfABadURL();
399          return new BadURLToken();
400        }
401      } else {
402        token.value += stringFromCode(code);
403      }
404    }
405  };
406
407  var consumeEscape = function() {
408    // Assume the the current character is the \
409    // and the next code point is not a newline.
410    consume();
411    if(hexdigit(code)) {
412      // Consume 1-6 hex digits
413      var digits = [code];
414      for(var total = 0; total < 5; total++) {
415        if(hexdigit(next())) {
416          consume();
417          digits.push(code);
418        } else {
419          break;
420        }
421      }
422      if(whitespace(next())) consume();
423      var value = parseInt(digits.map(function(x){return String.fromCharCode(x);}).join(''), 16);
424      if( value > maximumallowedcodepoint ) value = 0xfffd;
425      return value;
426    } else if(eof()) {
427      return 0xfffd;
428    } else {
429      return code;
430    }
431  };
432
433  var areAValidEscape = function(c1, c2) {
434    if(c1 != 0x5c) return false;
435    if(newline(c2)) return false;
436    return true;
437  };
438  var startsWithAValidEscape = function() {
439    return areAValidEscape(code, next());
440  };
441
442  var wouldStartAnIdentifier = function(c1, c2, c3) {
443    if(c1 == 0x2d) {
444      return namestartchar(c2) || c2 == 0x2d || areAValidEscape(c2, c3);
445    } else if(namestartchar(c1)) {
446      return true;
447    } else if(c1 == 0x5c) {
448      return areAValidEscape(c1, c2);
449    } else {
450      return false;
451    }
452  };
453  var startsWithAnIdentifier = function() {
454    return wouldStartAnIdentifier(code, next(1), next(2));
455  };
456
457  var wouldStartANumber = function(c1, c2, c3) {
458    if(c1 == 0x2b || c1 == 0x2d) {
459      if(digit(c2)) return true;
460      if(c2 == 0x2e && digit(c3)) return true;
461      return false;
462    } else if(c1 == 0x2e) {
463      if(digit(c2)) return true;
464      return false;
465    } else if(digit(c1)) {
466      return true;
467    } else {
468      return false;
469    }
470  };
471  var startsWithANumber = function() {
472    return wouldStartANumber(code, next(1), next(2));
473  };
474
475  var consumeAName = function() {
476    var result = "";
477    while(consume()) {
478      if(namechar(code)) {
479        result += stringFromCode(code);
480      } else if(startsWithAValidEscape()) {
481        result += stringFromCode(consumeEscape());
482      } else {
483        reconsume();
484        return result;
485      }
486    }
487  };
488
489  var consumeANumber = function() {
490    var repr = [];
491    var type = "integer";
492    if(next() == 0x2b || next() == 0x2d) {
493      consume();
494      repr += stringFromCode(code);
495    }
496    while(digit(next())) {
497      consume();
498      repr += stringFromCode(code);
499    }
500    if(next(1) == 0x2e && digit(next(2))) {
501      consume();
502      repr += stringFromCode(code);
503      consume();
504      repr += stringFromCode(code);
505      type = "number";
506      while(digit(next())) {
507        consume();
508        repr += stringFromCode(code);
509      }
510    }
511    var c1 = next(1), c2 = next(2), c3 = next(3);
512    if((c1 == 0x45 || c1 == 0x65) && digit(c2)) {
513      consume();
514      repr += stringFromCode(code);
515      consume();
516      repr += stringFromCode(code);
517      type = "number";
518      while(digit(next())) {
519        consume();
520        repr += stringFromCode(code);
521      }
522    } else if((c1 == 0x45 || c1 == 0x65) && (c2 == 0x2b || c2 == 0x2d) && digit(c3)) {
523      consume();
524      repr += stringFromCode(code);
525      consume();
526      repr += stringFromCode(code);
527      consume();
528      repr += stringFromCode(code);
529      type = "number";
530      while(digit(next())) {
531        consume();
532        repr += stringFromCode(code);
533      }
534    }
535    var value = convertAStringToANumber(repr);
536    return {type:type, value:value, repr:repr};
537  };
538
539  var convertAStringToANumber = function(string) {
540    // CSS's number rules are identical to JS, afaik.
541    return +string;
542  };
543
544  var consumeTheRemnantsOfABadURL = function() {
545    while(consume()) {
546      if(code == 0x29 || eof()) {
547        return;
548      } else if(startsWithAValidEscape()) {
549        consumeEscape();
550        donothing();
551      } else {
552        donothing();
553      }
554    }
555  };
556
557
558
559  var iterationCount = 0;
560  while(!eof(next())) {
561    tokens.push(consumeAToken());
562    iterationCount++;
563    if(iterationCount > str.length*2) return "I'm infinite-looping!";
564  }
565  return tokens;
566}
567
568function CSSParserToken() { throw "Abstract Base Class"; }
569CSSParserToken.prototype.toJSON = function() {
570  return {token: this.tokenType};
571}
572CSSParserToken.prototype.toString = function() { return this.tokenType; }
573CSSParserToken.prototype.toSource = function() { return ''+this; }
574
575function BadStringToken() { return this; }
576BadStringToken.prototype = Object.create(CSSParserToken.prototype);
577BadStringToken.prototype.tokenType = "BADSTRING";
578
579function BadURLToken() { return this; }
580BadURLToken.prototype = Object.create(CSSParserToken.prototype);
581BadURLToken.prototype.tokenType = "BADURL";
582
583function WhitespaceToken() { return this; }
584WhitespaceToken.prototype = Object.create(CSSParserToken.prototype);
585WhitespaceToken.prototype.tokenType = "WHITESPACE";
586WhitespaceToken.prototype.toString = function() { return "WS"; }
587WhitespaceToken.prototype.toSource = function() { return " "; }
588
589function CDOToken() { return this; }
590CDOToken.prototype = Object.create(CSSParserToken.prototype);
591CDOToken.prototype.tokenType = "CDO";
592CDOToken.prototype.toSource = function() { return "<!--"; }
593
594function CDCToken() { return this; }
595CDCToken.prototype = Object.create(CSSParserToken.prototype);
596CDCToken.prototype.tokenType = "CDC";
597CDCToken.prototype.toSource = function() { return "-->"; }
598
599function ColonToken() { return this; }
600ColonToken.prototype = Object.create(CSSParserToken.prototype);
601ColonToken.prototype.tokenType = ":";
602
603function SemicolonToken() { return this; }
604SemicolonToken.prototype = Object.create(CSSParserToken.prototype);
605SemicolonToken.prototype.tokenType = ";";
606
607function CommaToken() { return this; }
608CommaToken.prototype = Object.create(CSSParserToken.prototype);
609CommaToken.prototype.tokenType = ",";
610
611function GroupingToken() { throw "Abstract Base Class"; }
612GroupingToken.prototype = Object.create(CSSParserToken.prototype);
613
614function OpenCurlyToken() { this.value = "{"; this.mirror = "}"; return this; }
615OpenCurlyToken.prototype = Object.create(GroupingToken.prototype);
616OpenCurlyToken.prototype.tokenType = "{";
617
618function CloseCurlyToken() { this.value = "}"; this.mirror = "{"; return this; }
619CloseCurlyToken.prototype = Object.create(GroupingToken.prototype);
620CloseCurlyToken.prototype.tokenType = "}";
621
622function OpenSquareToken() { this.value = "["; this.mirror = "]"; return this; }
623OpenSquareToken.prototype = Object.create(GroupingToken.prototype);
624OpenSquareToken.prototype.tokenType = "[";
625
626function CloseSquareToken() { this.value = "]"; this.mirror = "["; return this; }
627CloseSquareToken.prototype = Object.create(GroupingToken.prototype);
628CloseSquareToken.prototype.tokenType = "]";
629
630function OpenParenToken() { this.value = "("; this.mirror = ")"; return this; }
631OpenParenToken.prototype = Object.create(GroupingToken.prototype);
632OpenParenToken.prototype.tokenType = "(";
633
634function CloseParenToken() { this.value = ")"; this.mirror = "("; return this; }
635CloseParenToken.prototype = Object.create(GroupingToken.prototype);
636CloseParenToken.prototype.tokenType = ")";
637
638function IncludeMatchToken() { return this; }
639IncludeMatchToken.prototype = Object.create(CSSParserToken.prototype);
640IncludeMatchToken.prototype.tokenType = "~=";
641
642function DashMatchToken() { return this; }
643DashMatchToken.prototype = Object.create(CSSParserToken.prototype);
644DashMatchToken.prototype.tokenType = "|=";
645
646function PrefixMatchToken() { return this; }
647PrefixMatchToken.prototype = Object.create(CSSParserToken.prototype);
648PrefixMatchToken.prototype.tokenType = "^=";
649
650function SuffixMatchToken() { return this; }
651SuffixMatchToken.prototype = Object.create(CSSParserToken.prototype);
652SuffixMatchToken.prototype.tokenType = "$=";
653
654function SubstringMatchToken() { return this; }
655SubstringMatchToken.prototype = Object.create(CSSParserToken.prototype);
656SubstringMatchToken.prototype.tokenType = "*=";
657
658function ColumnToken() { return this; }
659ColumnToken.prototype = Object.create(CSSParserToken.prototype);
660ColumnToken.prototype.tokenType = "||";
661
662function EOFToken() { return this; }
663EOFToken.prototype = Object.create(CSSParserToken.prototype);
664EOFToken.prototype.tokenType = "EOF";
665EOFToken.prototype.toSource = function() { return ""; }
666
667function DelimToken(code) {
668  this.value = stringFromCode(code);
669  return this;
670}
671DelimToken.prototype = Object.create(CSSParserToken.prototype);
672DelimToken.prototype.tokenType = "DELIM";
673DelimToken.prototype.toString = function() { return "DELIM("+this.value+")"; }
674DelimToken.prototype.toJSON = function() {
675  var json = this.constructor.prototype.constructor.prototype.toJSON.call(this);
676  json.value = this.value;
677  return json;
678}
679DelimToken.prototype.toSource = function() {
680  if(this.value == "\\")
681    return "\\\n";
682  else
683    return this.value;
684}
685
686function StringValuedToken() { throw "Abstract Base Class"; }
687StringValuedToken.prototype = Object.create(CSSParserToken.prototype);
688StringValuedToken.prototype.ASCIIMatch = function(str) {
689  return this.value.toLowerCase() == str.toLowerCase();
690}
691StringValuedToken.prototype.toJSON = function() {
692  var json = this.constructor.prototype.constructor.prototype.toJSON.call(this);
693  json.value = this.value;
694  return json;
695}
696
697function IdentToken(val) {
698  this.value = val;
699}
700IdentToken.prototype = Object.create(StringValuedToken.prototype);
701IdentToken.prototype.tokenType = "IDENT";
702IdentToken.prototype.toString = function() { return "IDENT("+this.value+")"; }
703IdentToken.prototype.toSource = function() {
704  return escapeIdent(this.value);
705}
706
707function FunctionToken(val) {
708  this.value = val;
709  this.mirror = ")";
710}
711FunctionToken.prototype = Object.create(StringValuedToken.prototype);
712FunctionToken.prototype.tokenType = "FUNCTION";
713FunctionToken.prototype.toString = function() { return "FUNCTION("+this.value+")"; }
714FunctionToken.prototype.toSource = function() {
715  return escapeIdent(this.value) + "(";
716}
717
718function AtKeywordToken(val) {
719  this.value = val;
720}
721AtKeywordToken.prototype = Object.create(StringValuedToken.prototype);
722AtKeywordToken.prototype.tokenType = "AT-KEYWORD";
723AtKeywordToken.prototype.toString = function() { return "AT("+this.value+")"; }
724AtKeywordToken.prototype.toSource = function() {
725  return "@" + escapeIdent(this.value);
726}
727
728function HashToken(val) {
729  this.value = val;
730  this.type = "unrestricted";
731}
732HashToken.prototype = Object.create(StringValuedToken.prototype);
733HashToken.prototype.tokenType = "HASH";
734HashToken.prototype.toString = function() { return "HASH("+this.value+")"; }
735HashToken.prototype.toJSON = function() {
736  var json = this.constructor.prototype.constructor.prototype.toJSON.call(this);
737  json.value = this.value;
738  json.type = this.type;
739  return json;
740}
741HashToken.prototype.toSource = function() {
742  if(this.type == "id") {
743    return "#" + escapeIdent(this.value);
744  } else {
745    return "#" + escapeHash(this.value);
746  }
747}
748
749function StringToken(val) {
750  this.value = val;
751}
752StringToken.prototype = Object.create(StringValuedToken.prototype);
753StringToken.prototype.tokenType = "STRING";
754StringToken.prototype.toString = function() {
755  return '"' + escapeString(this.value) + '"';
756}
757
758function URLToken(val) {
759  this.value = val;
760}
761URLToken.prototype = Object.create(StringValuedToken.prototype);
762URLToken.prototype.tokenType = "URL";
763URLToken.prototype.toString = function() { return "URL("+this.value+")"; }
764URLToken.prototype.toSource = function() {
765  return 'url("' + escapeString(this.value) + '")';
766}
767
768function NumberToken() {
769  this.value = null;
770  this.type = "integer";
771  this.repr = "";
772}
773NumberToken.prototype = Object.create(CSSParserToken.prototype);
774NumberToken.prototype.tokenType = "NUMBER";
775NumberToken.prototype.toString = function() {
776  if(this.type == "integer")
777    return "INT("+this.value+")";
778  return "NUMBER("+this.value+")";
779}
780NumberToken.prototype.toJSON = function() {
781  var json = this.constructor.prototype.constructor.prototype.toJSON.call(this);
782  json.value = this.value;
783  json.type = this.type;
784  json.repr = this.repr;
785  return json;
786}
787NumberToken.prototype.toSource = function() { return this.repr; };
788
789function PercentageToken() {
790  this.value = null;
791  this.repr = "";
792}
793PercentageToken.prototype = Object.create(CSSParserToken.prototype);
794PercentageToken.prototype.tokenType = "PERCENTAGE";
795PercentageToken.prototype.toString = function() { return "PERCENTAGE("+this.value+")"; }
796PercentageToken.prototype.toJSON = function() {
797  var json = this.constructor.prototype.constructor.prototype.toJSON.call(this);
798  json.value = this.value;
799  json.repr = this.repr;
800  return json;
801}
802PercentageToken.prototype.toSource = function() { return this.repr + "%"; }
803
804function DimensionToken() {
805  this.value = null;
806  this.type = "integer";
807  this.repr = "";
808  this.unit = "";
809}
810DimensionToken.prototype = Object.create(CSSParserToken.prototype);
811DimensionToken.prototype.tokenType = "DIMENSION";
812DimensionToken.prototype.toString = function() { return "DIM("+this.value+","+this.unit+")"; }
813DimensionToken.prototype.toJSON = function() {
814  var json = this.constructor.prototype.constructor.prototype.toJSON.call(this);
815  json.value = this.value;
816  json.type = this.type;
817  json.repr = this.repr;
818  json.unit = this.unit;
819  return json;
820}
821DimensionToken.prototype.toSource = function() {
822  var source = this.repr;
823  var unit = escapeIdent(this.unit);
824  if(unit[0].toLowerCase() == "e" && (unit[1] == "-" || between(unit.charCodeAt(1), 0x30, 0x39))) {
825    // Unit is ambiguous with scinot
826    // Remove the leading "e", replace with escape.
827    unit = "\\65 " + unit.slice(1, unit.length);
828  }
829  return source+unit;
830}
831
832function escapeIdent(string) {
833  string = ''+string;
834  var result = '';
835  var firstcode = string.charCodeAt(0);
836  for(var i = 0; i < string.length; i++) {
837    var code = string.charCodeAt(i);
838    if(code == 0x0) {
839      throw new InvalidCharacterError('Invalid character: the input contains U+0000.');
840    }
841
842    if(
843      between(code, 0x1, 0x1f) || code == 0x7f ||
844      (i == 0 && between(code, 0x30, 0x39)) ||
845      (i == 1 && between(code, 0x30, 0x39) && firstcode == 0x2d)
846    ) {
847      result += '\\' + code.toString(16) + ' ';
848    } else if(
849      code >= 0x80 ||
850      code == 0x2d ||
851      code == 0x5f ||
852      between(code, 0x30, 0x39) ||
853      between(code, 0x41, 0x5a) ||
854      between(code, 0x61, 0x7a)
855    ) {
856      result += string[i];
857    } else {
858      result += '\\' + string[i];
859    }
860  }
861  return result;
862}
863
864function escapeHash(string) {
865  // Escapes the contents of "unrestricted"-type hash tokens.
866  // Won't preserve the ID-ness of "id"-type hash tokens;
867  // use escapeIdent() for that.
868  string = ''+string;
869  var result = '';
870  var firstcode = string.charCodeAt(0);
871  for(var i = 0; i < string.length; i++) {
872    var code = string.charCodeAt(i);
873    if(code == 0x0) {
874      throw new InvalidCharacterError('Invalid character: the input contains U+0000.');
875    }
876
877    if(
878      code >= 0x80 ||
879      code == 0x2d ||
880      code == 0x5f ||
881      between(code, 0x30, 0x39) ||
882      between(code, 0x41, 0x5a) ||
883      between(code, 0x61, 0x7a)
884    ) {
885      result += string[i];
886    } else {
887      result += '\\' + code.toString(16) + ' ';
888    }
889  }
890  return result;
891}
892
893function escapeString(string) {
894  string = ''+string;
895  var result = '';
896  for(var i = 0; i < string.length; i++) {
897    var code = string.charCodeAt(i);
898
899    if(code == 0x0) {
900      throw new InvalidCharacterError('Invalid character: the input contains U+0000.');
901    }
902
903    if(between(code, 0x1, 0x1f) || code == 0x7f) {
904      result += '\\' + code.toString(16) + ' ';
905    } else if(code == 0x22 || code == 0x5c) {
906      result += '\\' + string[i];
907    } else {
908      result += string[i];
909    }
910  }
911  return result;
912}
913
914// Exportation.
915exports.tokenize = tokenize;
916exports.IdentToken = IdentToken;
917exports.FunctionToken = FunctionToken;
918exports.AtKeywordToken = AtKeywordToken;
919exports.HashToken = HashToken;
920exports.StringToken = StringToken;
921exports.BadStringToken = BadStringToken;
922exports.URLToken = URLToken;
923exports.BadURLToken = BadURLToken;
924exports.DelimToken = DelimToken;
925exports.NumberToken = NumberToken;
926exports.PercentageToken = PercentageToken;
927exports.DimensionToken = DimensionToken;
928exports.IncludeMatchToken = IncludeMatchToken;
929exports.DashMatchToken = DashMatchToken;
930exports.PrefixMatchToken = PrefixMatchToken;
931exports.SuffixMatchToken = SuffixMatchToken;
932exports.SubstringMatchToken = SubstringMatchToken;
933exports.ColumnToken = ColumnToken;
934exports.WhitespaceToken = WhitespaceToken;
935exports.CDOToken = CDOToken;
936exports.CDCToken = CDCToken;
937exports.ColonToken = ColonToken;
938exports.SemicolonToken = SemicolonToken;
939exports.CommaToken = CommaToken;
940exports.OpenParenToken = OpenParenToken;
941exports.CloseParenToken = CloseParenToken;
942exports.OpenSquareToken = OpenSquareToken;
943exports.CloseSquareToken = CloseSquareToken;
944exports.OpenCurlyToken = OpenCurlyToken;
945exports.CloseCurlyToken = CloseCurlyToken;
946exports.EOFToken = EOFToken;
947exports.CSSParserToken = CSSParserToken;
948exports.GroupingToken = GroupingToken;
949
950}));
951
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Internal on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)