123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- "use strict";
- function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; 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) {
- _inheritsLoose(ScssParser, _Parser);
- function ScssParser() {
- return _Parser.apply(this, arguments) || this;
- }
- var _proto = ScssParser.prototype;
- _proto.createTokenizer = function createTokenizer() {
- this.tokenizer = scssTokenizer(this.input);
- };
- _proto.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 _i2 = _ref;
- if (withColon) {
- if (_i2[0] !== 'comment' && _i2[0] !== '{') {
- value += _i2[1];
- }
- } else if (_i2[0] === 'space' && _i2[1].indexOf('\n') !== -1) {
- break;
- } else if (_i2[0] === '(') {
- brackets += 1;
- } else if (_i2[0] === ')') {
- brackets -= 1;
- } else if (brackets === 0 && _i2[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;
- 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 i = tokens.length - 1; i > 0; i--) {
- token = tokens[i];
- if (token[1] === '!important') {
- node.important = true;
- var string = this.stringFrom(tokens, i);
- 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 = i; 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;
- }
- };
- _proto.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);
- }
- };
- _proto.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;
|