This repository was archived by the owner on Dec 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcssSections.js
More file actions
532 lines (455 loc) · 12.4 KB
/
cssSections.js
File metadata and controls
532 lines (455 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
if (typeof module === 'object' && typeof define !== 'function') {
var define = function (factory) {
module.exports = factory(require, exports, module);
};
}
define(function(require, exports, module) {
var utils = require('./common');
var commentsUtils = require('./comments');
var range = require('../assets/range');
var stringStream = require('../assets/stringStream');
var cssParser = require('../parser/css');
var htmlMatcher = require('../assets/htmlMatcher');
var xmlEditTree = require('../editTree/xml');
var idCounter = 1;
var maxId = 1000000;
var reSpaceTrim = /^(\s*).+?(\s*)$/;
var reSpace = /\s/g;
var reComma = /,/;
function isQuote(ch) {
return ch == '"' || ch == "'";
}
function getId() {
idCounter = (idCounter + 1) % maxId;
return 's' + idCounter;
}
/**
* @param {Range} range Full selector range with additional
* properties for matching name and content (@see findAllRules())
* @param {String} source CSS source
*/
function CSSSection(rng, source) {
this.id = getId();
/** @type {CSSSection} */
this.parent = null;
/** @type {CSSSection} */
this.nextSibling = null;
/** @type {CSSSection} */
this.previousSibling = null;
this._source = source;
this._name = null;
this._content = null;
/**
* Custom data for current nodes, used by other modules for
* caching etc.
* @type {Object}
*/
this._data = {};
if (!rng && source) {
rng = range(0, source);
}
this.range = rng;
this.children = [];
}
CSSSection.prototype = {
addChild: function(section) {
if (!(section instanceof CSSSection)) {
section = new CSSSection(section);
}
var lastChild = utils.last(this.children);
if (lastChild) {
lastChild.nextSibling = section;
section.previousSibling = lastChild;
}
section.parent = this;
this.children.push(section);
return section;
},
/**
* Returns root node
* @return {CSSSection}
*/
root: function() {
var root = this;
do {
if (!root.parent) {
return root;
}
} while(root = root.parent);
return root;
},
/**
* Returns currect CSS source
* @return {String}
*/
source: function() {
return this._source || this.root()._source;
},
/**
* Returns section name
* @return {String}
*/
name: function() {
if (this._name === null) {
var range = this.nameRange();
if (range) {
this._name = range.substring(this.source());
}
}
return this._name;
},
/**
* Returns section name range
* @return {[type]} [description]
*/
nameRange: function() {
if (this.range && '_selectorEnd' in this.range) {
return range.create2(this.range.start, this.range._selectorEnd);
}
},
/**
* Returns deepest child of current section (or section itself)
* which includes given position.
* @param {Number} pos
* @return {CSSSection}
*/
matchDeep: function(pos) {
if (!this.range.inside(pos)) {
return null;
}
for (var i = 0, il = this.children.length, m; i < il; i++) {
m = this.children[i].matchDeep(pos);
if (m) {
return m;
}
};
return this.parent ? this : null;
},
/**
* Returns current and all nested sections ranges
* @return {Array}
*/
allRanges: function() {
var out = [];
if (this.parent) {
// add current range if it is not root node
out.push(this.range);
}
this.children.forEach(function(child) {
out = out.concat(child.allRanges());
});
return out;
},
data: function(key, value) {
if (typeof value !== 'undefined') {
this._data[key] = value;
}
return this._data[key];
},
stringify: function(indent) {
indent = indent || '';
var out = '';
this.children.forEach(function(item) {
out += indent + item.name().replace(/\n/g, '\\n') + '\n';
out += item.stringify(indent + '--');
});
return out;
},
/**
* Returns current section’s actual content,
* e.g. content without nested sections
* @return {String}
*/
content: function() {
if (this._content !== null) {
return this._content;
}
if (!this.range || !('_contentStart' in this.range)) {
return '';
}
var r = range.create2(this.range._contentStart + 1, this.range.end - 1);
var source = this.source();
var start = r.start;
var out = '';
this.children.forEach(function(child) {
out += source.substring(start, child.range.start);
start = child.range.end;
});
out += source.substring(start, r.end);
return this._content = utils.trim(out);
}
};
return {
/**
* Finds all CSS rules‘ ranges in given CSS source
* @param {String} content CSS source
* @return {Array} Array of ranges
*/
findAllRules: function(content) {
content = this.sanitize(content);
var stream = stringStream(content);
var ranges = [], matchedRanges;
var self = this;
var saveRule = function(r) {
var selRange = self.extractSelector(content, r.start);
var rule = range.create2(selRange.start, r.end);
rule._selectorEnd = selRange.end;
rule._contentStart = r.start;
ranges.push(rule);
};
var ch;
while (ch = stream.next()) {
if (isQuote(ch)) {
if (!stream.skipString(ch)) {
break; // unterminated string
}
continue;
}
if (ch == '{') {
matchedRanges = this.matchBracesRanges(content, stream.pos - 1);
matchedRanges.forEach(saveRule);
if (matchedRanges.length) {
stream.pos = utils.last(matchedRanges).end;
continue;
}
}
}
return ranges.sort(function(a, b) {
return a.start - b.start;
});
},
/**
* Matches curly braces content right after given position
* @param {String} content CSS content. Must not contain comments!
* @param {Number} pos Search start position
* @return {Range}
*/
matchBracesRanges: function(content, pos, sanitize) {
if (sanitize) {
content = this.sanitize(content);
}
var stream = stringStream(content);
stream.start = stream.pos = pos;
var stack = [], ranges = [];
var ch;
while (ch = stream.next()) {
if (ch == '{') {
stack.push(stream.pos - 1);
} else if (ch == '}') {
if (!stack.length) {
throw 'Invalid source structure (check for curly braces)';
}
ranges.push(range.create2(stack.pop(), stream.pos));
if (!stack.length) {
return ranges;
}
} else {
stream.skipQuoted();
}
}
return ranges;
},
/**
* Extracts CSS selector from CSS document from
* given position. The selector is located by moving backward
* from given position which means that passed position
* must point to the end of selector
* @param {String} content CSS source
* @param {Number} pos Search position
* @param {Boolean} sanitize Sanitize CSS source before processing.
* Off by default and assumes that CSS must be comment-free already
* (for performance)
* @return {Range}
*/
extractSelector: function(content, pos, sanitize) {
if (sanitize) {
content = this.sanitize(content);
}
var skipString = function() {
var quote = content.charAt(pos);
if (quote == '"' || quote == "'") {
while (--pos >= 0) {
if (content.charAt(pos) == quote && content.charAt(pos - 1) != '\\') {
break;
}
}
return true;
}
return false;
};
// find CSS selector
var ch;
var endPos = pos;
while (--pos >= 0) {
if (skipString()) continue;
ch = content.charAt(pos);
if (ch == ')') {
// looks like it’s a preprocessor thing,
// most likely a mixin arguments list, e.g.
// .mixin (@arg1; @arg2) {...}
while (--pos >= 0) {
if (skipString()) continue;
if (content.charAt(pos) == '(') {
break;
}
}
continue;
}
if (ch == '{' || ch == '}' || ch == ';') {
pos++;
break;
}
}
if (pos < 0) {
pos = 0;
}
var selector = content.substring(pos, endPos);
// trim whitespace from matched selector
var m = selector.replace(reSpace, ' ').match(reSpaceTrim);
if (m) {
pos += m[1].length;
endPos -= m[2].length;
}
return range.create2(pos, endPos);
},
/**
* Search for nearest CSS rule/section that contains
* given position
* @param {String} content CSS content or matched CSS rules (array of ranges)
* @param {Number} pos Search position
* @return {Range}
*/
matchEnclosingRule: function(content, pos) {
if (typeof content === 'string') {
content = this.findAllRules(content);
}
var rules = content.filter(function(r) {
return r.inside(pos);
});
return utils.last(rules);
},
/**
* Locates CSS rule next or before given position
* @param {String} content CSS content
* @param {Number} pos Search start position
* @param {Boolean} isBackward Search backward (find previous rule insteaf of next one)
* @return {Range}
*/
locateRule: function(content, pos, isBackward) {
// possible case: editor reported that current syntax is
// CSS, but it’s actually a HTML document (either `style` tag or attribute)
var offset = 0;
var subrange = this.styleTagRange(content, pos);
if (subrange) {
offset = subrange.start;
pos -= subrange.start;
content = subrange.substring(content);
}
var rules = this.findAllRules(content);
var ctxRule = this.matchEnclosingRule(rules, pos);
if (ctxRule) {
return ctxRule.shift(offset);
}
for (var i = 0, il = rules.length; i < il; i++) {
if (rules[i].start > pos) {
return rules[isBackward && i > 0 ? i - 1 : i].shift(offset);
}
}
},
/**
* Sanitizes given CSS content: replaces content that may
* interfere with parsing (comments, interpolations, etc.)
* with spaces. Sanitized content MUST NOT be used for
* editing or outputting, it just simplifies searching
* @param {String} content CSS content
* @return {String}
*/
sanitize: function(content) {
content = commentsUtils.strip(content);
// remove preprocessor string interpolations like #{var}
var stream = stringStream(content);
var replaceRanges = [];
var ch, ch2;
while ((ch = stream.next())) {
if (isQuote(ch)) {
// skip string
stream.skipString(ch)
continue;
} else if (ch === '#' || ch === '@') {
ch2 = stream.peek();
if (ch2 === '{') { // string interpolation
stream.start = stream.pos - 1;
if (stream.skipTo('}')) {
stream.pos += 1;
} else {
throw 'Invalid string interpolation at ' + stream.start;
}
replaceRanges.push([stream.start, stream.pos]);
}
}
}
return utils.replaceWith(content, replaceRanges, 'a');
},
/**
* Parses and returns all sections in given CSS
* as tree-like structure, e.g. provides nesting
* info
* @param {String} content CSS content
* @return {CSSSection}
*/
sectionTree: function(content) {
var root = new CSSSection(null, content);
var rules = this.findAllRules(content);
// rules are sorted in order they appear in CSS source
// so we can optimize their nesting routine
var insert = function(range, ctx) {
while (ctx && ctx.range) {
if (ctx.range.contains(range)) {
return ctx.addChild(range);
}
ctx = ctx.parent;
}
// if we are here then given range is a top-level section
return root.addChild(range);
};
var ctx = root;
rules.forEach(function(r) {
ctx = insert(r, ctx);
});
return root;
},
/**
* Returns ranges for all nested sections, available in
* given CSS rule
* @param {CSSEditContainer} rule
* @return {Array}
*/
nestedSectionsInRule: function(rule) {
var offset = rule.valueRange(true).start;
var nestedSections = this.findAllRules(rule.valueRange().substring(rule.source));
nestedSections.forEach(function(section) {
section.start += offset;
section.end += offset;
section._selectorEnd += offset;
section._contentStart += offset;
});
return nestedSections;
},
styleTagRange: function(content, pos) {
var tag = htmlMatcher.tag(content, pos);
return tag && tag.open.name.toLowerCase() == 'style'
&& tag.innerRange.cmp(pos, 'lte', 'gte')
&& tag.innerRange;
},
styleAttrRange: function(content, pos) {
var tree = xmlEditTree.parseFromPosition(content, pos, true);
if (tree) {
var attr = tree.itemFromPosition(pos, true);
return attr && attr.name().toLowerCase() == 'style'
&& attr.valueRange(true).cmp(pos, 'lte', 'gte')
&& attr.valueRange(true);
}
},
CSSSection: CSSSection
};
});