forked from flaviohenriquecbc/react-native-htmlview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtmlToElement.js
231 lines (207 loc) · 6.82 KB
/
htmlToElement.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import React from 'react';
import {StyleSheet, Text, Dimensions, View} from 'react-native';
import htmlparser from 'htmlparser2-without-node-native';
import entities from 'entities';
import AutoSizedImage from './AutoSizedImage';
import Iframe from 'react-native-htmlview/component/Iframe';
const {width: _width} = Dimensions.get('window');
const defaultOpts = {
lineBreak: '\n',
paragraphBreak: '\n',
bullet: '\u2022 ',
TextComponent: Text,
textComponentProps: null,
NodeComponent: Text,
nodeComponentProps: null,
};
const Img = (props) => {
const imgStyle = {
maxWidth: _width,
resizeMode: 'cover',
};
const source = {
uri: props.attribs.src,
width: _width,
};
// if no src is passed, add generic image
if (!props.attribs.src) {
imgStyle.width = 16;
imgStyle.height = 16;
source.uri =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAWRJREFUOI21k7FLAnEUx9/vFF08rzjw8oImTZoi6A9odwpcIhyCxubW3Kr1FCIcRM4llRKCBtdybLbi5+Lw87w47DjJBvU1xC/0upIb+m7v8b6f33vvxyOINKvrjVPbdhTwIUmKmNns7jEplc4NxxnG4nHlxQ+g1zOTkiT2g44zjKmq8pzJHG34AdTrhTZj/ZRAiDBBBOLHDACACIQQYSL4Nbr1fwBE6vkriFRCpN8jewIQ6V61eveASPdd+XSr9XgNAEAIoCcAke4Ui1cXhvGa0LRyBZHmEKmKSHP5fPm23e5sEZJAXh90mTd1vVEZjT4kntO08oksLx9Y1mANACAcDr3/2AFvp9m8vxwM7FV3V9zspbkRul2W+q2Qazweh2bjuRFUVelY1tvKXwBZXjLmAIjTAA/S6cPtRR3MCnEaCIpixGTMXK/VCk98F4uNQBgzk9Go2Cdf53xzZtvDmJ/X+Tl/ArQ4nue4vQorAAAAAElFTkSuQmCC';
}
return <AutoSizedImage source={source} style={imgStyle} />;
};
export default function htmlToElement(rawHtml, customOpts = {}, done) {
const opts = {
...defaultOpts,
...customOpts,
};
function inheritedStyle(parent) {
if (!parent) return null;
const style = StyleSheet.flatten(opts.styles[parent.name]) || {};
const parentStyle = inheritedStyle(parent.parent) || {};
return {...parentStyle, ...style};
}
function domToElement(dom, parent) {
if (!dom) return null;
const renderNode = opts.customRenderer;
let orderedListCounter = 1;
return dom.map((node, index, list) => {
if (renderNode) {
const rendered = renderNode(node, index, list, parent, domToElement);
if (rendered || rendered === null) return rendered;
}
const {TextComponent} = opts;
if (node.type === 'text') {
const defaultStyle = opts.textComponentProps
? opts.textComponentProps.style
: null;
const customStyle = inheritedStyle(parent);
const text =
parent && parent.name === 'figcaption'
? node.data && node.data.toUpperCase()
: node.data;
const liStyle =
parent && parent.name === 'li'
? inheritedStyle({name: 'itemText'})
: {};
return (
<TextComponent
{...opts.textComponentProps}
key={index}
style={[defaultStyle, customStyle, liStyle]}
>
{entities.decodeHTML(text)}
</TextComponent>
);
}
if (node.type === 'tag') {
if (node.name === 'img') {
return <Img key={index} attribs={node.attribs} />;
} else if (node.name === 'iframe') {
return (
<Iframe
key={index}
attribs={node.attribs}
opts={opts}
index={index}
/>
);
}
let linkPressHandler = null;
let linkLongPressHandler = null;
if (node.name === 'a' && node.attribs && node.attribs.href) {
let link = entities.decodeHTML(node.attribs.href);
// remove not expected codes
link = link && link.replace(/^'|"|”/, '').replace(/$'|"|”/, '');
linkPressHandler = () => opts.linkHandler(link);
if (opts.linkLongPressHandler) {
linkLongPressHandler = () => opts.linkLongPressHandler(link);
}
}
let linebreakBefore = null;
let linebreakAfter = null;
if (opts.addLineBreaks) {
switch (node.name) {
case 'pre':
case 'footer':
linebreakBefore = opts.lineBreak;
break;
case 'p':
if (index < list.length - 1) {
linebreakAfter = opts.paragraphBreak;
}
break;
case 'br':
case 'h1':
case 'h2':
case 'h3':
case 'h4':
case 'h5':
case 'h6':
case 'div':
case 'figure':
linebreakAfter = opts.lineBreak;
break;
}
}
const {NodeComponent, styles} = opts;
let children = [
linebreakBefore,
domToElement(node.children, node),
linebreakAfter,
];
let hasView = false;
if (
node.name === 'ul' ||
node.name === 'ol' ||
node.name === 'figure' ||
node.name === 'blockquote' ||
node.name === 'div'
) {
hasView = true;
children = domToElement(node.children, node);
} else if (node.name === 'li') {
hasView = true;
children = [
<TextComponent
key={`${index}-1`}
style={inheritedStyle({
name: parent.name === 'ol' ? 'listNumber' : 'listBullet',
})}
>
{parent.name === 'ol' ? `${orderedListCounter++}.` : opts.bullet}
</TextComponent>,
<TextComponent
key={`${index}-2`}
style={inheritedStyle({name: 'itemText'})}
>
{domToElement([node.children[0]], node)}
</TextComponent>,
];
if (node.children.length > 1) {
return [
renderChildrenView(node, styles, index, children),
domToElement([node.children[1]], node),
];
}
if (opts.addLineBreaks && index < list.length - 1) {
linebreakAfter = opts.lineBreak;
}
}
if (hasView) {
return renderChildrenView(node, styles, index, children);
}
return (
<NodeComponent
{...opts.nodeComponentProps}
key={index}
onPress={linkPressHandler}
style={[!node.parent ? styles[node.name] : null]}
onLongPress={linkLongPressHandler}
>
{children}
</NodeComponent>
);
}
});
}
function renderChildrenView(node, styles, index, children) {
return (
<View
style={!node.parent || node.name === 'li' ? styles[node.name] : null}
key={index}
>
{children}
</View>
);
}
const handler = new htmlparser.DomHandler(function(err, dom) {
if (err) done(err);
done(null, domToElement(dom));
});
const parser = new htmlparser.Parser(handler);
parser.write(rawHtml);
parser.done();
}