Skip to content

Commit 0eea307

Browse files
committed
refactor(HTMLUtils): restore backwards compatibility with RN < 0.58
// PropTypes have been deprecated since 0.58. This is why these array are now hardcoded. This lets us preserve // a style checking at runtime while keeping the module compatible with all react-native versions.
1 parent a79ce0d commit 0eea307

File tree

4 files changed

+37
-128
lines changed

4 files changed

+37
-128
lines changed

src/HTML.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ export default class HTML extends PureComponent {
350350
let textChildrenInheritedStyles = {};
351351
Object.keys(wrapperStyles).forEach((styleKey) => {
352352
// Extract text-only styles
353-
if (TextOnlyPropTypes[styleKey]) {
353+
if (TextOnlyPropTypes.indexOf(styleKey) !== -1) {
354354
textChildrenInheritedStyles[styleKey] = wrapperStyles[styleKey];
355355
delete wrapperStyles[styleKey];
356356
}

src/HTMLStyles.js

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { PERC_SUPPORTED_STYLES, STYLESETS, ABSOLUTE_FONT_SIZE, stylePropTypes } from './HTMLUtils';
22
import { generateDefaultBlockStyles, generateDefaultTextStyles } from './HTMLDefaultStyles';
3-
import checkPropTypes from './checkPropTypes';
43

54
/**
65
* Converts a html style string to an object
@@ -174,48 +173,38 @@ function cssToRNStyle (css, styleset, { emSize, ptSize, ignoredStyles, allowedSt
174173
.split('-')
175174
.map((item, index) => index === 0 ? item : item[0].toUpperCase() + item.substr(1))
176175
.join(''),
177-
value];
176+
value
177+
];
178178
})
179179
.map(([key, value]) => {
180-
if (!styleProps[key]) {
180+
if (styleProps.indexOf(key) === -1) {
181181
return undefined;
182182
}
183183

184-
const testStyle = {};
185-
testStyle[key] = value;
186-
const styleProp = {};
187-
styleProp[key] = styleProps[key];
188-
if (checkPropTypes(styleProp, testStyle, key, 'react-native-render-html') == null) {
189-
if (typeof value === 'string') {
190-
if (value.search('inherit') !== -1) {
191-
return undefined;
192-
}
193-
value = value.replace('!important', '');
194-
// See if we can use the percentage directly
195-
if (value.search('%') !== -1 && PERC_SUPPORTED_STYLES.indexOf(key) !== -1) {
196-
return [key, value];
197-
}
198-
if (value.search('em') !== -1) {
199-
const pxSize = parseFloat(value.replace('em', '')) * emSize;
200-
return [key, pxSize];
201-
}
202-
if (value.search('pt') !== -1) {
203-
const pxSize = parseFloat(value.replace('pt', '')) * ptSize;
204-
return [key, pxSize];
205-
}
206-
// See if we can convert a 20px to a 20 automagically
207-
const numericValue = parseFloat(value.replace('px', ''));
208-
if (key !== 'fontWeight' && !isNaN(numericValue)) {
209-
testStyle[key] = numericValue;
210-
if (checkPropTypes(styleProp, testStyle, key, 'react-native-render-html') == null) {
211-
return [key, numericValue];
212-
}
213-
}
214-
if (key === 'fontSize') {
215-
return mapAbsoluteFontSize(key, value);
184+
if (typeof value === 'string') {
185+
value = value.replace('!important', '');
186+
// See if we can use the percentage directly
187+
if (value.search('%') !== -1 && PERC_SUPPORTED_STYLES.indexOf(key) !== -1) {
188+
return [key, value];
189+
}
190+
if (value.search('em') !== -1) {
191+
const pxSize = parseFloat(value.replace('em', '')) * emSize;
192+
return [key, pxSize];
193+
}
194+
if (value.search('pt') !== -1) {
195+
const pxSize = parseFloat(value.replace('pt', '')) * ptSize;
196+
return [key, pxSize];
197+
}
198+
// See if we can convert a 20px to a 20 automagically
199+
const numericValue = parseFloat(value.replace('px', ''));
200+
if (key !== 'fontWeight' && !isNaN(numericValue)) {
201+
if (styleProps.indexOf(key) !== -1) {
202+
return [key, numericValue];
216203
}
217204
}
218-
return [key, value];
205+
if (key === 'fontSize') {
206+
return mapAbsoluteFontSize(key, value);
207+
}
219208
}
220209
return [key, value];
221210
})

src/HTMLUtils.js

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import TextStylesPropTypes from 'react-native/Libraries/Text/TextStylePropTypes';
2-
import DeprecatedViewStylePropTypes from 'react-native/Libraries/DeprecatedPropTypes/DeprecatedViewStylePropTypes';
3-
import DeprecatedImageStylesPropTypes from 'react-native/Libraries/DeprecatedPropTypes/DeprecatedImageStylePropTypes';
1+
// PropTypes have been deprecated since 0.58. This is why these array are now hardcoded. This lets us preserve
2+
// a style checking at runtime while keeping the module compatible with all react-native versions.
3+
const ImageStylePropTypes = ['display', 'width', 'height', 'start', 'end', 'top', 'left', 'right', 'bottom', 'minWidth', 'maxWidth', 'minHeight', 'maxHeight', 'margin', 'marginVertical', 'marginHorizontal', 'marginTop', 'marginBottom', 'marginLeft', 'marginRight', 'marginStart', 'marginEnd', 'padding', 'paddingVertical', 'paddingHorizontal', 'paddingTop', 'paddingBottom', 'paddingLeft', 'paddingRight', 'paddingStart', 'paddingEnd', 'borderWidth', 'borderTopWidth', 'borderStartWidth', 'borderEndWidth', 'borderRightWidth', 'borderBottomWidth', 'borderLeftWidth', 'position', 'flexDirection', 'flexWrap', 'justifyContent', 'alignItems', 'alignSelf', 'alignContent', 'overflow', 'flex', 'flexGrow', 'flexShrink', 'flexBasis', 'aspectRatio', 'zIndex', 'direction', 'shadowColor', 'shadowOffset', 'shadowOpacity', 'shadowRadius', 'transform', 'transformMatrix', 'decomposedMatrix', 'scaleX', 'scaleY', 'rotation', 'translateX', 'translateY', 'resizeMode', 'backfaceVisibility', 'backgroundColor', 'borderColor', 'borderRadius', 'tintColor', 'opacity', 'overlayColor', 'borderTopLeftRadius', 'borderTopRightRadius', 'borderBottomLeftRadius', 'borderBottomRightRadius'];
4+
const ViewStylePropTypes = ['display', 'width', 'height', 'start', 'end', 'top', 'left', 'right', 'bottom', 'minWidth', 'maxWidth', 'minHeight', 'maxHeight', 'margin', 'marginVertical', 'marginHorizontal', 'marginTop', 'marginBottom', 'marginLeft', 'marginRight', 'marginStart', 'marginEnd', 'padding', 'paddingVertical', 'paddingHorizontal', 'paddingTop', 'paddingBottom', 'paddingLeft', 'paddingRight', 'paddingStart', 'paddingEnd', 'borderWidth', 'borderTopWidth', 'borderStartWidth', 'borderEndWidth', 'borderRightWidth', 'borderBottomWidth', 'borderLeftWidth', 'position', 'flexDirection', 'flexWrap', 'justifyContent', 'alignItems', 'alignSelf', 'alignContent', 'overflow', 'flex', 'flexGrow', 'flexShrink', 'flexBasis', 'aspectRatio', 'zIndex', 'direction', 'shadowColor', 'shadowOffset', 'shadowOpacity', 'shadowRadius', 'transform', 'transformMatrix', 'decomposedMatrix', 'scaleX', 'scaleY', 'rotation', 'translateX', 'translateY', 'backfaceVisibility', 'backgroundColor', 'borderColor', 'borderTopColor', 'borderRightColor', 'borderBottomColor', 'borderLeftColor', 'borderStartColor', 'borderEndColor', 'borderRadius', 'borderTopLeftRadius', 'borderTopRightRadius', 'borderTopStartRadius', 'borderTopEndRadius', 'borderBottomLeftRadius', 'borderBottomRightRadius', 'borderBottomStartRadius', 'borderBottomEndRadius', 'borderStyle', 'opacity', 'elevation'];
5+
const TextStylePropTypes = ['display', 'width', 'height', 'start', 'end', 'top', 'left', 'right', 'bottom', 'minWidth', 'maxWidth', 'minHeight', 'maxHeight', 'margin', 'marginVertical', 'marginHorizontal', 'marginTop', 'marginBottom', 'marginLeft', 'marginRight', 'marginStart', 'marginEnd', 'padding', 'paddingVertical', 'paddingHorizontal', 'paddingTop', 'paddingBottom', 'paddingLeft', 'paddingRight', 'paddingStart', 'paddingEnd', 'borderWidth', 'borderTopWidth', 'borderStartWidth', 'borderEndWidth', 'borderRightWidth', 'borderBottomWidth', 'borderLeftWidth', 'position', 'flexDirection', 'flexWrap', 'justifyContent', 'alignItems', 'alignSelf', 'alignContent', 'overflow', 'flex', 'flexGrow', 'flexShrink', 'flexBasis', 'aspectRatio', 'zIndex', 'direction', 'shadowColor', 'shadowOffset', 'shadowOpacity', 'shadowRadius', 'transform', 'transformMatrix', 'decomposedMatrix', 'scaleX', 'scaleY', 'rotation', 'translateX', 'translateY', 'backfaceVisibility', 'backgroundColor', 'borderColor', 'borderTopColor', 'borderRightColor', 'borderBottomColor', 'borderLeftColor', 'borderStartColor', 'borderEndColor', 'borderRadius', 'borderTopLeftRadius', 'borderTopRightRadius', 'borderTopStartRadius', 'borderTopEndRadius', 'borderBottomLeftRadius', 'borderBottomRightRadius', 'borderBottomStartRadius', 'borderBottomEndRadius', 'borderStyle', 'opacity', 'elevation', 'color', 'fontFamily', 'fontSize', 'fontStyle', 'fontWeight', 'fontVariant', 'textShadowOffset', 'textShadowRadius', 'textShadowColor', 'letterSpacing', 'lineHeight', 'textAlign', 'textAlignVertical', 'includeFontPadding', 'textDecorationLine', 'textDecorationStyle', 'textDecorationColor', 'textTransform', 'writingDirection']
46

57
// Filter prop-types that are only applicable to <Text> and not <View>
6-
export let TextOnlyPropTypes = {};
7-
Object.keys(TextStylesPropTypes).forEach((prop) => {
8-
if (!DeprecatedViewStylePropTypes[prop]) {
9-
TextOnlyPropTypes[prop] = TextStylesPropTypes[prop];
10-
}
11-
});
8+
export const TextOnlyPropTypes = TextStylePropTypes.filter((prop) => ViewStylePropTypes.indexOf(prop) === -1);
129

1310
// These tags should ALWAYS be mapped to View wrappers
1411
export const BLOCK_TAGS = ['address', 'article', 'aside', 'footer', 'hgroup', 'nav', 'section', 'blockquote', 'dd',
@@ -57,20 +54,12 @@ export const PERC_SUPPORTED_STYLES = [
5754
'padding', 'paddingBottom', 'paddingTop', 'paddingLeft', 'paddingRight', 'paddingHorizontal', 'paddingVertical'
5855
];
5956

60-
// We have to do some munging here as the objects are wrapped
61-
const RNTextStylePropTypes = Object.keys(TextStylesPropTypes)
62-
.reduce((acc, k) => { acc[k] = TextStylesPropTypes[k]; return acc; }, {});
63-
const RNViewStylePropTypes = Object.keys(DeprecatedViewStylePropTypes)
64-
.reduce((acc, k) => { acc[k] = DeprecatedViewStylePropTypes[k]; return acc; }, {});
65-
const RNImageStylePropTypes = Object.keys(DeprecatedImageStylesPropTypes)
66-
.reduce((acc, k) => { acc[k] = DeprecatedImageStylesPropTypes[k]; return acc; }, {});
67-
6857
export const STYLESETS = Object.freeze({ VIEW: 'view', TEXT: 'text', IMAGE: 'image' });
69-
70-
export const stylePropTypes = {};
71-
stylePropTypes[STYLESETS.VIEW] = Object.assign({}, RNViewStylePropTypes);
72-
stylePropTypes[STYLESETS.TEXT] = Object.assign({}, RNViewStylePropTypes, RNTextStylePropTypes);
73-
stylePropTypes[STYLESETS.IMAGE] = Object.assign({}, RNViewStylePropTypes, RNImageStylePropTypes);
58+
export const stylePropTypes = {
59+
[STYLESETS.VIEW]: ViewStylePropTypes,
60+
[STYLESETS.TEXT]: TextStylePropTypes,
61+
[STYLESETS.IMAGE]: ImageStylePropTypes
62+
};
7463

7564
/**
7665
* Returns an array with the tagname of every parent of a node.

src/checkPropTypes.js

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)