-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
119 lines (103 loc) · 4 KB
/
index.js
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
'use strict';
var postcss = require('postcss');
var extend = require('extend');
var variablesPattern;
var parametersPattern;
var propertyKeyDelimiter;
var customPropertyPattern;
var signatureSeparator = ':';
// Adds a property definition to properties
var define = function (properties, rule, options) {
var signature = rule.selector || rule.params;
var name = signature.match(customPropertyPattern).shift().replace(propertyKeyDelimiter, '').trim();
var parameters = signature.replace(customPropertyPattern, '').match(parametersPattern).map(function (parameter) {
return parameter.replace(options.syntax.parameter, options.syntax.variable);
});
var property = {
name: name,
parameters: parameters,
declarations: rule.nodes.map(function (node) {
return {
property: node.prop,
value: node.value,
// Parses variables that are also parameters. Ignores other variables for compatability with Sass variables.
// We only need the index of each parameter
variables: (node.value.match(variablesPattern) || [])
.filter(function (variable) {
return node.value.indexOf(variable) !== -1;
}).map(function (variable) {
return parameters.indexOf(variable);
})
};
})
};
properties[options.syntax.property + property.name + signatureSeparator + parameters.length] = property;
rule.remove();
};
// Applies the custom property to the given declaration
var apply = function (customProperty, declaration) {
customProperty.declarations.forEach(function (customDeclaration) {
// No need to copy value here as replace will copy value
var value = customDeclaration.value;
// Replace parameter variables with user given values
customDeclaration.variables.forEach(function (variable) {
value = value.replace(customProperty.parameters[variable],
declaration.values[variable]);
});
// Using cloneBefore to insert declaration provides sourcemap support
declaration.cloneBefore({
prop: customDeclaration.property,
value: value
});
});
declaration.remove();
};
module.exports = postcss.plugin('postcss-properties-properties', function (options) {
var defaults = {
syntax: {
atrule: '',
parameter: '$',
property: '',
separator: ':',
variable: '$'
}
};
// Apply defaults and normalise options
options = options == null ? defaults : extend(true, defaults, options);
options.syntax.atrule = options.syntax.atrule === true ? 'property' :
options.syntax.atrule === false ? '' :
options.syntax.atrule;
// Check for invalid option combinations
if (options.syntax.separator === '' && options.syntax.atrule === '') {
throw new Error('Invalid Syntax Options: The separator cannot be removed for non atrules');
}
// Set patterns based on options
parametersPattern = new RegExp(
(options.syntax.parameter ? '\\' : '') + options.syntax.parameter + '(\\w|\\d|[-_])+', 'g');
variablesPattern = new RegExp(
(options.syntax.variable ? '\\' : '') + options.syntax.variable + '(\\w|\\d|[-_])+', 'g');
propertyKeyDelimiter = options.syntax.separator;
customPropertyPattern = new RegExp('^.+?' + propertyKeyDelimiter + ' ');
return function (css) {
var properties = Object.create(null);
css.walk(function (node) {
if (options.syntax.atrule === '' && node.type === 'rule') {
if (node.selector.match(customPropertyPattern)) {
define(properties, node, options);
}
}
else if (options.syntax.atrule !== '' && node.type === 'atrule') {
if (node.name === options.syntax.atrule) {
define(properties, node, options);
}
}
else if (node.type === 'decl') {
node.values = postcss.list.space(node.value);
var property = node.prop + signatureSeparator + node.values.length;
if (properties[property]) {
apply(properties[property], node, options);
}
}
});
};
});