PseudoClassSelector.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import {
  2. Ident,
  3. Function as FunctionToken,
  4. Colon,
  5. RightParenthesis
  6. } from '../../tokenizer/index.js';
  7. export const name = 'PseudoClassSelector';
  8. export const walkContext = 'function';
  9. export const structure = {
  10. name: String,
  11. children: [['Raw'], null]
  12. };
  13. // : [ <ident> | <function-token> <any-value>? ) ]
  14. export function parse() {
  15. const start = this.tokenStart;
  16. let children = null;
  17. let name;
  18. let nameLowerCase;
  19. this.eat(Colon);
  20. if (this.tokenType === FunctionToken) {
  21. name = this.consumeFunctionName();
  22. nameLowerCase = name.toLowerCase();
  23. if (this.lookupNonWSType(0) == RightParenthesis) {
  24. children = this.createList();
  25. } else if (hasOwnProperty.call(this.pseudo, nameLowerCase)) {
  26. this.skipSC();
  27. children = this.pseudo[nameLowerCase].call(this);
  28. this.skipSC();
  29. } else {
  30. children = this.createList();
  31. children.push(
  32. this.Raw(null, false)
  33. );
  34. }
  35. this.eat(RightParenthesis);
  36. } else {
  37. name = this.consume(Ident);
  38. }
  39. return {
  40. type: 'PseudoClassSelector',
  41. loc: this.getLocation(start, this.tokenStart),
  42. name,
  43. children
  44. };
  45. }
  46. export function generate(node) {
  47. this.token(Colon, ':');
  48. if (node.children === null) {
  49. this.token(Ident, node.name);
  50. } else {
  51. this.token(FunctionToken, node.name + '(');
  52. this.children(node);
  53. this.token(RightParenthesis, ')');
  54. }
  55. }