walkCssTokens.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /**
  7. * @typedef {Object} CssTokenCallbacks
  8. * @property {function(string, number): boolean} isSelector
  9. * @property {function(string, number, number, number, number): number=} url
  10. * @property {function(string, number, number): number=} string
  11. * @property {function(string, number, number): number=} leftParenthesis
  12. * @property {function(string, number, number): number=} rightParenthesis
  13. * @property {function(string, number, number): number=} pseudoFunction
  14. * @property {function(string, number, number): number=} function
  15. * @property {function(string, number, number): number=} pseudoClass
  16. * @property {function(string, number, number): number=} atKeyword
  17. * @property {function(string, number, number): number=} class
  18. * @property {function(string, number, number): number=} identifier
  19. * @property {function(string, number, number): number=} id
  20. * @property {function(string, number, number): number=} leftCurlyBracket
  21. * @property {function(string, number, number): number=} rightCurlyBracket
  22. * @property {function(string, number, number): number=} semicolon
  23. * @property {function(string, number, number): number=} comma
  24. */
  25. /** @typedef {function(string, number, CssTokenCallbacks): number} CharHandler */
  26. // spec: https://drafts.csswg.org/css-syntax/
  27. const CC_LINE_FEED = "\n".charCodeAt(0);
  28. const CC_CARRIAGE_RETURN = "\r".charCodeAt(0);
  29. const CC_FORM_FEED = "\f".charCodeAt(0);
  30. const CC_TAB = "\t".charCodeAt(0);
  31. const CC_SPACE = " ".charCodeAt(0);
  32. const CC_SOLIDUS = "/".charCodeAt(0);
  33. const CC_REVERSE_SOLIDUS = "\\".charCodeAt(0);
  34. const CC_ASTERISK = "*".charCodeAt(0);
  35. const CC_LEFT_PARENTHESIS = "(".charCodeAt(0);
  36. const CC_RIGHT_PARENTHESIS = ")".charCodeAt(0);
  37. const CC_LEFT_CURLY = "{".charCodeAt(0);
  38. const CC_RIGHT_CURLY = "}".charCodeAt(0);
  39. const CC_LEFT_SQUARE = "[".charCodeAt(0);
  40. const CC_RIGHT_SQUARE = "]".charCodeAt(0);
  41. const CC_QUOTATION_MARK = '"'.charCodeAt(0);
  42. const CC_APOSTROPHE = "'".charCodeAt(0);
  43. const CC_FULL_STOP = ".".charCodeAt(0);
  44. const CC_COLON = ":".charCodeAt(0);
  45. const CC_SEMICOLON = ";".charCodeAt(0);
  46. const CC_COMMA = ",".charCodeAt(0);
  47. const CC_PERCENTAGE = "%".charCodeAt(0);
  48. const CC_AT_SIGN = "@".charCodeAt(0);
  49. const CC_LOW_LINE = "_".charCodeAt(0);
  50. const CC_LOWER_A = "a".charCodeAt(0);
  51. const CC_LOWER_U = "u".charCodeAt(0);
  52. const CC_LOWER_E = "e".charCodeAt(0);
  53. const CC_LOWER_Z = "z".charCodeAt(0);
  54. const CC_UPPER_A = "A".charCodeAt(0);
  55. const CC_UPPER_E = "E".charCodeAt(0);
  56. const CC_UPPER_U = "U".charCodeAt(0);
  57. const CC_UPPER_Z = "Z".charCodeAt(0);
  58. const CC_0 = "0".charCodeAt(0);
  59. const CC_9 = "9".charCodeAt(0);
  60. const CC_NUMBER_SIGN = "#".charCodeAt(0);
  61. const CC_PLUS_SIGN = "+".charCodeAt(0);
  62. const CC_HYPHEN_MINUS = "-".charCodeAt(0);
  63. const CC_LESS_THAN_SIGN = "<".charCodeAt(0);
  64. const CC_GREATER_THAN_SIGN = ">".charCodeAt(0);
  65. const _isNewLine = cc => {
  66. return (
  67. cc === CC_LINE_FEED || cc === CC_CARRIAGE_RETURN || cc === CC_FORM_FEED
  68. );
  69. };
  70. /** @type {CharHandler} */
  71. const consumeSpace = (input, pos, callbacks) => {
  72. let cc;
  73. do {
  74. pos++;
  75. cc = input.charCodeAt(pos);
  76. } while (_isWhiteSpace(cc));
  77. return pos;
  78. };
  79. const _isWhiteSpace = cc => {
  80. return (
  81. cc === CC_LINE_FEED ||
  82. cc === CC_CARRIAGE_RETURN ||
  83. cc === CC_FORM_FEED ||
  84. cc === CC_TAB ||
  85. cc === CC_SPACE
  86. );
  87. };
  88. const _isIdentStartCodePoint = cc => {
  89. return (
  90. (cc >= CC_LOWER_A && cc <= CC_LOWER_Z) ||
  91. (cc >= CC_UPPER_A && cc <= CC_UPPER_Z) ||
  92. cc === CC_LOW_LINE ||
  93. cc >= 0x80
  94. );
  95. };
  96. /** @type {CharHandler} */
  97. const consumeDelimToken = (input, pos, callbacks) => {
  98. return pos + 1;
  99. };
  100. /** @type {CharHandler} */
  101. const consumeComments = (input, pos, callbacks) => {
  102. // If the next two input code point are U+002F SOLIDUS (/) followed by a U+002A
  103. // ASTERISK (*), consume them and all following code points up to and including
  104. // the first U+002A ASTERISK (*) followed by a U+002F SOLIDUS (/), or up to an
  105. // EOF code point. Return to the start of this step.
  106. //
  107. // If the preceding paragraph ended by consuming an EOF code point, this is a parse error.
  108. // But we are silent on errors.
  109. if (
  110. input.charCodeAt(pos) === CC_SOLIDUS &&
  111. input.charCodeAt(pos + 1) === CC_ASTERISK
  112. ) {
  113. pos += 1;
  114. while (pos < input.length) {
  115. if (
  116. input.charCodeAt(pos) === CC_ASTERISK &&
  117. input.charCodeAt(pos + 1) === CC_SOLIDUS
  118. ) {
  119. pos += 2;
  120. break;
  121. }
  122. pos++;
  123. }
  124. }
  125. return pos;
  126. };
  127. /** @type {function(number): CharHandler} */
  128. const consumeString = end => (input, pos, callbacks) => {
  129. const start = pos;
  130. pos = _consumeString(input, pos, end);
  131. if (callbacks.string !== undefined) {
  132. pos = callbacks.string(input, start, pos);
  133. }
  134. return pos;
  135. };
  136. const _consumeString = (input, pos, end) => {
  137. pos++;
  138. for (;;) {
  139. if (pos === input.length) return pos;
  140. const cc = input.charCodeAt(pos);
  141. if (cc === end) return pos + 1;
  142. if (_isNewLine(cc)) {
  143. // bad string
  144. return pos;
  145. }
  146. if (cc === CC_REVERSE_SOLIDUS) {
  147. // we don't need to fully parse the escaped code point
  148. // just skip over a potential new line
  149. pos++;
  150. if (pos === input.length) return pos;
  151. pos++;
  152. } else {
  153. pos++;
  154. }
  155. }
  156. };
  157. const _isIdentifierStartCode = cc => {
  158. return (
  159. cc === CC_LOW_LINE ||
  160. (cc >= CC_LOWER_A && cc <= CC_LOWER_Z) ||
  161. (cc >= CC_UPPER_A && cc <= CC_UPPER_Z) ||
  162. cc > 0x80
  163. );
  164. };
  165. const _isTwoCodePointsAreValidEscape = (first, second) => {
  166. if (first !== CC_REVERSE_SOLIDUS) return false;
  167. if (_isNewLine(second)) return false;
  168. return true;
  169. };
  170. const _isDigit = cc => {
  171. return cc >= CC_0 && cc <= CC_9;
  172. };
  173. const _startsIdentifier = (input, pos) => {
  174. const cc = input.charCodeAt(pos);
  175. if (cc === CC_HYPHEN_MINUS) {
  176. if (pos === input.length) return false;
  177. const cc = input.charCodeAt(pos + 1);
  178. if (cc === CC_HYPHEN_MINUS) return true;
  179. if (cc === CC_REVERSE_SOLIDUS) {
  180. const cc = input.charCodeAt(pos + 2);
  181. return !_isNewLine(cc);
  182. }
  183. return _isIdentifierStartCode(cc);
  184. }
  185. if (cc === CC_REVERSE_SOLIDUS) {
  186. const cc = input.charCodeAt(pos + 1);
  187. return !_isNewLine(cc);
  188. }
  189. return _isIdentifierStartCode(cc);
  190. };
  191. /** @type {CharHandler} */
  192. const consumeNumberSign = (input, pos, callbacks) => {
  193. const start = pos;
  194. pos++;
  195. if (pos === input.length) return pos;
  196. if (callbacks.isSelector(input, pos) && _startsIdentifier(input, pos)) {
  197. pos = _consumeIdentifier(input, pos);
  198. if (callbacks.id !== undefined) {
  199. return callbacks.id(input, start, pos);
  200. }
  201. }
  202. return pos;
  203. };
  204. /** @type {CharHandler} */
  205. const consumeMinus = (input, pos, callbacks) => {
  206. const start = pos;
  207. pos++;
  208. if (pos === input.length) return pos;
  209. const cc = input.charCodeAt(pos);
  210. // If the input stream starts with a number, reconsume the current input code point, consume a numeric token, and return it.
  211. if (cc === CC_FULL_STOP || _isDigit(cc)) {
  212. return consumeNumericToken(input, pos, callbacks);
  213. } else if (cc === CC_HYPHEN_MINUS) {
  214. pos++;
  215. if (pos === input.length) return pos;
  216. const cc = input.charCodeAt(pos);
  217. if (cc === CC_GREATER_THAN_SIGN) {
  218. return pos + 1;
  219. } else {
  220. pos = _consumeIdentifier(input, pos);
  221. if (callbacks.identifier !== undefined) {
  222. return callbacks.identifier(input, start, pos);
  223. }
  224. }
  225. } else if (cc === CC_REVERSE_SOLIDUS) {
  226. if (pos + 1 === input.length) return pos;
  227. const cc = input.charCodeAt(pos + 1);
  228. if (_isNewLine(cc)) return pos;
  229. pos = _consumeIdentifier(input, pos);
  230. if (callbacks.identifier !== undefined) {
  231. return callbacks.identifier(input, start, pos);
  232. }
  233. } else if (_isIdentifierStartCode(cc)) {
  234. pos = consumeOtherIdentifier(input, pos - 1, callbacks);
  235. }
  236. return pos;
  237. };
  238. /** @type {CharHandler} */
  239. const consumeDot = (input, pos, callbacks) => {
  240. const start = pos;
  241. pos++;
  242. if (pos === input.length) return pos;
  243. const cc = input.charCodeAt(pos);
  244. if (_isDigit(cc)) return consumeNumericToken(input, pos - 2, callbacks);
  245. if (!callbacks.isSelector(input, pos) || !_startsIdentifier(input, pos))
  246. return pos;
  247. pos = _consumeIdentifier(input, pos);
  248. if (callbacks.class !== undefined) return callbacks.class(input, start, pos);
  249. return pos;
  250. };
  251. /** @type {CharHandler} */
  252. const consumeNumericToken = (input, pos, callbacks) => {
  253. pos = _consumeNumber(input, pos);
  254. if (pos === input.length) return pos;
  255. if (_startsIdentifier(input, pos)) return _consumeIdentifier(input, pos);
  256. const cc = input.charCodeAt(pos);
  257. if (cc === CC_PERCENTAGE) return pos + 1;
  258. return pos;
  259. };
  260. /** @type {CharHandler} */
  261. const consumeOtherIdentifier = (input, pos, callbacks) => {
  262. const start = pos;
  263. pos = _consumeIdentifier(input, pos);
  264. if (
  265. pos !== input.length &&
  266. !callbacks.isSelector(input, pos) &&
  267. input.charCodeAt(pos) === CC_LEFT_PARENTHESIS
  268. ) {
  269. pos++;
  270. if (callbacks.function !== undefined) {
  271. return callbacks.function(input, start, pos);
  272. }
  273. } else {
  274. if (callbacks.identifier !== undefined) {
  275. return callbacks.identifier(input, start, pos);
  276. }
  277. }
  278. return pos;
  279. };
  280. /** @type {CharHandler} */
  281. const consumePotentialUrl = (input, pos, callbacks) => {
  282. const start = pos;
  283. pos = _consumeIdentifier(input, pos);
  284. const nextPos = pos + 1;
  285. if (
  286. pos === start + 3 &&
  287. input.slice(start, nextPos).toLowerCase() === "url("
  288. ) {
  289. pos++;
  290. let cc = input.charCodeAt(pos);
  291. while (_isWhiteSpace(cc)) {
  292. pos++;
  293. if (pos === input.length) return pos;
  294. cc = input.charCodeAt(pos);
  295. }
  296. if (cc === CC_QUOTATION_MARK || cc === CC_APOSTROPHE) {
  297. if (callbacks.function !== undefined) {
  298. return callbacks.function(input, start, nextPos);
  299. }
  300. return nextPos;
  301. } else {
  302. const contentStart = pos;
  303. let contentEnd;
  304. for (;;) {
  305. if (cc === CC_REVERSE_SOLIDUS) {
  306. pos++;
  307. if (pos === input.length) return pos;
  308. pos++;
  309. } else if (_isWhiteSpace(cc)) {
  310. contentEnd = pos;
  311. do {
  312. pos++;
  313. if (pos === input.length) return pos;
  314. cc = input.charCodeAt(pos);
  315. } while (_isWhiteSpace(cc));
  316. if (cc !== CC_RIGHT_PARENTHESIS) return pos;
  317. pos++;
  318. if (callbacks.url !== undefined) {
  319. return callbacks.url(input, start, pos, contentStart, contentEnd);
  320. }
  321. return pos;
  322. } else if (cc === CC_RIGHT_PARENTHESIS) {
  323. contentEnd = pos;
  324. pos++;
  325. if (callbacks.url !== undefined) {
  326. return callbacks.url(input, start, pos, contentStart, contentEnd);
  327. }
  328. return pos;
  329. } else if (cc === CC_LEFT_PARENTHESIS) {
  330. return pos;
  331. } else {
  332. pos++;
  333. }
  334. if (pos === input.length) return pos;
  335. cc = input.charCodeAt(pos);
  336. }
  337. }
  338. } else {
  339. if (callbacks.identifier !== undefined) {
  340. return callbacks.identifier(input, start, pos);
  341. }
  342. return pos;
  343. }
  344. };
  345. /** @type {CharHandler} */
  346. const consumePotentialPseudo = (input, pos, callbacks) => {
  347. const start = pos;
  348. pos++;
  349. if (!callbacks.isSelector(input, pos) || !_startsIdentifier(input, pos))
  350. return pos;
  351. pos = _consumeIdentifier(input, pos);
  352. let cc = input.charCodeAt(pos);
  353. if (cc === CC_LEFT_PARENTHESIS) {
  354. pos++;
  355. if (callbacks.pseudoFunction !== undefined) {
  356. return callbacks.pseudoFunction(input, start, pos);
  357. }
  358. return pos;
  359. }
  360. if (callbacks.pseudoClass !== undefined) {
  361. return callbacks.pseudoClass(input, start, pos);
  362. }
  363. return pos;
  364. };
  365. /** @type {CharHandler} */
  366. const consumeLeftParenthesis = (input, pos, callbacks) => {
  367. pos++;
  368. if (callbacks.leftParenthesis !== undefined) {
  369. return callbacks.leftParenthesis(input, pos - 1, pos);
  370. }
  371. return pos;
  372. };
  373. /** @type {CharHandler} */
  374. const consumeRightParenthesis = (input, pos, callbacks) => {
  375. pos++;
  376. if (callbacks.rightParenthesis !== undefined) {
  377. return callbacks.rightParenthesis(input, pos - 1, pos);
  378. }
  379. return pos;
  380. };
  381. /** @type {CharHandler} */
  382. const consumeLeftCurlyBracket = (input, pos, callbacks) => {
  383. pos++;
  384. if (callbacks.leftCurlyBracket !== undefined) {
  385. return callbacks.leftCurlyBracket(input, pos - 1, pos);
  386. }
  387. return pos;
  388. };
  389. /** @type {CharHandler} */
  390. const consumeRightCurlyBracket = (input, pos, callbacks) => {
  391. pos++;
  392. if (callbacks.rightCurlyBracket !== undefined) {
  393. return callbacks.rightCurlyBracket(input, pos - 1, pos);
  394. }
  395. return pos;
  396. };
  397. /** @type {CharHandler} */
  398. const consumeSemicolon = (input, pos, callbacks) => {
  399. pos++;
  400. if (callbacks.semicolon !== undefined) {
  401. return callbacks.semicolon(input, pos - 1, pos);
  402. }
  403. return pos;
  404. };
  405. /** @type {CharHandler} */
  406. const consumeComma = (input, pos, callbacks) => {
  407. pos++;
  408. if (callbacks.comma !== undefined) {
  409. return callbacks.comma(input, pos - 1, pos);
  410. }
  411. return pos;
  412. };
  413. const _consumeIdentifier = (input, pos) => {
  414. for (;;) {
  415. const cc = input.charCodeAt(pos);
  416. if (cc === CC_REVERSE_SOLIDUS) {
  417. pos++;
  418. if (pos === input.length) return pos;
  419. pos++;
  420. } else if (
  421. _isIdentifierStartCode(cc) ||
  422. _isDigit(cc) ||
  423. cc === CC_HYPHEN_MINUS
  424. ) {
  425. pos++;
  426. } else {
  427. return pos;
  428. }
  429. }
  430. };
  431. const _consumeNumber = (input, pos) => {
  432. pos++;
  433. if (pos === input.length) return pos;
  434. let cc = input.charCodeAt(pos);
  435. while (_isDigit(cc)) {
  436. pos++;
  437. if (pos === input.length) return pos;
  438. cc = input.charCodeAt(pos);
  439. }
  440. if (cc === CC_FULL_STOP && pos + 1 !== input.length) {
  441. const next = input.charCodeAt(pos + 1);
  442. if (_isDigit(next)) {
  443. pos += 2;
  444. cc = input.charCodeAt(pos);
  445. while (_isDigit(cc)) {
  446. pos++;
  447. if (pos === input.length) return pos;
  448. cc = input.charCodeAt(pos);
  449. }
  450. }
  451. }
  452. if (cc === CC_LOWER_E || cc === CC_UPPER_E) {
  453. if (pos + 1 !== input.length) {
  454. const next = input.charCodeAt(pos + 2);
  455. if (_isDigit(next)) {
  456. pos += 2;
  457. } else if (
  458. (next === CC_HYPHEN_MINUS || next === CC_PLUS_SIGN) &&
  459. pos + 2 !== input.length
  460. ) {
  461. const next = input.charCodeAt(pos + 2);
  462. if (_isDigit(next)) {
  463. pos += 3;
  464. } else {
  465. return pos;
  466. }
  467. } else {
  468. return pos;
  469. }
  470. }
  471. } else {
  472. return pos;
  473. }
  474. cc = input.charCodeAt(pos);
  475. while (_isDigit(cc)) {
  476. pos++;
  477. if (pos === input.length) return pos;
  478. cc = input.charCodeAt(pos);
  479. }
  480. return pos;
  481. };
  482. /** @type {CharHandler} */
  483. const consumeLessThan = (input, pos, callbacks) => {
  484. if (input.slice(pos + 1, pos + 4) === "!--") return pos + 4;
  485. return pos + 1;
  486. };
  487. const consumeAt = (input, pos, callbacks) => {
  488. const start = pos;
  489. pos++;
  490. if (pos === input.length) return pos;
  491. if (_startsIdentifier(input, pos)) {
  492. pos = _consumeIdentifier(input, pos);
  493. if (callbacks.atKeyword !== undefined) {
  494. pos = callbacks.atKeyword(input, start, pos);
  495. }
  496. }
  497. return pos;
  498. };
  499. /** @type {CharHandler} */
  500. const consumeReverseSolidus = (input, pos, callbacks) => {
  501. const start = pos;
  502. pos++;
  503. if (pos === input.length) return pos;
  504. // If the input stream starts with a valid escape, reconsume the current input code point, consume an ident-like token, and return it.
  505. if (
  506. _isTwoCodePointsAreValidEscape(
  507. input.charCodeAt(start),
  508. input.charCodeAt(pos)
  509. )
  510. ) {
  511. return consumeOtherIdentifier(input, pos - 1, callbacks);
  512. }
  513. // Otherwise, this is a parse error. Return a <delim-token> with its value set to the current input code point.
  514. return pos;
  515. };
  516. const CHAR_MAP = Array.from({ length: 0x80 }, (_, cc) => {
  517. // https://drafts.csswg.org/css-syntax/#consume-token
  518. switch (cc) {
  519. // whitespace
  520. case CC_LINE_FEED:
  521. case CC_CARRIAGE_RETURN:
  522. case CC_FORM_FEED:
  523. case CC_TAB:
  524. case CC_SPACE:
  525. return consumeSpace;
  526. // U+0022 QUOTATION MARK (")
  527. case CC_QUOTATION_MARK:
  528. return consumeString(cc);
  529. // U+0023 NUMBER SIGN (#)
  530. case CC_NUMBER_SIGN:
  531. return consumeNumberSign;
  532. // U+0027 APOSTROPHE (')
  533. case CC_APOSTROPHE:
  534. return consumeString(cc);
  535. // U+0028 LEFT PARENTHESIS (()
  536. case CC_LEFT_PARENTHESIS:
  537. return consumeLeftParenthesis;
  538. // U+0029 RIGHT PARENTHESIS ())
  539. case CC_RIGHT_PARENTHESIS:
  540. return consumeRightParenthesis;
  541. // U+002B PLUS SIGN (+)
  542. case CC_PLUS_SIGN:
  543. return consumeNumericToken;
  544. // U+002C COMMA (,)
  545. case CC_COMMA:
  546. return consumeComma;
  547. // U+002D HYPHEN-MINUS (-)
  548. case CC_HYPHEN_MINUS:
  549. return consumeMinus;
  550. // U+002E FULL STOP (.)
  551. case CC_FULL_STOP:
  552. return consumeDot;
  553. // U+003A COLON (:)
  554. case CC_COLON:
  555. return consumePotentialPseudo;
  556. // U+003B SEMICOLON (;)
  557. case CC_SEMICOLON:
  558. return consumeSemicolon;
  559. // U+003C LESS-THAN SIGN (<)
  560. case CC_LESS_THAN_SIGN:
  561. return consumeLessThan;
  562. // U+0040 COMMERCIAL AT (@)
  563. case CC_AT_SIGN:
  564. return consumeAt;
  565. // U+005B LEFT SQUARE BRACKET ([)
  566. case CC_LEFT_SQUARE:
  567. return consumeDelimToken;
  568. // U+005C REVERSE SOLIDUS (\)
  569. case CC_REVERSE_SOLIDUS:
  570. return consumeReverseSolidus;
  571. // U+005D RIGHT SQUARE BRACKET (])
  572. case CC_RIGHT_SQUARE:
  573. return consumeDelimToken;
  574. // U+007B LEFT CURLY BRACKET ({)
  575. case CC_LEFT_CURLY:
  576. return consumeLeftCurlyBracket;
  577. // U+007D RIGHT CURLY BRACKET (})
  578. case CC_RIGHT_CURLY:
  579. return consumeRightCurlyBracket;
  580. // Optimization
  581. case CC_LOWER_U:
  582. case CC_UPPER_U:
  583. return consumePotentialUrl;
  584. default:
  585. // digit
  586. if (_isDigit(cc)) return consumeNumericToken;
  587. // ident-start code point
  588. if (_isIdentStartCodePoint(cc)) {
  589. return consumeOtherIdentifier;
  590. }
  591. // EOF, but we don't have it
  592. // anything else
  593. return consumeDelimToken;
  594. }
  595. });
  596. /**
  597. * @param {string} input input css
  598. * @param {CssTokenCallbacks} callbacks callbacks
  599. * @returns {void}
  600. */
  601. module.exports = (input, callbacks) => {
  602. // This section describes how to consume a token from a stream of code points. It will return a single token of any type.
  603. let pos = 0;
  604. while (pos < input.length) {
  605. // Consume comments.
  606. pos = consumeComments(input, pos, callbacks);
  607. const cc = input.charCodeAt(pos);
  608. // Consume the next input code point.
  609. if (cc < 0x80) {
  610. pos = CHAR_MAP[cc](input, pos, callbacks);
  611. } else {
  612. pos++;
  613. }
  614. }
  615. };
  616. module.exports.eatComments = (input, pos) => {
  617. loop: for (;;) {
  618. const cc = input.charCodeAt(pos);
  619. if (cc === CC_SOLIDUS) {
  620. if (pos === input.length) return pos;
  621. let cc = input.charCodeAt(pos + 1);
  622. if (cc !== CC_ASTERISK) return pos;
  623. pos++;
  624. for (;;) {
  625. pos++;
  626. if (pos === input.length) return pos;
  627. cc = input.charCodeAt(pos);
  628. while (cc === CC_ASTERISK) {
  629. pos++;
  630. if (pos === input.length) return pos;
  631. cc = input.charCodeAt(pos);
  632. if (cc === CC_SOLIDUS) {
  633. pos++;
  634. continue loop;
  635. }
  636. }
  637. }
  638. }
  639. return pos;
  640. }
  641. };
  642. module.exports.eatWhitespaceAndComments = (input, pos) => {
  643. loop: for (;;) {
  644. const cc = input.charCodeAt(pos);
  645. if (cc === CC_SOLIDUS) {
  646. if (pos === input.length) return pos;
  647. let cc = input.charCodeAt(pos + 1);
  648. if (cc !== CC_ASTERISK) return pos;
  649. pos++;
  650. for (;;) {
  651. pos++;
  652. if (pos === input.length) return pos;
  653. cc = input.charCodeAt(pos);
  654. while (cc === CC_ASTERISK) {
  655. pos++;
  656. if (pos === input.length) return pos;
  657. cc = input.charCodeAt(pos);
  658. if (cc === CC_SOLIDUS) {
  659. pos++;
  660. continue loop;
  661. }
  662. }
  663. }
  664. } else if (_isWhiteSpace(cc)) {
  665. pos++;
  666. continue;
  667. }
  668. return pos;
  669. }
  670. };