default.js 2.7 KB

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