'use strict'; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var Comment = require('postcss/lib/comment'); var Parser = require('postcss/lib/parser'); var NestedDeclaration = require('./nested-declaration'); var scssTokenizer = require('./scss-tokenize'); var ScssParser = function (_Parser) { _inherits(ScssParser, _Parser); function ScssParser() { _classCallCheck(this, ScssParser); return _possibleConstructorReturn(this, _Parser.apply(this, arguments)); } ScssParser.prototype.createTokenizer = function createTokenizer() { this.tokenizer = scssTokenizer(this.input); }; ScssParser.prototype.rule = function rule(tokens) { var withColon = false; var brackets = 0; var value = ''; for (var _iterator = tokens, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { var _ref; if (_isArray) { if (_i >= _iterator.length) break; _ref = _iterator[_i++]; } else { _i = _iterator.next(); if (_i.done) break; _ref = _i.value; } var i = _ref; if (withColon) { if (i[0] !== 'comment' && i[0] !== '{') { value += i[1]; } } else if (i[0] === 'space' && i[1].indexOf('\n') !== -1) { break; } else if (i[0] === '(') { brackets += 1; } else if (i[0] === ')') { brackets -= 1; } else if (brackets === 0 && i[0] === ':') { withColon = true; } } if (!withColon || value.trim() === '' || /^[a-zA-Z-:#]/.test(value)) { _Parser.prototype.rule.call(this, tokens); } else { tokens.pop(); var node = new NestedDeclaration(); this.init(node); var last = tokens[tokens.length - 1]; if (last[4]) { node.source.end = { line: last[4], column: last[5] }; } else { node.source.end = { line: last[2], column: last[3] }; } while (tokens[0][0] !== 'word') { node.raws.before += tokens.shift()[1]; } node.source.start = { line: tokens[0][2], column: tokens[0][3] }; node.prop = ''; while (tokens.length) { var type = tokens[0][0]; if (type === ':' || type === 'space' || type === 'comment') { break; } node.prop += tokens.shift()[1]; } node.raws.between = ''; var token = void 0; while (tokens.length) { token = tokens.shift(); if (token[0] === ':') { node.raws.between += token[1]; break; } else { node.raws.between += token[1]; } } if (node.prop[0] === '_' || node.prop[0] === '*') { node.raws.before += node.prop[0]; node.prop = node.prop.slice(1); } node.raws.between += this.spacesAndCommentsFromStart(tokens); this.precheckMissedSemicolon(tokens); for (var _i2 = tokens.length - 1; _i2 > 0; _i2--) { token = tokens[_i2]; if (token[1] === '!important') { node.important = true; var string = this.stringFrom(tokens, _i2); string = this.spacesFromEnd(tokens) + string; if (string !== ' !important') { node.raws.important = string; } break; } else if (token[1] === 'important') { var cache = tokens.slice(0); var str = ''; for (var j = _i2; j > 0; j--) { var _type = cache[j][0]; if (str.trim().indexOf('!') === 0 && _type !== 'space') { break; } str = cache.pop()[1] + str; } if (str.trim().indexOf('!') === 0) { node.important = true; node.raws.important = str; tokens = cache; } } if (token[0] !== 'space' && token[0] !== 'comment') { break; } } this.raw(node, 'value', tokens); if (node.value.indexOf(':') !== -1) { this.checkMissedSemicolon(tokens); } this.current = node; } }; ScssParser.prototype.comment = function comment(token) { if (token[6] === 'inline') { var node = new Comment(); this.init(node, token[2], token[3]); node.raws.inline = true; node.source.end = { line: token[4], column: token[5] }; var text = token[1].slice(2); if (/^\s*$/.test(text)) { node.text = ''; node.raws.left = text; node.raws.right = ''; } else { var match = text.match(/^(\s*)([^]*[^\s])(\s*)$/); var fixed = match[2].replace(/(\*\/|\/\*)/g, '*//*'); node.text = fixed; node.raws.left = match[1]; node.raws.right = match[3]; node.raws.text = match[2]; } } else { _Parser.prototype.comment.call(this, token); } }; ScssParser.prototype.raw = function raw(node, prop, tokens) { _Parser.prototype.raw.call(this, node, prop, tokens); if (node.raws[prop]) { var scss = node.raws[prop].raw; node.raws[prop].raw = tokens.reduce(function (all, i) { if (i[0] === 'comment' && i[6] === 'inline') { var text = i[1].slice(2).replace(/(\*\/|\/\*)/g, '*//*'); return all + '/*' + text + '*/'; } else { return all + i[1]; } }, ''); if (scss !== node.raws[prop].raw) { node.raws[prop].scss = scss; } } }; return ScssParser; }(Parser); module.exports = ScssParser; //# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNjc3MtcGFyc2VyLmVzNiJdLCJuYW1lcyI6WyJDb21tZW50IiwicmVxdWlyZSIsIlBhcnNlciIsIk5lc3RlZERlY2xhcmF0aW9uIiwic2Nzc1Rva2VuaXplciIsIlNjc3NQYXJzZXIiLCJjcmVhdGVUb2tlbml6ZXIiLCJ0b2tlbml6ZXIiLCJpbnB1dCIsInJ1bGUiLCJ0b2tlbnMiLCJ3aXRoQ29sb24iLCJicmFja2V0cyIsInZhbHVlIiwiaSIsImluZGV4T2YiLCJ0cmltIiwidGVzdCIsInBvcCIsIm5vZGUiLCJpbml0IiwibGFzdCIsImxlbmd0aCIsInNvdXJjZSIsImVuZCIsImxpbmUiLCJjb2x1bW4iLCJyYXdzIiwiYmVmb3JlIiwic2hpZnQiLCJzdGFydCIsInByb3AiLCJ0eXBlIiwiYmV0d2VlbiIsInRva2VuIiwic2xpY2UiLCJzcGFjZXNBbmRDb21tZW50c0Zyb21TdGFydCIsInByZWNoZWNrTWlzc2VkU2VtaWNvbG9uIiwiaW1wb3J0YW50Iiwic3RyaW5nIiwic3RyaW5nRnJvbSIsInNwYWNlc0Zyb21FbmQiLCJjYWNoZSIsInN0ciIsImoiLCJyYXciLCJjaGVja01pc3NlZFNlbWljb2xvbiIsImN1cnJlbnQiLCJjb21tZW50IiwiaW5saW5lIiwidGV4dCIsImxlZnQiLCJyaWdodCIsIm1hdGNoIiwiZml4ZWQiLCJyZXBsYWNlIiwic2NzcyIsInJlZHVjZSIsImFsbC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SB0b2tlbnMuc2hpZnQoKVxuXG4gICAgICAgIGlmICh0b2tlblswXSA9PT0gJzonKSB7XG4gICAgICAgICAgbm9kZS5yYXdzLmJldHdlZW4gKz0gdG9rZW5bMV1cbiAgICAgICAgICBicmVha1xuICAgICAgICB9IGVsc2Uge1xuICAgICAgICAgIG5vZGUucmF3cy5iZXR3ZWVuICs9IHRva2VuWzFdXG4gICAgICAgIH1cbiAgICAgIH1cblxuICAgICAgaWYgKG5vZGUucHJvcFswXSA9PT0gJ18nIHx8IG5vZGUucHJvcFswXSA9PT0gJyonKSB7XG4gICAgICAgIG5vZGUucmF3cy5iZWZvcmUgKz0gbm9kZS5wcm9wWzBdXG4gICAgICAgIG5vZGUucHJvcCA9IG5vZGUucHJvcC5zbGljZSgxKVxuICAgICAgfVxuICAgICAgbm9kZS5yYXdzLmJldHdlZW4gKz0gdGhpcy5zcGFjZXNBbmRDb21tZW50c0Zyb21TdGFydCh0b2tlbnMpXG4gICAgICB0aGlzLnByZWNoZWNrTWlzc2VkU2VtaWNvbG9uKHRva2VucylcblxuICAgICAgZm9yIChsZXQgaSA9IHRva2Vucy5sZW5ndGggLSAxOyBpID4gMDsgaS0tKSB7XG4gICAgICAgIHRva2VuID0gdG9rZW5zW2ldXG4gICAgICAgIGlmICh0b2tlblsxXSA9PT0gJyFpbXBvcnRhbnQnKSB7XG4gICAgICAgICAgbm9kZS5pbXBvcnRhbnQgPSB0cnVlXG4gICAgICAgICAgbGV0IHN0cmluZyA9IHRoaXMuc3RyaW5nRnJvbSh0b2tlbnMsIGkpXG4gICAgICAgICAgc3RyaW5nID0gdGhpcy5zcGFjZXNGcm9tRW5kKHRva2VucykgKyBzdHJpbmdcbiAgICAgICAgICBpZiAoc3RyaW5nICE9PSAnICFpbXBvcnRhbnQnKSB7XG4gICAgICAgICAgICBub2RlLnJhd3MuaW1wb3J0YW50ID0gc3RyaW5nXG4gICAgICAgICAgfVxuICAgICAgICAgIGJyZWFrXG4gICAgICAgIH0gZWxzZSBpZiAodG9rZW5bMV0gPT09ICdpbXBvcnRhbnQnKSB7XG4gICAgICAgICAgY29uc3QgY2FjaGUgPSB0b2tlbnMuc2xpY2UoMClcbiAgICAgICAgICBsZXQgc3RyID0gJydcbiAgICAgICAgICBmb3IgKGxldCBqID0gaTsgaiA+IDA7IGotLSkge1xuICAgICAgICAgICAgY29uc3QgdHlwZSA9IGNhY2hlW2pdWzBdXG4gICAgICAgICAgICBpZiAoc3RyLnRyaW0oKS5pbmRleE9mKCchJykgPT09IDAgJiZcbiAgICAgICAgICAgICAgICAgICAgICAgICAgICB0eXBlICE9PSAnc3BhY2UnXG4gICAgICAgICAgICApIHtcbiAgICAgICAgICAgICAgYnJlYWtcbiAgICAgICAgICAgIH1cbiAgICAgICAgICAgIHN0ciA9IGNhY2hlLnBvcCgpWzFdICsgc3RyXG4gICAgICAgICAgfVxuICAgICAgICAgIGlmIChzdHIudHJpbSgpLmluZGV4T2YoJyEnKSA9PT0gMCkge1xuICAgICAgICAgICAgbm9kZS5pbXBvcnRhbnQgPSB0cnVlXG4gICAgICAgICAgICBub2RlLnJhd3MuaW1wb3J0YW50ID0gc3RyXG4gICAgICAgICAgICB0b2tlbnMgPSBjYWNoZVxuICAgICAgICAgIH1cbiAgICAgICAgfVxuXG4gICAgICAgIGlmICh0b2tlblswXSAhPT0gJ3NwYWNlJyAmJiB0b2tlblswXSAhPT0gJ2NvbW1lbnQnKSB7XG4gICAgICAgICAgYnJlYWtcbiAgICAgICAgfVxuICAgICAgfVxuXG4gICAgICB0aGlzLnJhdyhub2RlLCAndmFsdWUnLCB0b2tlbnMpXG5cbiAgICAgIGlmIChub2RlLnZhbHVlLmluZGV4T2YoJzonKSAhPT0gLTEpIHtcbiAgICAgICAgdGhpcy5jaGVja01pc3NlZFNlbWljb2xvbih0b2tlbnMpXG4gICAgICB9XG5cbiAgICAgIHRoaXMuY3VycmVudCA9IG5vZGVcbiAgICB9XG4gIH1cblxuICBjb21tZW50ICh0b2tlbikge1xuICAg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