UNPKG

2.66 kBJavaScriptView Raw
1"use strict";
2var _a;
3Object.defineProperty(exports, "__esModule", { value: true });
4var LEFT = 'left';
5var RIGHT = 'right';
6var NO_FLIP = '@noflip';
7var NAME_REPLACEMENTS = (_a = {},
8 _a[LEFT] = RIGHT,
9 _a[RIGHT] = LEFT,
10 _a);
11var VALUE_REPLACEMENTS = {
12 'w-resize': 'e-resize',
13 'sw-resize': 'se-resize',
14 'nw-resize': 'ne-resize',
15};
16/**
17 * RTLifies the rulePair in the array at the current index. This mutates the array for performance
18 * reasons.
19 */
20function rtlifyRules(options, rulePairs, index) {
21 if (options.rtl) {
22 var name_1 = rulePairs[index];
23 if (!name_1) {
24 return;
25 }
26 var value = rulePairs[index + 1];
27 if (typeof value === 'string' && value.indexOf(NO_FLIP) >= 0) {
28 rulePairs[index + 1] = value.replace(/\s*(?:\/\*\s*)?\@noflip\b(?:\s*\*\/)?\s*?/g, '');
29 }
30 else if (name_1.indexOf(LEFT) >= 0) {
31 rulePairs[index] = name_1.replace(LEFT, RIGHT);
32 }
33 else if (name_1.indexOf(RIGHT) >= 0) {
34 rulePairs[index] = name_1.replace(RIGHT, LEFT);
35 }
36 else if (String(value).indexOf(LEFT) >= 0) {
37 rulePairs[index + 1] = value.replace(LEFT, RIGHT);
38 }
39 else if (String(value).indexOf(RIGHT) >= 0) {
40 rulePairs[index + 1] = value.replace(RIGHT, LEFT);
41 }
42 else if (NAME_REPLACEMENTS[name_1]) {
43 rulePairs[index] = NAME_REPLACEMENTS[name_1];
44 }
45 else if (VALUE_REPLACEMENTS[value]) {
46 rulePairs[index + 1] = VALUE_REPLACEMENTS[value];
47 }
48 else {
49 switch (name_1) {
50 case 'margin':
51 case 'padding':
52 rulePairs[index + 1] = flipQuad(value);
53 break;
54 case 'box-shadow':
55 rulePairs[index + 1] = negateNum(value, 0);
56 break;
57 }
58 }
59 }
60}
61exports.rtlifyRules = rtlifyRules;
62/**
63 * Given a string value in a space delimited format (e.g. "1 2 3 4"), negates a particular value.
64 */
65function negateNum(value, partIndex) {
66 var parts = value.split(' ');
67 var numberVal = parseInt(parts[partIndex], 10);
68 parts[0] = parts[0].replace(String(numberVal), String(numberVal * -1));
69 return parts.join(' ');
70}
71/**
72 * Given a string quad, flips the left and right values.
73 */
74function flipQuad(value) {
75 if (typeof value === 'string') {
76 var parts = value.split(' ');
77 if (parts.length === 4) {
78 return parts[0] + " " + parts[3] + " " + parts[2] + " " + parts[1];
79 }
80 }
81 return value;
82}
83//# sourceMappingURL=rtlifyRules.js.map
\No newline at end of file