1 | var isMergeable = require('./is-mergeable');
|
---|
2 |
|
---|
3 | var optimizeProperties = require('./properties/optimize');
|
---|
4 |
|
---|
5 | var sortSelectors = require('../level-1/sort-selectors');
|
---|
6 | var tidyRules = require('../level-1/tidy-rules');
|
---|
7 |
|
---|
8 | var OptimizationLevel = require('../../options/optimization-level').OptimizationLevel;
|
---|
9 |
|
---|
10 | var serializeBody = require('../../writer/one-time').body;
|
---|
11 | var serializeRules = require('../../writer/one-time').rules;
|
---|
12 |
|
---|
13 | var Token = require('../../tokenizer/token');
|
---|
14 |
|
---|
15 | function mergeAdjacent(tokens, context) {
|
---|
16 | var lastToken = [null, [], []];
|
---|
17 | var options = context.options;
|
---|
18 | var adjacentSpace = options.compatibility.selectors.adjacentSpace;
|
---|
19 | var selectorsSortingMethod = options.level[OptimizationLevel.One].selectorsSortingMethod;
|
---|
20 | var mergeablePseudoClasses = options.compatibility.selectors.mergeablePseudoClasses;
|
---|
21 | var mergeablePseudoElements = options.compatibility.selectors.mergeablePseudoElements;
|
---|
22 | var mergeLimit = options.compatibility.selectors.mergeLimit;
|
---|
23 | var multiplePseudoMerging = options.compatibility.selectors.multiplePseudoMerging;
|
---|
24 |
|
---|
25 | for (var i = 0, l = tokens.length; i < l; i++) {
|
---|
26 | var token = tokens[i];
|
---|
27 |
|
---|
28 | if (token[0] != Token.RULE) {
|
---|
29 | lastToken = [null, [], []];
|
---|
30 | continue;
|
---|
31 | }
|
---|
32 |
|
---|
33 | if (lastToken[0] == Token.RULE && serializeRules(token[1]) == serializeRules(lastToken[1])) {
|
---|
34 | Array.prototype.push.apply(lastToken[2], token[2]);
|
---|
35 | optimizeProperties(lastToken[2], true, true, context);
|
---|
36 | token[2] = [];
|
---|
37 | } else if (lastToken[0] == Token.RULE && serializeBody(token[2]) == serializeBody(lastToken[2])
|
---|
38 | && isMergeable(serializeRules(token[1]), mergeablePseudoClasses, mergeablePseudoElements, multiplePseudoMerging)
|
---|
39 | && isMergeable(
|
---|
40 | serializeRules(lastToken[1]),
|
---|
41 | mergeablePseudoClasses,
|
---|
42 | mergeablePseudoElements,
|
---|
43 | multiplePseudoMerging
|
---|
44 | )
|
---|
45 | && lastToken[1].length < mergeLimit) {
|
---|
46 | lastToken[1] = tidyRules(lastToken[1].concat(token[1]), false, adjacentSpace, false, context.warnings);
|
---|
47 | lastToken[1] = lastToken.length > 1 ? sortSelectors(lastToken[1], selectorsSortingMethod) : lastToken[1];
|
---|
48 | token[2] = [];
|
---|
49 | } else {
|
---|
50 | lastToken = token;
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | module.exports = mergeAdjacent;
|
---|