default.cjs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. 'use strict';
  2. const types = require('../../tokenizer/types.cjs');
  3. const NUMBERSIGN = 0x0023; // U+0023 NUMBER SIGN (#)
  4. const ASTERISK = 0x002A; // U+002A ASTERISK (*)
  5. const PLUSSIGN = 0x002B; // U+002B PLUS SIGN (+)
  6. const HYPHENMINUS = 0x002D; // U+002D HYPHEN-MINUS (-)
  7. const SOLIDUS = 0x002F; // U+002F SOLIDUS (/)
  8. const U = 0x0075; // U+0075 LATIN SMALL LETTER U (u)
  9. function defaultRecognizer(context) {
  10. switch (this.tokenType) {
  11. case types.Hash:
  12. return this.Hash();
  13. case types.Comma:
  14. return this.Operator();
  15. case types.LeftParenthesis:
  16. return this.Parentheses(this.readSequence, context.recognizer);
  17. case types.LeftSquareBracket:
  18. return this.Brackets(this.readSequence, context.recognizer);
  19. case types.String:
  20. return this.String();
  21. case types.Dimension:
  22. return this.Dimension();
  23. case types.Percentage:
  24. return this.Percentage();
  25. case types.Number:
  26. return this.Number();
  27. case types.Function:
  28. return this.cmpStr(this.tokenStart, this.tokenEnd, 'url(')
  29. ? this.Url()
  30. : this.Function(this.readSequence, context.recognizer);
  31. case types.Url:
  32. return this.Url();
  33. case types.Ident:
  34. // check for unicode range, it should start with u+ or U+
  35. if (this.cmpChar(this.tokenStart, U) &&
  36. this.cmpChar(this.tokenStart + 1, PLUSSIGN)) {
  37. return this.UnicodeRange();
  38. } else {
  39. return this.Identifier();
  40. }
  41. case types.Delim: {
  42. const code = this.charCodeAt(this.tokenStart);
  43. if (code === SOLIDUS ||
  44. code === ASTERISK ||
  45. code === PLUSSIGN ||
  46. code === HYPHENMINUS) {
  47. return this.Operator(); // TODO: replace with Delim
  48. }
  49. // TODO: produce a node with Delim node type
  50. if (code === NUMBERSIGN) {
  51. this.error('Hex or identifier is expected', this.tokenStart + 1);
  52. }
  53. break;
  54. }
  55. }
  56. }
  57. module.exports = defaultRecognizer;