You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
4.5 KiB

4 years ago
  1. var TYPE = require('../../tokenizer').TYPE;
  2. var IDENT = TYPE.Ident;
  3. var STRING = TYPE.String;
  4. var COLON = TYPE.Colon;
  5. var LEFTSQUAREBRACKET = TYPE.LeftSquareBracket;
  6. var RIGHTSQUAREBRACKET = TYPE.RightSquareBracket;
  7. var DOLLARSIGN = 0x0024; // U+0024 DOLLAR SIGN ($)
  8. var ASTERISK = 0x002A; // U+002A ASTERISK (*)
  9. var EQUALSSIGN = 0x003D; // U+003D EQUALS SIGN (=)
  10. var CIRCUMFLEXACCENT = 0x005E; // U+005E (^)
  11. var VERTICALLINE = 0x007C; // U+007C VERTICAL LINE (|)
  12. var TILDE = 0x007E; // U+007E TILDE (~)
  13. function getAttributeName() {
  14. if (this.scanner.eof) {
  15. this.error('Unexpected end of input');
  16. }
  17. var start = this.scanner.tokenStart;
  18. var expectIdent = false;
  19. var checkColon = true;
  20. if (this.scanner.isDelim(ASTERISK)) {
  21. expectIdent = true;
  22. checkColon = false;
  23. this.scanner.next();
  24. } else if (!this.scanner.isDelim(VERTICALLINE)) {
  25. this.eat(IDENT);
  26. }
  27. if (this.scanner.isDelim(VERTICALLINE)) {
  28. if (this.scanner.source.charCodeAt(this.scanner.tokenStart + 1) !== EQUALSSIGN) {
  29. this.scanner.next();
  30. this.eat(IDENT);
  31. } else if (expectIdent) {
  32. this.error('Identifier is expected', this.scanner.tokenEnd);
  33. }
  34. } else if (expectIdent) {
  35. this.error('Vertical line is expected');
  36. }
  37. if (checkColon && this.scanner.tokenType === COLON) {
  38. this.scanner.next();
  39. this.eat(IDENT);
  40. }
  41. return {
  42. type: 'Identifier',
  43. loc: this.getLocation(start, this.scanner.tokenStart),
  44. name: this.scanner.substrToCursor(start)
  45. };
  46. }
  47. function getOperator() {
  48. var start = this.scanner.tokenStart;
  49. var code = this.scanner.source.charCodeAt(start);
  50. if (code !== EQUALSSIGN && // =
  51. code !== TILDE && // ~=
  52. code !== CIRCUMFLEXACCENT && // ^=
  53. code !== DOLLARSIGN && // $=
  54. code !== ASTERISK && // *=
  55. code !== VERTICALLINE // |=
  56. ) {
  57. this.error('Attribute selector (=, ~=, ^=, $=, *=, |=) is expected');
  58. }
  59. this.scanner.next();
  60. if (code !== EQUALSSIGN) {
  61. if (!this.scanner.isDelim(EQUALSSIGN)) {
  62. this.error('Equal sign is expected');
  63. }
  64. this.scanner.next();
  65. }
  66. return this.scanner.substrToCursor(start);
  67. }
  68. // '[' <wq-name> ']'
  69. // '[' <wq-name> <attr-matcher> [ <string-token> | <ident-token> ] <attr-modifier>? ']'
  70. module.exports = {
  71. name: 'AttributeSelector',
  72. structure: {
  73. name: 'Identifier',
  74. matcher: [String, null],
  75. value: ['String', 'Identifier', null],
  76. flags: [String, null]
  77. },
  78. parse: function() {
  79. var start = this.scanner.tokenStart;
  80. var name;
  81. var matcher = null;
  82. var value = null;
  83. var flags = null;
  84. this.eat(LEFTSQUAREBRACKET);
  85. this.scanner.skipSC();
  86. name = getAttributeName.call(this);
  87. this.scanner.skipSC();
  88. if (this.scanner.tokenType !== RIGHTSQUAREBRACKET) {
  89. // avoid case `[name i]`
  90. if (this.scanner.tokenType !== IDENT) {
  91. matcher = getOperator.call(this);
  92. this.scanner.skipSC();
  93. value = this.scanner.tokenType === STRING
  94. ? this.String()
  95. : this.Identifier();
  96. this.scanner.skipSC();
  97. }
  98. // attribute flags
  99. if (this.scanner.tokenType === IDENT) {
  100. flags = this.scanner.getTokenValue();
  101. this.scanner.next();
  102. this.scanner.skipSC();
  103. }
  104. }
  105. this.eat(RIGHTSQUAREBRACKET);
  106. return {
  107. type: 'AttributeSelector',
  108. loc: this.getLocation(start, this.scanner.tokenStart),
  109. name: name,
  110. matcher: matcher,
  111. value: value,
  112. flags: flags
  113. };
  114. },
  115. generate: function(node) {
  116. var flagsPrefix = ' ';
  117. this.chunk('[');
  118. this.node(node.name);
  119. if (node.matcher !== null) {
  120. this.chunk(node.matcher);
  121. if (node.value !== null) {
  122. this.node(node.value);
  123. // space between string and flags is not required
  124. if (node.value.type === 'String') {
  125. flagsPrefix = '';
  126. }
  127. }
  128. }
  129. if (node.flags !== null) {
  130. this.chunk(flagsPrefix);
  131. this.chunk(node.flags);
  132. }
  133. this.chunk(']');
  134. }
  135. };