selector.cjs 2.5 KB

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