forked from jsdf/react-native-htmlview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTMLView.js
132 lines (115 loc) · 3.19 KB
/
HTMLView.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
var htmlparser = require('./vendor/htmlparser2')
var entities = require('./vendor/entities')
var React = require('react-native')
var {
LinkingIOS,
StyleSheet,
Text,
} = React
var LINE_BREAK = '\n'
var PARAGRAPH_BREAK = '\n\n'
var BULLET = ' \u2022 '
function htmlToElement(rawHtml, opts, done) {
function domToElement(dom, parent) {
if (!dom) return null
return dom.map((node, index, list) => {
if (opts.customRenderer) {
var rendered = opts.customRenderer(node, index, list)
if (rendered || rendered === null) return rendered
}
if (node.type == 'text') {
return (
<Text key={index} style={parent ? inheritedStyle(parent) : null}>
{entities.decodeHTML(node.data)}
</Text>
)
}
if (node.type == 'tag') {
var linkPressHandler = null
if (node.name == 'a' && node.attribs && node.attribs.href) {
linkPressHandler = () => opts.linkHandler(entities.decodeHTML(node.attribs.href))
}
return (
<Text key={index} onPress={linkPressHandler}>
{node.name == 'pre' ? LINE_BREAK : null}
{node.name == 'li' ? BULLET : null}
{domToElement(node.children, node)}
{node.name == 'br' ? LINE_BREAK : null}
{node.name == 'li' ? LINE_BREAK : null}
{node.name == 'p' && index < list.length-1 ? PARAGRAPH_BREAK : null}
</Text>
)
}
})
}
function inheritedStyle(parent) {
var style = [opts.styles[parent.name] || {}];
return parent.parent ? style.concat(inheritedStyle(parent.parent)) : style;
}
var handler = new htmlparser.DomHandler(function (err, dom) {
if (err) done(err)
done(null, domToElement(dom))
})
var parser = new htmlparser.Parser(handler)
parser.write(rawHtml)
parser.done()
}
var HTMLView = React.createClass({
mixins: [
React.addons.PureRenderMixin,
],
getDefaultProps() {
return {
onLinkPress: LinkingIOS.openURL,
}
},
getInitialState() {
return {
element: null,
}
},
componentWillReceiveProps() {
if (this.state.element) return
this.startHtmlRender()
},
componentDidMount() {
this.startHtmlRender()
},
startHtmlRender() {
if (!this.props.value) return
if (this.renderingHtml) return
var opts = {
linkHandler: this.props.onLinkPress,
styles: Object.assign({}, baseStyles, this.props.stylesheet),
customRenderer: this.props.renderNode,
}
this.renderingHtml = true
htmlToElement(this.props.value, opts, (err, element) => {
this.renderingHtml = false
if (err) return (this.props.onError || console.error)(err)
if (this.isMounted()) this.setState({element})
})
},
render() {
if (this.state.element) {
return <Text children={this.state.element} />
}
return <Text />
}
})
var boldStyle = {fontWeight: '500'}
var italicStyle = {fontStyle: 'italic'}
var codeStyle = {fontFamily: 'Menlo'}
var baseStyles = StyleSheet.create({
b: boldStyle,
strong: boldStyle,
i: italicStyle,
em: italicStyle,
pre: codeStyle,
code: codeStyle,
a: {
fontWeight: '500',
color: '#007AFF',
},
})
module.exports = HTMLView