AnPlusB.cjs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. 'use strict';
  2. const types = require('../../tokenizer/types.cjs');
  3. const charCodeDefinitions = require('../../tokenizer/char-code-definitions.cjs');
  4. const PLUSSIGN = 0x002B; // U+002B PLUS SIGN (+)
  5. const HYPHENMINUS = 0x002D; // U+002D HYPHEN-MINUS (-)
  6. const N = 0x006E; // U+006E LATIN SMALL LETTER N (n)
  7. const DISALLOW_SIGN = true;
  8. const ALLOW_SIGN = false;
  9. function checkInteger(offset, disallowSign) {
  10. let pos = this.tokenStart + offset;
  11. const code = this.charCodeAt(pos);
  12. if (code === PLUSSIGN || code === HYPHENMINUS) {
  13. if (disallowSign) {
  14. this.error('Number sign is not allowed');
  15. }
  16. pos++;
  17. }
  18. for (; pos < this.tokenEnd; pos++) {
  19. if (!charCodeDefinitions.isDigit(this.charCodeAt(pos))) {
  20. this.error('Integer is expected', pos);
  21. }
  22. }
  23. }
  24. function checkTokenIsInteger(disallowSign) {
  25. return checkInteger.call(this, 0, disallowSign);
  26. }
  27. function expectCharCode(offset, code) {
  28. if (!this.cmpChar(this.tokenStart + offset, code)) {
  29. let msg = '';
  30. switch (code) {
  31. case N:
  32. msg = 'N is expected';
  33. break;
  34. case HYPHENMINUS:
  35. msg = 'HyphenMinus is expected';
  36. break;
  37. }
  38. this.error(msg, this.tokenStart + offset);
  39. }
  40. }
  41. // ... <signed-integer>
  42. // ... ['+' | '-'] <signless-integer>
  43. function consumeB() {
  44. let offset = 0;
  45. let sign = 0;
  46. let type = this.tokenType;
  47. while (type === types.WhiteSpace || type === types.Comment) {
  48. type = this.lookupType(++offset);
  49. }
  50. if (type !== types.Number) {
  51. if (this.isDelim(PLUSSIGN, offset) ||
  52. this.isDelim(HYPHENMINUS, offset)) {
  53. sign = this.isDelim(PLUSSIGN, offset) ? PLUSSIGN : HYPHENMINUS;
  54. do {
  55. type = this.lookupType(++offset);
  56. } while (type === types.WhiteSpace || type === types.Comment);
  57. if (type !== types.Number) {
  58. this.skip(offset);
  59. checkTokenIsInteger.call(this, DISALLOW_SIGN);
  60. }
  61. } else {
  62. return null;
  63. }
  64. }
  65. if (offset > 0) {
  66. this.skip(offset);
  67. }
  68. if (sign === 0) {
  69. type = this.charCodeAt(this.tokenStart);
  70. if (type !== PLUSSIGN && type !== HYPHENMINUS) {
  71. this.error('Number sign is expected');
  72. }
  73. }
  74. checkTokenIsInteger.call(this, sign !== 0);
  75. return sign === HYPHENMINUS ? '-' + this.consume(types.Number) : this.consume(types.Number);
  76. }
  77. // An+B microsyntax https://www.w3.org/TR/css-syntax-3/#anb
  78. const name = 'AnPlusB';
  79. const structure = {
  80. a: [String, null],
  81. b: [String, null]
  82. };
  83. function parse() {
  84. /* eslint-disable brace-style*/
  85. const start = this.tokenStart;
  86. let a = null;
  87. let b = null;
  88. // <integer>
  89. if (this.tokenType === types.Number) {
  90. checkTokenIsInteger.call(this, ALLOW_SIGN);
  91. b = this.consume(types.Number);
  92. }
  93. // -n
  94. // -n <signed-integer>
  95. // -n ['+' | '-'] <signless-integer>
  96. // -n- <signless-integer>
  97. // <dashndashdigit-ident>
  98. else if (this.tokenType === types.Ident && this.cmpChar(this.tokenStart, HYPHENMINUS)) {
  99. a = '-1';
  100. expectCharCode.call(this, 1, N);
  101. switch (this.tokenEnd - this.tokenStart) {
  102. // -n
  103. // -n <signed-integer>
  104. // -n ['+' | '-'] <signless-integer>
  105. case 2:
  106. this.next();
  107. b = consumeB.call(this);
  108. break;
  109. // -n- <signless-integer>
  110. case 3:
  111. expectCharCode.call(this, 2, HYPHENMINUS);
  112. this.next();
  113. this.skipSC();
  114. checkTokenIsInteger.call(this, DISALLOW_SIGN);
  115. b = '-' + this.consume(types.Number);
  116. break;
  117. // <dashndashdigit-ident>
  118. default:
  119. expectCharCode.call(this, 2, HYPHENMINUS);
  120. checkInteger.call(this, 3, DISALLOW_SIGN);
  121. this.next();
  122. b = this.substrToCursor(start + 2);
  123. }
  124. }
  125. // '+'? n
  126. // '+'? n <signed-integer>
  127. // '+'? n ['+' | '-'] <signless-integer>
  128. // '+'? n- <signless-integer>
  129. // '+'? <ndashdigit-ident>
  130. else if (this.tokenType === types.Ident || (this.isDelim(PLUSSIGN) && this.lookupType(1) === types.Ident)) {
  131. let sign = 0;
  132. a = '1';
  133. // just ignore a plus
  134. if (this.isDelim(PLUSSIGN)) {
  135. sign = 1;
  136. this.next();
  137. }
  138. expectCharCode.call(this, 0, N);
  139. switch (this.tokenEnd - this.tokenStart) {
  140. // '+'? n
  141. // '+'? n <signed-integer>
  142. // '+'? n ['+' | '-'] <signless-integer>
  143. case 1:
  144. this.next();
  145. b = consumeB.call(this);
  146. break;
  147. // '+'? n- <signless-integer>
  148. case 2:
  149. expectCharCode.call(this, 1, HYPHENMINUS);
  150. this.next();
  151. this.skipSC();
  152. checkTokenIsInteger.call(this, DISALLOW_SIGN);
  153. b = '-' + this.consume(types.Number);
  154. break;
  155. // '+'? <ndashdigit-ident>
  156. default:
  157. expectCharCode.call(this, 1, HYPHENMINUS);
  158. checkInteger.call(this, 2, DISALLOW_SIGN);
  159. this.next();
  160. b = this.substrToCursor(start + sign + 1);
  161. }
  162. }
  163. // <ndashdigit-dimension>
  164. // <ndash-dimension> <signless-integer>
  165. // <n-dimension>
  166. // <n-dimension> <signed-integer>
  167. // <n-dimension> ['+' | '-'] <signless-integer>
  168. else if (this.tokenType === types.Dimension) {
  169. const code = this.charCodeAt(this.tokenStart);
  170. const sign = code === PLUSSIGN || code === HYPHENMINUS;
  171. let i = this.tokenStart + sign;
  172. for (; i < this.tokenEnd; i++) {
  173. if (!charCodeDefinitions.isDigit(this.charCodeAt(i))) {
  174. break;
  175. }
  176. }
  177. if (i === this.tokenStart + sign) {
  178. this.error('Integer is expected', this.tokenStart + sign);
  179. }
  180. expectCharCode.call(this, i - this.tokenStart, N);
  181. a = this.substring(start, i);
  182. // <n-dimension>
  183. // <n-dimension> <signed-integer>
  184. // <n-dimension> ['+' | '-'] <signless-integer>
  185. if (i + 1 === this.tokenEnd) {
  186. this.next();
  187. b = consumeB.call(this);
  188. } else {
  189. expectCharCode.call(this, i - this.tokenStart + 1, HYPHENMINUS);
  190. // <ndash-dimension> <signless-integer>
  191. if (i + 2 === this.tokenEnd) {
  192. this.next();
  193. this.skipSC();
  194. checkTokenIsInteger.call(this, DISALLOW_SIGN);
  195. b = '-' + this.consume(types.Number);
  196. }
  197. // <ndashdigit-dimension>
  198. else {
  199. checkInteger.call(this, i - this.tokenStart + 2, DISALLOW_SIGN);
  200. this.next();
  201. b = this.substrToCursor(i + 1);
  202. }
  203. }
  204. } else {
  205. this.error();
  206. }
  207. if (a !== null && a.charCodeAt(0) === PLUSSIGN) {
  208. a = a.substr(1);
  209. }
  210. if (b !== null && b.charCodeAt(0) === PLUSSIGN) {
  211. b = b.substr(1);
  212. }
  213. return {
  214. type: 'AnPlusB',
  215. loc: this.getLocation(start, this.tokenStart),
  216. a,
  217. b
  218. };
  219. }
  220. function generate(node) {
  221. if (node.a) {
  222. const a =
  223. node.a === '+1' && 'n' ||
  224. node.a === '1' && 'n' ||
  225. node.a === '-1' && '-n' ||
  226. node.a + 'n';
  227. if (node.b) {
  228. const b = node.b[0] === '-' || node.b[0] === '+'
  229. ? node.b
  230. : '+' + node.b;
  231. this.tokenize(a + b);
  232. } else {
  233. this.tokenize(a);
  234. }
  235. } else {
  236. this.tokenize(node.b);
  237. }
  238. }
  239. exports.generate = generate;
  240. exports.name = name;
  241. exports.parse = parse;
  242. exports.structure = structure;