css-declarations-parser.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. /*
  2. * The code in this file is licensed under the CC0 license.
  3. *
  4. * http://creativecommons.org/publicdomain/zero/1.0/
  5. *
  6. * It is free to use for any purpose. No attribution, permission, or reproduction of this license is require
  7. */
  8. // Modified by Gildas Lormeau (ES5 -> ES6, removed unused code)
  9. // https://github.com/tabatkins/parse-css
  10. this.parseCss = this.parseCss || (() => {
  11. function between(num, first, last) { return num >= first && num <= last; }
  12. function digit(code) { return between(code, 0x30, 0x39); }
  13. function hexdigit(code) { return digit(code) || between(code, 0x41, 0x46) || between(code, 0x61, 0x66); }
  14. function uppercaseletter(code) { return between(code, 0x41, 0x5a); }
  15. function lowercaseletter(code) { return between(code, 0x61, 0x7a); }
  16. function letter(code) { return uppercaseletter(code) || lowercaseletter(code); }
  17. function nonascii(code) { return code >= 0x80; }
  18. function namestartchar(code) { return letter(code) || nonascii(code) || code == 0x5f; }
  19. function namechar(code) { return namestartchar(code) || digit(code) || code == 0x2d; }
  20. function nonprintable(code) { return between(code, 0, 8) || code == 0xb || between(code, 0xe, 0x1f) || code == 0x7f; }
  21. function newline(code) { return code == 0xa; }
  22. function whitespace(code) { return newline(code) || code == 9 || code == 0x20; }
  23. const maximumallowedcodepoint = 0x10ffff;
  24. const InvalidCharacterError = function (message) {
  25. this.message = message;
  26. };
  27. InvalidCharacterError.prototype = new Error;
  28. InvalidCharacterError.prototype.name = "InvalidCharacterError";
  29. function preprocess(str) {
  30. // Turn a string into an array of code points,
  31. // following the preprocessing cleanup rules.
  32. const codepoints = [];
  33. for (let i = 0; i < str.length; i++) {
  34. let code = str.charCodeAt(i);
  35. if (code == 0xd && str.charCodeAt(i + 1) == 0xa) {
  36. code = 0xa; i++;
  37. }
  38. if (code == 0xd || code == 0xc) code = 0xa;
  39. if (code == 0x0) code = 0xfffd;
  40. if (between(code, 0xd800, 0xdbff) && between(str.charCodeAt(i + 1), 0xdc00, 0xdfff)) {
  41. // Decode a surrogate pair into an astral codepoint.
  42. const lead = code - 0xd800;
  43. const trail = str.charCodeAt(i + 1) - 0xdc00;
  44. code = Math.pow(2, 20) + lead * Math.pow(2, 10) + trail;
  45. i++;
  46. }
  47. codepoints.push(code);
  48. }
  49. return codepoints;
  50. }
  51. function stringFromCode(code) {
  52. if (code <= 0xffff) return String.fromCharCode(code);
  53. // Otherwise, encode astral char as surrogate pair.
  54. code -= Math.pow(2, 20);
  55. const lead = Math.floor(code / Math.pow(2, 10)) + 0xd800;
  56. const trail = code % Math.pow(2, 10) + 0xdc00;
  57. return String.fromCharCode(lead) + String.fromCharCode(trail);
  58. }
  59. function tokenize(str) {
  60. str = preprocess(str);
  61. let i = -1;
  62. const tokens = [];
  63. let code;
  64. // Line number information.
  65. let line = 0;
  66. let column = 0;
  67. // The only use of lastLineLength is in reconsume().
  68. let lastLineLength = 0;
  69. const incrLineno = function () {
  70. line += 1;
  71. lastLineLength = column;
  72. column = 0;
  73. };
  74. const locStart = { line: line, column: column };
  75. const codepoint = function (i) {
  76. if (i >= str.length) {
  77. return -1;
  78. }
  79. return str[i];
  80. };
  81. const next = function (num) {
  82. if (num === undefined)
  83. num = 1;
  84. if (num > 3)
  85. throw "Spec Error: no more than three codepoints of lookahead.";
  86. return codepoint(i + num);
  87. };
  88. const consume = function (num) {
  89. if (num === undefined)
  90. num = 1;
  91. i += num;
  92. code = codepoint(i);
  93. if (newline(code)) incrLineno();
  94. else column += num;
  95. //console.log('Consume '+i+' '+String.fromCharCode(code) + ' 0x' + code.toString(16));
  96. return true;
  97. };
  98. const reconsume = function () {
  99. i -= 1;
  100. if (newline(code)) {
  101. line -= 1;
  102. column = lastLineLength;
  103. } else {
  104. column -= 1;
  105. }
  106. locStart.line = line;
  107. locStart.column = column;
  108. return true;
  109. };
  110. const eof = function (codepoint) {
  111. if (codepoint === undefined) codepoint = code;
  112. return codepoint == -1;
  113. };
  114. const donothing = function () { };
  115. const parseerror = function () { throw new Error("Parse error at index " + i + ", processing codepoint 0x" + code.toString(16) + "."); };
  116. const consumeAToken = function () {
  117. consumeComments();
  118. consume();
  119. if (whitespace(code)) {
  120. while (whitespace(next())) consume();
  121. return new WhitespaceToken;
  122. }
  123. else if (code == 0x22) return consumeAStringToken();
  124. else if (code == 0x23) {
  125. if (namechar(next()) || areAValidEscape(next(1), next(2))) {
  126. const token = new HashToken();
  127. if (wouldStartAnIdentifier(next(1), next(2), next(3))) token.type = "id";
  128. token.value = consumeAName();
  129. return token;
  130. } else {
  131. return new DelimToken(code);
  132. }
  133. }
  134. else if (code == 0x24) {
  135. if (next() == 0x3d) {
  136. consume();
  137. return new SuffixMatchToken();
  138. } else {
  139. return new DelimToken(code);
  140. }
  141. }
  142. else if (code == 0x27) return consumeAStringToken();
  143. else if (code == 0x28) return new OpenParenToken();
  144. else if (code == 0x29) return new CloseParenToken();
  145. else if (code == 0x2a) {
  146. if (next() == 0x3d) {
  147. consume();
  148. return new SubstringMatchToken();
  149. } else {
  150. return new DelimToken(code);
  151. }
  152. }
  153. else if (code == 0x2b) {
  154. if (startsWithANumber()) {
  155. reconsume();
  156. return consumeANumericToken();
  157. } else {
  158. return new DelimToken(code);
  159. }
  160. }
  161. else if (code == 0x2c) return new CommaToken();
  162. else if (code == 0x2d) {
  163. if (startsWithANumber()) {
  164. reconsume();
  165. return consumeANumericToken();
  166. } else if (next(1) == 0x2d && next(2) == 0x3e) {
  167. consume(2);
  168. return new CDCToken();
  169. } else if (startsWithAnIdentifier()) {
  170. reconsume();
  171. return consumeAnIdentlikeToken();
  172. } else {
  173. return new DelimToken(code);
  174. }
  175. }
  176. else if (code == 0x2e) {
  177. if (startsWithANumber()) {
  178. reconsume();
  179. return consumeANumericToken();
  180. } else {
  181. return new DelimToken(code);
  182. }
  183. }
  184. else if (code == 0x3a) return new ColonToken;
  185. else if (code == 0x3b) return new SemicolonToken;
  186. else if (code == 0x3c) {
  187. if (next(1) == 0x21 && next(2) == 0x2d && next(3) == 0x2d) {
  188. consume(3);
  189. return new CDOToken();
  190. } else {
  191. return new DelimToken(code);
  192. }
  193. }
  194. else if (code == 0x40) {
  195. return new DelimToken(code);
  196. }
  197. else if (code == 0x5b) return new OpenSquareToken();
  198. else if (code == 0x5c) {
  199. if (startsWithAValidEscape()) {
  200. reconsume();
  201. return consumeAnIdentlikeToken();
  202. } else {
  203. parseerror();
  204. return new DelimToken(code);
  205. }
  206. }
  207. else if (code == 0x5d) return new CloseSquareToken();
  208. else if (code == 0x5e) {
  209. if (next() == 0x3d) {
  210. consume();
  211. return new PrefixMatchToken();
  212. } else {
  213. return new DelimToken(code);
  214. }
  215. }
  216. else if (code == 0x7b) return new OpenCurlyToken();
  217. else if (code == 0x7c) {
  218. if (next() == 0x3d) {
  219. consume();
  220. return new DashMatchToken();
  221. } else if (next() == 0x7c) {
  222. consume();
  223. return new ColumnToken();
  224. } else {
  225. return new DelimToken(code);
  226. }
  227. }
  228. else if (code == 0x7d) return new CloseCurlyToken();
  229. else if (code == 0x7e) {
  230. if (next() == 0x3d) {
  231. consume();
  232. return new IncludeMatchToken();
  233. } else {
  234. return new DelimToken(code);
  235. }
  236. }
  237. else if (digit(code)) {
  238. reconsume();
  239. return consumeANumericToken();
  240. }
  241. else if (namestartchar(code)) {
  242. reconsume();
  243. return consumeAnIdentlikeToken();
  244. }
  245. else if (eof()) return new EOFToken();
  246. else return new DelimToken(code);
  247. };
  248. const consumeComments = function () {
  249. while (next(1) == 0x2f && next(2) == 0x2a) {
  250. consume(2);
  251. while (true) { // eslint-disable-line no-constant-condition
  252. consume();
  253. if (code == 0x2a && next() == 0x2f) {
  254. consume();
  255. break;
  256. } else if (eof()) {
  257. parseerror();
  258. return;
  259. }
  260. }
  261. }
  262. };
  263. const consumeANumericToken = function () {
  264. const num = consumeANumber();
  265. if (wouldStartAnIdentifier(next(1), next(2), next(3))) {
  266. const token = new DimensionToken();
  267. token.value = num.value;
  268. token.repr = num.repr;
  269. token.type = num.type;
  270. token.unit = consumeAName();
  271. return token;
  272. } else if (next() == 0x25) {
  273. consume();
  274. const token = new PercentageToken();
  275. token.value = num.value;
  276. token.repr = num.repr;
  277. return token;
  278. } else {
  279. const token = new NumberToken();
  280. token.value = num.value;
  281. token.repr = num.repr;
  282. token.type = num.type;
  283. return token;
  284. }
  285. };
  286. const consumeAnIdentlikeToken = function () {
  287. const str = consumeAName();
  288. if (str.toLowerCase() == "url" && next() == 0x28) {
  289. consume();
  290. while (whitespace(next(1)) && whitespace(next(2))) consume();
  291. if (next() == 0x22 || next() == 0x27) {
  292. return new FunctionToken(str);
  293. } else if (whitespace(next()) && (next(2) == 0x22 || next(2) == 0x27)) {
  294. return new FunctionToken(str);
  295. } else {
  296. return consumeAURLToken();
  297. }
  298. } else if (next() == 0x28) {
  299. consume();
  300. return new FunctionToken(str);
  301. } else {
  302. return new IdentToken(str);
  303. }
  304. };
  305. const consumeAStringToken = function (endingCodePoint) {
  306. if (endingCodePoint === undefined) endingCodePoint = code;
  307. let string = "";
  308. while (consume()) {
  309. if (code == endingCodePoint || eof()) {
  310. return new StringToken(string);
  311. } else if (newline(code)) {
  312. parseerror();
  313. reconsume();
  314. return new BadStringToken();
  315. } else if (code == 0x5c) {
  316. if (eof(next())) {
  317. donothing();
  318. } else if (newline(next())) {
  319. consume();
  320. } else {
  321. string += stringFromCode(consumeEscape());
  322. }
  323. } else {
  324. string += stringFromCode(code);
  325. }
  326. }
  327. };
  328. const consumeAURLToken = function () {
  329. const token = new URLToken("");
  330. while (whitespace(next())) consume();
  331. if (eof(next())) return token;
  332. while (consume()) {
  333. if (code == 0x29 || eof()) {
  334. return token;
  335. } else if (whitespace(code)) {
  336. while (whitespace(next())) consume();
  337. if (next() == 0x29 || eof(next())) {
  338. consume();
  339. return token;
  340. } else {
  341. consumeTheRemnantsOfABadURL();
  342. return new BadURLToken();
  343. }
  344. } else if (code == 0x22 || code == 0x27 || code == 0x28 || nonprintable(code)) {
  345. parseerror();
  346. consumeTheRemnantsOfABadURL();
  347. return new BadURLToken();
  348. } else if (code == 0x5c) {
  349. if (startsWithAValidEscape()) {
  350. token.value += stringFromCode(consumeEscape());
  351. } else {
  352. parseerror();
  353. consumeTheRemnantsOfABadURL();
  354. return new BadURLToken();
  355. }
  356. } else {
  357. token.value += stringFromCode(code);
  358. }
  359. }
  360. };
  361. const consumeEscape = function () {
  362. // Assume the the current character is the \
  363. // and the next code point is not a newline.
  364. consume();
  365. if (hexdigit(code)) {
  366. // Consume 1-6 hex digits
  367. const digits = [code];
  368. for (let total = 0; total < 5; total++) {
  369. if (hexdigit(next())) {
  370. consume();
  371. digits.push(code);
  372. } else {
  373. break;
  374. }
  375. }
  376. if (whitespace(next())) consume();
  377. let value = parseInt(digits.map(function (x) { return String.fromCharCode(x); }).join(""), 16);
  378. if (value > maximumallowedcodepoint) value = 0xfffd;
  379. return value;
  380. } else if (eof()) {
  381. return 0xfffd;
  382. } else {
  383. return code;
  384. }
  385. };
  386. const areAValidEscape = function (c1, c2) {
  387. if (c1 != 0x5c) return false;
  388. if (newline(c2)) return false;
  389. return true;
  390. };
  391. const startsWithAValidEscape = function () {
  392. return areAValidEscape(code, next());
  393. };
  394. const wouldStartAnIdentifier = function (c1, c2, c3) {
  395. if (c1 == 0x2d) {
  396. return namestartchar(c2) || c2 == 0x2d || areAValidEscape(c2, c3);
  397. } else if (namestartchar(c1)) {
  398. return true;
  399. } else if (c1 == 0x5c) {
  400. return areAValidEscape(c1, c2);
  401. } else {
  402. return false;
  403. }
  404. };
  405. const startsWithAnIdentifier = function () {
  406. return wouldStartAnIdentifier(code, next(1), next(2));
  407. };
  408. const wouldStartANumber = function (c1, c2, c3) {
  409. if (c1 == 0x2b || c1 == 0x2d) {
  410. if (digit(c2)) return true;
  411. if (c2 == 0x2e && digit(c3)) return true;
  412. return false;
  413. } else if (c1 == 0x2e) {
  414. if (digit(c2)) return true;
  415. return false;
  416. } else if (digit(c1)) {
  417. return true;
  418. } else {
  419. return false;
  420. }
  421. };
  422. const startsWithANumber = function () {
  423. return wouldStartANumber(code, next(1), next(2));
  424. };
  425. const consumeAName = function () {
  426. let result = "";
  427. while (consume()) {
  428. if (namechar(code)) {
  429. result += stringFromCode(code);
  430. } else if (startsWithAValidEscape()) {
  431. result += stringFromCode(consumeEscape());
  432. } else {
  433. reconsume();
  434. return result;
  435. }
  436. }
  437. };
  438. const consumeANumber = function () {
  439. let repr = [];
  440. let type = "integer";
  441. if (next() == 0x2b || next() == 0x2d) {
  442. consume();
  443. repr += stringFromCode(code);
  444. }
  445. while (digit(next())) {
  446. consume();
  447. repr += stringFromCode(code);
  448. }
  449. if (next(1) == 0x2e && digit(next(2))) {
  450. consume();
  451. repr += stringFromCode(code);
  452. consume();
  453. repr += stringFromCode(code);
  454. type = "number";
  455. while (digit(next())) {
  456. consume();
  457. repr += stringFromCode(code);
  458. }
  459. }
  460. const c1 = next(1), c2 = next(2), c3 = next(3);
  461. if ((c1 == 0x45 || c1 == 0x65) && digit(c2)) {
  462. consume();
  463. repr += stringFromCode(code);
  464. consume();
  465. repr += stringFromCode(code);
  466. type = "number";
  467. while (digit(next())) {
  468. consume();
  469. repr += stringFromCode(code);
  470. }
  471. } else if ((c1 == 0x45 || c1 == 0x65) && (c2 == 0x2b || c2 == 0x2d) && digit(c3)) {
  472. consume();
  473. repr += stringFromCode(code);
  474. consume();
  475. repr += stringFromCode(code);
  476. consume();
  477. repr += stringFromCode(code);
  478. type = "number";
  479. while (digit(next())) {
  480. consume();
  481. repr += stringFromCode(code);
  482. }
  483. }
  484. const value = convertAStringToANumber(repr);
  485. return { type: type, value: value, repr: repr };
  486. };
  487. const convertAStringToANumber = function (string) {
  488. // CSS's number rules are identical to JS, afaik.
  489. return +string;
  490. };
  491. const consumeTheRemnantsOfABadURL = function () {
  492. while (consume()) {
  493. if (code == 0x29 || eof()) {
  494. return;
  495. } else if (startsWithAValidEscape()) {
  496. consumeEscape();
  497. donothing();
  498. } else {
  499. donothing();
  500. }
  501. }
  502. };
  503. let iterationCount = 0;
  504. while (!eof(next())) {
  505. tokens.push(consumeAToken());
  506. iterationCount++;
  507. if (iterationCount > str.length * 2) return "I'm infinite-looping!";
  508. }
  509. return tokens;
  510. }
  511. function CSSParserToken() { throw "Abstract Base Class"; }
  512. CSSParserToken.prototype.toString = function () { return this.tokenType; };
  513. function BadStringToken() { return this; }
  514. BadStringToken.prototype = Object.create(CSSParserToken.prototype);
  515. BadStringToken.prototype.tokenType = "BADSTRING";
  516. function BadURLToken() { return this; }
  517. BadURLToken.prototype = Object.create(CSSParserToken.prototype);
  518. BadURLToken.prototype.tokenType = "BADURL";
  519. function WhitespaceToken() { return this; }
  520. WhitespaceToken.prototype = Object.create(CSSParserToken.prototype);
  521. WhitespaceToken.prototype.tokenType = "WHITESPACE";
  522. WhitespaceToken.prototype.toString = function () { return "WS"; };
  523. function CDOToken() { return this; }
  524. CDOToken.prototype = Object.create(CSSParserToken.prototype);
  525. CDOToken.prototype.tokenType = "CDO";
  526. function CDCToken() { return this; }
  527. CDCToken.prototype = Object.create(CSSParserToken.prototype);
  528. CDCToken.prototype.tokenType = "CDC";
  529. function ColonToken() { return this; }
  530. ColonToken.prototype = Object.create(CSSParserToken.prototype);
  531. ColonToken.prototype.tokenType = ":";
  532. function SemicolonToken() { return this; }
  533. SemicolonToken.prototype = Object.create(CSSParserToken.prototype);
  534. SemicolonToken.prototype.tokenType = ";";
  535. function CommaToken() { return this; }
  536. CommaToken.prototype = Object.create(CSSParserToken.prototype);
  537. CommaToken.prototype.tokenType = ",";
  538. function GroupingToken() { throw "Abstract Base Class"; }
  539. GroupingToken.prototype = Object.create(CSSParserToken.prototype);
  540. function OpenCurlyToken() { this.value = "{"; this.mirror = "}"; return this; }
  541. OpenCurlyToken.prototype = Object.create(GroupingToken.prototype);
  542. OpenCurlyToken.prototype.tokenType = "{";
  543. function CloseCurlyToken() { this.value = "}"; this.mirror = "{"; return this; }
  544. CloseCurlyToken.prototype = Object.create(GroupingToken.prototype);
  545. CloseCurlyToken.prototype.tokenType = "}";
  546. function OpenSquareToken() { this.value = "["; this.mirror = "]"; return this; }
  547. OpenSquareToken.prototype = Object.create(GroupingToken.prototype);
  548. OpenSquareToken.prototype.tokenType = "[";
  549. function CloseSquareToken() { this.value = "]"; this.mirror = "["; return this; }
  550. CloseSquareToken.prototype = Object.create(GroupingToken.prototype);
  551. CloseSquareToken.prototype.tokenType = "]";
  552. function OpenParenToken() { this.value = "("; this.mirror = ")"; return this; }
  553. OpenParenToken.prototype = Object.create(GroupingToken.prototype);
  554. OpenParenToken.prototype.tokenType = "(";
  555. function CloseParenToken() { this.value = ")"; this.mirror = "("; return this; }
  556. CloseParenToken.prototype = Object.create(GroupingToken.prototype);
  557. CloseParenToken.prototype.tokenType = ")";
  558. function IncludeMatchToken() { return this; }
  559. IncludeMatchToken.prototype = Object.create(CSSParserToken.prototype);
  560. IncludeMatchToken.prototype.tokenType = "~=";
  561. function DashMatchToken() { return this; }
  562. DashMatchToken.prototype = Object.create(CSSParserToken.prototype);
  563. DashMatchToken.prototype.tokenType = "|=";
  564. function PrefixMatchToken() { return this; }
  565. PrefixMatchToken.prototype = Object.create(CSSParserToken.prototype);
  566. PrefixMatchToken.prototype.tokenType = "^=";
  567. function SuffixMatchToken() { return this; }
  568. SuffixMatchToken.prototype = Object.create(CSSParserToken.prototype);
  569. SuffixMatchToken.prototype.tokenType = "$=";
  570. function SubstringMatchToken() { return this; }
  571. SubstringMatchToken.prototype = Object.create(CSSParserToken.prototype);
  572. SubstringMatchToken.prototype.tokenType = "*=";
  573. function ColumnToken() { return this; }
  574. ColumnToken.prototype = Object.create(CSSParserToken.prototype);
  575. ColumnToken.prototype.tokenType = "||";
  576. function EOFToken() { return this; }
  577. EOFToken.prototype = Object.create(CSSParserToken.prototype);
  578. EOFToken.prototype.tokenType = "EOF";
  579. function DelimToken(code) {
  580. this.value = stringFromCode(code);
  581. return this;
  582. }
  583. DelimToken.prototype = Object.create(CSSParserToken.prototype);
  584. DelimToken.prototype.tokenType = "DELIM";
  585. DelimToken.prototype.toString = function () { return "DELIM(" + this.value + ")"; };
  586. function StringValuedToken() { throw "Abstract Base Class"; }
  587. StringValuedToken.prototype = Object.create(CSSParserToken.prototype);
  588. StringValuedToken.prototype.ASCIIMatch = function (str) {
  589. return this.value.toLowerCase() == str.toLowerCase();
  590. };
  591. function IdentToken(val) {
  592. this.value = val;
  593. }
  594. IdentToken.prototype = Object.create(StringValuedToken.prototype);
  595. IdentToken.prototype.tokenType = "IDENT";
  596. IdentToken.prototype.toString = function () { return "IDENT(" + this.value + ")"; };
  597. function FunctionToken(val) {
  598. this.value = val;
  599. this.mirror = ")";
  600. }
  601. FunctionToken.prototype = Object.create(StringValuedToken.prototype);
  602. FunctionToken.prototype.tokenType = "FUNCTION";
  603. FunctionToken.prototype.toString = function () { return "FUNCTION(" + this.value + ")"; };
  604. function HashToken(val) {
  605. this.value = val;
  606. this.type = "unrestricted";
  607. }
  608. HashToken.prototype = Object.create(StringValuedToken.prototype);
  609. HashToken.prototype.tokenType = "HASH";
  610. HashToken.prototype.toString = function () { return "HASH(" + this.value + ")"; };
  611. function StringToken(val) {
  612. this.value = val;
  613. }
  614. StringToken.prototype = Object.create(StringValuedToken.prototype);
  615. StringToken.prototype.tokenType = "STRING";
  616. function URLToken(val) {
  617. this.value = val;
  618. }
  619. URLToken.prototype = Object.create(StringValuedToken.prototype);
  620. URLToken.prototype.tokenType = "URL";
  621. URLToken.prototype.toString = function () { return "URL(" + this.value + ")"; };
  622. function NumberToken() {
  623. this.value = null;
  624. this.type = "integer";
  625. this.repr = "";
  626. }
  627. NumberToken.prototype = Object.create(CSSParserToken.prototype);
  628. NumberToken.prototype.tokenType = "NUMBER";
  629. NumberToken.prototype.toString = function () {
  630. if (this.type == "integer")
  631. return "INT(" + this.value + ")";
  632. return "NUMBER(" + this.value + ")";
  633. };
  634. function PercentageToken() {
  635. this.value = null;
  636. this.repr = "";
  637. }
  638. PercentageToken.prototype = Object.create(CSSParserToken.prototype);
  639. PercentageToken.prototype.tokenType = "PERCENTAGE";
  640. PercentageToken.prototype.toString = function () { return "PERCENTAGE(" + this.value + ")"; };
  641. function DimensionToken() {
  642. this.value = null;
  643. this.type = "integer";
  644. this.repr = "";
  645. this.unit = "";
  646. }
  647. DimensionToken.prototype = Object.create(CSSParserToken.prototype);
  648. DimensionToken.prototype.tokenType = "DIMENSION";
  649. DimensionToken.prototype.toString = function () { return "DIM(" + this.value + "," + this.unit + ")"; };
  650. // ---
  651. function TokenStream(tokens) {
  652. // Assume that tokens is an array.
  653. this.tokens = tokens;
  654. this.i = -1;
  655. }
  656. TokenStream.prototype.tokenAt = function (i) {
  657. if (i < this.tokens.length)
  658. return this.tokens[i];
  659. return new EOFToken();
  660. };
  661. TokenStream.prototype.consume = function (num) {
  662. if (num === undefined) num = 1;
  663. this.i += num;
  664. this.token = this.tokenAt(this.i);
  665. //console.log(this.i, this.token);
  666. return true;
  667. };
  668. TokenStream.prototype.next = function () {
  669. return this.tokenAt(this.i + 1);
  670. };
  671. TokenStream.prototype.reconsume = function () {
  672. this.i--;
  673. };
  674. function parseerror(s, msg) {
  675. throw new Error("Parse error at token " + s.i + ": " + s.token + ".\n" + msg);
  676. }
  677. function donothing() { return true; }
  678. function consumeAListOfDeclarations(s) {
  679. const decls = [];
  680. while (s.consume()) {
  681. if (s.token instanceof WhitespaceToken || s.token instanceof SemicolonToken) {
  682. donothing();
  683. } else if (s.token instanceof EOFToken) {
  684. return decls;
  685. } else if (s.token instanceof IdentToken) {
  686. const temp = [s.token];
  687. while (!(s.next() instanceof SemicolonToken || s.next() instanceof EOFToken))
  688. temp.push(consumeAComponentValue(s));
  689. let decl = consumeADeclaration(new TokenStream(temp));
  690. if (decl) decls.push(decl);
  691. } else {
  692. parseerror(s);
  693. s.reconsume();
  694. while (!(s.next() instanceof SemicolonToken || s.next() instanceof EOFToken))
  695. consumeAComponentValue(s);
  696. }
  697. }
  698. }
  699. function consumeADeclaration(s) {
  700. // Assumes that the next input token will be an ident token.
  701. s.consume();
  702. const decl = new Declaration(s.token.value);
  703. while (s.next() instanceof WhitespaceToken) s.consume();
  704. if (!(s.next() instanceof ColonToken)) {
  705. parseerror(s);
  706. return;
  707. } else {
  708. s.consume();
  709. }
  710. while (!(s.next() instanceof EOFToken)) {
  711. decl.value.push(consumeAComponentValue(s));
  712. }
  713. let foundImportant = false;
  714. for (let i = decl.value.length - 1; i >= 0; i--) {
  715. if (decl.value[i] instanceof WhitespaceToken) {
  716. continue;
  717. } else if (decl.value[i] instanceof IdentToken && decl.value[i].ASCIIMatch("important")) {
  718. foundImportant = true;
  719. } else if (foundImportant && decl.value[i] instanceof DelimToken && decl.value[i].value == "!") {
  720. decl.value.splice(i, decl.value.length);
  721. decl.important = true;
  722. break;
  723. } else {
  724. break;
  725. }
  726. }
  727. return decl;
  728. }
  729. function consumeAComponentValue(s) {
  730. s.consume();
  731. if (s.token instanceof FunctionToken)
  732. return consumeAFunction(s);
  733. return s.token;
  734. }
  735. function consumeAFunction(s) {
  736. const func = new Func(s.token.value);
  737. while (s.consume()) {
  738. if (s.token instanceof EOFToken || s.token instanceof CloseParenToken)
  739. return func;
  740. else {
  741. s.reconsume();
  742. func.value.push(consumeAComponentValue(s));
  743. }
  744. }
  745. }
  746. function normalizeInput(input) {
  747. if (typeof input == "string")
  748. return new TokenStream(tokenize(input));
  749. else throw SyntaxError(input);
  750. }
  751. function parseAListOfDeclarations(s) {
  752. s = normalizeInput(s);
  753. return consumeAListOfDeclarations(s);
  754. }
  755. function CSSParserRule() { throw "Abstract Base Class"; }
  756. function Declaration(name) {
  757. this.name = name;
  758. this.value = [];
  759. this.important = false;
  760. return this;
  761. }
  762. Declaration.prototype = Object.create(CSSParserRule.prototype);
  763. Declaration.prototype.type = "DECLARATION";
  764. function Func(name) {
  765. this.name = name;
  766. this.value = [];
  767. return this;
  768. }
  769. Func.prototype = Object.create(CSSParserRule.prototype);
  770. Func.prototype.type = "FUNCTION";
  771. // Exportation.
  772. return {
  773. parseAListOfDeclarations: parseAListOfDeclarations
  774. };
  775. })();