1
0

parse-css.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  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)
  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. // function badescape(code) { return newline(code) || isNaN(code); }
  24. const maximumallowedcodepoint = 0x10ffff;
  25. const InvalidCharacterError = function (message) {
  26. this.message = message;
  27. };
  28. InvalidCharacterError.prototype = new Error;
  29. InvalidCharacterError.prototype.name = "InvalidCharacterError";
  30. function preprocess(str) {
  31. // Turn a string into an array of code points,
  32. // following the preprocessing cleanup rules.
  33. const codepoints = [];
  34. for (let i = 0; i < str.length; i++) {
  35. let code = str.charCodeAt(i);
  36. if (code == 0xd && str.charCodeAt(i + 1) == 0xa) {
  37. code = 0xa; i++;
  38. }
  39. if (code == 0xd || code == 0xc) code = 0xa;
  40. if (code == 0x0) code = 0xfffd;
  41. if (between(code, 0xd800, 0xdbff) && between(str.charCodeAt(i + 1), 0xdc00, 0xdfff)) {
  42. // Decode a surrogate pair into an astral codepoint.
  43. const lead = code - 0xd800;
  44. const trail = str.charCodeAt(i + 1) - 0xdc00;
  45. code = Math.pow(2, 20) + lead * Math.pow(2, 10) + trail;
  46. i++;
  47. }
  48. codepoints.push(code);
  49. }
  50. return codepoints;
  51. }
  52. function stringFromCode(code) {
  53. if (code <= 0xffff) return String.fromCharCode(code);
  54. // Otherwise, encode astral char as surrogate pair.
  55. code -= Math.pow(2, 20);
  56. const lead = Math.floor(code / Math.pow(2, 10)) + 0xd800;
  57. const trail = code % Math.pow(2, 10) + 0xdc00;
  58. return String.fromCharCode(lead) + String.fromCharCode(trail);
  59. }
  60. function tokenize(str) {
  61. str = preprocess(str);
  62. let i = -1;
  63. const tokens = [];
  64. let code;
  65. // Line number information.
  66. let line = 0;
  67. let column = 0;
  68. // The only use of lastLineLength is in reconsume().
  69. let lastLineLength = 0;
  70. const incrLineno = function () {
  71. line += 1;
  72. lastLineLength = column;
  73. column = 0;
  74. };
  75. const locStart = { line: line, column: column };
  76. const codepoint = function (i) {
  77. if (i >= str.length) {
  78. return -1;
  79. }
  80. return str[i];
  81. };
  82. const next = function (num) {
  83. if (num === undefined)
  84. num = 1;
  85. if (num > 3)
  86. throw "Spec Error: no more than three codepoints of lookahead.";
  87. return codepoint(i + num);
  88. };
  89. const consume = function (num) {
  90. if (num === undefined)
  91. num = 1;
  92. i += num;
  93. code = codepoint(i);
  94. if (newline(code)) incrLineno();
  95. else column += num;
  96. //console.log('Consume '+i+' '+String.fromCharCode(code) + ' 0x' + code.toString(16));
  97. return true;
  98. };
  99. const reconsume = function () {
  100. i -= 1;
  101. if (newline(code)) {
  102. line -= 1;
  103. column = lastLineLength;
  104. } else {
  105. column -= 1;
  106. }
  107. locStart.line = line;
  108. locStart.column = column;
  109. return true;
  110. };
  111. const eof = function (codepoint) {
  112. if (codepoint === undefined) codepoint = code;
  113. return codepoint == -1;
  114. };
  115. const donothing = function () { };
  116. const parseerror = function () { throw new Error("Parse error at index " + i + ", processing codepoint 0x" + code.toString(16) + "."); };
  117. const consumeAToken = function () {
  118. consumeComments();
  119. consume();
  120. if (whitespace(code)) {
  121. while (whitespace(next())) consume();
  122. return new WhitespaceToken;
  123. }
  124. else if (code == 0x22) return consumeAStringToken();
  125. else if (code == 0x23) {
  126. if (namechar(next()) || areAValidEscape(next(1), next(2))) {
  127. const token = new HashToken();
  128. if (wouldStartAnIdentifier(next(1), next(2), next(3))) token.type = "id";
  129. token.value = consumeAName();
  130. return token;
  131. } else {
  132. return new DelimToken(code);
  133. }
  134. }
  135. else if (code == 0x24) {
  136. if (next() == 0x3d) {
  137. consume();
  138. return new SuffixMatchToken();
  139. } else {
  140. return new DelimToken(code);
  141. }
  142. }
  143. else if (code == 0x27) return consumeAStringToken();
  144. else if (code == 0x28) return new OpenParenToken();
  145. else if (code == 0x29) return new CloseParenToken();
  146. else if (code == 0x2a) {
  147. if (next() == 0x3d) {
  148. consume();
  149. return new SubstringMatchToken();
  150. } else {
  151. return new DelimToken(code);
  152. }
  153. }
  154. else if (code == 0x2b) {
  155. if (startsWithANumber()) {
  156. reconsume();
  157. return consumeANumericToken();
  158. } else {
  159. return new DelimToken(code);
  160. }
  161. }
  162. else if (code == 0x2c) return new CommaToken();
  163. else if (code == 0x2d) {
  164. if (startsWithANumber()) {
  165. reconsume();
  166. return consumeANumericToken();
  167. } else if (next(1) == 0x2d && next(2) == 0x3e) {
  168. consume(2);
  169. return new CDCToken();
  170. } else if (startsWithAnIdentifier()) {
  171. reconsume();
  172. return consumeAnIdentlikeToken();
  173. } else {
  174. return new DelimToken(code);
  175. }
  176. }
  177. else if (code == 0x2e) {
  178. if (startsWithANumber()) {
  179. reconsume();
  180. return consumeANumericToken();
  181. } else {
  182. return new DelimToken(code);
  183. }
  184. }
  185. else if (code == 0x3a) return new ColonToken;
  186. else if (code == 0x3b) return new SemicolonToken;
  187. else if (code == 0x3c) {
  188. if (next(1) == 0x21 && next(2) == 0x2d && next(3) == 0x2d) {
  189. consume(3);
  190. return new CDOToken();
  191. } else {
  192. return new DelimToken(code);
  193. }
  194. }
  195. else if (code == 0x40) {
  196. if (wouldStartAnIdentifier(next(1), next(2), next(3))) {
  197. return new AtKeywordToken(consumeAName());
  198. } else {
  199. return new DelimToken(code);
  200. }
  201. }
  202. else if (code == 0x5b) return new OpenSquareToken();
  203. else if (code == 0x5c) {
  204. if (startsWithAValidEscape()) {
  205. reconsume();
  206. return consumeAnIdentlikeToken();
  207. } else {
  208. parseerror();
  209. return new DelimToken(code);
  210. }
  211. }
  212. else if (code == 0x5d) return new CloseSquareToken();
  213. else if (code == 0x5e) {
  214. if (next() == 0x3d) {
  215. consume();
  216. return new PrefixMatchToken();
  217. } else {
  218. return new DelimToken(code);
  219. }
  220. }
  221. else if (code == 0x7b) return new OpenCurlyToken();
  222. else if (code == 0x7c) {
  223. if (next() == 0x3d) {
  224. consume();
  225. return new DashMatchToken();
  226. } else if (next() == 0x7c) {
  227. consume();
  228. return new ColumnToken();
  229. } else {
  230. return new DelimToken(code);
  231. }
  232. }
  233. else if (code == 0x7d) return new CloseCurlyToken();
  234. else if (code == 0x7e) {
  235. if (next() == 0x3d) {
  236. consume();
  237. return new IncludeMatchToken();
  238. } else {
  239. return new DelimToken(code);
  240. }
  241. }
  242. else if (digit(code)) {
  243. reconsume();
  244. return consumeANumericToken();
  245. }
  246. else if (namestartchar(code)) {
  247. reconsume();
  248. return consumeAnIdentlikeToken();
  249. }
  250. else if (eof()) return new EOFToken();
  251. else return new DelimToken(code);
  252. };
  253. const consumeComments = function () {
  254. while (next(1) == 0x2f && next(2) == 0x2a) {
  255. consume(2);
  256. while (true) {
  257. consume();
  258. if (code == 0x2a && next() == 0x2f) {
  259. consume();
  260. break;
  261. } else if (eof()) {
  262. parseerror();
  263. return;
  264. }
  265. }
  266. }
  267. };
  268. const consumeANumericToken = function () {
  269. const num = consumeANumber();
  270. if (wouldStartAnIdentifier(next(1), next(2), next(3))) {
  271. const token = new DimensionToken();
  272. token.value = num.value;
  273. token.repr = num.repr;
  274. token.type = num.type;
  275. token.unit = consumeAName();
  276. return token;
  277. } else if (next() == 0x25) {
  278. consume();
  279. const token = new PercentageToken();
  280. token.value = num.value;
  281. token.repr = num.repr;
  282. return token;
  283. } else {
  284. const token = new NumberToken();
  285. token.value = num.value;
  286. token.repr = num.repr;
  287. token.type = num.type;
  288. return token;
  289. }
  290. };
  291. const consumeAnIdentlikeToken = function () {
  292. const str = consumeAName();
  293. if (str.toLowerCase() == "url" && next() == 0x28) {
  294. consume();
  295. while (whitespace(next(1)) && whitespace(next(2))) consume();
  296. if (next() == 0x22 || next() == 0x27) {
  297. return new FunctionToken(str);
  298. } else if (whitespace(next()) && (next(2) == 0x22 || next(2) == 0x27)) {
  299. return new FunctionToken(str);
  300. } else {
  301. return consumeAURLToken();
  302. }
  303. } else if (next() == 0x28) {
  304. consume();
  305. return new FunctionToken(str);
  306. } else {
  307. return new IdentToken(str);
  308. }
  309. };
  310. const consumeAStringToken = function (endingCodePoint) {
  311. if (endingCodePoint === undefined) endingCodePoint = code;
  312. let string = "";
  313. while (consume()) {
  314. if (code == endingCodePoint || eof()) {
  315. return new StringToken(string);
  316. } else if (newline(code)) {
  317. parseerror();
  318. reconsume();
  319. return new BadStringToken();
  320. } else if (code == 0x5c) {
  321. if (eof(next())) {
  322. donothing();
  323. } else if (newline(next())) {
  324. consume();
  325. } else {
  326. string += stringFromCode(consumeEscape());
  327. }
  328. } else {
  329. string += stringFromCode(code);
  330. }
  331. }
  332. };
  333. const consumeAURLToken = function () {
  334. const token = new URLToken("");
  335. while (whitespace(next())) consume();
  336. if (eof(next())) return token;
  337. while (consume()) {
  338. if (code == 0x29 || eof()) {
  339. return token;
  340. } else if (whitespace(code)) {
  341. while (whitespace(next())) consume();
  342. if (next() == 0x29 || eof(next())) {
  343. consume();
  344. return token;
  345. } else {
  346. consumeTheRemnantsOfABadURL();
  347. return new BadURLToken();
  348. }
  349. } else if (code == 0x22 || code == 0x27 || code == 0x28 || nonprintable(code)) {
  350. parseerror();
  351. consumeTheRemnantsOfABadURL();
  352. return new BadURLToken();
  353. } else if (code == 0x5c) {
  354. if (startsWithAValidEscape()) {
  355. token.value += stringFromCode(consumeEscape());
  356. } else {
  357. parseerror();
  358. consumeTheRemnantsOfABadURL();
  359. return new BadURLToken();
  360. }
  361. } else {
  362. token.value += stringFromCode(code);
  363. }
  364. }
  365. };
  366. const consumeEscape = function () {
  367. // Assume the the current character is the \
  368. // and the next code point is not a newline.
  369. consume();
  370. if (hexdigit(code)) {
  371. // Consume 1-6 hex digits
  372. const digits = [code];
  373. for (let total = 0; total < 5; total++) {
  374. if (hexdigit(next())) {
  375. consume();
  376. digits.push(code);
  377. } else {
  378. break;
  379. }
  380. }
  381. if (whitespace(next())) consume();
  382. let value = parseInt(digits.map(function (x) { return String.fromCharCode(x); }).join(""), 16);
  383. if (value > maximumallowedcodepoint) value = 0xfffd;
  384. return value;
  385. } else if (eof()) {
  386. return 0xfffd;
  387. } else {
  388. return code;
  389. }
  390. };
  391. const areAValidEscape = function (c1, c2) {
  392. if (c1 != 0x5c) return false;
  393. if (newline(c2)) return false;
  394. return true;
  395. };
  396. const startsWithAValidEscape = function () {
  397. return areAValidEscape(code, next());
  398. };
  399. const wouldStartAnIdentifier = function (c1, c2, c3) {
  400. if (c1 == 0x2d) {
  401. return namestartchar(c2) || c2 == 0x2d || areAValidEscape(c2, c3);
  402. } else if (namestartchar(c1)) {
  403. return true;
  404. } else if (c1 == 0x5c) {
  405. return areAValidEscape(c1, c2);
  406. } else {
  407. return false;
  408. }
  409. };
  410. const startsWithAnIdentifier = function () {
  411. return wouldStartAnIdentifier(code, next(1), next(2));
  412. };
  413. const wouldStartANumber = function (c1, c2, c3) {
  414. if (c1 == 0x2b || c1 == 0x2d) {
  415. if (digit(c2)) return true;
  416. if (c2 == 0x2e && digit(c3)) return true;
  417. return false;
  418. } else if (c1 == 0x2e) {
  419. if (digit(c2)) return true;
  420. return false;
  421. } else if (digit(c1)) {
  422. return true;
  423. } else {
  424. return false;
  425. }
  426. };
  427. const startsWithANumber = function () {
  428. return wouldStartANumber(code, next(1), next(2));
  429. };
  430. const consumeAName = function () {
  431. let result = "";
  432. while (consume()) {
  433. if (namechar(code)) {
  434. result += stringFromCode(code);
  435. } else if (startsWithAValidEscape()) {
  436. result += stringFromCode(consumeEscape());
  437. } else {
  438. reconsume();
  439. return result;
  440. }
  441. }
  442. };
  443. const consumeANumber = function () {
  444. let repr = [];
  445. let type = "integer";
  446. if (next() == 0x2b || next() == 0x2d) {
  447. consume();
  448. repr += stringFromCode(code);
  449. }
  450. while (digit(next())) {
  451. consume();
  452. repr += stringFromCode(code);
  453. }
  454. if (next(1) == 0x2e && digit(next(2))) {
  455. consume();
  456. repr += stringFromCode(code);
  457. consume();
  458. repr += stringFromCode(code);
  459. type = "number";
  460. while (digit(next())) {
  461. consume();
  462. repr += stringFromCode(code);
  463. }
  464. }
  465. const c1 = next(1), c2 = next(2), c3 = next(3);
  466. if ((c1 == 0x45 || c1 == 0x65) && digit(c2)) {
  467. consume();
  468. repr += stringFromCode(code);
  469. consume();
  470. repr += stringFromCode(code);
  471. type = "number";
  472. while (digit(next())) {
  473. consume();
  474. repr += stringFromCode(code);
  475. }
  476. } else if ((c1 == 0x45 || c1 == 0x65) && (c2 == 0x2b || c2 == 0x2d) && digit(c3)) {
  477. consume();
  478. repr += stringFromCode(code);
  479. consume();
  480. repr += stringFromCode(code);
  481. consume();
  482. repr += stringFromCode(code);
  483. type = "number";
  484. while (digit(next())) {
  485. consume();
  486. repr += stringFromCode(code);
  487. }
  488. }
  489. const value = convertAStringToANumber(repr);
  490. return { type: type, value: value, repr: repr };
  491. };
  492. const convertAStringToANumber = function (string) {
  493. // CSS's number rules are identical to JS, afaik.
  494. return +string;
  495. };
  496. const consumeTheRemnantsOfABadURL = function () {
  497. while (consume()) {
  498. if (code == 0x29 || eof()) {
  499. return;
  500. } else if (startsWithAValidEscape()) {
  501. consumeEscape();
  502. donothing();
  503. } else {
  504. donothing();
  505. }
  506. }
  507. };
  508. let iterationCount = 0;
  509. while (!eof(next())) {
  510. tokens.push(consumeAToken());
  511. iterationCount++;
  512. if (iterationCount > str.length * 2) return "I'm infinite-looping!";
  513. }
  514. return tokens;
  515. }
  516. function CSSParserToken() { throw "Abstract Base Class"; }
  517. CSSParserToken.prototype.toJSON = function () {
  518. return { token: this.tokenType };
  519. };
  520. CSSParserToken.prototype.toString = function () { return this.tokenType; };
  521. CSSParserToken.prototype.toSource = function () { return "" + this; };
  522. function BadStringToken() { return this; }
  523. BadStringToken.prototype = Object.create(CSSParserToken.prototype);
  524. BadStringToken.prototype.tokenType = "BADSTRING";
  525. function BadURLToken() { return this; }
  526. BadURLToken.prototype = Object.create(CSSParserToken.prototype);
  527. BadURLToken.prototype.tokenType = "BADURL";
  528. function WhitespaceToken() { return this; }
  529. WhitespaceToken.prototype = Object.create(CSSParserToken.prototype);
  530. WhitespaceToken.prototype.tokenType = "WHITESPACE";
  531. WhitespaceToken.prototype.toString = function () { return "WS"; };
  532. WhitespaceToken.prototype.toSource = function () { return " "; };
  533. function CDOToken() { return this; }
  534. CDOToken.prototype = Object.create(CSSParserToken.prototype);
  535. CDOToken.prototype.tokenType = "CDO";
  536. CDOToken.prototype.toSource = function () { return "<!--"; };
  537. function CDCToken() { return this; }
  538. CDCToken.prototype = Object.create(CSSParserToken.prototype);
  539. CDCToken.prototype.tokenType = "CDC";
  540. CDCToken.prototype.toSource = function () { return "-->"; };
  541. function ColonToken() { return this; }
  542. ColonToken.prototype = Object.create(CSSParserToken.prototype);
  543. ColonToken.prototype.tokenType = ":";
  544. function SemicolonToken() { return this; }
  545. SemicolonToken.prototype = Object.create(CSSParserToken.prototype);
  546. SemicolonToken.prototype.tokenType = ";";
  547. function CommaToken() { return this; }
  548. CommaToken.prototype = Object.create(CSSParserToken.prototype);
  549. CommaToken.prototype.tokenType = ",";
  550. function GroupingToken() { throw "Abstract Base Class"; }
  551. GroupingToken.prototype = Object.create(CSSParserToken.prototype);
  552. function OpenCurlyToken() { this.value = "{"; this.mirror = "}"; return this; }
  553. OpenCurlyToken.prototype = Object.create(GroupingToken.prototype);
  554. OpenCurlyToken.prototype.tokenType = "{";
  555. function CloseCurlyToken() { this.value = "}"; this.mirror = "{"; return this; }
  556. CloseCurlyToken.prototype = Object.create(GroupingToken.prototype);
  557. CloseCurlyToken.prototype.tokenType = "}";
  558. function OpenSquareToken() { this.value = "["; this.mirror = "]"; return this; }
  559. OpenSquareToken.prototype = Object.create(GroupingToken.prototype);
  560. OpenSquareToken.prototype.tokenType = "[";
  561. function CloseSquareToken() { this.value = "]"; this.mirror = "["; return this; }
  562. CloseSquareToken.prototype = Object.create(GroupingToken.prototype);
  563. CloseSquareToken.prototype.tokenType = "]";
  564. function OpenParenToken() { this.value = "("; this.mirror = ")"; return this; }
  565. OpenParenToken.prototype = Object.create(GroupingToken.prototype);
  566. OpenParenToken.prototype.tokenType = "(";
  567. function CloseParenToken() { this.value = ")"; this.mirror = "("; return this; }
  568. CloseParenToken.prototype = Object.create(GroupingToken.prototype);
  569. CloseParenToken.prototype.tokenType = ")";
  570. function IncludeMatchToken() { return this; }
  571. IncludeMatchToken.prototype = Object.create(CSSParserToken.prototype);
  572. IncludeMatchToken.prototype.tokenType = "~=";
  573. function DashMatchToken() { return this; }
  574. DashMatchToken.prototype = Object.create(CSSParserToken.prototype);
  575. DashMatchToken.prototype.tokenType = "|=";
  576. function PrefixMatchToken() { return this; }
  577. PrefixMatchToken.prototype = Object.create(CSSParserToken.prototype);
  578. PrefixMatchToken.prototype.tokenType = "^=";
  579. function SuffixMatchToken() { return this; }
  580. SuffixMatchToken.prototype = Object.create(CSSParserToken.prototype);
  581. SuffixMatchToken.prototype.tokenType = "$=";
  582. function SubstringMatchToken() { return this; }
  583. SubstringMatchToken.prototype = Object.create(CSSParserToken.prototype);
  584. SubstringMatchToken.prototype.tokenType = "*=";
  585. function ColumnToken() { return this; }
  586. ColumnToken.prototype = Object.create(CSSParserToken.prototype);
  587. ColumnToken.prototype.tokenType = "||";
  588. function EOFToken() { return this; }
  589. EOFToken.prototype = Object.create(CSSParserToken.prototype);
  590. EOFToken.prototype.tokenType = "EOF";
  591. EOFToken.prototype.toSource = function () { return ""; };
  592. function DelimToken(code) {
  593. this.value = stringFromCode(code);
  594. return this;
  595. }
  596. DelimToken.prototype = Object.create(CSSParserToken.prototype);
  597. DelimToken.prototype.tokenType = "DELIM";
  598. DelimToken.prototype.toString = function () { return "DELIM(" + this.value + ")"; };
  599. DelimToken.prototype.toJSON = function () {
  600. const json = this.constructor.prototype.constructor.prototype.toJSON.call(this);
  601. json.value = this.value;
  602. return json;
  603. };
  604. DelimToken.prototype.toSource = function () {
  605. if (this.value == "\\")
  606. return "\\\n";
  607. else
  608. return this.value;
  609. };
  610. function StringValuedToken() { throw "Abstract Base Class"; }
  611. StringValuedToken.prototype = Object.create(CSSParserToken.prototype);
  612. StringValuedToken.prototype.ASCIIMatch = function (str) {
  613. return this.value.toLowerCase() == str.toLowerCase();
  614. };
  615. StringValuedToken.prototype.toJSON = function () {
  616. const json = this.constructor.prototype.constructor.prototype.toJSON.call(this);
  617. json.value = this.value;
  618. return json;
  619. };
  620. function IdentToken(val) {
  621. this.value = val;
  622. }
  623. IdentToken.prototype = Object.create(StringValuedToken.prototype);
  624. IdentToken.prototype.tokenType = "IDENT";
  625. IdentToken.prototype.toString = function () { return "IDENT(" + this.value + ")"; };
  626. IdentToken.prototype.toSource = function () {
  627. return escapeIdent(this.value);
  628. };
  629. function FunctionToken(val) {
  630. this.value = val;
  631. this.mirror = ")";
  632. }
  633. FunctionToken.prototype = Object.create(StringValuedToken.prototype);
  634. FunctionToken.prototype.tokenType = "FUNCTION";
  635. FunctionToken.prototype.toString = function () { return "FUNCTION(" + this.value + ")"; };
  636. FunctionToken.prototype.toSource = function () {
  637. return escapeIdent(this.value) + "(";
  638. };
  639. function AtKeywordToken(val) {
  640. this.value = val;
  641. }
  642. AtKeywordToken.prototype = Object.create(StringValuedToken.prototype);
  643. AtKeywordToken.prototype.tokenType = "AT-KEYWORD";
  644. AtKeywordToken.prototype.toString = function () { return "AT(" + this.value + ")"; };
  645. AtKeywordToken.prototype.toSource = function () {
  646. return "@" + escapeIdent(this.value);
  647. };
  648. function HashToken(val) {
  649. this.value = val;
  650. this.type = "unrestricted";
  651. }
  652. HashToken.prototype = Object.create(StringValuedToken.prototype);
  653. HashToken.prototype.tokenType = "HASH";
  654. HashToken.prototype.toString = function () { return "HASH(" + this.value + ")"; };
  655. HashToken.prototype.toJSON = function () {
  656. const json = this.constructor.prototype.constructor.prototype.toJSON.call(this);
  657. json.value = this.value;
  658. json.type = this.type;
  659. return json;
  660. };
  661. HashToken.prototype.toSource = function () {
  662. if (this.type == "id") {
  663. return "#" + escapeIdent(this.value);
  664. } else {
  665. return "#" + escapeHash(this.value);
  666. }
  667. };
  668. function StringToken(val) {
  669. this.value = val;
  670. }
  671. StringToken.prototype = Object.create(StringValuedToken.prototype);
  672. StringToken.prototype.tokenType = "STRING";
  673. StringToken.prototype.toString = function () {
  674. return "\"" + escapeString(this.value) + "\"";
  675. };
  676. function URLToken(val) {
  677. this.value = val;
  678. }
  679. URLToken.prototype = Object.create(StringValuedToken.prototype);
  680. URLToken.prototype.tokenType = "URL";
  681. URLToken.prototype.toString = function () { return "URL(" + this.value + ")"; };
  682. URLToken.prototype.toSource = function () {
  683. return "url(\"" + escapeString(this.value) + "\")";
  684. };
  685. function NumberToken() {
  686. this.value = null;
  687. this.type = "integer";
  688. this.repr = "";
  689. }
  690. NumberToken.prototype = Object.create(CSSParserToken.prototype);
  691. NumberToken.prototype.tokenType = "NUMBER";
  692. NumberToken.prototype.toString = function () {
  693. if (this.type == "integer")
  694. return "INT(" + this.value + ")";
  695. return "NUMBER(" + this.value + ")";
  696. };
  697. NumberToken.prototype.toJSON = function () {
  698. const json = this.constructor.prototype.constructor.prototype.toJSON.call(this);
  699. json.value = this.value;
  700. json.type = this.type;
  701. json.repr = this.repr;
  702. return json;
  703. };
  704. NumberToken.prototype.toSource = function () { return this.repr; };
  705. function PercentageToken() {
  706. this.value = null;
  707. this.repr = "";
  708. }
  709. PercentageToken.prototype = Object.create(CSSParserToken.prototype);
  710. PercentageToken.prototype.tokenType = "PERCENTAGE";
  711. PercentageToken.prototype.toString = function () { return "PERCENTAGE(" + this.value + ")"; };
  712. PercentageToken.prototype.toJSON = function () {
  713. const json = this.constructor.prototype.constructor.prototype.toJSON.call(this);
  714. json.value = this.value;
  715. json.repr = this.repr;
  716. return json;
  717. };
  718. PercentageToken.prototype.toSource = function () { return this.repr + "%"; };
  719. function DimensionToken() {
  720. this.value = null;
  721. this.type = "integer";
  722. this.repr = "";
  723. this.unit = "";
  724. }
  725. DimensionToken.prototype = Object.create(CSSParserToken.prototype);
  726. DimensionToken.prototype.tokenType = "DIMENSION";
  727. DimensionToken.prototype.toString = function () { return "DIM(" + this.value + "," + this.unit + ")"; };
  728. DimensionToken.prototype.toJSON = function () {
  729. const json = this.constructor.prototype.constructor.prototype.toJSON.call(this);
  730. json.value = this.value;
  731. json.type = this.type;
  732. json.repr = this.repr;
  733. json.unit = this.unit;
  734. return json;
  735. };
  736. DimensionToken.prototype.toSource = function () {
  737. const source = this.repr;
  738. let unit = escapeIdent(this.unit);
  739. if (unit[0].toLowerCase() == "e" && (unit[1] == "-" || between(unit.charCodeAt(1), 0x30, 0x39))) {
  740. // Unit is ambiguous with scinot
  741. // Remove the leading "e", replace with escape.
  742. unit = "\\65 " + unit.slice(1, unit.length);
  743. }
  744. return source + unit;
  745. };
  746. function escapeIdent(string) {
  747. string = "" + string;
  748. let result = "";
  749. const firstcode = string.charCodeAt(0);
  750. for (let i = 0; i < string.length; i++) {
  751. const code = string.charCodeAt(i);
  752. if (code == 0x0) {
  753. throw new InvalidCharacterError("Invalid character: the input contains U+0000.");
  754. }
  755. if (
  756. between(code, 0x1, 0x1f) || code == 0x7f ||
  757. (i == 0 && between(code, 0x30, 0x39)) ||
  758. (i == 1 && between(code, 0x30, 0x39) && firstcode == 0x2d)
  759. ) {
  760. result += "\\" + code.toString(16) + " ";
  761. } else if (
  762. code >= 0x80 ||
  763. code == 0x2d ||
  764. code == 0x5f ||
  765. between(code, 0x30, 0x39) ||
  766. between(code, 0x41, 0x5a) ||
  767. between(code, 0x61, 0x7a)
  768. ) {
  769. result += string[i];
  770. } else {
  771. result += "\\" + string[i];
  772. }
  773. }
  774. return result;
  775. }
  776. function escapeHash(string) {
  777. // Escapes the contents of "unrestricted"-type hash tokens.
  778. // Won't preserve the ID-ness of "id"-type hash tokens;
  779. // use escapeIdent() for that.
  780. string = "" + string;
  781. let result = "";
  782. // let firstcode = string.charCodeAt(0);
  783. for (let i = 0; i < string.length; i++) {
  784. const code = string.charCodeAt(i);
  785. if (code == 0x0) {
  786. throw new InvalidCharacterError("Invalid character: the input contains U+0000.");
  787. }
  788. if (
  789. code >= 0x80 ||
  790. code == 0x2d ||
  791. code == 0x5f ||
  792. between(code, 0x30, 0x39) ||
  793. between(code, 0x41, 0x5a) ||
  794. between(code, 0x61, 0x7a)
  795. ) {
  796. result += string[i];
  797. } else {
  798. result += "\\" + code.toString(16) + " ";
  799. }
  800. }
  801. return result;
  802. }
  803. function escapeString(string) {
  804. string = "" + string;
  805. let result = "";
  806. for (let i = 0; i < string.length; i++) {
  807. const code = string.charCodeAt(i);
  808. if (code == 0x0) {
  809. throw new InvalidCharacterError("Invalid character: the input contains U+0000.");
  810. }
  811. if (between(code, 0x1, 0x1f) || code == 0x7f) {
  812. result += "\\" + code.toString(16) + " ";
  813. } else if (code == 0x22 || code == 0x5c) {
  814. result += "\\" + string[i];
  815. } else {
  816. result += string[i];
  817. }
  818. }
  819. return result;
  820. }
  821. // ---
  822. function TokenStream(tokens) {
  823. // Assume that tokens is an array.
  824. this.tokens = tokens;
  825. this.i = -1;
  826. }
  827. TokenStream.prototype.tokenAt = function (i) {
  828. if (i < this.tokens.length)
  829. return this.tokens[i];
  830. return new EOFToken();
  831. };
  832. TokenStream.prototype.consume = function (num) {
  833. if (num === undefined) num = 1;
  834. this.i += num;
  835. this.token = this.tokenAt(this.i);
  836. //console.log(this.i, this.token);
  837. return true;
  838. };
  839. TokenStream.prototype.next = function () {
  840. return this.tokenAt(this.i + 1);
  841. };
  842. TokenStream.prototype.reconsume = function () {
  843. this.i--;
  844. };
  845. function parseerror(s, msg) {
  846. throw new Error("Parse error at token " + s.i + ": " + s.token + ".\n" + msg);
  847. }
  848. function donothing() { return true; }
  849. function consumeAListOfRules(s, topLevel) {
  850. const rules = [];
  851. let rule;
  852. while (s.consume()) {
  853. if (s.token instanceof WhitespaceToken) {
  854. continue;
  855. } else if (s.token instanceof EOFToken) {
  856. return rules;
  857. } else if (s.token instanceof CDOToken || s.token instanceof CDCToken) {
  858. if (topLevel == "top-level") continue;
  859. s.reconsume();
  860. if (rule = consumeAQualifiedRule(s)) rules.push(rule);
  861. } else if (s.token instanceof AtKeywordToken) {
  862. s.reconsume();
  863. if (rule = consumeAnAtRule(s)) rules.push(rule);
  864. } else {
  865. s.reconsume();
  866. if (rule = consumeAQualifiedRule(s)) rules.push(rule);
  867. }
  868. }
  869. }
  870. function consumeAnAtRule(s) {
  871. s.consume();
  872. const rule = new AtRule(s.token.value);
  873. while (s.consume()) {
  874. if (s.token instanceof SemicolonToken || s.token instanceof EOFToken) {
  875. return rule;
  876. } else if (s.token instanceof OpenCurlyToken) {
  877. rule.value = consumeASimpleBlock(s);
  878. return rule;
  879. } else if (s.token instanceof SimpleBlock && s.token.name == "{") {
  880. rule.value = s.token;
  881. return rule;
  882. } else {
  883. s.reconsume();
  884. rule.prelude.push(consumeAComponentValue(s));
  885. }
  886. }
  887. }
  888. function consumeAQualifiedRule(s) {
  889. const rule = new QualifiedRule();
  890. while (s.consume()) {
  891. if (s.token instanceof EOFToken) {
  892. parseerror(s, "Hit EOF when trying to parse the prelude of a qualified rule.");
  893. return;
  894. } else if (s.token instanceof OpenCurlyToken) {
  895. rule.value = consumeASimpleBlock(s);
  896. return rule;
  897. } else if (s.token instanceof SimpleBlock && s.token.name == "{") {
  898. rule.value = s.token;
  899. return rule;
  900. } else {
  901. s.reconsume();
  902. rule.prelude.push(consumeAComponentValue(s));
  903. }
  904. }
  905. }
  906. function consumeAListOfDeclarations(s) {
  907. const decls = [];
  908. while (s.consume()) {
  909. if (s.token instanceof WhitespaceToken || s.token instanceof SemicolonToken) {
  910. donothing();
  911. } else if (s.token instanceof EOFToken) {
  912. return decls;
  913. } else if (s.token instanceof AtKeywordToken) {
  914. s.reconsume();
  915. decls.push(consumeAnAtRule(s));
  916. } else if (s.token instanceof IdentToken) {
  917. const temp = [s.token];
  918. while (!(s.next() instanceof SemicolonToken || s.next() instanceof EOFToken))
  919. temp.push(consumeAComponentValue(s));
  920. let decl;
  921. if (decl = consumeADeclaration(new TokenStream(temp))) decls.push(decl);
  922. } else {
  923. parseerror(s);
  924. s.reconsume();
  925. while (!(s.next() instanceof SemicolonToken || s.next() instanceof EOFToken))
  926. consumeAComponentValue(s);
  927. }
  928. }
  929. }
  930. function consumeADeclaration(s) {
  931. // Assumes that the next input token will be an ident token.
  932. s.consume();
  933. const decl = new Declaration(s.token.value);
  934. while (s.next() instanceof WhitespaceToken) s.consume();
  935. if (!(s.next() instanceof ColonToken)) {
  936. parseerror(s);
  937. return;
  938. } else {
  939. s.consume();
  940. }
  941. while (!(s.next() instanceof EOFToken)) {
  942. decl.value.push(consumeAComponentValue(s));
  943. }
  944. let foundImportant = false;
  945. for (let i = decl.value.length - 1; i >= 0; i--) {
  946. if (decl.value[i] instanceof WhitespaceToken) {
  947. continue;
  948. } else if (decl.value[i] instanceof IdentToken && decl.value[i].ASCIIMatch("important")) {
  949. foundImportant = true;
  950. } else if (foundImportant && decl.value[i] instanceof DelimToken && decl.value[i].value == "!") {
  951. decl.value.splice(i, decl.value.length);
  952. decl.important = true;
  953. break;
  954. } else {
  955. break;
  956. }
  957. }
  958. return decl;
  959. }
  960. function consumeAComponentValue(s) {
  961. s.consume();
  962. if (s.token instanceof OpenCurlyToken || s.token instanceof OpenSquareToken || s.token instanceof OpenParenToken)
  963. return consumeASimpleBlock(s);
  964. if (s.token instanceof FunctionToken)
  965. return consumeAFunction(s);
  966. return s.token;
  967. }
  968. function consumeASimpleBlock(s) {
  969. const mirror = s.token.mirror;
  970. const block = new SimpleBlock(s.token.value);
  971. while (s.consume()) {
  972. if (s.token instanceof EOFToken || (s.token instanceof GroupingToken && s.token.value == mirror))
  973. return block;
  974. else {
  975. s.reconsume();
  976. block.value.push(consumeAComponentValue(s));
  977. }
  978. }
  979. }
  980. function consumeAFunction(s) {
  981. const func = new Func(s.token.value);
  982. while (s.consume()) {
  983. if (s.token instanceof EOFToken || s.token instanceof CloseParenToken)
  984. return func;
  985. else {
  986. s.reconsume();
  987. func.value.push(consumeAComponentValue(s));
  988. }
  989. }
  990. }
  991. function normalizeInput(input) {
  992. if (typeof input == "string")
  993. return new TokenStream(tokenize(input));
  994. if (input instanceof TokenStream)
  995. return input;
  996. if (input.length !== undefined)
  997. return new TokenStream(input);
  998. else throw SyntaxError(input);
  999. }
  1000. function parseAStylesheet(s) {
  1001. s = normalizeInput(s);
  1002. const sheet = new Stylesheet();
  1003. sheet.value = consumeAListOfRules(s, "top-level");
  1004. return sheet;
  1005. }
  1006. function parseAListOfRules(s) {
  1007. s = normalizeInput(s);
  1008. return consumeAListOfRules(s);
  1009. }
  1010. function parseARule(s) {
  1011. s = normalizeInput(s);
  1012. while (s.next() instanceof WhitespaceToken) s.consume();
  1013. if (s.next() instanceof EOFToken) throw SyntaxError();
  1014. let rule;
  1015. if (s.next() instanceof AtKeywordToken) {
  1016. rule = consumeAnAtRule(s);
  1017. } else {
  1018. rule = consumeAQualifiedRule(s);
  1019. if (!rule) throw SyntaxError();
  1020. }
  1021. while (s.next() instanceof WhitespaceToken) s.consume();
  1022. if (s.next() instanceof EOFToken)
  1023. return rule;
  1024. throw SyntaxError();
  1025. }
  1026. function parseADeclaration(s) {
  1027. s = normalizeInput(s);
  1028. while (s.next() instanceof WhitespaceToken) s.consume();
  1029. if (!(s.next() instanceof IdentToken)) throw SyntaxError();
  1030. const decl = consumeADeclaration(s);
  1031. if (decl)
  1032. return decl;
  1033. else
  1034. throw SyntaxError();
  1035. }
  1036. function parseAListOfDeclarations(s) {
  1037. s = normalizeInput(s);
  1038. return consumeAListOfDeclarations(s);
  1039. }
  1040. function parseAComponentValue(s) {
  1041. s = normalizeInput(s);
  1042. while (s.next() instanceof WhitespaceToken) s.consume();
  1043. if (s.next() instanceof EOFToken) throw SyntaxError();
  1044. const val = consumeAComponentValue(s);
  1045. if (!val) throw SyntaxError();
  1046. while (s.next() instanceof WhitespaceToken) s.consume();
  1047. if (s.next() instanceof EOFToken)
  1048. return val;
  1049. throw SyntaxError();
  1050. }
  1051. function parseAListOfComponentValues(s) {
  1052. s = normalizeInput(s);
  1053. const vals = [];
  1054. while (true) {
  1055. const val = consumeAComponentValue(s);
  1056. if (val instanceof EOFToken)
  1057. return vals;
  1058. else
  1059. vals.push(val);
  1060. }
  1061. }
  1062. function parseACommaSeparatedListOfComponentValues(s) {
  1063. s = normalizeInput(s);
  1064. const listOfCVLs = [];
  1065. while (true) {
  1066. const vals = [];
  1067. while (true) {
  1068. const val = consumeAComponentValue(s);
  1069. if (val instanceof EOFToken) {
  1070. listOfCVLs.push(vals);
  1071. return listOfCVLs;
  1072. } else if (val instanceof CommaToken) {
  1073. listOfCVLs.push(vals);
  1074. break;
  1075. } else {
  1076. vals.push(val);
  1077. }
  1078. }
  1079. }
  1080. }
  1081. function CSSParserRule() { throw "Abstract Base Class"; }
  1082. CSSParserRule.prototype.toString = function (indent) {
  1083. return JSON.stringify(this, null, indent);
  1084. };
  1085. CSSParserRule.prototype.toJSON = function () {
  1086. return { type: this.type, value: this.value };
  1087. };
  1088. function Stylesheet() {
  1089. this.value = [];
  1090. return this;
  1091. }
  1092. Stylesheet.prototype = Object.create(CSSParserRule.prototype);
  1093. Stylesheet.prototype.type = "STYLESHEET";
  1094. function AtRule(name) {
  1095. this.name = name;
  1096. this.prelude = [];
  1097. this.value = null;
  1098. return this;
  1099. }
  1100. AtRule.prototype = Object.create(CSSParserRule.prototype);
  1101. AtRule.prototype.type = "AT-RULE";
  1102. AtRule.prototype.toJSON = function () {
  1103. const json = this.constructor.prototype.constructor.prototype.toJSON.call(this);
  1104. json.name = this.name;
  1105. json.prelude = this.prelude;
  1106. return json;
  1107. };
  1108. function QualifiedRule() {
  1109. this.prelude = [];
  1110. this.value = [];
  1111. return this;
  1112. }
  1113. QualifiedRule.prototype = Object.create(CSSParserRule.prototype);
  1114. QualifiedRule.prototype.type = "QUALIFIED-RULE";
  1115. QualifiedRule.prototype.toJSON = function () {
  1116. const json = this.constructor.prototype.constructor.prototype.toJSON.call(this);
  1117. json.prelude = this.prelude;
  1118. return json;
  1119. };
  1120. function Declaration(name) {
  1121. this.name = name;
  1122. this.value = [];
  1123. this.important = false;
  1124. return this;
  1125. }
  1126. Declaration.prototype = Object.create(CSSParserRule.prototype);
  1127. Declaration.prototype.type = "DECLARATION";
  1128. Declaration.prototype.toJSON = function () {
  1129. const json = this.constructor.prototype.constructor.prototype.toJSON.call(this);
  1130. json.name = this.name;
  1131. json.important = this.important;
  1132. return json;
  1133. };
  1134. function SimpleBlock(type) {
  1135. this.name = type;
  1136. this.value = [];
  1137. return this;
  1138. }
  1139. SimpleBlock.prototype = Object.create(CSSParserRule.prototype);
  1140. SimpleBlock.prototype.type = "BLOCK";
  1141. SimpleBlock.prototype.toJSON = function () {
  1142. const json = this.constructor.prototype.constructor.prototype.toJSON.call(this);
  1143. json.name = this.name;
  1144. return json;
  1145. };
  1146. function Func(name) {
  1147. this.name = name;
  1148. this.value = [];
  1149. return this;
  1150. }
  1151. Func.prototype = Object.create(CSSParserRule.prototype);
  1152. Func.prototype.type = "FUNCTION";
  1153. Func.prototype.toJSON = function () {
  1154. const json = this.constructor.prototype.constructor.prototype.toJSON.call(this);
  1155. json.name = this.name;
  1156. return json;
  1157. };
  1158. /* Grammar Application */
  1159. function canonicalize(rule, grammar, topGrammar) {
  1160. if (grammar === undefined) grammar = CSSGrammar;
  1161. if (topGrammar === undefined) topGrammar = grammar;
  1162. let unknownTransformer;
  1163. if (grammar) {
  1164. if (grammar.stylesheet) grammar = topGrammar;
  1165. unknownTransformer = grammar.unknown || function () { return; };
  1166. }
  1167. const ret = { "type": rule.type.toLowerCase() };
  1168. let contents, unparsedContents;
  1169. if (rule.type == "STYLESHEET") {
  1170. contents = rule.value;
  1171. } else if (rule.type == "BLOCK") {
  1172. unparsedContents = rule.value;
  1173. ret.name = rule.name;
  1174. } else if (rule.type == "QUALIFIED-RULE") {
  1175. unparsedContents = rule.value.value;
  1176. ret.prelude = rule.prelude;
  1177. } else if (rule.type == "AT-RULE") {
  1178. unparsedContents = rule.value.value;
  1179. ret.name = rule.name;
  1180. ret.prelude = rule.prelude;
  1181. } else if (rule.type == "DECLARATION") {
  1182. // I don't do grammar-checking of declarations yet.
  1183. ret.name = rule.name;
  1184. ret.value = rule.value;
  1185. ret.important = rule.important;
  1186. return ret;
  1187. }
  1188. if (unparsedContents) {
  1189. if (grammar.declarations) {
  1190. contents = parseAListOfDeclarations(unparsedContents);
  1191. } else if (grammar.qualified) {
  1192. contents = parseAListOfRules(unparsedContents);
  1193. }
  1194. }
  1195. if (!grammar) {
  1196. return ret;
  1197. } else if (grammar.declarations) {
  1198. ret.declarations = {}; // simple key/value map of declarations
  1199. ret.rules = []; // in-order list of both decls and at-rules
  1200. ret.errors = [];
  1201. for (let i = 0; i < contents.length; i++) {
  1202. const rule = contents[i];
  1203. if (rule instanceof Declaration) {
  1204. const decl = canonicalize(rule, {}, topGrammar);
  1205. ret.declarations[rule.name] = decl;
  1206. ret.rules.push(decl);
  1207. } else { // rule is instanceof AtRule
  1208. const subGrammar = grammar["@" + rule.name];
  1209. if (subGrammar) { // Rule is valid in this context
  1210. ret.rules.push(canonicalize(rule, subGrammar, topGrammar));
  1211. } else {
  1212. const result = unknownTransformer(rule);
  1213. if (result) {
  1214. ret.rules.push(result);
  1215. } else {
  1216. ret.errors.push(result);
  1217. }
  1218. }
  1219. }
  1220. }
  1221. } else {
  1222. ret.rules = [];
  1223. ret.errors = [];
  1224. for (let i = 0; i < contents.length; i++) {
  1225. const rule = contents[i];
  1226. if (rule instanceof QualifiedRule) {
  1227. ret.rules.push(canonicalize(rule, grammar.qualified, topGrammar));
  1228. } else {
  1229. const subGrammar = grammar["@" + rule.name];
  1230. if (subGrammar) { // Rule is valid in this context
  1231. ret.rules.push(canonicalize(rule, subGrammar, topGrammar));
  1232. } else {
  1233. const result = unknownTransformer(rule);
  1234. if (result) {
  1235. ret.rules.push(result);
  1236. } else {
  1237. ret.errors.push(result);
  1238. }
  1239. }
  1240. }
  1241. }
  1242. }
  1243. return ret;
  1244. }
  1245. const CSSGrammar = {
  1246. qualified: { declarations: true },
  1247. "@media": { stylesheet: true },
  1248. "@keyframes": { qualified: { declarations: true } },
  1249. "@font-face": { declarations: true },
  1250. "@supports": { stylesheet: true },
  1251. "@scope": { stylesheet: true },
  1252. "@counter-style": { declarations: true },
  1253. "@import": null,
  1254. "@font-feature-values": {
  1255. // No qualified rules actually allowed,
  1256. // but have to declare it one way or the other.
  1257. qualified: true,
  1258. "@stylistic": { declarations: true },
  1259. "@styleset": { declarations: true },
  1260. "@character-variants": { declarations: true },
  1261. "@swash": { declarations: true },
  1262. "@ornaments": { declarations: true },
  1263. "@annotation": { declarations: true },
  1264. },
  1265. "@viewport": { declarations: true },
  1266. "@page": {
  1267. declarations: true,
  1268. "@top-left-corner": { declarations: true },
  1269. "@top-left": { declarations: true },
  1270. "@top-center": { declarations: true },
  1271. "@top-right": { declarations: true },
  1272. "@top-right-corner": { declarations: true },
  1273. "@right-top": { declarations: true },
  1274. "@right-middle": { declarations: true },
  1275. "@right-bottom": { declarations: true },
  1276. "@right-bottom-corner": { declarations: true },
  1277. "@bottom-right": { declarations: true },
  1278. "@bottom-center": { declarations: true },
  1279. "@bottom-left": { declarations: true },
  1280. "@bottom-left-corner": { declarations: true },
  1281. "@left-bottom": { declarations: true },
  1282. "@left-center": { declarations: true },
  1283. "@left-top": { declarations: true },
  1284. },
  1285. "@custom-selector": null,
  1286. "@custom-media": null
  1287. };
  1288. // Exportation.
  1289. return {
  1290. CSSParserRule: CSSParserRule,
  1291. Stylesheet: Stylesheet,
  1292. AtRule: AtRule,
  1293. QualifiedRule: QualifiedRule,
  1294. Declaration: Declaration,
  1295. SimpleBlock: SimpleBlock,
  1296. Func: Func,
  1297. parseAStylesheet: parseAStylesheet,
  1298. parseAListOfRules: parseAListOfRules,
  1299. parseARule: parseARule,
  1300. parseADeclaration: parseADeclaration,
  1301. parseAListOfDeclarations: parseAListOfDeclarations,
  1302. parseAComponentValue: parseAComponentValue,
  1303. parseAListOfComponentValues: parseAListOfComponentValues,
  1304. parseACommaSeparatedListOfComponentValues: parseACommaSeparatedListOfComponentValues,
  1305. canonicalizeRule: canonicalize,
  1306. CSSGrammar: CSSGrammar
  1307. };
  1308. })();