-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
125 lines (101 loc) · 3.14 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
120
121
122
123
124
125
var postcss = require('postcss');
var merge = require('merge');
module.exports = postcss.plugin('postcss-logical-props', function (opts) {
var _DIR_LTR = 'ltr';
var _DIR_RTL = 'rtl';
var _REGEX_BASE = {
location: '(?:(inline)|(block))-(?:(end)|(start))',
replacement: '((?:(inline)|(block))-)?(?:(end)|(start))',
box: '(?:(margin)|(border)|(padding))-',
property: '(?:(float)|(clear)|(text-align))'
};
var _REGEX = {
location: new RegExp(_REGEX_BASE.location, 'i'),
replacement: new RegExp(_REGEX_BASE.replacement, 'i'),
boxModel: new RegExp(_REGEX_BASE.box + _REGEX_BASE.location, 'i'),
position: new RegExp('offset-' + _REGEX_BASE.location, 'i'),
property: new RegExp(_REGEX_BASE.property, 'i')
};
var PROPERTY_MAP = {};
PROPERTY_MAP[_DIR_LTR] = {
start: 'left',
end: 'right'
};
PROPERTY_MAP[_DIR_RTL] = {
start: 'right',
end: 'left'
};
opts = merge({
dir: _DIR_LTR,
replace: true
}, opts);
function getPropertyReplacement(matches) {
var position = matches[4] || matches[5];
return PROPERTY_MAP[opts.dir][position];
}
function getPartialReplacement(property) {
var matches = _REGEX.replacement.exec(property);
if(matches !== null) {
return property.replace(
_REGEX.location,
getPropertyReplacement(matches)
);
}
return property;
}
function getFullReplacement(property) {
var matches = _REGEX.replacement.exec(property);
if(matches !== null) {
return getPropertyReplacement(matches);
}
return property;
}
function replaceDeclaration(decl, name) {
decl.replaceWith(decl.clone({
prop: name
}));
}
function replaceValue(decl, val) {
decl.replaceWith(decl.clone({
value: val
}));
}
function addDeclaration(decl, name) {
decl.cloneBefore({
prop: name
});
}
function addValue(decl, val) {
decl.cloneBefore({
value: val
});
}
function handleDeclarationChange(decl, name) {
if(opts.replace) {
replaceDeclaration(decl, name);
} else {
addDeclaration(decl, name);
}
}
function handleFullDeclaration(decl) {
handleDeclarationChange(decl, getFullReplacement(decl.prop));
}
function handlePartialDeclaration(decl) {
handleDeclarationChange(decl, getPartialReplacement(decl.prop));
}
function handleValue(decl) {
var value = getFullReplacement(decl.value);
if(opts.replace) {
replaceValue(decl, value);
} else {
addValue(decl, value);
}
}
return function (css) {
css.walkRules(function (rule) {
rule.walkDecls(_REGEX.boxModel, handlePartialDeclaration);
rule.walkDecls(_REGEX.position, handleFullDeclaration);
rule.walkDecls(_REGEX.property, handleValue);
});
};
});