UNPKG

9.74 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _createClass = function () { 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8
9var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
10
11var _comment = require('postcss/lib/comment');
12
13var _comment2 = _interopRequireDefault(_comment);
14
15var _parser = require('postcss/lib/parser');
16
17var _parser2 = _interopRequireDefault(_parser);
18
19var _findExtendRule = require('./find-extend-rule');
20
21var _findExtendRule2 = _interopRequireDefault(_findExtendRule);
22
23var _isMixinToken = require('./is-mixin-token');
24
25var _isMixinToken2 = _interopRequireDefault(_isMixinToken);
26
27var _lessTokenize = require('./less-tokenize');
28
29var _lessTokenize2 = _interopRequireDefault(_lessTokenize);
30
31function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
32
33function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
34
35function _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; }
36
37function _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; }
38
39var LessParser = function (_Parser) {
40 _inherits(LessParser, _Parser);
41
42 function LessParser() {
43 _classCallCheck(this, LessParser);
44
45 return _possibleConstructorReturn(this, Object.getPrototypeOf(LessParser).apply(this, arguments));
46 }
47
48 _createClass(LessParser, [{
49 key: 'tokenize',
50 value: function tokenize() {
51 this.tokens = (0, _lessTokenize2.default)(this.input);
52 }
53 }, {
54 key: 'comment',
55 value: function comment(token) {
56 if (token[6] === 'inline') {
57 var node = new _comment2.default();
58
59 this.init(node, token[2], token[3]);
60 node.raws.inline = true;
61 node.source.end = { line: token[4], column: token[5] };
62
63 var text = token[1].slice(2);
64
65 if (/^\s*$/.test(text)) {
66 node.text = '';
67 node.raws.left = text;
68 node.raws.right = '';
69 } else {
70 var match = text.match(/^(\s*)([^]*[^\s])(\s*)$/);
71
72 node.text = match[2];
73 node.raws.left = match[1];
74 node.raws.right = match[3];
75 }
76 } else {
77 _get(Object.getPrototypeOf(LessParser.prototype), 'comment', this).call(this, token);
78 }
79 }
80
81 /**
82 * @description Create a Rule node
83 * @param options {{start: number, params: Array}}
84 */
85
86 }, {
87 key: 'createRule',
88 value: function createRule(options) {
89 this.rule(this.tokens.slice(options.start, this.pos + 1));
90
91 /**
92 * By default in PostCSS `Rule.params` is `undefined`. There are rules to save the compability with PostCSS:
93 * - Don't set empty params for Rule node.
94 * - Set params fro Rule node only if it can be a mixin or &:extend rule.
95 */
96 if (options.params[0] && (options.isMixin || options.isExtendRule)) {
97 this.raw(this.current, 'params', options.params);
98 }
99 }
100
101 /**
102 * @description Create a Declaration
103 * @param options {{start: number}}
104 */
105
106 }, {
107 key: 'createDeclaration',
108 value: function createDeclaration(options) {
109 this.decl(this.tokens.slice(options.start, this.pos + 1));
110 }
111
112 /**
113 * @description Create a Rule block and close it, because this mixin doesn't have a body
114 * @param options
115 */
116
117 }, {
118 key: 'ruleWithoutBody',
119 value: function ruleWithoutBody(options) {
120 this.createRule(options);
121
122 /**
123 * @description Mark mixin without body.
124 * @type {boolean}
125 */
126 this.current.ruleWithoutBody = true;
127 this.current.extendRule = this.current.selector.indexOf('&:extend') >= 0;
128 this.current.important = this.current.selector.indexOf('!important') >= 0;
129 this.end(this.tokens[this.pos]);
130 }
131
132 /**
133 * @description
134 * @param options
135 * @returns {boolean}
136 */
137
138 }, {
139 key: 'processEndOfRule',
140 value: function processEndOfRule(options) {
141 var start = options.start;
142
143
144 if (options.isExtendRule || options.isMixin) {
145 this.ruleWithoutBody(options);
146 return true;
147 }
148
149 if (options.colon) {
150 if (options.isEndOfBlock) {
151 while (this.pos > start) {
152 var token = this.tokens[this.pos][0];
153
154 if (token !== 'space' && token !== 'comment') {
155 break;
156 }
157
158 this.pos -= 1;
159 }
160 }
161
162 this.createDeclaration({ start: start });
163 return true;
164 }
165
166 return false;
167 }
168
169 /* eslint-disable max-statements, complexity */
170
171 }, {
172 key: 'word',
173 value: function word() {
174 var end = false;
175 var colon = false;
176 var bracket = null;
177 var brackets = 0;
178 var start = this.pos;
179 var isMixin = (0, _isMixinToken2.default)(this.tokens[start]);
180 var isExtendRule = Boolean((0, _findExtendRule2.default)(this.tokens, start));
181 var params = [];
182
183 this.pos += 1;
184
185 while (this.pos < this.tokens.length) {
186 var token = this.tokens[this.pos];
187 var type = token[0];
188
189 if (type === '(') {
190 if (!bracket) {
191 bracket = token;
192 }
193
194 brackets += 1;
195 } else if (brackets === 0) {
196 if (type === ';') {
197
198 var foundEndOfRule = this.processEndOfRule({
199 start: start,
200 params: params,
201 colon: colon,
202 isMixin: isMixin,
203 isExtendRule: isExtendRule
204 });
205
206 if (foundEndOfRule) {
207 return;
208 }
209
210 break;
211 } else if (type === '{') {
212 this.createRule({ start: start, params: params, isMixin: isMixin });
213 return;
214 } else if (type === '}') {
215 this.pos -= 1;
216 end = true;
217 break;
218 } else if (type === ':') {
219 colon = true;
220 }
221 } else if (type === ')') {
222 brackets -= 1;
223 if (brackets === 0) {
224 bracket = null;
225 }
226 }
227
228 if (brackets || type === 'brackets' || params[0]) {
229 params.push(token);
230 }
231
232 this.pos += 1;
233 }
234
235 if (this.pos === this.tokens.length) {
236 this.pos -= 1;
237 end = true;
238 }
239
240 if (brackets > 0) {
241 this.unclosedBracket(bracket);
242 }
243
244 if (end) {
245 var _foundEndOfRule = this.processEndOfRule({
246 start: start,
247 params: params,
248 colon: colon,
249 isMixin: isMixin,
250 isExtendRule: isExtendRule,
251 isEndOfBlock: true
252 });
253
254 if (_foundEndOfRule) {
255 return;
256 }
257 }
258
259 this.unknownWord(start);
260 }
261
262 /* eslint-enable max-statements, complexity */
263
264 }]);
265
266 return LessParser;
267}(_parser2.default);
268
269exports.default = LessParser;
270module.exports = exports['default'];
\No newline at end of file