UNPKG

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