Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Made WebView an optional dependency which can be passed as a prop to <HTML> to support iframes #301

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Demo/src/HTMLDemo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { Component } from 'react';
import { View, Text, ScrollView, TouchableOpacity, Dimensions, Linking } from 'react-native';
import { View, Text, ScrollView, TouchableOpacity, Dimensions, Linking, WebView } from 'react-native';
import HTML from 'react-native-render-html';
import EXAMPLES, * as snippets from './snippets';
import styles from './styles';
Expand All @@ -10,6 +10,7 @@ const CUSTOM_RENDERERS = {};
const DEFAULT_PROPS = {
htmlStyles: CUSTOM_STYLES,
renderers: CUSTOM_RENDERERS,
WebView: WebView,
imagesMaxWidth: IMAGES_MAX_WIDTH,
onLinkPress: (evt, href) => { Linking.openURL(href); },
debug: true
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ An iOS/Android pure javascript react-native component that renders your HTML int
- [Lists prefixes](#lists-prefixes)
- [Styling](#styling)
- [Images](#images)
- [IFrames](#iframes)
- [Altering content](#altering-content)
- [alterData](#alterdata)
- [alterChildren](#alterchildren)
Expand Down Expand Up @@ -70,6 +71,7 @@ Prop | Description | Type | Required/Default
------ | ------ | ------ | ------
`renderers` | Your [custom renderers](#creating-custom-renderers) | `object` | Optional, some default ones are supplied (`<a>`, `<img>`...)
`renderersProps` | Set of props accessible into your [custom renderers](#creating-custom-renderers) in `passProps` (4th argument) | `object` | Optional
`WebView`| Sets the WebView component to use when rendering `<iframe>` elements | `Component` | Optional
`html` | HTML string to parse and render | `string` | Required
`uri` | *(experimental)* remote website to parse and render | `string` | Optional
`decodeEntities` | Decode HTML entities of your content | `bool` | Optional, defaults to `true`
Expand Down Expand Up @@ -211,6 +213,27 @@ Images with broken links will render an empty square with a thin border, similar

Please note that all of these behaviors are implemented in the default `<img>` renderer. If you want to provide your own `<img>` renderer, you'll have to make this happen by yourself. You can use the `img` function in `HTMLRenderers.js` as a starting point.

## IFrames

In order to render `<iframe>` elements, a WebView component needs to be imported and passed to the `<HTML>` element.

```javascript
import React from 'react';
import HTML from 'react-native-render-html';
import { WebView } from 'react-native-webview';

const htmlContent = `
<p>Yes you read that right, those damn iframes can render with this plugin.</p>
<p>Check this out</p>
<iframe width="560" height="315" src="https://www.youtube.com/embed/ZZ5LpwO-An4" frameborder="0" allowfullscreen></iframe>
<p style="text-align:center;"><em>We've just rendered a meme</em></p>
`;

export default IFrameDemo = () => (
<HTML html={htmlContent} WebView={WebView}/>
);
```

## Altering content

`alterData` and `alterChildren` props are very useful to make some modifications on the structure of your HTML before it's actually rendered with react components.
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"buffer": "^4.5.1",
"events": "^1.1.0",
"html-entities": "^1.2.0",
"htmlparser2": "^3.10.1",
"react-native-webview": "^5.6.0"
"htmlparser2": "^3.10.1"
},
"peerDependencies": {
"prop-types": ">=15.5.10",
Expand Down
3 changes: 2 additions & 1 deletion src/HTML.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export default class HTML extends PureComponent {
baseFontStyle: PropTypes.object.isRequired,
textSelectable: PropTypes.bool,
renderersProps: PropTypes.object,
allowFontScaling: PropTypes.bool
allowFontScaling: PropTypes.bool,
WebView: PropTypes.elementType
}

static defaultProps = {
Expand Down
8 changes: 6 additions & 2 deletions src/HTMLRenderers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import { TouchableOpacity, Text, View, Platform } from 'react-native';
import { WebView } from 'react-native-webview';
import { _constructStyles, _getElementClassStyles } from './HTMLStyles';
import HTMLImage from './HTMLImage';

Expand Down Expand Up @@ -114,7 +113,7 @@ export function ul (htmlAttribs, children, convertedCSSStyles, passProps = {}) {
export const ol = ul;

export function iframe (htmlAttribs, children, convertedCSSStyles, passProps) {
const { staticContentMaxWidth, tagsStyles, classesStyles } = passProps;
const { staticContentMaxWidth, tagsStyles, classesStyles, WebView } = passProps;

const tagStyleHeight = tagsStyles.iframe && tagsStyles.iframe.height;
const tagStyleWidth = tagsStyles.iframe && tagsStyles.iframe.width;
Expand All @@ -139,6 +138,11 @@ export function iframe (htmlAttribs, children, convertedCSSStyles, passProps) {

const source = htmlAttribs.srcdoc ? { html: htmlAttribs.srcdoc } : { uri: htmlAttribs.src };

if (!WebView) {
console.warn('react-native-render-html', 'Unable to render <iframe>, please specify the `WebView` prop in <HTML>')
return null;
}

return (
<WebView key={passProps.key} source={source} style={style} />
);
Expand Down