Skip to content

Commit 6886705

Browse files
committed
update code style
1 parent 07923f8 commit 6886705

File tree

7 files changed

+54
-49
lines changed

7 files changed

+54
-49
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module.exports = {
2525
"rules": {
2626
"no-console": 0,
2727
"no-shadow": 2,
28+
"no-var": 2,
2829
// style
2930
"semi": [2, 'always'],
3031
"comma-dangle": [2, "always-multiline"],
Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,56 @@
1-
var React = require('react');
2-
var ReactNative = require('react-native');
3-
var {
1+
import React from 'react';
2+
import {
43
Image,
54
Dimensions,
6-
} = ReactNative;
5+
} from 'react-native';
76

8-
var {width} = Dimensions.get('window');
7+
const {width} = Dimensions.get('window');
98

10-
var baseStyle = {
9+
const baseStyle = {
1110
backgroundColor: 'transparent',
1211
};
13-
var ResizableImage = React.createClass({
14-
getInitialState: function() {
15-
return {
12+
13+
export default class AutoSizedImage extends React.Component {
14+
constructor(props) {
15+
super(props);
16+
this.state = {
1617
// set width 1 is for preventing the warning
1718
// You must specify a width and height for the image %s
1819
width: this.props.style.width || 1,
1920
height: this.props.style.height || 1,
2021
};
21-
},
22-
componentDidMount: function() {
22+
}
23+
24+
componentDidMount() {
2325
//avoid repaint if width/height is given
2426
if (this.props.style.width || this.props.style.height) {
2527
return;
2628
}
2729
Image.getSize(this.props.source.uri, (w, h) => {
2830
this.setState({width: w, height: h});
2931
});
30-
},
31-
render: function() {
32-
var finalSize = {};
32+
}
33+
34+
render() {
35+
const finalSize = {};
3336
if (this.state.width > width) {
3437
finalSize.width = width;
35-
var ratio = width / this.state.width;
38+
const ratio = width / this.state.width;
3639
finalSize.height = this.state.height * ratio;
3740
}
38-
var style = Object.assign(
41+
const style = Object.assign(
3942
baseStyle,
4043
this.props.style,
4144
this.state,
4245
finalSize
4346
);
44-
var source = {};
47+
let source = {};
4548
if (!finalSize.width || !finalSize.height) {
4649
source = Object.assign(source, this.props.source, this.state);
4750
} else {
4851
source = Object.assign(source, this.props.source, finalSize);
4952
}
5053

5154
return <Image style={style} source={source} />;
52-
},
53-
});
54-
55-
module.exports = ResizableImage;
55+
}
56+
}

__tests__/HTMLView-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ describe('<HTMLView/>', () => {
7777
}
7878

7979
expect(
80-
renderer.create(
81-
<HTMLView value={htmlContent} renderNode={renderNode} />
82-
).toJSON()
80+
renderer
81+
.create(<HTMLView value={htmlContent} renderNode={renderNode} />)
82+
.toJSON()
8383
).toMatchSnapshot();
8484
});
8585
});

example/Example.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import React from 'react';
2-
import { StyleSheet, View, Text, ScrollView } from 'react-native';
3-
import HTMLView from 'react-native-htmlview';
2+
import {StyleSheet, View, Text, ScrollView} from 'react-native';
3+
import HTMLView from '../';
44

55
function renderNode(node, index) {
66
if (node.name == 'iframe') {
7-
return <View key={index} style={{width: 200, height: 200}}><Text>{node.attribs.src}</Text></View>;
7+
return (
8+
<View key={index} style={{width: 200, height: 200}}>
9+
<Text>{node.attribs.src}</Text>
10+
</View>
11+
);
812
}
913
}
1014

@@ -35,7 +39,6 @@ const htmlContent = `
3539
</div>
3640
`;
3741

38-
3942
export default class App extends React.Component {
4043
render() {
4144
return (

htmlToElement.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
var React = require('react');
2-
var ReactNative = require('react-native');
3-
var htmlparser = require('htmlparser2-without-node-native');
4-
var entities = require('entities');
5-
6-
var {
1+
import React from 'react';
2+
import {
73
Text,
8-
} = ReactNative;
4+
} from 'react-native';
5+
import htmlparser from 'htmlparser2-without-node-native';
6+
import entities from 'entities';
97

10-
var Img = require('./Img');
8+
import AutoSizedImage from './AutoSizedImage';
119

12-
var LINE_BREAK = '\n';
13-
var BULLET = '\u2022 ';
10+
const LINE_BREAK = '\n';
11+
const BULLET = '\u2022 ';
1412

15-
const ImgTag = props => {
13+
const Img = props => {
1614
const width = Number(props.attribs['width']) || Number(props.attribs['data-width']) || 0;
1715
const height = Number(props.attribs['height']) || Number(props.attribs['data-height']) || 0;
1816

@@ -26,17 +24,17 @@ const ImgTag = props => {
2624
height,
2725
};
2826
return (
29-
<Img source={source} style={imgStyle} />
27+
<AutoSizedImage source={source} style={imgStyle} />
3028
);
3129
};
3230

33-
function htmlToElement(rawHtml, opts, done) {
31+
export default function htmlToElement(rawHtml, opts, done) {
3432
function domToElement(dom, parent) {
3533
if (!dom) return null;
3634

3735
return dom.map((node, index, list) => {
3836
if (opts.customRenderer) {
39-
var rendered = opts.customRenderer(node, index, list, parent);
37+
const rendered = opts.customRenderer(node, index, list, parent);
4038
if (rendered || rendered === null) return rendered;
4139
}
4240

@@ -51,7 +49,7 @@ function htmlToElement(rawHtml, opts, done) {
5149
if (node.type == 'tag') {
5250
if (node.name == 'img') {
5351
return (
54-
<ImgTag key={index} attribs={node.attribs} />
52+
<Img key={index} attribs={node.attribs} />
5553
);
5654
}
5755

@@ -74,13 +72,12 @@ function htmlToElement(rawHtml, opts, done) {
7472
});
7573
}
7674

77-
var handler = new htmlparser.DomHandler(function(err, dom) {
75+
const handler = new htmlparser.DomHandler(function(err, dom) {
7876
if (err) done(err);
7977
done(null, domToElement(dom));
8078
});
81-
var parser = new htmlparser.Parser(handler);
79+
const parser = new htmlparser.Parser(handler);
8280
parser.write(rawHtml);
8381
parser.done();
8482
}
8583

86-
module.exports = htmlToElement;

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
module.exports = require('./HTMLView');
1+
import HTMLView from './HTMLView';
2+
3+
export default HTMLView;

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
"main": "index.js",
66
"scripts": {
77
"lint": "eslint .",
8-
"test": "yarn run lint-format && yarn run lint && yarn run jest",
9-
"lint-format": "./format.sh --list-different; if [ $? != 0 ]; then echo \"CODE FORMATTING: please run 'yarn run format' and commit the changes\"; exit 1; fi",
8+
"lint-fix": "eslint . --fix",
9+
"test": "yarn run format-lint && yarn run lint && yarn run jest",
10+
"format-lint": "./format.sh --list-different; if [ $? != 0 ]; then echo \"CODE FORMATTING: please run 'yarn run format' and commit the changes\"; exit 1; fi",
1011
"format": "./format.sh --write && eslint . --fix",
1112
"jest": "jest"
1213
},

0 commit comments

Comments
 (0)