/** * Copyright (c) Nicolas Gallagher. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * */ import I18nManager from '../I18nManager'; import multiplyStyleLengthValue from '../../modules/multiplyStyleLengthValue'; var emptyObject = {}; var borderTopLeftRadius = 'borderTopLeftRadius'; var borderTopRightRadius = 'borderTopRightRadius'; var borderBottomLeftRadius = 'borderBottomLeftRadius'; var borderBottomRightRadius = 'borderBottomRightRadius'; var borderLeftColor = 'borderLeftColor'; var borderLeftStyle = 'borderLeftStyle'; var borderLeftWidth = 'borderLeftWidth'; var borderRightColor = 'borderRightColor'; var borderRightStyle = 'borderRightStyle'; var borderRightWidth = 'borderRightWidth'; var right = 'right'; var marginLeft = 'marginLeft'; var marginRight = 'marginRight'; var paddingLeft = 'paddingLeft'; var paddingRight = 'paddingRight'; var left = 'left'; // Map of LTR property names to their BiDi equivalent. var PROPERTIES_FLIP = { borderTopLeftRadius: borderTopRightRadius, borderTopRightRadius: borderTopLeftRadius, borderBottomLeftRadius: borderBottomRightRadius, borderBottomRightRadius: borderBottomLeftRadius, borderLeftColor: borderRightColor, borderLeftStyle: borderRightStyle, borderLeftWidth: borderRightWidth, borderRightColor: borderLeftColor, borderRightStyle: borderLeftStyle, borderRightWidth: borderLeftWidth, left: right, marginLeft: marginRight, marginRight: marginLeft, paddingLeft: paddingRight, paddingRight: paddingLeft, right: left }; // Map of I18N property names to their LTR equivalent. var PROPERTIES_I18N = { borderTopStartRadius: borderTopLeftRadius, borderTopEndRadius: borderTopRightRadius, borderBottomStartRadius: borderBottomLeftRadius, borderBottomEndRadius: borderBottomRightRadius, borderStartColor: borderLeftColor, borderStartStyle: borderLeftStyle, borderStartWidth: borderLeftWidth, borderEndColor: borderRightColor, borderEndStyle: borderRightStyle, borderEndWidth: borderRightWidth, end: right, marginStart: marginLeft, marginEnd: marginRight, paddingStart: paddingLeft, paddingEnd: paddingRight, start: left }; var PROPERTIES_VALUE = { clear: true, float: true, textAlign: true }; // Invert the sign of a numeric-like value var additiveInverse = function additiveInverse(value) { return multiplyStyleLengthValue(value, -1); }; var i18nStyle = function i18nStyle(originalStyle) { var doLeftAndRightSwapInRTL = I18nManager.doLeftAndRightSwapInRTL, isRTL = I18nManager.isRTL; var style = originalStyle || emptyObject; var frozenProps = {}; var nextStyle = {}; for (var originalProp in style) { if (!Object.prototype.hasOwnProperty.call(style, originalProp)) { continue; } var originalValue = style[originalProp]; var prop = originalProp; var value = originalValue; // BiDi flip properties if (PROPERTIES_I18N.hasOwnProperty(originalProp)) { // convert start/end var convertedProp = PROPERTIES_I18N[originalProp]; prop = isRTL ? PROPERTIES_FLIP[convertedProp] : convertedProp; } else if (isRTL && doLeftAndRightSwapInRTL && PROPERTIES_FLIP[originalProp]) { prop = PROPERTIES_FLIP[originalProp]; } // BiDi flip values if (PROPERTIES_VALUE.hasOwnProperty(originalProp)) { if (originalValue === 'start') { value = isRTL ? 'right' : 'left'; } else if (originalValue === 'end') { value = isRTL ? 'left' : 'right'; } else if (isRTL && doLeftAndRightSwapInRTL) { if (originalValue === 'left') { value = 'right'; } else if (originalValue === 'right') { value = 'left'; } } } // BiDi flip transitionProperty value if (prop === 'transitionProperty') { // BiDi flip properties if (PROPERTIES_I18N.hasOwnProperty(value)) { // convert start/end var convertedValue = PROPERTIES_I18N[originalValue]; value = isRTL ? PROPERTIES_FLIP[convertedValue] : convertedValue; } else if (isRTL && doLeftAndRightSwapInRTL && PROPERTIES_FLIP[originalValue]) { value = PROPERTIES_FLIP[originalValue]; } } // Create finalized style if (isRTL && prop === 'textShadowOffset') { nextStyle[prop] = value; nextStyle[prop].width = additiveInverse(value.width); } else if (!frozenProps[prop]) { nextStyle[prop] = value; } if (PROPERTIES_I18N[originalProp]) { frozenProps[prop] = true; } } return nextStyle; }; export default i18nStyle;