rtl-css-js
Advanced tools
Comparing version 1.10.0 to 1.11.0
@@ -5,8 +5,2 @@ 'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { | ||
return typeof obj; | ||
} : function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
/** | ||
@@ -21,3 +15,2 @@ * Takes an array of [keyValue1, keyValue2] pairs and creates an object of {keyValue1: keyValue2, keyValue2: keyValue1} | ||
prop2 = _ref[1]; | ||
obj[prop1] = prop2; | ||
@@ -42,3 +35,3 @@ obj[prop2] = prop1; | ||
function isObject(val) { | ||
return val && (typeof val === 'undefined' ? 'undefined' : _typeof(val)) === 'object'; | ||
return val && typeof val === 'object'; | ||
} | ||
@@ -53,3 +46,2 @@ | ||
} | ||
/** | ||
@@ -64,2 +56,4 @@ * Flip the sign of a CSS value, possibly with a unit. | ||
*/ | ||
function flipSign(value) { | ||
@@ -75,3 +69,3 @@ if (parseFloat(value) === 0) { | ||
return '-' + value; | ||
return "-" + value; | ||
} | ||
@@ -82,3 +76,2 @@ | ||
} | ||
/** | ||
@@ -91,6 +84,9 @@ * Takes a percentage for background position and inverts it. | ||
*/ | ||
function calculateNewBackgroundPosition(value) { | ||
var idx = value.indexOf('.'); | ||
if (idx === -1) { | ||
value = 100 - parseFloat(value) + '%'; | ||
value = 100 - parseFloat(value) + "%"; | ||
} else { | ||
@@ -100,7 +96,7 @@ // Two off, one for the "%" at the end, one for the dot itself | ||
value = 100 - parseFloat(value); | ||
value = value.toFixed(len) + '%'; | ||
value = value.toFixed(len) + "%"; | ||
} | ||
return value; | ||
} | ||
/** | ||
@@ -111,2 +107,4 @@ * This takes a list of CSS values and converts it to an array | ||
*/ | ||
function getValuesAsList(value) { | ||
@@ -123,15 +121,23 @@ return value.replace(/ +/g, ' ') // remove all extraneous spaces | ||
state = _ref2.state; | ||
var openParansCount = (item.match(/\(/g) || []).length; | ||
var closedParansCount = (item.match(/\)/g) || []).length; | ||
if (state.parensDepth > 0) { | ||
list[list.length - 1] = list[list.length - 1] + ' ' + item; | ||
list[list.length - 1] = list[list.length - 1] + " " + item; | ||
} else { | ||
list.push(item); | ||
} | ||
state.parensDepth += openParansCount - closedParansCount; | ||
return { list: list, state: state }; | ||
}, { list: [], state: { parensDepth: 0 } }).list; | ||
return { | ||
list: list, | ||
state: state | ||
}; | ||
}, { | ||
list: [], | ||
state: { | ||
parensDepth: 0 | ||
} | ||
}).list; | ||
} | ||
/** | ||
@@ -143,7 +149,11 @@ * This is intended for properties that are `top right bottom left` and will switch them to `top left bottom right` | ||
*/ | ||
function handleQuartetValues(value) { | ||
var splitValues = getValuesAsList(value); | ||
if (splitValues.length <= 3 || splitValues.length > 4) { | ||
return value; | ||
} | ||
var top = splitValues[0], | ||
@@ -153,7 +163,5 @@ right = splitValues[1], | ||
left = splitValues[3]; | ||
return [top, left, bottom, right].join(' '); | ||
} | ||
// some values require a little fudging, that fudging goes here. | ||
var propertyValueConverters = { | ||
@@ -166,2 +174,3 @@ padding: function padding(_ref) { | ||
} | ||
return handleQuartetValues(value); | ||
@@ -171,3 +180,2 @@ }, | ||
var value = _ref2.value; | ||
// intentionally leaving off the `g` flag here because we only want to change the first number (which is the offset-x) | ||
@@ -178,4 +186,5 @@ return value.replace(/(-*)([.|\d]+)/, function (match, negative, number) { | ||
} | ||
var doubleNegative = negative === '' ? '-' : ''; | ||
return '' + doubleNegative + number; | ||
return "" + doubleNegative + number; | ||
}); | ||
@@ -185,3 +194,2 @@ }, | ||
var value = _ref3.value; | ||
return handleQuartetValues(value); | ||
@@ -195,2 +203,3 @@ }, | ||
} | ||
if (includes(value, '/')) { | ||
@@ -207,5 +216,7 @@ var _value$split = value.split('/'), | ||
}); | ||
return convertedRadius1 + ' / ' + convertedRadius2; | ||
return convertedRadius1 + " / " + convertedRadius2; | ||
} | ||
var splitValues = getValuesAsList(value); | ||
switch (splitValues.length) { | ||
@@ -216,2 +227,3 @@ case 2: | ||
} | ||
case 4: | ||
@@ -223,5 +235,5 @@ { | ||
bottomLeft = splitValues[3]; | ||
return [topRight, topLeft, bottomLeft, bottomRight].join(' '); | ||
} | ||
default: | ||
@@ -239,8 +251,7 @@ { | ||
bgPosDirectionRegex = _ref5.bgPosDirectionRegex; | ||
// Yeah, this is in need of a refactor 🙃... | ||
// but this property is a tough cookie 🍪 | ||
// get the backgroundPosition out of the string by removing everything that couldn't be the backgroundPosition value | ||
var backgroundPositionValue = value.replace(/(url\(.*?\))|(rgba?\(.*?\))|(hsl\(.*?\))|(#[a-fA-F0-9]+)|((^| )(\D)+( |$))/g, '').trim(); | ||
// replace that backgroundPosition value with the converted version | ||
var backgroundPositionValue = value.replace(/(url\(.*?\))|(rgba?\(.*?\))|(hsl\(.*?\))|(#[a-fA-F0-9]+)|((^| )(\D)+( |$))/g, '').trim(); // replace that backgroundPosition value with the converted version | ||
value = value.replace(backgroundPositionValue, propertyValueConverters.backgroundPosition({ | ||
@@ -251,4 +262,4 @@ value: backgroundPositionValue, | ||
bgPosDirectionRegex: bgPosDirectionRegex | ||
})); | ||
// do the backgroundImage value replacing on the whole value (because why not?) | ||
})); // do the backgroundImage value replacing on the whole value (because why not?) | ||
return propertyValueConverters.backgroundImage({ | ||
@@ -268,2 +279,3 @@ value: value, | ||
} | ||
return value.replace(bgImgDirectionRegex, function (match, g1, group2) { | ||
@@ -278,5 +290,3 @@ return match.replace(group2, valuesToConvert[group2]); | ||
bgPosDirectionRegex = _ref7.bgPosDirectionRegex; | ||
return value | ||
// intentionally only grabbing the first instance of this because that represents `left` | ||
return value // intentionally only grabbing the first instance of this because that represents `left` | ||
.replace(isRtl ? /^((-|\d|\.)+%)/ : null, function (match, group) { | ||
@@ -297,2 +307,3 @@ return calculateNewBackgroundPosition(group); | ||
} | ||
return propertyValueConverters.backgroundPosition({ | ||
@@ -307,18 +318,14 @@ value: value, | ||
var value = _ref9.value; | ||
// This was copied and modified from CSSJanus: | ||
// https://github.com/cssjanus/cssjanus/blob/4a40f001b1ba35567112d8b8e1d9d95eda4234c3/src/cssjanus.js#L152-L153 | ||
var nonAsciiPattern = '[^\\u0020-\\u007e]'; | ||
var escapePattern = '(?:' + '(?:(?:\\[0-9a-f]{1,6})(?:\\r\\n|\\s)?)' + '|\\\\[^\\r\\n\\f0-9a-f])'; | ||
var signedQuantPattern = '((?:-?' + ('(?:[0-9]*\\.[0-9]+|[0-9]+)' + '(?:\\s*' + '(?:em|ex|px|cm|mm|in|pt|pc|deg|rad|grad|ms|s|hz|khz|%)' + '|' + ('-?' + ('(?:[_a-z]|' + nonAsciiPattern + '|' + escapePattern + ')') + ('(?:[_a-z0-9-]|' + nonAsciiPattern + '|' + escapePattern + ')') + '*') + ')?') + ')|(?:inherit|auto))'; | ||
var translateXRegExp = new RegExp('(translateX\\s*\\(\\s*)' + signedQuantPattern + '(\\s*\\))', 'gi'); | ||
var translateRegExp = new RegExp('(translate\\s*\\(\\s*)' + signedQuantPattern + '((?:\\s*,\\s*' + signedQuantPattern + '){0,1}\\s*\\))', 'gi'); | ||
var translate3dRegExp = new RegExp('(translate3d\\s*\\(\\s*)' + signedQuantPattern + '((?:\\s*,\\s*' + signedQuantPattern + '){0,2}\\s*\\))', 'gi'); | ||
var rotateRegExp = new RegExp('(rotate[ZY]?\\s*\\(\\s*)' + signedQuantPattern + '(\\s*\\))', 'gi'); | ||
var nonAsciiPattern = "[^\\u0020-\\u007e]"; | ||
var escapePattern = "(?:" + '(?:(?:\\[0-9a-f]{1,6})(?:\\r\\n|\\s)?)' + "|\\\\[^\\r\\n\\f0-9a-f])"; | ||
var signedQuantPattern = "((?:-?" + ('(?:[0-9]*\\.[0-9]+|[0-9]+)' + "(?:\\s*" + '(?:em|ex|px|cm|mm|in|pt|pc|deg|rad|grad|ms|s|hz|khz|%)' + "|" + ("-?" + ("(?:[_a-z]|" + nonAsciiPattern + "|" + escapePattern + ")") + ("(?:[_a-z0-9-]|" + nonAsciiPattern + "|" + escapePattern + ")") + "*") + ")?") + ")|(?:inherit|auto))"; | ||
var translateXRegExp = new RegExp("(translateX\\s*\\(\\s*)" + signedQuantPattern + "(\\s*\\))", 'gi'); | ||
var translateRegExp = new RegExp("(translate\\s*\\(\\s*)" + signedQuantPattern + "((?:\\s*,\\s*" + signedQuantPattern + "){0,1}\\s*\\))", 'gi'); | ||
var translate3dRegExp = new RegExp("(translate3d\\s*\\(\\s*)" + signedQuantPattern + "((?:\\s*,\\s*" + signedQuantPattern + "){0,2}\\s*\\))", 'gi'); | ||
var rotateRegExp = new RegExp("(rotate[ZY]?\\s*\\(\\s*)" + signedQuantPattern + "(\\s*\\))", 'gi'); | ||
return value.replace(translateXRegExp, flipTransformSign).replace(translateRegExp, flipTransformSign).replace(translate3dRegExp, flipTransformSign).replace(rotateRegExp, flipTransformSign); | ||
} | ||
}; | ||
propertyValueConverters.margin = propertyValueConverters.padding; | ||
@@ -340,6 +347,4 @@ propertyValueConverters.borderWidth = propertyValueConverters.padding; | ||
propertyValueConverters.WebkitTransformOrigin = propertyValueConverters.transformOrigin; | ||
propertyValueConverters.MozTransformOrigin = propertyValueConverters.transformOrigin; | ||
propertyValueConverters.MozTransformOrigin = propertyValueConverters.transformOrigin; // kebab-case versions | ||
// kebab-case versions | ||
propertyValueConverters['text-shadow'] = propertyValueConverters.textShadow; | ||
@@ -346,0 +351,0 @@ propertyValueConverters['border-color'] = propertyValueConverters.borderColor; |
'use strict'; | ||
var __core_js = require('./core.js'); | ||
var core = require('./core.js'); | ||
// this will be an object of properties that map to their corresponding rtl property (their doppelganger) | ||
var propertiesToConvert = __core_js.arrayToObject([['paddingLeft', 'paddingRight'], ['marginLeft', 'marginRight'], ['left', 'right'], ['borderLeft', 'borderRight'], ['borderLeftColor', 'borderRightColor'], ['borderLeftStyle', 'borderRightStyle'], ['borderLeftWidth', 'borderRightWidth'], ['borderTopLeftRadius', 'borderTopRightRadius'], ['borderBottomLeftRadius', 'borderBottomRightRadius'], | ||
// kebab-case versions | ||
var propertiesToConvert = core.arrayToObject([['paddingLeft', 'paddingRight'], ['marginLeft', 'marginRight'], ['left', 'right'], ['borderLeft', 'borderRight'], ['borderLeftColor', 'borderRightColor'], ['borderLeftStyle', 'borderRightStyle'], ['borderLeftWidth', 'borderRightWidth'], ['borderTopLeftRadius', 'borderTopRightRadius'], ['borderBottomLeftRadius', 'borderBottomRightRadius'], // kebab-case versions | ||
['padding-left', 'padding-right'], ['margin-left', 'margin-right'], ['border-left', 'border-right'], ['border-left-color', 'border-right-color'], ['border-left-style', 'border-right-style'], ['border-left-width', 'border-right-width'], ['border-top-left-radius', 'border-top-right-radius'], ['border-bottom-left-radius', 'border-bottom-right-radius']]); | ||
var propsToIgnore = ['content']; // this is the same as the propertiesToConvert except for values | ||
var propsToIgnore = ['content']; | ||
// this is the same as the propertiesToConvert except for values | ||
var valuesToConvert = __core_js.arrayToObject([['ltr', 'rtl'], ['left', 'right'], ['w-resize', 'e-resize'], ['sw-resize', 'se-resize'], ['nw-resize', 'ne-resize']]); | ||
// Sorry for the regex 😞, but basically thisis used to replace _every_ instance of | ||
var valuesToConvert = core.arrayToObject([['ltr', 'rtl'], ['left', 'right'], ['w-resize', 'e-resize'], ['sw-resize', 'se-resize'], ['nw-resize', 'ne-resize']]); // Sorry for the regex 😞, but basically thisis used to replace _every_ instance of | ||
// `ltr`, `rtl`, `right`, and `left` in `backgroundimage` with the corresponding opposite. | ||
@@ -20,5 +14,5 @@ // A situation we're accepting here: | ||
// Definite trade-offs here, but I think it's a good call. | ||
var bgImgDirectionRegex = new RegExp('(^|\\W|_)((ltr)|(rtl)|(left)|(right))(\\W|_|$)', 'g'); | ||
var bgPosDirectionRegex = new RegExp('(left)|(right)'); | ||
/** | ||
@@ -29,12 +23,14 @@ * converts properties and values in the CSS in JS object to their corresponding RTL values | ||
*/ | ||
function convert(object) { | ||
return Object.keys(object).reduce(function (newObj, originalKey) { | ||
var originalValue = object[originalKey]; | ||
if (__core_js.isString(originalValue)) { | ||
if (core.isString(originalValue)) { | ||
// you're welcome to later code 😺 | ||
originalValue = originalValue.trim(); | ||
} | ||
} // Some properties should never be transformed | ||
// Some properties should never be transformed | ||
if (__core_js.includes(propsToIgnore, originalKey)) { | ||
if (core.includes(propsToIgnore, originalKey)) { | ||
newObj[originalKey] = originalValue; | ||
@@ -52,3 +48,2 @@ return newObj; | ||
} | ||
/** | ||
@@ -60,2 +55,4 @@ * Converts a property and its value to the corresponding RTL key and value | ||
*/ | ||
function convertProperty(originalKey, originalValue) { | ||
@@ -65,5 +62,7 @@ var isNoFlip = /\/\*\s?@noflip\s?\*\//.test(originalValue); | ||
var value = isNoFlip ? originalValue : getValueDoppelganger(key, originalValue); | ||
return { key: key, value: value }; | ||
return { | ||
key: key, | ||
value: value | ||
}; | ||
} | ||
/** | ||
@@ -74,6 +73,7 @@ * This gets the RTL version of the given property if it has a corresponding RTL property | ||
*/ | ||
function getPropertyDoppelganger(property) { | ||
return propertiesToConvert[property] || property; | ||
} | ||
/** | ||
@@ -85,16 +85,21 @@ * This converts the given value to the RTL version of that value based on the key | ||
*/ | ||
function getValueDoppelganger(key, originalValue) { | ||
/* eslint complexity:[2, 9] */ // let's try to keep the complexity down... If we have to do this much more, let's break this up | ||
if (__core_js.isNullOrUndefined(originalValue) || __core_js.isBoolean(originalValue)) { | ||
/* eslint complexity:[2, 9] */ | ||
// let's try to keep the complexity down... If we have to do this much more, let's break this up | ||
if (core.isNullOrUndefined(originalValue) || core.isBoolean(originalValue)) { | ||
return originalValue; | ||
} | ||
if (__core_js.isObject(originalValue)) { | ||
if (core.isObject(originalValue)) { | ||
return convert(originalValue); // recurssion 🌀 | ||
} | ||
var isNum = __core_js.isNumber(originalValue); | ||
var isNum = core.isNumber(originalValue); | ||
var importantlessValue = isNum ? originalValue : originalValue.replace(/ !important.*?$/, ''); | ||
var isImportant = !isNum && importantlessValue.length !== originalValue.length; | ||
var valueConverter = __core_js.propertyValueConverters[key]; | ||
var newValue = void 0; | ||
var valueConverter = core.propertyValueConverters[key]; | ||
var newValue; | ||
if (valueConverter) { | ||
@@ -111,8 +116,10 @@ newValue = valueConverter({ | ||
} | ||
if (isImportant) { | ||
return newValue + ' !important'; | ||
return newValue + " !important"; | ||
} | ||
return newValue; | ||
} | ||
} // here's our main export! 👋 | ||
module.exports = convert; |
@@ -1,7 +0,1 @@ | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { | ||
return typeof obj; | ||
} : function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
/** | ||
@@ -16,3 +10,2 @@ * Takes an array of [keyValue1, keyValue2] pairs and creates an object of {keyValue1: keyValue2, keyValue2: keyValue1} | ||
prop2 = _ref[1]; | ||
obj[prop1] = prop2; | ||
@@ -37,3 +30,3 @@ obj[prop2] = prop1; | ||
function isObject(val) { | ||
return val && (typeof val === 'undefined' ? 'undefined' : _typeof(val)) === 'object'; | ||
return val && typeof val === 'object'; | ||
} | ||
@@ -48,3 +41,2 @@ | ||
} | ||
/** | ||
@@ -59,2 +51,4 @@ * Flip the sign of a CSS value, possibly with a unit. | ||
*/ | ||
function flipSign(value) { | ||
@@ -70,3 +64,3 @@ if (parseFloat(value) === 0) { | ||
return '-' + value; | ||
return "-" + value; | ||
} | ||
@@ -77,3 +71,2 @@ | ||
} | ||
/** | ||
@@ -86,6 +79,9 @@ * Takes a percentage for background position and inverts it. | ||
*/ | ||
function calculateNewBackgroundPosition(value) { | ||
var idx = value.indexOf('.'); | ||
if (idx === -1) { | ||
value = 100 - parseFloat(value) + '%'; | ||
value = 100 - parseFloat(value) + "%"; | ||
} else { | ||
@@ -95,7 +91,7 @@ // Two off, one for the "%" at the end, one for the dot itself | ||
value = 100 - parseFloat(value); | ||
value = value.toFixed(len) + '%'; | ||
value = value.toFixed(len) + "%"; | ||
} | ||
return value; | ||
} | ||
/** | ||
@@ -106,2 +102,4 @@ * This takes a list of CSS values and converts it to an array | ||
*/ | ||
function getValuesAsList(value) { | ||
@@ -118,15 +116,23 @@ return value.replace(/ +/g, ' ') // remove all extraneous spaces | ||
state = _ref2.state; | ||
var openParansCount = (item.match(/\(/g) || []).length; | ||
var closedParansCount = (item.match(/\)/g) || []).length; | ||
if (state.parensDepth > 0) { | ||
list[list.length - 1] = list[list.length - 1] + ' ' + item; | ||
list[list.length - 1] = list[list.length - 1] + " " + item; | ||
} else { | ||
list.push(item); | ||
} | ||
state.parensDepth += openParansCount - closedParansCount; | ||
return { list: list, state: state }; | ||
}, { list: [], state: { parensDepth: 0 } }).list; | ||
return { | ||
list: list, | ||
state: state | ||
}; | ||
}, { | ||
list: [], | ||
state: { | ||
parensDepth: 0 | ||
} | ||
}).list; | ||
} | ||
/** | ||
@@ -138,7 +144,11 @@ * This is intended for properties that are `top right bottom left` and will switch them to `top left bottom right` | ||
*/ | ||
function handleQuartetValues(value) { | ||
var splitValues = getValuesAsList(value); | ||
if (splitValues.length <= 3 || splitValues.length > 4) { | ||
return value; | ||
} | ||
var top = splitValues[0], | ||
@@ -148,7 +158,5 @@ right = splitValues[1], | ||
left = splitValues[3]; | ||
return [top, left, bottom, right].join(' '); | ||
} | ||
// some values require a little fudging, that fudging goes here. | ||
var propertyValueConverters = { | ||
@@ -161,2 +169,3 @@ padding: function padding(_ref) { | ||
} | ||
return handleQuartetValues(value); | ||
@@ -166,3 +175,2 @@ }, | ||
var value = _ref2.value; | ||
// intentionally leaving off the `g` flag here because we only want to change the first number (which is the offset-x) | ||
@@ -173,4 +181,5 @@ return value.replace(/(-*)([.|\d]+)/, function (match, negative, number) { | ||
} | ||
var doubleNegative = negative === '' ? '-' : ''; | ||
return '' + doubleNegative + number; | ||
return "" + doubleNegative + number; | ||
}); | ||
@@ -180,3 +189,2 @@ }, | ||
var value = _ref3.value; | ||
return handleQuartetValues(value); | ||
@@ -190,2 +198,3 @@ }, | ||
} | ||
if (includes(value, '/')) { | ||
@@ -202,5 +211,7 @@ var _value$split = value.split('/'), | ||
}); | ||
return convertedRadius1 + ' / ' + convertedRadius2; | ||
return convertedRadius1 + " / " + convertedRadius2; | ||
} | ||
var splitValues = getValuesAsList(value); | ||
switch (splitValues.length) { | ||
@@ -211,2 +222,3 @@ case 2: | ||
} | ||
case 4: | ||
@@ -218,5 +230,5 @@ { | ||
bottomLeft = splitValues[3]; | ||
return [topRight, topLeft, bottomLeft, bottomRight].join(' '); | ||
} | ||
default: | ||
@@ -234,8 +246,7 @@ { | ||
bgPosDirectionRegex = _ref5.bgPosDirectionRegex; | ||
// Yeah, this is in need of a refactor 🙃... | ||
// but this property is a tough cookie 🍪 | ||
// get the backgroundPosition out of the string by removing everything that couldn't be the backgroundPosition value | ||
var backgroundPositionValue = value.replace(/(url\(.*?\))|(rgba?\(.*?\))|(hsl\(.*?\))|(#[a-fA-F0-9]+)|((^| )(\D)+( |$))/g, '').trim(); | ||
// replace that backgroundPosition value with the converted version | ||
var backgroundPositionValue = value.replace(/(url\(.*?\))|(rgba?\(.*?\))|(hsl\(.*?\))|(#[a-fA-F0-9]+)|((^| )(\D)+( |$))/g, '').trim(); // replace that backgroundPosition value with the converted version | ||
value = value.replace(backgroundPositionValue, propertyValueConverters.backgroundPosition({ | ||
@@ -246,4 +257,4 @@ value: backgroundPositionValue, | ||
bgPosDirectionRegex: bgPosDirectionRegex | ||
})); | ||
// do the backgroundImage value replacing on the whole value (because why not?) | ||
})); // do the backgroundImage value replacing on the whole value (because why not?) | ||
return propertyValueConverters.backgroundImage({ | ||
@@ -263,2 +274,3 @@ value: value, | ||
} | ||
return value.replace(bgImgDirectionRegex, function (match, g1, group2) { | ||
@@ -273,5 +285,3 @@ return match.replace(group2, valuesToConvert[group2]); | ||
bgPosDirectionRegex = _ref7.bgPosDirectionRegex; | ||
return value | ||
// intentionally only grabbing the first instance of this because that represents `left` | ||
return value // intentionally only grabbing the first instance of this because that represents `left` | ||
.replace(isRtl ? /^((-|\d|\.)+%)/ : null, function (match, group) { | ||
@@ -292,2 +302,3 @@ return calculateNewBackgroundPosition(group); | ||
} | ||
return propertyValueConverters.backgroundPosition({ | ||
@@ -302,18 +313,14 @@ value: value, | ||
var value = _ref9.value; | ||
// This was copied and modified from CSSJanus: | ||
// https://github.com/cssjanus/cssjanus/blob/4a40f001b1ba35567112d8b8e1d9d95eda4234c3/src/cssjanus.js#L152-L153 | ||
var nonAsciiPattern = '[^\\u0020-\\u007e]'; | ||
var escapePattern = '(?:' + '(?:(?:\\[0-9a-f]{1,6})(?:\\r\\n|\\s)?)' + '|\\\\[^\\r\\n\\f0-9a-f])'; | ||
var signedQuantPattern = '((?:-?' + ('(?:[0-9]*\\.[0-9]+|[0-9]+)' + '(?:\\s*' + '(?:em|ex|px|cm|mm|in|pt|pc|deg|rad|grad|ms|s|hz|khz|%)' + '|' + ('-?' + ('(?:[_a-z]|' + nonAsciiPattern + '|' + escapePattern + ')') + ('(?:[_a-z0-9-]|' + nonAsciiPattern + '|' + escapePattern + ')') + '*') + ')?') + ')|(?:inherit|auto))'; | ||
var translateXRegExp = new RegExp('(translateX\\s*\\(\\s*)' + signedQuantPattern + '(\\s*\\))', 'gi'); | ||
var translateRegExp = new RegExp('(translate\\s*\\(\\s*)' + signedQuantPattern + '((?:\\s*,\\s*' + signedQuantPattern + '){0,1}\\s*\\))', 'gi'); | ||
var translate3dRegExp = new RegExp('(translate3d\\s*\\(\\s*)' + signedQuantPattern + '((?:\\s*,\\s*' + signedQuantPattern + '){0,2}\\s*\\))', 'gi'); | ||
var rotateRegExp = new RegExp('(rotate[ZY]?\\s*\\(\\s*)' + signedQuantPattern + '(\\s*\\))', 'gi'); | ||
var nonAsciiPattern = "[^\\u0020-\\u007e]"; | ||
var escapePattern = "(?:" + '(?:(?:\\[0-9a-f]{1,6})(?:\\r\\n|\\s)?)' + "|\\\\[^\\r\\n\\f0-9a-f])"; | ||
var signedQuantPattern = "((?:-?" + ('(?:[0-9]*\\.[0-9]+|[0-9]+)' + "(?:\\s*" + '(?:em|ex|px|cm|mm|in|pt|pc|deg|rad|grad|ms|s|hz|khz|%)' + "|" + ("-?" + ("(?:[_a-z]|" + nonAsciiPattern + "|" + escapePattern + ")") + ("(?:[_a-z0-9-]|" + nonAsciiPattern + "|" + escapePattern + ")") + "*") + ")?") + ")|(?:inherit|auto))"; | ||
var translateXRegExp = new RegExp("(translateX\\s*\\(\\s*)" + signedQuantPattern + "(\\s*\\))", 'gi'); | ||
var translateRegExp = new RegExp("(translate\\s*\\(\\s*)" + signedQuantPattern + "((?:\\s*,\\s*" + signedQuantPattern + "){0,1}\\s*\\))", 'gi'); | ||
var translate3dRegExp = new RegExp("(translate3d\\s*\\(\\s*)" + signedQuantPattern + "((?:\\s*,\\s*" + signedQuantPattern + "){0,2}\\s*\\))", 'gi'); | ||
var rotateRegExp = new RegExp("(rotate[ZY]?\\s*\\(\\s*)" + signedQuantPattern + "(\\s*\\))", 'gi'); | ||
return value.replace(translateXRegExp, flipTransformSign).replace(translateRegExp, flipTransformSign).replace(translate3dRegExp, flipTransformSign).replace(rotateRegExp, flipTransformSign); | ||
} | ||
}; | ||
propertyValueConverters.margin = propertyValueConverters.padding; | ||
@@ -335,6 +342,4 @@ propertyValueConverters.borderWidth = propertyValueConverters.padding; | ||
propertyValueConverters.WebkitTransformOrigin = propertyValueConverters.transformOrigin; | ||
propertyValueConverters.MozTransformOrigin = propertyValueConverters.transformOrigin; | ||
propertyValueConverters.MozTransformOrigin = propertyValueConverters.transformOrigin; // kebab-case versions | ||
// kebab-case versions | ||
propertyValueConverters['text-shadow'] = propertyValueConverters.textShadow; | ||
@@ -341,0 +346,0 @@ propertyValueConverters['border-color'] = propertyValueConverters.borderColor; |
import { includes, arrayToObject, isBoolean, isNumber, isObject, isString, isNullOrUndefined, propertyValueConverters } from './core.js'; | ||
// this will be an object of properties that map to their corresponding rtl property (their doppelganger) | ||
var propertiesToConvert = arrayToObject([['paddingLeft', 'paddingRight'], ['marginLeft', 'marginRight'], ['left', 'right'], ['borderLeft', 'borderRight'], ['borderLeftColor', 'borderRightColor'], ['borderLeftStyle', 'borderRightStyle'], ['borderLeftWidth', 'borderRightWidth'], ['borderTopLeftRadius', 'borderTopRightRadius'], ['borderBottomLeftRadius', 'borderBottomRightRadius'], | ||
// kebab-case versions | ||
var propertiesToConvert = arrayToObject([['paddingLeft', 'paddingRight'], ['marginLeft', 'marginRight'], ['left', 'right'], ['borderLeft', 'borderRight'], ['borderLeftColor', 'borderRightColor'], ['borderLeftStyle', 'borderRightStyle'], ['borderLeftWidth', 'borderRightWidth'], ['borderTopLeftRadius', 'borderTopRightRadius'], ['borderBottomLeftRadius', 'borderBottomRightRadius'], // kebab-case versions | ||
['padding-left', 'padding-right'], ['margin-left', 'margin-right'], ['border-left', 'border-right'], ['border-left-color', 'border-right-color'], ['border-left-style', 'border-right-style'], ['border-left-width', 'border-right-width'], ['border-top-left-radius', 'border-top-right-radius'], ['border-bottom-left-radius', 'border-bottom-right-radius']]); | ||
var propsToIgnore = ['content']; // this is the same as the propertiesToConvert except for values | ||
var propsToIgnore = ['content']; | ||
// this is the same as the propertiesToConvert except for values | ||
var valuesToConvert = arrayToObject([['ltr', 'rtl'], ['left', 'right'], ['w-resize', 'e-resize'], ['sw-resize', 'se-resize'], ['nw-resize', 'ne-resize']]); | ||
// Sorry for the regex 😞, but basically thisis used to replace _every_ instance of | ||
var valuesToConvert = arrayToObject([['ltr', 'rtl'], ['left', 'right'], ['w-resize', 'e-resize'], ['sw-resize', 'se-resize'], ['nw-resize', 'ne-resize']]); // Sorry for the regex 😞, but basically thisis used to replace _every_ instance of | ||
// `ltr`, `rtl`, `right`, and `left` in `backgroundimage` with the corresponding opposite. | ||
@@ -18,5 +12,5 @@ // A situation we're accepting here: | ||
// Definite trade-offs here, but I think it's a good call. | ||
var bgImgDirectionRegex = new RegExp('(^|\\W|_)((ltr)|(rtl)|(left)|(right))(\\W|_|$)', 'g'); | ||
var bgPosDirectionRegex = new RegExp('(left)|(right)'); | ||
/** | ||
@@ -27,11 +21,13 @@ * converts properties and values in the CSS in JS object to their corresponding RTL values | ||
*/ | ||
function convert(object) { | ||
return Object.keys(object).reduce(function (newObj, originalKey) { | ||
var originalValue = object[originalKey]; | ||
if (isString(originalValue)) { | ||
// you're welcome to later code 😺 | ||
originalValue = originalValue.trim(); | ||
} | ||
} // Some properties should never be transformed | ||
// Some properties should never be transformed | ||
if (includes(propsToIgnore, originalKey)) { | ||
@@ -50,3 +46,2 @@ newObj[originalKey] = originalValue; | ||
} | ||
/** | ||
@@ -58,2 +53,4 @@ * Converts a property and its value to the corresponding RTL key and value | ||
*/ | ||
function convertProperty(originalKey, originalValue) { | ||
@@ -63,5 +60,7 @@ var isNoFlip = /\/\*\s?@noflip\s?\*\//.test(originalValue); | ||
var value = isNoFlip ? originalValue : getValueDoppelganger(key, originalValue); | ||
return { key: key, value: value }; | ||
return { | ||
key: key, | ||
value: value | ||
}; | ||
} | ||
/** | ||
@@ -72,6 +71,7 @@ * This gets the RTL version of the given property if it has a corresponding RTL property | ||
*/ | ||
function getPropertyDoppelganger(property) { | ||
return propertiesToConvert[property] || property; | ||
} | ||
/** | ||
@@ -83,4 +83,7 @@ * This converts the given value to the RTL version of that value based on the key | ||
*/ | ||
function getValueDoppelganger(key, originalValue) { | ||
/* eslint complexity:[2, 9] */ // let's try to keep the complexity down... If we have to do this much more, let's break this up | ||
/* eslint complexity:[2, 9] */ | ||
// let's try to keep the complexity down... If we have to do this much more, let's break this up | ||
if (isNullOrUndefined(originalValue) || isBoolean(originalValue)) { | ||
@@ -93,2 +96,3 @@ return originalValue; | ||
} | ||
var isNum = isNumber(originalValue); | ||
@@ -98,3 +102,4 @@ var importantlessValue = isNum ? originalValue : originalValue.replace(/ !important.*?$/, ''); | ||
var valueConverter = propertyValueConverters[key]; | ||
var newValue = void 0; | ||
var newValue; | ||
if (valueConverter) { | ||
@@ -111,8 +116,10 @@ newValue = valueConverter({ | ||
} | ||
if (isImportant) { | ||
return newValue + ' !important'; | ||
return newValue + " !important"; | ||
} | ||
return newValue; | ||
} | ||
} // here's our main export! 👋 | ||
export default convert; |
@@ -7,8 +7,2 @@ (function (global, factory) { | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { | ||
return typeof obj; | ||
} : function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
/** | ||
@@ -23,3 +17,2 @@ * Takes an array of [keyValue1, keyValue2] pairs and creates an object of {keyValue1: keyValue2, keyValue2: keyValue1} | ||
prop2 = _ref[1]; | ||
obj[prop1] = prop2; | ||
@@ -44,3 +37,3 @@ obj[prop2] = prop1; | ||
function isObject(val) { | ||
return val && (typeof val === 'undefined' ? 'undefined' : _typeof(val)) === 'object'; | ||
return val && typeof val === 'object'; | ||
} | ||
@@ -55,3 +48,2 @@ | ||
} | ||
/** | ||
@@ -66,2 +58,4 @@ * Flip the sign of a CSS value, possibly with a unit. | ||
*/ | ||
function flipSign(value) { | ||
@@ -77,3 +71,3 @@ if (parseFloat(value) === 0) { | ||
return '-' + value; | ||
return "-" + value; | ||
} | ||
@@ -84,3 +78,2 @@ | ||
} | ||
/** | ||
@@ -93,6 +86,9 @@ * Takes a percentage for background position and inverts it. | ||
*/ | ||
function calculateNewBackgroundPosition(value) { | ||
var idx = value.indexOf('.'); | ||
if (idx === -1) { | ||
value = 100 - parseFloat(value) + '%'; | ||
value = 100 - parseFloat(value) + "%"; | ||
} else { | ||
@@ -102,7 +98,7 @@ // Two off, one for the "%" at the end, one for the dot itself | ||
value = 100 - parseFloat(value); | ||
value = value.toFixed(len) + '%'; | ||
value = value.toFixed(len) + "%"; | ||
} | ||
return value; | ||
} | ||
/** | ||
@@ -113,2 +109,4 @@ * This takes a list of CSS values and converts it to an array | ||
*/ | ||
function getValuesAsList(value) { | ||
@@ -125,15 +123,23 @@ return value.replace(/ +/g, ' ') // remove all extraneous spaces | ||
state = _ref2.state; | ||
var openParansCount = (item.match(/\(/g) || []).length; | ||
var closedParansCount = (item.match(/\)/g) || []).length; | ||
if (state.parensDepth > 0) { | ||
list[list.length - 1] = list[list.length - 1] + ' ' + item; | ||
list[list.length - 1] = list[list.length - 1] + " " + item; | ||
} else { | ||
list.push(item); | ||
} | ||
state.parensDepth += openParansCount - closedParansCount; | ||
return { list: list, state: state }; | ||
}, { list: [], state: { parensDepth: 0 } }).list; | ||
return { | ||
list: list, | ||
state: state | ||
}; | ||
}, { | ||
list: [], | ||
state: { | ||
parensDepth: 0 | ||
} | ||
}).list; | ||
} | ||
/** | ||
@@ -145,7 +151,11 @@ * This is intended for properties that are `top right bottom left` and will switch them to `top left bottom right` | ||
*/ | ||
function handleQuartetValues(value) { | ||
var splitValues = getValuesAsList(value); | ||
if (splitValues.length <= 3 || splitValues.length > 4) { | ||
return value; | ||
} | ||
var top = splitValues[0], | ||
@@ -155,7 +165,5 @@ right = splitValues[1], | ||
left = splitValues[3]; | ||
return [top, left, bottom, right].join(' '); | ||
} | ||
// some values require a little fudging, that fudging goes here. | ||
var propertyValueConverters = { | ||
@@ -168,2 +176,3 @@ padding: function padding(_ref) { | ||
} | ||
return handleQuartetValues(value); | ||
@@ -173,3 +182,2 @@ }, | ||
var value = _ref2.value; | ||
// intentionally leaving off the `g` flag here because we only want to change the first number (which is the offset-x) | ||
@@ -180,4 +188,5 @@ return value.replace(/(-*)([.|\d]+)/, function (match, negative, number) { | ||
} | ||
var doubleNegative = negative === '' ? '-' : ''; | ||
return '' + doubleNegative + number; | ||
return "" + doubleNegative + number; | ||
}); | ||
@@ -187,3 +196,2 @@ }, | ||
var value = _ref3.value; | ||
return handleQuartetValues(value); | ||
@@ -197,2 +205,3 @@ }, | ||
} | ||
if (includes(value, '/')) { | ||
@@ -209,5 +218,7 @@ var _value$split = value.split('/'), | ||
}); | ||
return convertedRadius1 + ' / ' + convertedRadius2; | ||
return convertedRadius1 + " / " + convertedRadius2; | ||
} | ||
var splitValues = getValuesAsList(value); | ||
switch (splitValues.length) { | ||
@@ -218,2 +229,3 @@ case 2: | ||
} | ||
case 4: | ||
@@ -225,5 +237,5 @@ { | ||
bottomLeft = splitValues[3]; | ||
return [topRight, topLeft, bottomLeft, bottomRight].join(' '); | ||
} | ||
default: | ||
@@ -241,8 +253,7 @@ { | ||
bgPosDirectionRegex = _ref5.bgPosDirectionRegex; | ||
// Yeah, this is in need of a refactor 🙃... | ||
// but this property is a tough cookie 🍪 | ||
// get the backgroundPosition out of the string by removing everything that couldn't be the backgroundPosition value | ||
var backgroundPositionValue = value.replace(/(url\(.*?\))|(rgba?\(.*?\))|(hsl\(.*?\))|(#[a-fA-F0-9]+)|((^| )(\D)+( |$))/g, '').trim(); | ||
// replace that backgroundPosition value with the converted version | ||
var backgroundPositionValue = value.replace(/(url\(.*?\))|(rgba?\(.*?\))|(hsl\(.*?\))|(#[a-fA-F0-9]+)|((^| )(\D)+( |$))/g, '').trim(); // replace that backgroundPosition value with the converted version | ||
value = value.replace(backgroundPositionValue, propertyValueConverters.backgroundPosition({ | ||
@@ -253,4 +264,4 @@ value: backgroundPositionValue, | ||
bgPosDirectionRegex: bgPosDirectionRegex | ||
})); | ||
// do the backgroundImage value replacing on the whole value (because why not?) | ||
})); // do the backgroundImage value replacing on the whole value (because why not?) | ||
return propertyValueConverters.backgroundImage({ | ||
@@ -270,2 +281,3 @@ value: value, | ||
} | ||
return value.replace(bgImgDirectionRegex, function (match, g1, group2) { | ||
@@ -280,5 +292,3 @@ return match.replace(group2, valuesToConvert[group2]); | ||
bgPosDirectionRegex = _ref7.bgPosDirectionRegex; | ||
return value | ||
// intentionally only grabbing the first instance of this because that represents `left` | ||
return value // intentionally only grabbing the first instance of this because that represents `left` | ||
.replace(isRtl ? /^((-|\d|\.)+%)/ : null, function (match, group) { | ||
@@ -299,2 +309,3 @@ return calculateNewBackgroundPosition(group); | ||
} | ||
return propertyValueConverters.backgroundPosition({ | ||
@@ -309,18 +320,14 @@ value: value, | ||
var value = _ref9.value; | ||
// This was copied and modified from CSSJanus: | ||
// https://github.com/cssjanus/cssjanus/blob/4a40f001b1ba35567112d8b8e1d9d95eda4234c3/src/cssjanus.js#L152-L153 | ||
var nonAsciiPattern = '[^\\u0020-\\u007e]'; | ||
var escapePattern = '(?:' + '(?:(?:\\[0-9a-f]{1,6})(?:\\r\\n|\\s)?)' + '|\\\\[^\\r\\n\\f0-9a-f])'; | ||
var signedQuantPattern = '((?:-?' + ('(?:[0-9]*\\.[0-9]+|[0-9]+)' + '(?:\\s*' + '(?:em|ex|px|cm|mm|in|pt|pc|deg|rad|grad|ms|s|hz|khz|%)' + '|' + ('-?' + ('(?:[_a-z]|' + nonAsciiPattern + '|' + escapePattern + ')') + ('(?:[_a-z0-9-]|' + nonAsciiPattern + '|' + escapePattern + ')') + '*') + ')?') + ')|(?:inherit|auto))'; | ||
var translateXRegExp = new RegExp('(translateX\\s*\\(\\s*)' + signedQuantPattern + '(\\s*\\))', 'gi'); | ||
var translateRegExp = new RegExp('(translate\\s*\\(\\s*)' + signedQuantPattern + '((?:\\s*,\\s*' + signedQuantPattern + '){0,1}\\s*\\))', 'gi'); | ||
var translate3dRegExp = new RegExp('(translate3d\\s*\\(\\s*)' + signedQuantPattern + '((?:\\s*,\\s*' + signedQuantPattern + '){0,2}\\s*\\))', 'gi'); | ||
var rotateRegExp = new RegExp('(rotate[ZY]?\\s*\\(\\s*)' + signedQuantPattern + '(\\s*\\))', 'gi'); | ||
var nonAsciiPattern = "[^\\u0020-\\u007e]"; | ||
var escapePattern = "(?:" + '(?:(?:\\[0-9a-f]{1,6})(?:\\r\\n|\\s)?)' + "|\\\\[^\\r\\n\\f0-9a-f])"; | ||
var signedQuantPattern = "((?:-?" + ('(?:[0-9]*\\.[0-9]+|[0-9]+)' + "(?:\\s*" + '(?:em|ex|px|cm|mm|in|pt|pc|deg|rad|grad|ms|s|hz|khz|%)' + "|" + ("-?" + ("(?:[_a-z]|" + nonAsciiPattern + "|" + escapePattern + ")") + ("(?:[_a-z0-9-]|" + nonAsciiPattern + "|" + escapePattern + ")") + "*") + ")?") + ")|(?:inherit|auto))"; | ||
var translateXRegExp = new RegExp("(translateX\\s*\\(\\s*)" + signedQuantPattern + "(\\s*\\))", 'gi'); | ||
var translateRegExp = new RegExp("(translate\\s*\\(\\s*)" + signedQuantPattern + "((?:\\s*,\\s*" + signedQuantPattern + "){0,1}\\s*\\))", 'gi'); | ||
var translate3dRegExp = new RegExp("(translate3d\\s*\\(\\s*)" + signedQuantPattern + "((?:\\s*,\\s*" + signedQuantPattern + "){0,2}\\s*\\))", 'gi'); | ||
var rotateRegExp = new RegExp("(rotate[ZY]?\\s*\\(\\s*)" + signedQuantPattern + "(\\s*\\))", 'gi'); | ||
return value.replace(translateXRegExp, flipTransformSign).replace(translateRegExp, flipTransformSign).replace(translate3dRegExp, flipTransformSign).replace(rotateRegExp, flipTransformSign); | ||
} | ||
}; | ||
propertyValueConverters.margin = propertyValueConverters.padding; | ||
@@ -342,6 +349,4 @@ propertyValueConverters.borderWidth = propertyValueConverters.padding; | ||
propertyValueConverters.WebkitTransformOrigin = propertyValueConverters.transformOrigin; | ||
propertyValueConverters.MozTransformOrigin = propertyValueConverters.transformOrigin; | ||
propertyValueConverters.MozTransformOrigin = propertyValueConverters.transformOrigin; // kebab-case versions | ||
// kebab-case versions | ||
propertyValueConverters['text-shadow'] = propertyValueConverters.textShadow; | ||
@@ -348,0 +353,0 @@ propertyValueConverters['border-color'] = propertyValueConverters.borderColor; |
@@ -1,2 +0,2 @@ | ||
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(r.rtlCSSJSCore={})}(this,function(r){"use strict";var e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r};function n(r){return"number"==typeof r}function o(r,e){return-1!==r.indexOf(e)}function t(r){return 0===parseFloat(r)?r:"-"===r[0]?r.slice(1):"-"+r}function i(r,e,n,o){return e+t(n)+o}function a(r){var e=r.indexOf(".");if(-1===e)r=100-parseFloat(r)+"%";else{var n=r.length-e-2;r=(r=100-parseFloat(r)).toFixed(n)+"%"}return r}function u(r){return r.replace(/ +/g," ").split(" ").map(function(r){return r.trim()}).filter(Boolean).reduce(function(r,e){var n=r.list,o=r.state,t=(e.match(/\(/g)||[]).length,i=(e.match(/\)/g)||[]).length;return o.parensDepth>0?n[n.length-1]=n[n.length-1]+" "+e:n.push(e),o.parensDepth+=t-i,{list:n,state:o}},{list:[],state:{parensDepth:0}}).list}function s(r){var e=u(r);if(e.length<=3||e.length>4)return r;var n=e[0],o=e[1],t=e[2];return[n,e[3],t,o].join(" ")}var l={padding:function(r){var e=r.value;return n(e)?e:s(e)},textShadow:function(r){return r.value.replace(/(-*)([.|\d]+)/,function(r,e,n){return"0"===n?r:""+(""===e?"-":"")+n})},borderColor:function(r){return s(r.value)},borderRadius:function(r){var e=r.value;if(n(e))return e;if(o(e,"/")){var t=e.split("/"),i=t[0],a=t[1];return l.borderRadius({value:i.trim()})+" / "+l.borderRadius({value:a.trim()})}var s=u(e);switch(s.length){case 2:return s.reverse().join(" ");case 4:var f=s[0],c=s[1],d=s[2];return[c,f,s[3],d].join(" ");default:return e}},background:function(r){var e=r.value,n=r.valuesToConvert,o=r.isRtl,t=r.bgImgDirectionRegex,i=r.bgPosDirectionRegex,a=e.replace(/(url\(.*?\))|(rgba?\(.*?\))|(hsl\(.*?\))|(#[a-fA-F0-9]+)|((^| )(\D)+( |$))/g,"").trim();return e=e.replace(a,l.backgroundPosition({value:a,valuesToConvert:n,isRtl:o,bgPosDirectionRegex:i})),l.backgroundImage({value:e,valuesToConvert:n,bgImgDirectionRegex:t})},backgroundImage:function(r){var e=r.value,n=r.valuesToConvert,t=r.bgImgDirectionRegex;return o(e,"url(")||o(e,"linear-gradient(")?e.replace(t,function(r,e,o){return r.replace(o,n[o])}):e},backgroundPosition:function(r){var e=r.value,n=r.valuesToConvert,o=r.isRtl,t=r.bgPosDirectionRegex;return e.replace(o?/^((-|\d|\.)+%)/:null,function(r,e){return a(e)}).replace(t,function(r){return n[r]})},backgroundPositionX:function(r){var e=r.value,o=r.valuesToConvert,t=r.isRtl,i=r.bgPosDirectionRegex;return n(e)?e:l.backgroundPosition({value:e,valuesToConvert:o,isRtl:t,bgPosDirectionRegex:i})},transform:function(r){var e=r.value,n="(?:(?:(?:\\[0-9a-f]{1,6})(?:\\r\\n|\\s)?)|\\\\[^\\r\\n\\f0-9a-f])",o="((?:-?(?:[0-9]*\\.[0-9]+|[0-9]+)(?:\\s*(?:em|ex|px|cm|mm|in|pt|pc|deg|rad|grad|ms|s|hz|khz|%)|-?(?:[_a-z]|[^\\u0020-\\u007e]|"+n+")(?:[_a-z0-9-]|[^\\u0020-\\u007e]|"+n+")*)?)|(?:inherit|auto))",t=new RegExp("(translateX\\s*\\(\\s*)"+o+"(\\s*\\))","gi"),a=new RegExp("(translate\\s*\\(\\s*)"+o+"((?:\\s*,\\s*"+o+"){0,1}\\s*\\))","gi"),u=new RegExp("(translate3d\\s*\\(\\s*)"+o+"((?:\\s*,\\s*"+o+"){0,2}\\s*\\))","gi"),s=new RegExp("(rotate[ZY]?\\s*\\(\\s*)"+o+"(\\s*\\))","gi");return e.replace(t,i).replace(a,i).replace(u,i).replace(s,i)}};l.margin=l.padding,l.borderWidth=l.padding,l.boxShadow=l.textShadow,l.webkitBoxShadow=l.boxShadow,l.mozBoxShadow=l.boxShadow,l.WebkitBoxShadow=l.boxShadow,l.MozBoxShadow=l.boxShadow,l.borderStyle=l.borderColor,l.webkitTransform=l.transform,l.mozTransform=l.transform,l.WebkitTransform=l.transform,l.MozTransform=l.transform,l.transformOrigin=l.backgroundPosition,l.webkitTransformOrigin=l.transformOrigin,l.mozTransformOrigin=l.transformOrigin,l.WebkitTransformOrigin=l.transformOrigin,l.MozTransformOrigin=l.transformOrigin,l["text-shadow"]=l.textShadow,l["border-color"]=l.borderColor,l["border-radius"]=l.borderRadius,l["background-image"]=l.backgroundImage,l["background-position"]=l.backgroundPosition,l["background-position-x"]=l.backgroundPositionX,l["border-width"]=l.padding,l["box-shadow"]=l.textShadow,l["-webkit-box-shadow"]=l.textShadow,l["-moz-box-shadow"]=l.textShadow,l["border-style"]=l.borderColor,l["-webkit-transform"]=l.transform,l["-moz-transform"]=l.transform,l["transform-origin"]=l.transformOrigin,l["-webkit-transform-origin"]=l.transformOrigin,l["-moz-transform-origin"]=l.transformOrigin,r.propertyValueConverters=l,r.arrayToObject=function(r){return r.reduce(function(r,e){var n=e[0],o=e[1];return r[n]=o,r[o]=n,r},{})},r.calculateNewBackgroundPosition=a,r.calculateNewTranslate=i,r.flipTransformSign=i,r.flipSign=t,r.handleQuartetValues=s,r.includes=o,r.isBoolean=function(r){return"boolean"==typeof r},r.isNullOrUndefined=function(r){return null===r||void 0===r},r.isNumber=n,r.isObject=function(r){return r&&"object"===(void 0===r?"undefined":e(r))},r.isString=function(r){return"string"==typeof r},r.getValuesAsList=u,Object.defineProperty(r,"__esModule",{value:!0})}); | ||
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(r.rtlCSSJSCore={})}(this,function(r){"use strict";function e(r){return"number"==typeof r}function n(r,e){return-1!==r.indexOf(e)}function o(r){return 0===parseFloat(r)?r:"-"===r[0]?r.slice(1):"-"+r}function t(r,e,n,t){return e+o(n)+t}function a(r){var e=r.indexOf(".");if(-1===e)r=100-parseFloat(r)+"%";else{var n=r.length-e-2;r=(r=100-parseFloat(r)).toFixed(n)+"%"}return r}function i(r){return r.replace(/ +/g," ").split(" ").map(function(r){return r.trim()}).filter(Boolean).reduce(function(r,e){var n=r.list,o=r.state,t=(e.match(/\(/g)||[]).length,a=(e.match(/\)/g)||[]).length;return o.parensDepth>0?n[n.length-1]=n[n.length-1]+" "+e:n.push(e),o.parensDepth+=t-a,{list:n,state:o}},{list:[],state:{parensDepth:0}}).list}function u(r){var e=i(r);if(e.length<=3||e.length>4)return r;var n=e[0],o=e[1],t=e[2];return[n,e[3],t,o].join(" ")}var s={padding:function(r){var n=r.value;return e(n)?n:u(n)},textShadow:function(r){return r.value.replace(/(-*)([.|\d]+)/,function(r,e,n){return"0"===n?r:""+(""===e?"-":"")+n})},borderColor:function(r){return u(r.value)},borderRadius:function(r){var o=r.value;if(e(o))return o;if(n(o,"/")){var t=o.split("/"),a=t[0],u=t[1];return s.borderRadius({value:a.trim()})+" / "+s.borderRadius({value:u.trim()})}var l=i(o);switch(l.length){case 2:return l.reverse().join(" ");case 4:var d=l[0],c=l[1],f=l[2];return[c,d,l[3],f].join(" ");default:return o}},background:function(r){var e=r.value,n=r.valuesToConvert,o=r.isRtl,t=r.bgImgDirectionRegex,a=r.bgPosDirectionRegex,i=e.replace(/(url\(.*?\))|(rgba?\(.*?\))|(hsl\(.*?\))|(#[a-fA-F0-9]+)|((^| )(\D)+( |$))/g,"").trim();return e=e.replace(i,s.backgroundPosition({value:i,valuesToConvert:n,isRtl:o,bgPosDirectionRegex:a})),s.backgroundImage({value:e,valuesToConvert:n,bgImgDirectionRegex:t})},backgroundImage:function(r){var e=r.value,o=r.valuesToConvert,t=r.bgImgDirectionRegex;return n(e,"url(")||n(e,"linear-gradient(")?e.replace(t,function(r,e,n){return r.replace(n,o[n])}):e},backgroundPosition:function(r){var e=r.value,n=r.valuesToConvert,o=r.isRtl,t=r.bgPosDirectionRegex;return e.replace(o?/^((-|\d|\.)+%)/:null,function(r,e){return a(e)}).replace(t,function(r){return n[r]})},backgroundPositionX:function(r){var n=r.value,o=r.valuesToConvert,t=r.isRtl,a=r.bgPosDirectionRegex;return e(n)?n:s.backgroundPosition({value:n,valuesToConvert:o,isRtl:t,bgPosDirectionRegex:a})},transform:function(r){var e=r.value,n="(?:(?:(?:\\[0-9a-f]{1,6})(?:\\r\\n|\\s)?)|\\\\[^\\r\\n\\f0-9a-f])",o="((?:-?(?:[0-9]*\\.[0-9]+|[0-9]+)(?:\\s*(?:em|ex|px|cm|mm|in|pt|pc|deg|rad|grad|ms|s|hz|khz|%)|-?(?:[_a-z]|[^\\u0020-\\u007e]|"+n+")(?:[_a-z0-9-]|[^\\u0020-\\u007e]|"+n+")*)?)|(?:inherit|auto))",a=new RegExp("(translateX\\s*\\(\\s*)"+o+"(\\s*\\))","gi"),i=new RegExp("(translate\\s*\\(\\s*)"+o+"((?:\\s*,\\s*"+o+"){0,1}\\s*\\))","gi"),u=new RegExp("(translate3d\\s*\\(\\s*)"+o+"((?:\\s*,\\s*"+o+"){0,2}\\s*\\))","gi"),s=new RegExp("(rotate[ZY]?\\s*\\(\\s*)"+o+"(\\s*\\))","gi");return e.replace(a,t).replace(i,t).replace(u,t).replace(s,t)}};s.margin=s.padding,s.borderWidth=s.padding,s.boxShadow=s.textShadow,s.webkitBoxShadow=s.boxShadow,s.mozBoxShadow=s.boxShadow,s.WebkitBoxShadow=s.boxShadow,s.MozBoxShadow=s.boxShadow,s.borderStyle=s.borderColor,s.webkitTransform=s.transform,s.mozTransform=s.transform,s.WebkitTransform=s.transform,s.MozTransform=s.transform,s.transformOrigin=s.backgroundPosition,s.webkitTransformOrigin=s.transformOrigin,s.mozTransformOrigin=s.transformOrigin,s.WebkitTransformOrigin=s.transformOrigin,s.MozTransformOrigin=s.transformOrigin,s["text-shadow"]=s.textShadow,s["border-color"]=s.borderColor,s["border-radius"]=s.borderRadius,s["background-image"]=s.backgroundImage,s["background-position"]=s.backgroundPosition,s["background-position-x"]=s.backgroundPositionX,s["border-width"]=s.padding,s["box-shadow"]=s.textShadow,s["-webkit-box-shadow"]=s.textShadow,s["-moz-box-shadow"]=s.textShadow,s["border-style"]=s.borderColor,s["-webkit-transform"]=s.transform,s["-moz-transform"]=s.transform,s["transform-origin"]=s.transformOrigin,s["-webkit-transform-origin"]=s.transformOrigin,s["-moz-transform-origin"]=s.transformOrigin,r.propertyValueConverters=s,r.arrayToObject=function(r){return r.reduce(function(r,e){var n=e[0],o=e[1];return r[n]=o,r[o]=n,r},{})},r.calculateNewBackgroundPosition=a,r.calculateNewTranslate=t,r.flipTransformSign=t,r.flipSign=o,r.handleQuartetValues=u,r.includes=n,r.isBoolean=function(r){return"boolean"==typeof r},r.isNullOrUndefined=function(r){return null==r},r.isNumber=e,r.isObject=function(r){return r&&"object"==typeof r},r.isString=function(r){return"string"==typeof r},r.getValuesAsList=i,Object.defineProperty(r,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=rtl-css-js.core.umd.min.js.map |
@@ -7,8 +7,2 @@ (function (global, factory) { | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { | ||
return typeof obj; | ||
} : function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
/** | ||
@@ -23,3 +17,2 @@ * Takes an array of [keyValue1, keyValue2] pairs and creates an object of {keyValue1: keyValue2, keyValue2: keyValue1} | ||
prop2 = _ref[1]; | ||
obj[prop1] = prop2; | ||
@@ -44,3 +37,3 @@ obj[prop2] = prop1; | ||
function isObject(val) { | ||
return val && (typeof val === 'undefined' ? 'undefined' : _typeof(val)) === 'object'; | ||
return val && typeof val === 'object'; | ||
} | ||
@@ -55,3 +48,2 @@ | ||
} | ||
/** | ||
@@ -66,2 +58,4 @@ * Flip the sign of a CSS value, possibly with a unit. | ||
*/ | ||
function flipSign(value) { | ||
@@ -77,3 +71,3 @@ if (parseFloat(value) === 0) { | ||
return '-' + value; | ||
return "-" + value; | ||
} | ||
@@ -84,3 +78,2 @@ | ||
} | ||
/** | ||
@@ -93,6 +86,9 @@ * Takes a percentage for background position and inverts it. | ||
*/ | ||
function calculateNewBackgroundPosition(value) { | ||
var idx = value.indexOf('.'); | ||
if (idx === -1) { | ||
value = 100 - parseFloat(value) + '%'; | ||
value = 100 - parseFloat(value) + "%"; | ||
} else { | ||
@@ -102,7 +98,7 @@ // Two off, one for the "%" at the end, one for the dot itself | ||
value = 100 - parseFloat(value); | ||
value = value.toFixed(len) + '%'; | ||
value = value.toFixed(len) + "%"; | ||
} | ||
return value; | ||
} | ||
/** | ||
@@ -113,2 +109,4 @@ * This takes a list of CSS values and converts it to an array | ||
*/ | ||
function getValuesAsList(value) { | ||
@@ -125,15 +123,23 @@ return value.replace(/ +/g, ' ') // remove all extraneous spaces | ||
state = _ref2.state; | ||
var openParansCount = (item.match(/\(/g) || []).length; | ||
var closedParansCount = (item.match(/\)/g) || []).length; | ||
if (state.parensDepth > 0) { | ||
list[list.length - 1] = list[list.length - 1] + ' ' + item; | ||
list[list.length - 1] = list[list.length - 1] + " " + item; | ||
} else { | ||
list.push(item); | ||
} | ||
state.parensDepth += openParansCount - closedParansCount; | ||
return { list: list, state: state }; | ||
}, { list: [], state: { parensDepth: 0 } }).list; | ||
return { | ||
list: list, | ||
state: state | ||
}; | ||
}, { | ||
list: [], | ||
state: { | ||
parensDepth: 0 | ||
} | ||
}).list; | ||
} | ||
/** | ||
@@ -145,7 +151,11 @@ * This is intended for properties that are `top right bottom left` and will switch them to `top left bottom right` | ||
*/ | ||
function handleQuartetValues(value) { | ||
var splitValues = getValuesAsList(value); | ||
if (splitValues.length <= 3 || splitValues.length > 4) { | ||
return value; | ||
} | ||
var top = splitValues[0], | ||
@@ -155,7 +165,5 @@ right = splitValues[1], | ||
left = splitValues[3]; | ||
return [top, left, bottom, right].join(' '); | ||
} | ||
// some values require a little fudging, that fudging goes here. | ||
var propertyValueConverters = { | ||
@@ -168,2 +176,3 @@ padding: function padding(_ref) { | ||
} | ||
return handleQuartetValues(value); | ||
@@ -173,3 +182,2 @@ }, | ||
var value = _ref2.value; | ||
// intentionally leaving off the `g` flag here because we only want to change the first number (which is the offset-x) | ||
@@ -180,4 +188,5 @@ return value.replace(/(-*)([.|\d]+)/, function (match, negative, number) { | ||
} | ||
var doubleNegative = negative === '' ? '-' : ''; | ||
return '' + doubleNegative + number; | ||
return "" + doubleNegative + number; | ||
}); | ||
@@ -187,3 +196,2 @@ }, | ||
var value = _ref3.value; | ||
return handleQuartetValues(value); | ||
@@ -197,2 +205,3 @@ }, | ||
} | ||
if (includes(value, '/')) { | ||
@@ -209,5 +218,7 @@ var _value$split = value.split('/'), | ||
}); | ||
return convertedRadius1 + ' / ' + convertedRadius2; | ||
return convertedRadius1 + " / " + convertedRadius2; | ||
} | ||
var splitValues = getValuesAsList(value); | ||
switch (splitValues.length) { | ||
@@ -218,2 +229,3 @@ case 2: | ||
} | ||
case 4: | ||
@@ -225,5 +237,5 @@ { | ||
bottomLeft = splitValues[3]; | ||
return [topRight, topLeft, bottomLeft, bottomRight].join(' '); | ||
} | ||
default: | ||
@@ -241,8 +253,7 @@ { | ||
bgPosDirectionRegex = _ref5.bgPosDirectionRegex; | ||
// Yeah, this is in need of a refactor 🙃... | ||
// but this property is a tough cookie 🍪 | ||
// get the backgroundPosition out of the string by removing everything that couldn't be the backgroundPosition value | ||
var backgroundPositionValue = value.replace(/(url\(.*?\))|(rgba?\(.*?\))|(hsl\(.*?\))|(#[a-fA-F0-9]+)|((^| )(\D)+( |$))/g, '').trim(); | ||
// replace that backgroundPosition value with the converted version | ||
var backgroundPositionValue = value.replace(/(url\(.*?\))|(rgba?\(.*?\))|(hsl\(.*?\))|(#[a-fA-F0-9]+)|((^| )(\D)+( |$))/g, '').trim(); // replace that backgroundPosition value with the converted version | ||
value = value.replace(backgroundPositionValue, propertyValueConverters.backgroundPosition({ | ||
@@ -253,4 +264,4 @@ value: backgroundPositionValue, | ||
bgPosDirectionRegex: bgPosDirectionRegex | ||
})); | ||
// do the backgroundImage value replacing on the whole value (because why not?) | ||
})); // do the backgroundImage value replacing on the whole value (because why not?) | ||
return propertyValueConverters.backgroundImage({ | ||
@@ -270,2 +281,3 @@ value: value, | ||
} | ||
return value.replace(bgImgDirectionRegex, function (match, g1, group2) { | ||
@@ -280,5 +292,3 @@ return match.replace(group2, valuesToConvert[group2]); | ||
bgPosDirectionRegex = _ref7.bgPosDirectionRegex; | ||
return value | ||
// intentionally only grabbing the first instance of this because that represents `left` | ||
return value // intentionally only grabbing the first instance of this because that represents `left` | ||
.replace(isRtl ? /^((-|\d|\.)+%)/ : null, function (match, group) { | ||
@@ -299,2 +309,3 @@ return calculateNewBackgroundPosition(group); | ||
} | ||
return propertyValueConverters.backgroundPosition({ | ||
@@ -309,18 +320,14 @@ value: value, | ||
var value = _ref9.value; | ||
// This was copied and modified from CSSJanus: | ||
// https://github.com/cssjanus/cssjanus/blob/4a40f001b1ba35567112d8b8e1d9d95eda4234c3/src/cssjanus.js#L152-L153 | ||
var nonAsciiPattern = '[^\\u0020-\\u007e]'; | ||
var escapePattern = '(?:' + '(?:(?:\\[0-9a-f]{1,6})(?:\\r\\n|\\s)?)' + '|\\\\[^\\r\\n\\f0-9a-f])'; | ||
var signedQuantPattern = '((?:-?' + ('(?:[0-9]*\\.[0-9]+|[0-9]+)' + '(?:\\s*' + '(?:em|ex|px|cm|mm|in|pt|pc|deg|rad|grad|ms|s|hz|khz|%)' + '|' + ('-?' + ('(?:[_a-z]|' + nonAsciiPattern + '|' + escapePattern + ')') + ('(?:[_a-z0-9-]|' + nonAsciiPattern + '|' + escapePattern + ')') + '*') + ')?') + ')|(?:inherit|auto))'; | ||
var translateXRegExp = new RegExp('(translateX\\s*\\(\\s*)' + signedQuantPattern + '(\\s*\\))', 'gi'); | ||
var translateRegExp = new RegExp('(translate\\s*\\(\\s*)' + signedQuantPattern + '((?:\\s*,\\s*' + signedQuantPattern + '){0,1}\\s*\\))', 'gi'); | ||
var translate3dRegExp = new RegExp('(translate3d\\s*\\(\\s*)' + signedQuantPattern + '((?:\\s*,\\s*' + signedQuantPattern + '){0,2}\\s*\\))', 'gi'); | ||
var rotateRegExp = new RegExp('(rotate[ZY]?\\s*\\(\\s*)' + signedQuantPattern + '(\\s*\\))', 'gi'); | ||
var nonAsciiPattern = "[^\\u0020-\\u007e]"; | ||
var escapePattern = "(?:" + '(?:(?:\\[0-9a-f]{1,6})(?:\\r\\n|\\s)?)' + "|\\\\[^\\r\\n\\f0-9a-f])"; | ||
var signedQuantPattern = "((?:-?" + ('(?:[0-9]*\\.[0-9]+|[0-9]+)' + "(?:\\s*" + '(?:em|ex|px|cm|mm|in|pt|pc|deg|rad|grad|ms|s|hz|khz|%)' + "|" + ("-?" + ("(?:[_a-z]|" + nonAsciiPattern + "|" + escapePattern + ")") + ("(?:[_a-z0-9-]|" + nonAsciiPattern + "|" + escapePattern + ")") + "*") + ")?") + ")|(?:inherit|auto))"; | ||
var translateXRegExp = new RegExp("(translateX\\s*\\(\\s*)" + signedQuantPattern + "(\\s*\\))", 'gi'); | ||
var translateRegExp = new RegExp("(translate\\s*\\(\\s*)" + signedQuantPattern + "((?:\\s*,\\s*" + signedQuantPattern + "){0,1}\\s*\\))", 'gi'); | ||
var translate3dRegExp = new RegExp("(translate3d\\s*\\(\\s*)" + signedQuantPattern + "((?:\\s*,\\s*" + signedQuantPattern + "){0,2}\\s*\\))", 'gi'); | ||
var rotateRegExp = new RegExp("(rotate[ZY]?\\s*\\(\\s*)" + signedQuantPattern + "(\\s*\\))", 'gi'); | ||
return value.replace(translateXRegExp, flipTransformSign).replace(translateRegExp, flipTransformSign).replace(translate3dRegExp, flipTransformSign).replace(rotateRegExp, flipTransformSign); | ||
} | ||
}; | ||
propertyValueConverters.margin = propertyValueConverters.padding; | ||
@@ -342,6 +349,4 @@ propertyValueConverters.borderWidth = propertyValueConverters.padding; | ||
propertyValueConverters.WebkitTransformOrigin = propertyValueConverters.transformOrigin; | ||
propertyValueConverters.MozTransformOrigin = propertyValueConverters.transformOrigin; | ||
propertyValueConverters.MozTransformOrigin = propertyValueConverters.transformOrigin; // kebab-case versions | ||
// kebab-case versions | ||
propertyValueConverters['text-shadow'] = propertyValueConverters.textShadow; | ||
@@ -364,13 +369,7 @@ propertyValueConverters['border-color'] = propertyValueConverters.borderColor; | ||
// this will be an object of properties that map to their corresponding rtl property (their doppelganger) | ||
var propertiesToConvert = arrayToObject([['paddingLeft', 'paddingRight'], ['marginLeft', 'marginRight'], ['left', 'right'], ['borderLeft', 'borderRight'], ['borderLeftColor', 'borderRightColor'], ['borderLeftStyle', 'borderRightStyle'], ['borderLeftWidth', 'borderRightWidth'], ['borderTopLeftRadius', 'borderTopRightRadius'], ['borderBottomLeftRadius', 'borderBottomRightRadius'], | ||
// kebab-case versions | ||
var propertiesToConvert = arrayToObject([['paddingLeft', 'paddingRight'], ['marginLeft', 'marginRight'], ['left', 'right'], ['borderLeft', 'borderRight'], ['borderLeftColor', 'borderRightColor'], ['borderLeftStyle', 'borderRightStyle'], ['borderLeftWidth', 'borderRightWidth'], ['borderTopLeftRadius', 'borderTopRightRadius'], ['borderBottomLeftRadius', 'borderBottomRightRadius'], // kebab-case versions | ||
['padding-left', 'padding-right'], ['margin-left', 'margin-right'], ['border-left', 'border-right'], ['border-left-color', 'border-right-color'], ['border-left-style', 'border-right-style'], ['border-left-width', 'border-right-width'], ['border-top-left-radius', 'border-top-right-radius'], ['border-bottom-left-radius', 'border-bottom-right-radius']]); | ||
var propsToIgnore = ['content']; // this is the same as the propertiesToConvert except for values | ||
var propsToIgnore = ['content']; | ||
// this is the same as the propertiesToConvert except for values | ||
var valuesToConvert = arrayToObject([['ltr', 'rtl'], ['left', 'right'], ['w-resize', 'e-resize'], ['sw-resize', 'se-resize'], ['nw-resize', 'ne-resize']]); | ||
// Sorry for the regex 😞, but basically thisis used to replace _every_ instance of | ||
var valuesToConvert = arrayToObject([['ltr', 'rtl'], ['left', 'right'], ['w-resize', 'e-resize'], ['sw-resize', 'se-resize'], ['nw-resize', 'ne-resize']]); // Sorry for the regex 😞, but basically thisis used to replace _every_ instance of | ||
// `ltr`, `rtl`, `right`, and `left` in `backgroundimage` with the corresponding opposite. | ||
@@ -380,5 +379,5 @@ // A situation we're accepting here: | ||
// Definite trade-offs here, but I think it's a good call. | ||
var bgImgDirectionRegex = new RegExp('(^|\\W|_)((ltr)|(rtl)|(left)|(right))(\\W|_|$)', 'g'); | ||
var bgPosDirectionRegex = new RegExp('(left)|(right)'); | ||
/** | ||
@@ -389,11 +388,13 @@ * converts properties and values in the CSS in JS object to their corresponding RTL values | ||
*/ | ||
function convert(object) { | ||
return Object.keys(object).reduce(function (newObj, originalKey) { | ||
var originalValue = object[originalKey]; | ||
if (isString(originalValue)) { | ||
// you're welcome to later code 😺 | ||
originalValue = originalValue.trim(); | ||
} | ||
} // Some properties should never be transformed | ||
// Some properties should never be transformed | ||
if (includes(propsToIgnore, originalKey)) { | ||
@@ -412,3 +413,2 @@ newObj[originalKey] = originalValue; | ||
} | ||
/** | ||
@@ -420,2 +420,4 @@ * Converts a property and its value to the corresponding RTL key and value | ||
*/ | ||
function convertProperty(originalKey, originalValue) { | ||
@@ -425,5 +427,7 @@ var isNoFlip = /\/\*\s?@noflip\s?\*\//.test(originalValue); | ||
var value = isNoFlip ? originalValue : getValueDoppelganger(key, originalValue); | ||
return { key: key, value: value }; | ||
return { | ||
key: key, | ||
value: value | ||
}; | ||
} | ||
/** | ||
@@ -434,6 +438,7 @@ * This gets the RTL version of the given property if it has a corresponding RTL property | ||
*/ | ||
function getPropertyDoppelganger(property) { | ||
return propertiesToConvert[property] || property; | ||
} | ||
/** | ||
@@ -445,4 +450,7 @@ * This converts the given value to the RTL version of that value based on the key | ||
*/ | ||
function getValueDoppelganger(key, originalValue) { | ||
/* eslint complexity:[2, 9] */ // let's try to keep the complexity down... If we have to do this much more, let's break this up | ||
/* eslint complexity:[2, 9] */ | ||
// let's try to keep the complexity down... If we have to do this much more, let's break this up | ||
if (isNullOrUndefined(originalValue) || isBoolean(originalValue)) { | ||
@@ -455,2 +463,3 @@ return originalValue; | ||
} | ||
var isNum = isNumber(originalValue); | ||
@@ -460,3 +469,4 @@ var importantlessValue = isNum ? originalValue : originalValue.replace(/ !important.*?$/, ''); | ||
var valueConverter = propertyValueConverters[key]; | ||
var newValue = void 0; | ||
var newValue; | ||
if (valueConverter) { | ||
@@ -473,7 +483,9 @@ newValue = valueConverter({ | ||
} | ||
if (isImportant) { | ||
return newValue + ' !important'; | ||
return newValue + " !important"; | ||
} | ||
return newValue; | ||
} | ||
} // here's our main export! 👋 | ||
@@ -480,0 +492,0 @@ return convert; |
@@ -1,2 +0,2 @@ | ||
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):r.RtlCssJs=e()}(this,function(){"use strict";var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r};function e(r){return r.reduce(function(r,e){var t=e[0],o=e[1];return r[t]=o,r[o]=t,r},{})}function t(r){return"number"==typeof r}function o(r,e){return-1!==r.indexOf(e)}function n(r,e,t,o){return e+(n=t,0===parseFloat(n)?n:"-"===n[0]?n.slice(1):"-"+n)+o;var n}function i(r){return r.replace(/ +/g," ").split(" ").map(function(r){return r.trim()}).filter(Boolean).reduce(function(r,e){var t=r.list,o=r.state,n=(e.match(/\(/g)||[]).length,i=(e.match(/\)/g)||[]).length;return o.parensDepth>0?t[t.length-1]=t[t.length-1]+" "+e:t.push(e),o.parensDepth+=n-i,{list:t,state:o}},{list:[],state:{parensDepth:0}}).list}function a(r){var e=i(r);if(e.length<=3||e.length>4)return r;var t=e[0],o=e[1],n=e[2];return[t,e[3],n,o].join(" ")}var u={padding:function(r){var e=r.value;return t(e)?e:a(e)},textShadow:function(r){return r.value.replace(/(-*)([.|\d]+)/,function(r,e,t){return"0"===t?r:""+(""===e?"-":"")+t})},borderColor:function(r){return a(r.value)},borderRadius:function(r){var e=r.value;if(t(e))return e;if(o(e,"/")){var n=e.split("/"),a=n[0],s=n[1];return u.borderRadius({value:a.trim()})+" / "+u.borderRadius({value:s.trim()})}var d=i(e);switch(d.length){case 2:return d.reverse().join(" ");case 4:var l=d[0],g=d[1],f=d[2];return[g,l,d[3],f].join(" ");default:return e}},background:function(r){var e=r.value,t=r.valuesToConvert,o=r.isRtl,n=r.bgImgDirectionRegex,i=r.bgPosDirectionRegex,a=e.replace(/(url\(.*?\))|(rgba?\(.*?\))|(hsl\(.*?\))|(#[a-fA-F0-9]+)|((^| )(\D)+( |$))/g,"").trim();return e=e.replace(a,u.backgroundPosition({value:a,valuesToConvert:t,isRtl:o,bgPosDirectionRegex:i})),u.backgroundImage({value:e,valuesToConvert:t,bgImgDirectionRegex:n})},backgroundImage:function(r){var e=r.value,t=r.valuesToConvert,n=r.bgImgDirectionRegex;return o(e,"url(")||o(e,"linear-gradient(")?e.replace(n,function(r,e,o){return r.replace(o,t[o])}):e},backgroundPosition:function(r){var e=r.value,t=r.valuesToConvert,o=r.isRtl,n=r.bgPosDirectionRegex;return e.replace(o?/^((-|\d|\.)+%)/:null,function(r,e){return function(r){var e=r.indexOf(".");if(-1===e)r=100-parseFloat(r)+"%";else{var t=r.length-e-2;r=(r=100-parseFloat(r)).toFixed(t)+"%"}return r}(e)}).replace(n,function(r){return t[r]})},backgroundPositionX:function(r){var e=r.value,o=r.valuesToConvert,n=r.isRtl,i=r.bgPosDirectionRegex;return t(e)?e:u.backgroundPosition({value:e,valuesToConvert:o,isRtl:n,bgPosDirectionRegex:i})},transform:function(r){var e=r.value,t="(?:(?:(?:\\[0-9a-f]{1,6})(?:\\r\\n|\\s)?)|\\\\[^\\r\\n\\f0-9a-f])",o="((?:-?(?:[0-9]*\\.[0-9]+|[0-9]+)(?:\\s*(?:em|ex|px|cm|mm|in|pt|pc|deg|rad|grad|ms|s|hz|khz|%)|-?(?:[_a-z]|[^\\u0020-\\u007e]|"+t+")(?:[_a-z0-9-]|[^\\u0020-\\u007e]|"+t+")*)?)|(?:inherit|auto))",i=new RegExp("(translateX\\s*\\(\\s*)"+o+"(\\s*\\))","gi"),a=new RegExp("(translate\\s*\\(\\s*)"+o+"((?:\\s*,\\s*"+o+"){0,1}\\s*\\))","gi"),u=new RegExp("(translate3d\\s*\\(\\s*)"+o+"((?:\\s*,\\s*"+o+"){0,2}\\s*\\))","gi"),s=new RegExp("(rotate[ZY]?\\s*\\(\\s*)"+o+"(\\s*\\))","gi");return e.replace(i,n).replace(a,n).replace(u,n).replace(s,n)}};u.margin=u.padding,u.borderWidth=u.padding,u.boxShadow=u.textShadow,u.webkitBoxShadow=u.boxShadow,u.mozBoxShadow=u.boxShadow,u.WebkitBoxShadow=u.boxShadow,u.MozBoxShadow=u.boxShadow,u.borderStyle=u.borderColor,u.webkitTransform=u.transform,u.mozTransform=u.transform,u.WebkitTransform=u.transform,u.MozTransform=u.transform,u.transformOrigin=u.backgroundPosition,u.webkitTransformOrigin=u.transformOrigin,u.mozTransformOrigin=u.transformOrigin,u.WebkitTransformOrigin=u.transformOrigin,u.MozTransformOrigin=u.transformOrigin,u["text-shadow"]=u.textShadow,u["border-color"]=u.borderColor,u["border-radius"]=u.borderRadius,u["background-image"]=u.backgroundImage,u["background-position"]=u.backgroundPosition,u["background-position-x"]=u.backgroundPositionX,u["border-width"]=u.padding,u["box-shadow"]=u.textShadow,u["-webkit-box-shadow"]=u.textShadow,u["-moz-box-shadow"]=u.textShadow,u["border-style"]=u.borderColor,u["-webkit-transform"]=u.transform,u["-moz-transform"]=u.transform,u["transform-origin"]=u.transformOrigin,u["-webkit-transform-origin"]=u.transformOrigin,u["-moz-transform-origin"]=u.transformOrigin;var s=e([["paddingLeft","paddingRight"],["marginLeft","marginRight"],["left","right"],["borderLeft","borderRight"],["borderLeftColor","borderRightColor"],["borderLeftStyle","borderRightStyle"],["borderLeftWidth","borderRightWidth"],["borderTopLeftRadius","borderTopRightRadius"],["borderBottomLeftRadius","borderBottomRightRadius"],["padding-left","padding-right"],["margin-left","margin-right"],["border-left","border-right"],["border-left-color","border-right-color"],["border-left-style","border-right-style"],["border-left-width","border-right-width"],["border-top-left-radius","border-top-right-radius"],["border-bottom-left-radius","border-bottom-right-radius"]]),d=["content"],l=e([["ltr","rtl"],["left","right"],["w-resize","e-resize"],["sw-resize","se-resize"],["nw-resize","ne-resize"]]),g=new RegExp("(^|\\W|_)((ltr)|(rtl)|(left)|(right))(\\W|_|$)","g"),f=new RegExp("(left)|(right)");function b(e){return Object.keys(e).reduce(function(n,i){var a=e[i];if("string"==typeof a&&(a=a.trim()),o(d,i))return n[i]=a,n;var c=function(e,o){var n=/\/\*\s?@noflip\s?\*\//.test(o),i=n?e:(d=e,s[d]||d),a=n?o:function(e,o){if(n=o,null===n||void 0===n||function(r){return"boolean"==typeof r}(o))return o;var n;if(function(e){return e&&"object"===(void 0===e?"undefined":r(e))}(o))return b(o);var i=t(o),a=i?o:o.replace(/ !important.*?$/,""),s=!i&&a.length!==o.length,d=u[e],c=void 0;c=d?d({value:a,valuesToConvert:l,isRtl:!0,bgImgDirectionRegex:g,bgPosDirectionRegex:f}):l[a]||a;if(s)return c+" !important";return c}(i,o);var d;return{key:i,value:a}}(i,a),m=c.key,h=c.value;return n[m]=h,n},Array.isArray(e)?[]:{})}return b}); | ||
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):r.RtlCssJs=e()}(this,function(){"use strict";function r(r){return r.reduce(function(r,e){var t=e[0],o=e[1];return r[t]=o,r[o]=t,r},{})}function e(r){return"number"==typeof r}function t(r,e){return-1!==r.indexOf(e)}function o(r,e,t,o){return e+(n=t,0===parseFloat(n)?n:"-"===n[0]?n.slice(1):"-"+n)+o;var n}function n(r){return r.replace(/ +/g," ").split(" ").map(function(r){return r.trim()}).filter(Boolean).reduce(function(r,e){var t=r.list,o=r.state,n=(e.match(/\(/g)||[]).length,i=(e.match(/\)/g)||[]).length;return o.parensDepth>0?t[t.length-1]=t[t.length-1]+" "+e:t.push(e),o.parensDepth+=n-i,{list:t,state:o}},{list:[],state:{parensDepth:0}}).list}function i(r){var e=n(r);if(e.length<=3||e.length>4)return r;var t=e[0],o=e[1],i=e[2];return[t,e[3],i,o].join(" ")}var a={padding:function(r){var t=r.value;return e(t)?t:i(t)},textShadow:function(r){return r.value.replace(/(-*)([.|\d]+)/,function(r,e,t){return"0"===t?r:""+(""===e?"-":"")+t})},borderColor:function(r){return i(r.value)},borderRadius:function(r){var o=r.value;if(e(o))return o;if(t(o,"/")){var i=o.split("/"),s=i[0],u=i[1];return a.borderRadius({value:s.trim()})+" / "+a.borderRadius({value:u.trim()})}var d=n(o);switch(d.length){case 2:return d.reverse().join(" ");case 4:var g=d[0],l=d[1],f=d[2];return[l,g,d[3],f].join(" ");default:return o}},background:function(r){var e=r.value,t=r.valuesToConvert,o=r.isRtl,n=r.bgImgDirectionRegex,i=r.bgPosDirectionRegex,s=e.replace(/(url\(.*?\))|(rgba?\(.*?\))|(hsl\(.*?\))|(#[a-fA-F0-9]+)|((^| )(\D)+( |$))/g,"").trim();return e=e.replace(s,a.backgroundPosition({value:s,valuesToConvert:t,isRtl:o,bgPosDirectionRegex:i})),a.backgroundImage({value:e,valuesToConvert:t,bgImgDirectionRegex:n})},backgroundImage:function(r){var e=r.value,o=r.valuesToConvert,n=r.bgImgDirectionRegex;return t(e,"url(")||t(e,"linear-gradient(")?e.replace(n,function(r,e,t){return r.replace(t,o[t])}):e},backgroundPosition:function(r){var e=r.value,t=r.valuesToConvert,o=r.isRtl,n=r.bgPosDirectionRegex;return e.replace(o?/^((-|\d|\.)+%)/:null,function(r,e){return function(r){var e=r.indexOf(".");if(-1===e)r=100-parseFloat(r)+"%";else{var t=r.length-e-2;r=(r=100-parseFloat(r)).toFixed(t)+"%"}return r}(e)}).replace(n,function(r){return t[r]})},backgroundPositionX:function(r){var t=r.value,o=r.valuesToConvert,n=r.isRtl,i=r.bgPosDirectionRegex;return e(t)?t:a.backgroundPosition({value:t,valuesToConvert:o,isRtl:n,bgPosDirectionRegex:i})},transform:function(r){var e=r.value,t="(?:(?:(?:\\[0-9a-f]{1,6})(?:\\r\\n|\\s)?)|\\\\[^\\r\\n\\f0-9a-f])",n="((?:-?(?:[0-9]*\\.[0-9]+|[0-9]+)(?:\\s*(?:em|ex|px|cm|mm|in|pt|pc|deg|rad|grad|ms|s|hz|khz|%)|-?(?:[_a-z]|[^\\u0020-\\u007e]|"+t+")(?:[_a-z0-9-]|[^\\u0020-\\u007e]|"+t+")*)?)|(?:inherit|auto))",i=new RegExp("(translateX\\s*\\(\\s*)"+n+"(\\s*\\))","gi"),a=new RegExp("(translate\\s*\\(\\s*)"+n+"((?:\\s*,\\s*"+n+"){0,1}\\s*\\))","gi"),s=new RegExp("(translate3d\\s*\\(\\s*)"+n+"((?:\\s*,\\s*"+n+"){0,2}\\s*\\))","gi"),u=new RegExp("(rotate[ZY]?\\s*\\(\\s*)"+n+"(\\s*\\))","gi");return e.replace(i,o).replace(a,o).replace(s,o).replace(u,o)}};a.margin=a.padding,a.borderWidth=a.padding,a.boxShadow=a.textShadow,a.webkitBoxShadow=a.boxShadow,a.mozBoxShadow=a.boxShadow,a.WebkitBoxShadow=a.boxShadow,a.MozBoxShadow=a.boxShadow,a.borderStyle=a.borderColor,a.webkitTransform=a.transform,a.mozTransform=a.transform,a.WebkitTransform=a.transform,a.MozTransform=a.transform,a.transformOrigin=a.backgroundPosition,a.webkitTransformOrigin=a.transformOrigin,a.mozTransformOrigin=a.transformOrigin,a.WebkitTransformOrigin=a.transformOrigin,a.MozTransformOrigin=a.transformOrigin,a["text-shadow"]=a.textShadow,a["border-color"]=a.borderColor,a["border-radius"]=a.borderRadius,a["background-image"]=a.backgroundImage,a["background-position"]=a.backgroundPosition,a["background-position-x"]=a.backgroundPositionX,a["border-width"]=a.padding,a["box-shadow"]=a.textShadow,a["-webkit-box-shadow"]=a.textShadow,a["-moz-box-shadow"]=a.textShadow,a["border-style"]=a.borderColor,a["-webkit-transform"]=a.transform,a["-moz-transform"]=a.transform,a["transform-origin"]=a.transformOrigin,a["-webkit-transform-origin"]=a.transformOrigin,a["-moz-transform-origin"]=a.transformOrigin;var s=r([["paddingLeft","paddingRight"],["marginLeft","marginRight"],["left","right"],["borderLeft","borderRight"],["borderLeftColor","borderRightColor"],["borderLeftStyle","borderRightStyle"],["borderLeftWidth","borderRightWidth"],["borderTopLeftRadius","borderTopRightRadius"],["borderBottomLeftRadius","borderBottomRightRadius"],["padding-left","padding-right"],["margin-left","margin-right"],["border-left","border-right"],["border-left-color","border-right-color"],["border-left-style","border-right-style"],["border-left-width","border-right-width"],["border-top-left-radius","border-top-right-radius"],["border-bottom-left-radius","border-bottom-right-radius"]]),u=["content"],d=r([["ltr","rtl"],["left","right"],["w-resize","e-resize"],["sw-resize","se-resize"],["nw-resize","ne-resize"]]),g=new RegExp("(^|\\W|_)((ltr)|(rtl)|(left)|(right))(\\W|_|$)","g"),l=new RegExp("(left)|(right)");function f(r){return Object.keys(r).reduce(function(o,n){var i=r[n];if("string"==typeof i&&(i=i.trim()),t(u,n))return o[n]=i,o;var c=function(r,t){var o=/\/\*\s?@noflip\s?\*\//.test(t),n=o?r:(u=r,s[u]||u),i=o?t:function(r,t){if(o=t,null==o||function(r){return"boolean"==typeof r}(t))return t;var o;if(function(r){return r&&"object"==typeof r}(t))return f(t);var n,i=e(t),s=i?t:t.replace(/ !important.*?$/,""),u=!i&&s.length!==t.length,c=a[r];n=c?c({value:s,valuesToConvert:d,isRtl:!0,bgImgDirectionRegex:g,bgPosDirectionRegex:l}):d[s]||s;if(u)return n+" !important";return n}(n,t);var u;return{key:n,value:i}}(n,i),b=c.key,m=c.value;return o[b]=m,o},Array.isArray(r)?[]:{})}return f}); | ||
//# sourceMappingURL=rtl-css-js.umd.min.js.map |
{ | ||
"name": "rtl-css-js", | ||
"version": "1.10.0", | ||
"version": "1.11.0", | ||
"description": "Right To Left conversion for CSS in JS objects", | ||
@@ -40,3 +40,3 @@ "main": "dist/cjs/index.js", | ||
"devDependencies": { | ||
"kcd-scripts": "^0.38.0", | ||
"kcd-scripts": "^0.45.0", | ||
"npm-run-all": "^4.1.1", | ||
@@ -61,3 +61,6 @@ "rimraf": "^2.6.2", | ||
}, | ||
"homepage": "https://github.com/kentcdodds/rtl-css-js#readme" | ||
"homepage": "https://github.com/kentcdodds/rtl-css-js#readme", | ||
"dependencies": { | ||
"@babel/runtime": "^7.1.2" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
196479
0
1
1662
+ Added@babel/runtime@^7.1.2
+ Added@babel/runtime@7.26.0(transitive)
+ Addedregenerator-runtime@0.14.1(transitive)