AnPlusB.js 7.7 KB

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