1
1
import React , { PureComponent } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
import { View , Text , ViewPropTypes , ActivityIndicator , Dimensions } from 'react-native' ;
4
- import { BLOCK_TAGS , TEXT_TAGS , MIXED_TAGS , IGNORED_TAGS , TEXT_TAGS_IGNORING_ASSOCIATION , STYLESETS , TextOnlyPropTypes } from './HTMLUtils' ;
5
- import { cssStringToRNStyle , _getElementClassStyles , cssStringToObject , cssObjectToString } from './HTMLStyles' ;
4
+ import { cssStringToRNStyle , _getElementClassStyles , cssStringToObject , cssObjectToString , computeTextStyles } from './HTMLStyles' ;
5
+ import {
6
+ BLOCK_TAGS ,
7
+ TEXT_TAGS ,
8
+ MIXED_TAGS ,
9
+ IGNORED_TAGS ,
10
+ TEXT_TAGS_IGNORING_ASSOCIATION ,
11
+ STYLESETS ,
12
+ TextOnlyPropTypes
13
+ } from './HTMLUtils' ;
6
14
import { generateDefaultBlockStyles , generateDefaultTextStyles } from './HTMLDefaultStyles' ;
7
15
import htmlparser2 from 'htmlparser2' ;
8
16
import * as HTMLRenderers from './HTMLRenderers' ;
@@ -37,7 +45,8 @@ export default class HTML extends PureComponent {
37
45
emSize : PropTypes . number . isRequired ,
38
46
ptSize : PropTypes . number . isRequired ,
39
47
baseFontStyle : PropTypes . object . isRequired ,
40
- textSelectable : PropTypes . bool
48
+ textSelectable : PropTypes . bool ,
49
+ renderersProps : PropTypes . object
41
50
}
42
51
43
52
static defaultProps = {
@@ -149,36 +158,6 @@ export default class HTML extends PureComponent {
149
158
this . defaultTextStyles = generateDefaultTextStyles ( baseFontStyle . fontSize || 14 ) ;
150
159
}
151
160
152
- filterBaseFontStyles ( element , classStyles , props = this . props ) {
153
- const { tagsStyles, baseFontStyle } = props ;
154
- const { tagName, parentTag, parent, attribs } = element ;
155
- const styles = Object . keys ( baseFontStyle ) ;
156
- let appliedStyles = { } ;
157
-
158
- for ( let i = 0 ; i < styles . length ; i ++ ) {
159
- const styleAttribute = styles [ i ] ;
160
- const tagToCheck = tagName === 'rawtext' ? parentTag : tagName ;
161
- const styleAttributeWithCSSDashes = styleAttribute . replace ( / [ A - Z ] / , ( match ) => { return `-${ match . toLowerCase ( ) } ` ; } ) ;
162
- const overridenFromStyle = attribs && attribs . style && attribs . style . search ( styleAttributeWithCSSDashes ) !== - 1 ;
163
- const overridenFromParentStyle = parent && parent . attribs && parent . attribs . style && parent . attribs . style . search ( styleAttributeWithCSSDashes ) !== - 1 ;
164
-
165
- const overridenFromTagStyle = tagToCheck && tagsStyles [ tagToCheck ] && tagsStyles [ tagToCheck ] [ styleAttribute ] ;
166
- const overridenFromParentTagStyle = parentTag && tagsStyles [ parentTag ] && tagsStyles [ parentTag ] [ styleAttribute ] ;
167
-
168
- const overridenFromClassStyles = classStyles && classStyles [ styleAttribute ] ;
169
- const overridenFromDefaultStyles = this . defaultTextStyles [ tagToCheck ] && this . defaultTextStyles [ tagToCheck ] [ styleAttribute ] ;
170
-
171
- const notOverriden = ! overridenFromStyle && ! overridenFromParentStyle &&
172
- ! overridenFromTagStyle && ! overridenFromParentTagStyle &&
173
- ! overridenFromClassStyles && ! overridenFromDefaultStyles ;
174
-
175
- if ( notOverriden ) {
176
- appliedStyles [ styleAttribute ] = baseFontStyle [ styleAttribute ] ;
177
- }
178
- }
179
- return appliedStyles ;
180
- }
181
-
182
161
/**
183
162
* Loop on children and return whether if their parent needs to be a <View>
184
163
* @param {any } children
@@ -216,7 +195,11 @@ export default class HTML extends PureComponent {
216
195
associateRawTexts ( children ) {
217
196
for ( let i = 0 ; i < children . length ; i ++ ) {
218
197
const child = children [ i ] ;
219
- if ( ( child . wrapper === 'Text' && TEXT_TAGS_IGNORING_ASSOCIATION . indexOf ( child . tagName ) === - 1 ) && children . length > 1 && ( ! child . parent || child . parent . name !== 'p' ) ) {
198
+ if (
199
+ ( child . wrapper === 'Text' && TEXT_TAGS_IGNORING_ASSOCIATION . indexOf ( child . tagName ) === - 1 ) &&
200
+ children . length > 1 &&
201
+ ( ! child . parent || TEXT_TAGS_IGNORING_ASSOCIATION . indexOf ( child . parent . name ) === - 1 )
202
+ ) {
220
203
// Texts outside <p> or not <p> themselves (with siblings)
221
204
let wrappedTexts = [ ] ;
222
205
for ( let j = i ; j < children . length ; j ++ ) {
@@ -238,7 +221,7 @@ export default class HTML extends PureComponent {
238
221
nodeIndex : i ,
239
222
parent : child . parent ,
240
223
parentTag : child . parentTag ,
241
- tagName : child . parent && child . parent . name === 'li' ? ' textwrapper' : 'p ',
224
+ tagName : ' textwrapper',
242
225
wrapper : 'Text'
243
226
} ;
244
227
}
@@ -399,7 +382,7 @@ export default class HTML extends PureComponent {
399
382
* @memberof HTML
400
383
*/
401
384
renderRNElements ( RNElements , parentWrapper = 'root' , parentIndex = 0 , props = this . props ) {
402
- const { tagsStyles, classesStyles, emSize, ptSize, ignoredStyles, allowedStyles } = props ;
385
+ const { tagsStyles, classesStyles, emSize, ptSize, ignoredStyles, allowedStyles, baseFontStyle } = props ;
403
386
return RNElements && RNElements . length ? RNElements . map ( ( element , index ) => {
404
387
const { attribs, data, tagName, parentTag, children, nodeIndex, wrapper } = element ;
405
388
const Wrapper = wrapper === 'Text' ? Text : View ;
@@ -445,9 +428,23 @@ export default class HTML extends PureComponent {
445
428
}
446
429
447
430
const classStyles = _getElementClassStyles ( attribs , classesStyles ) ;
448
- const textElementStyles = this . filterBaseFontStyles ( element , classStyles , props ) ;
449
431
const textElement = data ?
450
- < Text style = { textElementStyles } > { data } </ Text > :
432
+ < Text
433
+ style = { computeTextStyles (
434
+ element ,
435
+ {
436
+ defaultTextStyles : this . defaultTextStyles ,
437
+ tagsStyles,
438
+ classesStyles,
439
+ baseFontStyle,
440
+ emSize,
441
+ ptSize,
442
+ ignoredStyles,
443
+ allowedStyles
444
+ } ) }
445
+ >
446
+ { data }
447
+ </ Text > :
451
448
false ;
452
449
453
450
const style = [
@@ -458,10 +455,12 @@ export default class HTML extends PureComponent {
458
455
]
459
456
. filter ( ( s ) => s !== undefined ) ;
460
457
461
- const extraProps = { } ;
462
- if ( Wrapper === Text ) extraProps . selectable = this . props . textSelectable ;
458
+ const renderersProps = { } ;
459
+ if ( Wrapper === Text ) {
460
+ renderersProps . selectable = this . props . textSelectable ;
461
+ }
463
462
return (
464
- < Wrapper key = { key } style = { style } { ...extraProps } >
463
+ < Wrapper key = { key } style = { style } { ...renderersProps } >
465
464
{ textElement }
466
465
{ childElements }
467
466
</ Wrapper >
0 commit comments