AttributeSelector.js 4.4 KB

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