Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

parser.js 37KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. "use strict";
  2. exports.__esModule = true;
  3. exports["default"] = void 0;
  4. var _root = _interopRequireDefault(require("./selectors/root"));
  5. var _selector = _interopRequireDefault(require("./selectors/selector"));
  6. var _className = _interopRequireDefault(require("./selectors/className"));
  7. var _comment = _interopRequireDefault(require("./selectors/comment"));
  8. var _id = _interopRequireDefault(require("./selectors/id"));
  9. var _tag = _interopRequireDefault(require("./selectors/tag"));
  10. var _string = _interopRequireDefault(require("./selectors/string"));
  11. var _pseudo = _interopRequireDefault(require("./selectors/pseudo"));
  12. var _attribute = _interopRequireWildcard(require("./selectors/attribute"));
  13. var _universal = _interopRequireDefault(require("./selectors/universal"));
  14. var _combinator = _interopRequireDefault(require("./selectors/combinator"));
  15. var _nesting = _interopRequireDefault(require("./selectors/nesting"));
  16. var _sortAscending = _interopRequireDefault(require("./sortAscending"));
  17. var _tokenize = _interopRequireWildcard(require("./tokenize"));
  18. var tokens = _interopRequireWildcard(require("./tokenTypes"));
  19. var types = _interopRequireWildcard(require("./selectors/types"));
  20. var _util = require("./util");
  21. var _WHITESPACE_TOKENS, _Object$assign;
  22. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
  23. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  24. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  25. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  26. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  27. var WHITESPACE_TOKENS = (_WHITESPACE_TOKENS = {}, _WHITESPACE_TOKENS[tokens.space] = true, _WHITESPACE_TOKENS[tokens.cr] = true, _WHITESPACE_TOKENS[tokens.feed] = true, _WHITESPACE_TOKENS[tokens.newline] = true, _WHITESPACE_TOKENS[tokens.tab] = true, _WHITESPACE_TOKENS);
  28. var WHITESPACE_EQUIV_TOKENS = Object.assign({}, WHITESPACE_TOKENS, (_Object$assign = {}, _Object$assign[tokens.comment] = true, _Object$assign));
  29. function tokenStart(token) {
  30. return {
  31. line: token[_tokenize.FIELDS.START_LINE],
  32. column: token[_tokenize.FIELDS.START_COL]
  33. };
  34. }
  35. function tokenEnd(token) {
  36. return {
  37. line: token[_tokenize.FIELDS.END_LINE],
  38. column: token[_tokenize.FIELDS.END_COL]
  39. };
  40. }
  41. function getSource(startLine, startColumn, endLine, endColumn) {
  42. return {
  43. start: {
  44. line: startLine,
  45. column: startColumn
  46. },
  47. end: {
  48. line: endLine,
  49. column: endColumn
  50. }
  51. };
  52. }
  53. function getTokenSource(token) {
  54. return getSource(token[_tokenize.FIELDS.START_LINE], token[_tokenize.FIELDS.START_COL], token[_tokenize.FIELDS.END_LINE], token[_tokenize.FIELDS.END_COL]);
  55. }
  56. function getTokenSourceSpan(startToken, endToken) {
  57. if (!startToken) {
  58. return undefined;
  59. }
  60. return getSource(startToken[_tokenize.FIELDS.START_LINE], startToken[_tokenize.FIELDS.START_COL], endToken[_tokenize.FIELDS.END_LINE], endToken[_tokenize.FIELDS.END_COL]);
  61. }
  62. function unescapeProp(node, prop) {
  63. var value = node[prop];
  64. if (typeof value !== "string") {
  65. return;
  66. }
  67. if (value.indexOf("\\") !== -1) {
  68. (0, _util.ensureObject)(node, 'raws');
  69. node[prop] = (0, _util.unesc)(value);
  70. if (node.raws[prop] === undefined) {
  71. node.raws[prop] = value;
  72. }
  73. }
  74. return node;
  75. }
  76. function indexesOf(array, item) {
  77. var i = -1;
  78. var indexes = [];
  79. while ((i = array.indexOf(item, i + 1)) !== -1) {
  80. indexes.push(i);
  81. }
  82. return indexes;
  83. }
  84. function uniqs() {
  85. var list = Array.prototype.concat.apply([], arguments);
  86. return list.filter(function (item, i) {
  87. return i === list.indexOf(item);
  88. });
  89. }
  90. var Parser = /*#__PURE__*/function () {
  91. function Parser(rule, options) {
  92. if (options === void 0) {
  93. options = {};
  94. }
  95. this.rule = rule;
  96. this.options = Object.assign({
  97. lossy: false,
  98. safe: false
  99. }, options);
  100. this.position = 0;
  101. this.css = typeof this.rule === 'string' ? this.rule : this.rule.selector;
  102. this.tokens = (0, _tokenize["default"])({
  103. css: this.css,
  104. error: this._errorGenerator(),
  105. safe: this.options.safe
  106. });
  107. var rootSource = getTokenSourceSpan(this.tokens[0], this.tokens[this.tokens.length - 1]);
  108. this.root = new _root["default"]({
  109. source: rootSource
  110. });
  111. this.root.errorGenerator = this._errorGenerator();
  112. var selector = new _selector["default"]({
  113. source: {
  114. start: {
  115. line: 1,
  116. column: 1
  117. }
  118. }
  119. });
  120. this.root.append(selector);
  121. this.current = selector;
  122. this.loop();
  123. }
  124. var _proto = Parser.prototype;
  125. _proto._errorGenerator = function _errorGenerator() {
  126. var _this = this;
  127. return function (message, errorOptions) {
  128. if (typeof _this.rule === 'string') {
  129. return new Error(message);
  130. }
  131. return _this.rule.error(message, errorOptions);
  132. };
  133. };
  134. _proto.attribute = function attribute() {
  135. var attr = [];
  136. var startingToken = this.currToken;
  137. this.position++;
  138. while (this.position < this.tokens.length && this.currToken[_tokenize.FIELDS.TYPE] !== tokens.closeSquare) {
  139. attr.push(this.currToken);
  140. this.position++;
  141. }
  142. if (this.currToken[_tokenize.FIELDS.TYPE] !== tokens.closeSquare) {
  143. return this.expected('closing square bracket', this.currToken[_tokenize.FIELDS.START_POS]);
  144. }
  145. var len = attr.length;
  146. var node = {
  147. source: getSource(startingToken[1], startingToken[2], this.currToken[3], this.currToken[4]),
  148. sourceIndex: startingToken[_tokenize.FIELDS.START_POS]
  149. };
  150. if (len === 1 && !~[tokens.word].indexOf(attr[0][_tokenize.FIELDS.TYPE])) {
  151. return this.expected('attribute', attr[0][_tokenize.FIELDS.START_POS]);
  152. }
  153. var pos = 0;
  154. var spaceBefore = '';
  155. var commentBefore = '';
  156. var lastAdded = null;
  157. var spaceAfterMeaningfulToken = false;
  158. while (pos < len) {
  159. var token = attr[pos];
  160. var content = this.content(token);
  161. var next = attr[pos + 1];
  162. switch (token[_tokenize.FIELDS.TYPE]) {
  163. case tokens.space:
  164. // if (
  165. // len === 1 ||
  166. // pos === 0 && this.content(next) === '|'
  167. // ) {
  168. // return this.expected('attribute', token[TOKEN.START_POS], content);
  169. // }
  170. spaceAfterMeaningfulToken = true;
  171. if (this.options.lossy) {
  172. break;
  173. }
  174. if (lastAdded) {
  175. (0, _util.ensureObject)(node, 'spaces', lastAdded);
  176. var prevContent = node.spaces[lastAdded].after || '';
  177. node.spaces[lastAdded].after = prevContent + content;
  178. var existingComment = (0, _util.getProp)(node, 'raws', 'spaces', lastAdded, 'after') || null;
  179. if (existingComment) {
  180. node.raws.spaces[lastAdded].after = existingComment + content;
  181. }
  182. } else {
  183. spaceBefore = spaceBefore + content;
  184. commentBefore = commentBefore + content;
  185. }
  186. break;
  187. case tokens.asterisk:
  188. if (next[_tokenize.FIELDS.TYPE] === tokens.equals) {
  189. node.operator = content;
  190. lastAdded = 'operator';
  191. } else if ((!node.namespace || lastAdded === "namespace" && !spaceAfterMeaningfulToken) && next) {
  192. if (spaceBefore) {
  193. (0, _util.ensureObject)(node, 'spaces', 'attribute');
  194. node.spaces.attribute.before = spaceBefore;
  195. spaceBefore = '';
  196. }
  197. if (commentBefore) {
  198. (0, _util.ensureObject)(node, 'raws', 'spaces', 'attribute');
  199. node.raws.spaces.attribute.before = spaceBefore;
  200. commentBefore = '';
  201. }
  202. node.namespace = (node.namespace || "") + content;
  203. var rawValue = (0, _util.getProp)(node, 'raws', 'namespace') || null;
  204. if (rawValue) {
  205. node.raws.namespace += content;
  206. }
  207. lastAdded = 'namespace';
  208. }
  209. spaceAfterMeaningfulToken = false;
  210. break;
  211. case tokens.dollar:
  212. if (lastAdded === "value") {
  213. var oldRawValue = (0, _util.getProp)(node, 'raws', 'value');
  214. node.value += "$";
  215. if (oldRawValue) {
  216. node.raws.value = oldRawValue + "$";
  217. }
  218. break;
  219. }
  220. // Falls through
  221. case tokens.caret:
  222. if (next[_tokenize.FIELDS.TYPE] === tokens.equals) {
  223. node.operator = content;
  224. lastAdded = 'operator';
  225. }
  226. spaceAfterMeaningfulToken = false;
  227. break;
  228. case tokens.combinator:
  229. if (content === '~' && next[_tokenize.FIELDS.TYPE] === tokens.equals) {
  230. node.operator = content;
  231. lastAdded = 'operator';
  232. }
  233. if (content !== '|') {
  234. spaceAfterMeaningfulToken = false;
  235. break;
  236. }
  237. if (next[_tokenize.FIELDS.TYPE] === tokens.equals) {
  238. node.operator = content;
  239. lastAdded = 'operator';
  240. } else if (!node.namespace && !node.attribute) {
  241. node.namespace = true;
  242. }
  243. spaceAfterMeaningfulToken = false;
  244. break;
  245. case tokens.word:
  246. if (next && this.content(next) === '|' && attr[pos + 2] && attr[pos + 2][_tokenize.FIELDS.TYPE] !== tokens.equals && // this look-ahead probably fails with comment nodes involved.
  247. !node.operator && !node.namespace) {
  248. node.namespace = content;
  249. lastAdded = 'namespace';
  250. } else if (!node.attribute || lastAdded === "attribute" && !spaceAfterMeaningfulToken) {
  251. if (spaceBefore) {
  252. (0, _util.ensureObject)(node, 'spaces', 'attribute');
  253. node.spaces.attribute.before = spaceBefore;
  254. spaceBefore = '';
  255. }
  256. if (commentBefore) {
  257. (0, _util.ensureObject)(node, 'raws', 'spaces', 'attribute');
  258. node.raws.spaces.attribute.before = commentBefore;
  259. commentBefore = '';
  260. }
  261. node.attribute = (node.attribute || "") + content;
  262. var _rawValue = (0, _util.getProp)(node, 'raws', 'attribute') || null;
  263. if (_rawValue) {
  264. node.raws.attribute += content;
  265. }
  266. lastAdded = 'attribute';
  267. } else if (!node.value && node.value !== "" || lastAdded === "value" && !spaceAfterMeaningfulToken) {
  268. var _unescaped = (0, _util.unesc)(content);
  269. var _oldRawValue = (0, _util.getProp)(node, 'raws', 'value') || '';
  270. var oldValue = node.value || '';
  271. node.value = oldValue + _unescaped;
  272. node.quoteMark = null;
  273. if (_unescaped !== content || _oldRawValue) {
  274. (0, _util.ensureObject)(node, 'raws');
  275. node.raws.value = (_oldRawValue || oldValue) + content;
  276. }
  277. lastAdded = 'value';
  278. } else {
  279. var insensitive = content === 'i' || content === "I";
  280. if ((node.value || node.value === '') && (node.quoteMark || spaceAfterMeaningfulToken)) {
  281. node.insensitive = insensitive;
  282. if (!insensitive || content === "I") {
  283. (0, _util.ensureObject)(node, 'raws');
  284. node.raws.insensitiveFlag = content;
  285. }
  286. lastAdded = 'insensitive';
  287. if (spaceBefore) {
  288. (0, _util.ensureObject)(node, 'spaces', 'insensitive');
  289. node.spaces.insensitive.before = spaceBefore;
  290. spaceBefore = '';
  291. }
  292. if (commentBefore) {
  293. (0, _util.ensureObject)(node, 'raws', 'spaces', 'insensitive');
  294. node.raws.spaces.insensitive.before = commentBefore;
  295. commentBefore = '';
  296. }
  297. } else if (node.value || node.value === '') {
  298. lastAdded = 'value';
  299. node.value += content;
  300. if (node.raws.value) {
  301. node.raws.value += content;
  302. }
  303. }
  304. }
  305. spaceAfterMeaningfulToken = false;
  306. break;
  307. case tokens.str:
  308. if (!node.attribute || !node.operator) {
  309. return this.error("Expected an attribute followed by an operator preceding the string.", {
  310. index: token[_tokenize.FIELDS.START_POS]
  311. });
  312. }
  313. var _unescapeValue = (0, _attribute.unescapeValue)(content),
  314. unescaped = _unescapeValue.unescaped,
  315. quoteMark = _unescapeValue.quoteMark;
  316. node.value = unescaped;
  317. node.quoteMark = quoteMark;
  318. lastAdded = 'value';
  319. (0, _util.ensureObject)(node, 'raws');
  320. node.raws.value = content;
  321. spaceAfterMeaningfulToken = false;
  322. break;
  323. case tokens.equals:
  324. if (!node.attribute) {
  325. return this.expected('attribute', token[_tokenize.FIELDS.START_POS], content);
  326. }
  327. if (node.value) {
  328. return this.error('Unexpected "=" found; an operator was already defined.', {
  329. index: token[_tokenize.FIELDS.START_POS]
  330. });
  331. }
  332. node.operator = node.operator ? node.operator + content : content;
  333. lastAdded = 'operator';
  334. spaceAfterMeaningfulToken = false;
  335. break;
  336. case tokens.comment:
  337. if (lastAdded) {
  338. if (spaceAfterMeaningfulToken || next && next[_tokenize.FIELDS.TYPE] === tokens.space || lastAdded === 'insensitive') {
  339. var lastComment = (0, _util.getProp)(node, 'spaces', lastAdded, 'after') || '';
  340. var rawLastComment = (0, _util.getProp)(node, 'raws', 'spaces', lastAdded, 'after') || lastComment;
  341. (0, _util.ensureObject)(node, 'raws', 'spaces', lastAdded);
  342. node.raws.spaces[lastAdded].after = rawLastComment + content;
  343. } else {
  344. var lastValue = node[lastAdded] || '';
  345. var rawLastValue = (0, _util.getProp)(node, 'raws', lastAdded) || lastValue;
  346. (0, _util.ensureObject)(node, 'raws');
  347. node.raws[lastAdded] = rawLastValue + content;
  348. }
  349. } else {
  350. commentBefore = commentBefore + content;
  351. }
  352. break;
  353. default:
  354. return this.error("Unexpected \"" + content + "\" found.", {
  355. index: token[_tokenize.FIELDS.START_POS]
  356. });
  357. }
  358. pos++;
  359. }
  360. unescapeProp(node, "attribute");
  361. unescapeProp(node, "namespace");
  362. this.newNode(new _attribute["default"](node));
  363. this.position++;
  364. }
  365. /**
  366. * return a node containing meaningless garbage up to (but not including) the specified token position.
  367. * if the token position is negative, all remaining tokens are consumed.
  368. *
  369. * This returns an array containing a single string node if all whitespace,
  370. * otherwise an array of comment nodes with space before and after.
  371. *
  372. * These tokens are not added to the current selector, the caller can add them or use them to amend
  373. * a previous node's space metadata.
  374. *
  375. * In lossy mode, this returns only comments.
  376. */
  377. ;
  378. _proto.parseWhitespaceEquivalentTokens = function parseWhitespaceEquivalentTokens(stopPosition) {
  379. if (stopPosition < 0) {
  380. stopPosition = this.tokens.length;
  381. }
  382. var startPosition = this.position;
  383. var nodes = [];
  384. var space = "";
  385. var lastComment = undefined;
  386. do {
  387. if (WHITESPACE_TOKENS[this.currToken[_tokenize.FIELDS.TYPE]]) {
  388. if (!this.options.lossy) {
  389. space += this.content();
  390. }
  391. } else if (this.currToken[_tokenize.FIELDS.TYPE] === tokens.comment) {
  392. var spaces = {};
  393. if (space) {
  394. spaces.before = space;
  395. space = "";
  396. }
  397. lastComment = new _comment["default"]({
  398. value: this.content(),
  399. source: getTokenSource(this.currToken),
  400. sourceIndex: this.currToken[_tokenize.FIELDS.START_POS],
  401. spaces: spaces
  402. });
  403. nodes.push(lastComment);
  404. }
  405. } while (++this.position < stopPosition);
  406. if (space) {
  407. if (lastComment) {
  408. lastComment.spaces.after = space;
  409. } else if (!this.options.lossy) {
  410. var firstToken = this.tokens[startPosition];
  411. var lastToken = this.tokens[this.position - 1];
  412. nodes.push(new _string["default"]({
  413. value: '',
  414. source: getSource(firstToken[_tokenize.FIELDS.START_LINE], firstToken[_tokenize.FIELDS.START_COL], lastToken[_tokenize.FIELDS.END_LINE], lastToken[_tokenize.FIELDS.END_COL]),
  415. sourceIndex: firstToken[_tokenize.FIELDS.START_POS],
  416. spaces: {
  417. before: space,
  418. after: ''
  419. }
  420. }));
  421. }
  422. }
  423. return nodes;
  424. }
  425. /**
  426. *
  427. * @param {*} nodes
  428. */
  429. ;
  430. _proto.convertWhitespaceNodesToSpace = function convertWhitespaceNodesToSpace(nodes, requiredSpace) {
  431. var _this2 = this;
  432. if (requiredSpace === void 0) {
  433. requiredSpace = false;
  434. }
  435. var space = "";
  436. var rawSpace = "";
  437. nodes.forEach(function (n) {
  438. var spaceBefore = _this2.lossySpace(n.spaces.before, requiredSpace);
  439. var rawSpaceBefore = _this2.lossySpace(n.rawSpaceBefore, requiredSpace);
  440. space += spaceBefore + _this2.lossySpace(n.spaces.after, requiredSpace && spaceBefore.length === 0);
  441. rawSpace += spaceBefore + n.value + _this2.lossySpace(n.rawSpaceAfter, requiredSpace && rawSpaceBefore.length === 0);
  442. });
  443. if (rawSpace === space) {
  444. rawSpace = undefined;
  445. }
  446. var result = {
  447. space: space,
  448. rawSpace: rawSpace
  449. };
  450. return result;
  451. };
  452. _proto.isNamedCombinator = function isNamedCombinator(position) {
  453. if (position === void 0) {
  454. position = this.position;
  455. }
  456. return this.tokens[position + 0] && this.tokens[position + 0][_tokenize.FIELDS.TYPE] === tokens.slash && this.tokens[position + 1] && this.tokens[position + 1][_tokenize.FIELDS.TYPE] === tokens.word && this.tokens[position + 2] && this.tokens[position + 2][_tokenize.FIELDS.TYPE] === tokens.slash;
  457. };
  458. _proto.namedCombinator = function namedCombinator() {
  459. if (this.isNamedCombinator()) {
  460. var nameRaw = this.content(this.tokens[this.position + 1]);
  461. var name = (0, _util.unesc)(nameRaw).toLowerCase();
  462. var raws = {};
  463. if (name !== nameRaw) {
  464. raws.value = "/" + nameRaw + "/";
  465. }
  466. var node = new _combinator["default"]({
  467. value: "/" + name + "/",
  468. source: getSource(this.currToken[_tokenize.FIELDS.START_LINE], this.currToken[_tokenize.FIELDS.START_COL], this.tokens[this.position + 2][_tokenize.FIELDS.END_LINE], this.tokens[this.position + 2][_tokenize.FIELDS.END_COL]),
  469. sourceIndex: this.currToken[_tokenize.FIELDS.START_POS],
  470. raws: raws
  471. });
  472. this.position = this.position + 3;
  473. return node;
  474. } else {
  475. this.unexpected();
  476. }
  477. };
  478. _proto.combinator = function combinator() {
  479. var _this3 = this;
  480. if (this.content() === '|') {
  481. return this.namespace();
  482. } // We need to decide between a space that's a descendant combinator and meaningless whitespace at the end of a selector.
  483. var nextSigTokenPos = this.locateNextMeaningfulToken(this.position);
  484. if (nextSigTokenPos < 0 || this.tokens[nextSigTokenPos][_tokenize.FIELDS.TYPE] === tokens.comma) {
  485. var nodes = this.parseWhitespaceEquivalentTokens(nextSigTokenPos);
  486. if (nodes.length > 0) {
  487. var last = this.current.last;
  488. if (last) {
  489. var _this$convertWhitespa = this.convertWhitespaceNodesToSpace(nodes),
  490. space = _this$convertWhitespa.space,
  491. rawSpace = _this$convertWhitespa.rawSpace;
  492. if (rawSpace !== undefined) {
  493. last.rawSpaceAfter += rawSpace;
  494. }
  495. last.spaces.after += space;
  496. } else {
  497. nodes.forEach(function (n) {
  498. return _this3.newNode(n);
  499. });
  500. }
  501. }
  502. return;
  503. }
  504. var firstToken = this.currToken;
  505. var spaceOrDescendantSelectorNodes = undefined;
  506. if (nextSigTokenPos > this.position) {
  507. spaceOrDescendantSelectorNodes = this.parseWhitespaceEquivalentTokens(nextSigTokenPos);
  508. }
  509. var node;
  510. if (this.isNamedCombinator()) {
  511. node = this.namedCombinator();
  512. } else if (this.currToken[_tokenize.FIELDS.TYPE] === tokens.combinator) {
  513. node = new _combinator["default"]({
  514. value: this.content(),
  515. source: getTokenSource(this.currToken),
  516. sourceIndex: this.currToken[_tokenize.FIELDS.START_POS]
  517. });
  518. this.position++;
  519. } else if (WHITESPACE_TOKENS[this.currToken[_tokenize.FIELDS.TYPE]]) {// pass
  520. } else if (!spaceOrDescendantSelectorNodes) {
  521. this.unexpected();
  522. }
  523. if (node) {
  524. if (spaceOrDescendantSelectorNodes) {
  525. var _this$convertWhitespa2 = this.convertWhitespaceNodesToSpace(spaceOrDescendantSelectorNodes),
  526. _space = _this$convertWhitespa2.space,
  527. _rawSpace = _this$convertWhitespa2.rawSpace;
  528. node.spaces.before = _space;
  529. node.rawSpaceBefore = _rawSpace;
  530. }
  531. } else {
  532. // descendant combinator
  533. var _this$convertWhitespa3 = this.convertWhitespaceNodesToSpace(spaceOrDescendantSelectorNodes, true),
  534. _space2 = _this$convertWhitespa3.space,
  535. _rawSpace2 = _this$convertWhitespa3.rawSpace;
  536. if (!_rawSpace2) {
  537. _rawSpace2 = _space2;
  538. }
  539. var spaces = {};
  540. var raws = {
  541. spaces: {}
  542. };
  543. if (_space2.endsWith(' ') && _rawSpace2.endsWith(' ')) {
  544. spaces.before = _space2.slice(0, _space2.length - 1);
  545. raws.spaces.before = _rawSpace2.slice(0, _rawSpace2.length - 1);
  546. } else if (_space2.startsWith(' ') && _rawSpace2.startsWith(' ')) {
  547. spaces.after = _space2.slice(1);
  548. raws.spaces.after = _rawSpace2.slice(1);
  549. } else {
  550. raws.value = _rawSpace2;
  551. }
  552. node = new _combinator["default"]({
  553. value: ' ',
  554. source: getTokenSourceSpan(firstToken, this.tokens[this.position - 1]),
  555. sourceIndex: firstToken[_tokenize.FIELDS.START_POS],
  556. spaces: spaces,
  557. raws: raws
  558. });
  559. }
  560. if (this.currToken && this.currToken[_tokenize.FIELDS.TYPE] === tokens.space) {
  561. node.spaces.after = this.optionalSpace(this.content());
  562. this.position++;
  563. }
  564. return this.newNode(node);
  565. };
  566. _proto.comma = function comma() {
  567. if (this.position === this.tokens.length - 1) {
  568. this.root.trailingComma = true;
  569. this.position++;
  570. return;
  571. }
  572. this.current._inferEndPosition();
  573. var selector = new _selector["default"]({
  574. source: {
  575. start: tokenStart(this.tokens[this.position + 1])
  576. }
  577. });
  578. this.current.parent.append(selector);
  579. this.current = selector;
  580. this.position++;
  581. };
  582. _proto.comment = function comment() {
  583. var current = this.currToken;
  584. this.newNode(new _comment["default"]({
  585. value: this.content(),
  586. source: getTokenSource(current),
  587. sourceIndex: current[_tokenize.FIELDS.START_POS]
  588. }));
  589. this.position++;
  590. };
  591. _proto.error = function error(message, opts) {
  592. throw this.root.error(message, opts);
  593. };
  594. _proto.missingBackslash = function missingBackslash() {
  595. return this.error('Expected a backslash preceding the semicolon.', {
  596. index: this.currToken[_tokenize.FIELDS.START_POS]
  597. });
  598. };
  599. _proto.missingParenthesis = function missingParenthesis() {
  600. return this.expected('opening parenthesis', this.currToken[_tokenize.FIELDS.START_POS]);
  601. };
  602. _proto.missingSquareBracket = function missingSquareBracket() {
  603. return this.expected('opening square bracket', this.currToken[_tokenize.FIELDS.START_POS]);
  604. };
  605. _proto.unexpected = function unexpected() {
  606. return this.error("Unexpected '" + this.content() + "'. Escaping special characters with \\ may help.", this.currToken[_tokenize.FIELDS.START_POS]);
  607. };
  608. _proto.namespace = function namespace() {
  609. var before = this.prevToken && this.content(this.prevToken) || true;
  610. if (this.nextToken[_tokenize.FIELDS.TYPE] === tokens.word) {
  611. this.position++;
  612. return this.word(before);
  613. } else if (this.nextToken[_tokenize.FIELDS.TYPE] === tokens.asterisk) {
  614. this.position++;
  615. return this.universal(before);
  616. }
  617. };
  618. _proto.nesting = function nesting() {
  619. if (this.nextToken) {
  620. var nextContent = this.content(this.nextToken);
  621. if (nextContent === "|") {
  622. this.position++;
  623. return;
  624. }
  625. }
  626. var current = this.currToken;
  627. this.newNode(new _nesting["default"]({
  628. value: this.content(),
  629. source: getTokenSource(current),
  630. sourceIndex: current[_tokenize.FIELDS.START_POS]
  631. }));
  632. this.position++;
  633. };
  634. _proto.parentheses = function parentheses() {
  635. var last = this.current.last;
  636. var unbalanced = 1;
  637. this.position++;
  638. if (last && last.type === types.PSEUDO) {
  639. var selector = new _selector["default"]({
  640. source: {
  641. start: tokenStart(this.tokens[this.position - 1])
  642. }
  643. });
  644. var cache = this.current;
  645. last.append(selector);
  646. this.current = selector;
  647. while (this.position < this.tokens.length && unbalanced) {
  648. if (this.currToken[_tokenize.FIELDS.TYPE] === tokens.openParenthesis) {
  649. unbalanced++;
  650. }
  651. if (this.currToken[_tokenize.FIELDS.TYPE] === tokens.closeParenthesis) {
  652. unbalanced--;
  653. }
  654. if (unbalanced) {
  655. this.parse();
  656. } else {
  657. this.current.source.end = tokenEnd(this.currToken);
  658. this.current.parent.source.end = tokenEnd(this.currToken);
  659. this.position++;
  660. }
  661. }
  662. this.current = cache;
  663. } else {
  664. // I think this case should be an error. It's used to implement a basic parse of media queries
  665. // but I don't think it's a good idea.
  666. var parenStart = this.currToken;
  667. var parenValue = "(";
  668. var parenEnd;
  669. while (this.position < this.tokens.length && unbalanced) {
  670. if (this.currToken[_tokenize.FIELDS.TYPE] === tokens.openParenthesis) {
  671. unbalanced++;
  672. }
  673. if (this.currToken[_tokenize.FIELDS.TYPE] === tokens.closeParenthesis) {
  674. unbalanced--;
  675. }
  676. parenEnd = this.currToken;
  677. parenValue += this.parseParenthesisToken(this.currToken);
  678. this.position++;
  679. }
  680. if (last) {
  681. last.appendToPropertyAndEscape("value", parenValue, parenValue);
  682. } else {
  683. this.newNode(new _string["default"]({
  684. value: parenValue,
  685. source: getSource(parenStart[_tokenize.FIELDS.START_LINE], parenStart[_tokenize.FIELDS.START_COL], parenEnd[_tokenize.FIELDS.END_LINE], parenEnd[_tokenize.FIELDS.END_COL]),
  686. sourceIndex: parenStart[_tokenize.FIELDS.START_POS]
  687. }));
  688. }
  689. }
  690. if (unbalanced) {
  691. return this.expected('closing parenthesis', this.currToken[_tokenize.FIELDS.START_POS]);
  692. }
  693. };
  694. _proto.pseudo = function pseudo() {
  695. var _this4 = this;
  696. var pseudoStr = '';
  697. var startingToken = this.currToken;
  698. while (this.currToken && this.currToken[_tokenize.FIELDS.TYPE] === tokens.colon) {
  699. pseudoStr += this.content();
  700. this.position++;
  701. }
  702. if (!this.currToken) {
  703. return this.expected(['pseudo-class', 'pseudo-element'], this.position - 1);
  704. }
  705. if (this.currToken[_tokenize.FIELDS.TYPE] === tokens.word) {
  706. this.splitWord(false, function (first, length) {
  707. pseudoStr += first;
  708. _this4.newNode(new _pseudo["default"]({
  709. value: pseudoStr,
  710. source: getTokenSourceSpan(startingToken, _this4.currToken),
  711. sourceIndex: startingToken[_tokenize.FIELDS.START_POS]
  712. }));
  713. if (length > 1 && _this4.nextToken && _this4.nextToken[_tokenize.FIELDS.TYPE] === tokens.openParenthesis) {
  714. _this4.error('Misplaced parenthesis.', {
  715. index: _this4.nextToken[_tokenize.FIELDS.START_POS]
  716. });
  717. }
  718. });
  719. } else {
  720. return this.expected(['pseudo-class', 'pseudo-element'], this.currToken[_tokenize.FIELDS.START_POS]);
  721. }
  722. };
  723. _proto.space = function space() {
  724. var content = this.content(); // Handle space before and after the selector
  725. if (this.position === 0 || this.prevToken[_tokenize.FIELDS.TYPE] === tokens.comma || this.prevToken[_tokenize.FIELDS.TYPE] === tokens.openParenthesis || this.current.nodes.every(function (node) {
  726. return node.type === 'comment';
  727. })) {
  728. this.spaces = this.optionalSpace(content);
  729. this.position++;
  730. } else if (this.position === this.tokens.length - 1 || this.nextToken[_tokenize.FIELDS.TYPE] === tokens.comma || this.nextToken[_tokenize.FIELDS.TYPE] === tokens.closeParenthesis) {
  731. this.current.last.spaces.after = this.optionalSpace(content);
  732. this.position++;
  733. } else {
  734. this.combinator();
  735. }
  736. };
  737. _proto.string = function string() {
  738. var current = this.currToken;
  739. this.newNode(new _string["default"]({
  740. value: this.content(),
  741. source: getTokenSource(current),
  742. sourceIndex: current[_tokenize.FIELDS.START_POS]
  743. }));
  744. this.position++;
  745. };
  746. _proto.universal = function universal(namespace) {
  747. var nextToken = this.nextToken;
  748. if (nextToken && this.content(nextToken) === '|') {
  749. this.position++;
  750. return this.namespace();
  751. }
  752. var current = this.currToken;
  753. this.newNode(new _universal["default"]({
  754. value: this.content(),
  755. source: getTokenSource(current),
  756. sourceIndex: current[_tokenize.FIELDS.START_POS]
  757. }), namespace);
  758. this.position++;
  759. };
  760. _proto.splitWord = function splitWord(namespace, firstCallback) {
  761. var _this5 = this;
  762. var nextToken = this.nextToken;
  763. var word = this.content();
  764. while (nextToken && ~[tokens.dollar, tokens.caret, tokens.equals, tokens.word].indexOf(nextToken[_tokenize.FIELDS.TYPE])) {
  765. this.position++;
  766. var current = this.content();
  767. word += current;
  768. if (current.lastIndexOf('\\') === current.length - 1) {
  769. var next = this.nextToken;
  770. if (next && next[_tokenize.FIELDS.TYPE] === tokens.space) {
  771. word += this.requiredSpace(this.content(next));
  772. this.position++;
  773. }
  774. }
  775. nextToken = this.nextToken;
  776. }
  777. var hasClass = indexesOf(word, '.').filter(function (i) {
  778. return word[i - 1] !== '\\';
  779. });
  780. var hasId = indexesOf(word, '#').filter(function (i) {
  781. return word[i - 1] !== '\\';
  782. }); // Eliminate Sass interpolations from the list of id indexes
  783. var interpolations = indexesOf(word, '#{');
  784. if (interpolations.length) {
  785. hasId = hasId.filter(function (hashIndex) {
  786. return !~interpolations.indexOf(hashIndex);
  787. });
  788. }
  789. var indices = (0, _sortAscending["default"])(uniqs([0].concat(hasClass, hasId)));
  790. indices.forEach(function (ind, i) {
  791. var index = indices[i + 1] || word.length;
  792. var value = word.slice(ind, index);
  793. if (i === 0 && firstCallback) {
  794. return firstCallback.call(_this5, value, indices.length);
  795. }
  796. var node;
  797. var current = _this5.currToken;
  798. var sourceIndex = current[_tokenize.FIELDS.START_POS] + indices[i];
  799. var source = getSource(current[1], current[2] + ind, current[3], current[2] + (index - 1));
  800. if (~hasClass.indexOf(ind)) {
  801. var classNameOpts = {
  802. value: value.slice(1),
  803. source: source,
  804. sourceIndex: sourceIndex
  805. };
  806. node = new _className["default"](unescapeProp(classNameOpts, "value"));
  807. } else if (~hasId.indexOf(ind)) {
  808. var idOpts = {
  809. value: value.slice(1),
  810. source: source,
  811. sourceIndex: sourceIndex
  812. };
  813. node = new _id["default"](unescapeProp(idOpts, "value"));
  814. } else {
  815. var tagOpts = {
  816. value: value,
  817. source: source,
  818. sourceIndex: sourceIndex
  819. };
  820. unescapeProp(tagOpts, "value");
  821. node = new _tag["default"](tagOpts);
  822. }
  823. _this5.newNode(node, namespace); // Ensure that the namespace is used only once
  824. namespace = null;
  825. });
  826. this.position++;
  827. };
  828. _proto.word = function word(namespace) {
  829. var nextToken = this.nextToken;
  830. if (nextToken && this.content(nextToken) === '|') {
  831. this.position++;
  832. return this.namespace();
  833. }
  834. return this.splitWord(namespace);
  835. };
  836. _proto.loop = function loop() {
  837. while (this.position < this.tokens.length) {
  838. this.parse(true);
  839. }
  840. this.current._inferEndPosition();
  841. return this.root;
  842. };
  843. _proto.parse = function parse(throwOnParenthesis) {
  844. switch (this.currToken[_tokenize.FIELDS.TYPE]) {
  845. case tokens.space:
  846. this.space();
  847. break;
  848. case tokens.comment:
  849. this.comment();
  850. break;
  851. case tokens.openParenthesis:
  852. this.parentheses();
  853. break;
  854. case tokens.closeParenthesis:
  855. if (throwOnParenthesis) {
  856. this.missingParenthesis();
  857. }
  858. break;
  859. case tokens.openSquare:
  860. this.attribute();
  861. break;
  862. case tokens.dollar:
  863. case tokens.caret:
  864. case tokens.equals:
  865. case tokens.word:
  866. this.word();
  867. break;
  868. case tokens.colon:
  869. this.pseudo();
  870. break;
  871. case tokens.comma:
  872. this.comma();
  873. break;
  874. case tokens.asterisk:
  875. this.universal();
  876. break;
  877. case tokens.ampersand:
  878. this.nesting();
  879. break;
  880. case tokens.slash:
  881. case tokens.combinator:
  882. this.combinator();
  883. break;
  884. case tokens.str:
  885. this.string();
  886. break;
  887. // These cases throw; no break needed.
  888. case tokens.closeSquare:
  889. this.missingSquareBracket();
  890. case tokens.semicolon:
  891. this.missingBackslash();
  892. default:
  893. this.unexpected();
  894. }
  895. }
  896. /**
  897. * Helpers
  898. */
  899. ;
  900. _proto.expected = function expected(description, index, found) {
  901. if (Array.isArray(description)) {
  902. var last = description.pop();
  903. description = description.join(', ') + " or " + last;
  904. }
  905. var an = /^[aeiou]/.test(description[0]) ? 'an' : 'a';
  906. if (!found) {
  907. return this.error("Expected " + an + " " + description + ".", {
  908. index: index
  909. });
  910. }
  911. return this.error("Expected " + an + " " + description + ", found \"" + found + "\" instead.", {
  912. index: index
  913. });
  914. };
  915. _proto.requiredSpace = function requiredSpace(space) {
  916. return this.options.lossy ? ' ' : space;
  917. };
  918. _proto.optionalSpace = function optionalSpace(space) {
  919. return this.options.lossy ? '' : space;
  920. };
  921. _proto.lossySpace = function lossySpace(space, required) {
  922. if (this.options.lossy) {
  923. return required ? ' ' : '';
  924. } else {
  925. return space;
  926. }
  927. };
  928. _proto.parseParenthesisToken = function parseParenthesisToken(token) {
  929. var content = this.content(token);
  930. if (token[_tokenize.FIELDS.TYPE] === tokens.space) {
  931. return this.requiredSpace(content);
  932. } else {
  933. return content;
  934. }
  935. };
  936. _proto.newNode = function newNode(node, namespace) {
  937. if (namespace) {
  938. if (/^ +$/.test(namespace)) {
  939. if (!this.options.lossy) {
  940. this.spaces = (this.spaces || '') + namespace;
  941. }
  942. namespace = true;
  943. }
  944. node.namespace = namespace;
  945. unescapeProp(node, "namespace");
  946. }
  947. if (this.spaces) {
  948. node.spaces.before = this.spaces;
  949. this.spaces = '';
  950. }
  951. return this.current.append(node);
  952. };
  953. _proto.content = function content(token) {
  954. if (token === void 0) {
  955. token = this.currToken;
  956. }
  957. return this.css.slice(token[_tokenize.FIELDS.START_POS], token[_tokenize.FIELDS.END_POS]);
  958. };
  959. /**
  960. * returns the index of the next non-whitespace, non-comment token.
  961. * returns -1 if no meaningful token is found.
  962. */
  963. _proto.locateNextMeaningfulToken = function locateNextMeaningfulToken(startPosition) {
  964. if (startPosition === void 0) {
  965. startPosition = this.position + 1;
  966. }
  967. var searchPosition = startPosition;
  968. while (searchPosition < this.tokens.length) {
  969. if (WHITESPACE_EQUIV_TOKENS[this.tokens[searchPosition][_tokenize.FIELDS.TYPE]]) {
  970. searchPosition++;
  971. continue;
  972. } else {
  973. return searchPosition;
  974. }
  975. }
  976. return -1;
  977. };
  978. _createClass(Parser, [{
  979. key: "currToken",
  980. get: function get() {
  981. return this.tokens[this.position];
  982. }
  983. }, {
  984. key: "nextToken",
  985. get: function get() {
  986. return this.tokens[this.position + 1];
  987. }
  988. }, {
  989. key: "prevToken",
  990. get: function get() {
  991. return this.tokens[this.position - 1];
  992. }
  993. }]);
  994. return Parser;
  995. }();
  996. exports["default"] = Parser;
  997. module.exports = exports.default;