selector.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import {
  2. Delim,
  3. Ident,
  4. Dimension,
  5. Percentage,
  6. Number as NumberToken,
  7. Hash,
  8. Colon,
  9. LeftSquareBracket
  10. } from '../../tokenizer/index.js';
  11. const NUMBERSIGN = 0x0023; // U+0023 NUMBER SIGN (#)
  12. const AMPERSAND = 0x0026; // U+0026 AMPERSAND (&)
  13. const ASTERISK = 0x002A; // U+002A ASTERISK (*)
  14. const PLUSSIGN = 0x002B; // U+002B PLUS SIGN (+)
  15. const SOLIDUS = 0x002F; // U+002F SOLIDUS (/)
  16. const FULLSTOP = 0x002E; // U+002E FULL STOP (.)
  17. const GREATERTHANSIGN = 0x003E; // U+003E GREATER-THAN SIGN (>)
  18. const VERTICALLINE = 0x007C; // U+007C VERTICAL LINE (|)
  19. const TILDE = 0x007E; // U+007E TILDE (~)
  20. function onWhiteSpace(next, children) {
  21. if (children.last !== null && children.last.type !== 'Combinator' &&
  22. next !== null && next.type !== 'Combinator') {
  23. children.push({ // FIXME: this.Combinator() should be used instead
  24. type: 'Combinator',
  25. loc: null,
  26. name: ' '
  27. });
  28. }
  29. }
  30. function getNode() {
  31. switch (this.tokenType) {
  32. case LeftSquareBracket:
  33. return this.AttributeSelector();
  34. case Hash:
  35. return this.IdSelector();
  36. case Colon:
  37. if (this.lookupType(1) === Colon) {
  38. return this.PseudoElementSelector();
  39. } else {
  40. return this.PseudoClassSelector();
  41. }
  42. case Ident:
  43. return this.TypeSelector();
  44. case NumberToken:
  45. case Percentage:
  46. return this.Percentage();
  47. case Dimension:
  48. // throws when .123ident
  49. if (this.charCodeAt(this.tokenStart) === FULLSTOP) {
  50. this.error('Identifier is expected', this.tokenStart + 1);
  51. }
  52. break;
  53. case Delim: {
  54. const code = this.charCodeAt(this.tokenStart);
  55. switch (code) {
  56. case PLUSSIGN:
  57. case GREATERTHANSIGN:
  58. case TILDE:
  59. case SOLIDUS: // /deep/
  60. return this.Combinator();
  61. case FULLSTOP:
  62. return this.ClassSelector();
  63. case ASTERISK:
  64. case VERTICALLINE:
  65. return this.TypeSelector();
  66. case NUMBERSIGN:
  67. return this.IdSelector();
  68. case AMPERSAND:
  69. return this.NestingSelector();
  70. }
  71. break;
  72. }
  73. }
  74. };
  75. export default {
  76. onWhiteSpace,
  77. getNode
  78. };