diff --git a/Demo/package.json b/Demo/package.json index aa9e0b8ff..eabc35f68 100644 --- a/Demo/package.json +++ b/Demo/package.json @@ -9,7 +9,7 @@ "dependencies": { "react": "16.0.0-alpha.12", "react-native": "0.48.3", - "react-native-render-html": "3.4.0" + "react-native-render-html": "3.5.0" }, "devDependencies": { "babel-jest": "21.0.2", diff --git a/Demo/src/HTMLDemo.js b/Demo/src/HTMLDemo.js index c767e2739..b3f04620f 100644 --- a/Demo/src/HTMLDemo.js +++ b/Demo/src/HTMLDemo.js @@ -11,7 +11,8 @@ const DEFAULT_PROPS = { htmlStyles: CUSTOM_STYLES, renderers: CUSTOM_RENDERERS, imagesMaxWidth: IMAGES_MAX_WIDTH, - onLinkPress: (evt, href) => { Linking.openURL(href); } + onLinkPress: (evt, href) => { Linking.openURL(href); }, + debug: true }; export default class Demo extends Component { diff --git a/Demo/src/snippets.js b/Demo/src/snippets.js index 8b7e8dd03..8b1ff1b03 100644 --- a/Demo/src/snippets.js +++ b/Demo/src/snippets.js @@ -3,14 +3,14 @@ import { View } from 'react-native'; import { _constructStyles } from 'react-native-render-html/src/HTMLStyles'; export const paragraphs = ` -

This paragraph is styled a font size set in em !

- This one showcases the default renderer for the "em" HTML tag. -

This one features a padding in percentage !

-
- Here, we have a style set on the "i" tag with the "tagsStyles" prop. -

And This is a link !

-
-

Finally, this paragraph is styled through the classesStyles prop

+

This paragraph is styled a font size set in em !

+This one showcases the default renderer for the "em" HTML tag. +

This one features a padding in percentage !

+
+Here, we have a style set on the "i" tag with the "tagsStyles" prop. +

And This is a link !

+
+

Finally, this paragraph is styled through the classesStyles prop

`; export const lists = ` @@ -77,6 +77,31 @@ export const layoutStyles = ` `; +export const textsStylesBehaviour = ` +

Styling texts is a very tricky part of converting HTML into react-native components.

+

The way react-native's Text components behaves is a lot different from our browsers' implementation.

+

Let's see how styles are applied to texts with this plugin.

+ +
This text is inside a div, without a text tag wrapping it. The div tag only has color:red; as style.
+ +In the example above, you may find, if you inspect the rendered components, that it's the Text component inside that actually receives the color attribute. +This is because this library parses every text-only style of View wrappers and moves them to each Text child. + +
+

This first paragraph doesn't have a specific styling.

+

This one is blue.

+
+ +

Here, the div wrapper still has color:red; as style..

+ +

The first paragraph inside it doesn't have any style attribute, either from HTML or from the tagsStyles or classesStyles props.

+

The second one is set to be blue from its style attribute.

+ +

You can see the order of priorities that applies to styling. The less important are your tagsStyles, +then your classessStyles and finally the styles parsed from your HTML content.

+`; + + export const ignoringTagsAndStyles = `

The following tag (h2) is ignored with the "ignoredTags" prop

This shouldn't be rendered !

@@ -155,6 +180,14 @@ export default { images404: { name: '404 images', props: { baseFontSize: 20 } }, trickyStuff: { name: 'Tricky stuff' }, layoutStyles: { name: 'Layout styles' }, + textsStylesBehaviour: { + name: 'Texts styles behaviour', + props: { + tagsStyles: { + div: { borderWidth: 1, padding: 10, marginBottom: 10 } + } + } + }, ignoringTagsAndStyles: { name: 'Ignoring tags & styles', props: { diff --git a/README.md b/README.md index 126dca1c8..2cef26be3 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,8 @@ An iOS/Android pure javascript react-native component that renders your HTML int - [Props](#props) - [Demo](#demo) - [Creating custom renderers](#creating-custom-renderers) + - [Custom HTML tags](#custom-html-tags) + - [Lists prefixes](#lists-prefixes) - [Styling](#styling) - [Images](#images) - [Altering content](#altering-content) @@ -60,18 +62,24 @@ Prop | Description | Type | Required/Default `uri` | *(experimental)* remote website to parse and render | `string` | Optional `decodeEntities` | Decode HTML entities of your content | `bool` | Optional, defaults to `true` `imagesMaxWidth` | Resize your images to this maximum width, see [images](#images) | `number` | Optional +`imagesInitialDimensions` | Default width and height to display while image's dimensions are being retrieved, see [images](#images) | `{ width: 100, height: 100 }` | Optional `onLinkPress` | Fired with the event and the href as its arguments when tapping a link | `function` | Optional +`onParsed` | Fired when your HTML content has been parsed, 1st arg is `dom` from htmlparser2, 2nd is `RNElements` from this module | `function` | Optional `tagsStyles` | Provide your styles for specific HTML tags, see [styling](#styling) | `object` | Optional `classesStyles` | Provide your styles for specific HTML classes, see [styling](#styling) | `object` | Optional +`listsPrefixesRenderers` | Your custom renderers from `ul` and `ol` bullets, see [lists prefixes](#lists-prefixes) | `object` | Optional `containerStyle` | Custom style for the default container of the renderered HTML | `object` | Optional `customWrapper` | Replace the default wrapper with a function that takes your content as the first parameter | `function` | Optional +`remoteLoadingView` | Replace the default loader while fetching a remote website's content | `function` | Optional +`remoteErrorView` | Replace the default error if a remote website's content could not be fetched | `function` | Optional `emSize` | The default value in pixels for `1em` | `number` | `14` -`baseFontSize` | The default fontSize applied to `` components | `number` | `14` +`baseFontStyle` | The default style applied to `` components | `number` | `14` `alterData` | Target some specific texts and change their content, see [altering content](#altering-content) | `function` | Optional `alterChildren` | Target some specific nested nodes and change them, see [altering content](#altering-content) | `function` | Optional -`ignoredTags` | HTML tags you don't want rendered, see [ignoring HTML content](#ignoring-html-content) | `array` | Optional, `['head', 'scripts']` +`ignoredTags` | HTML tags you don't want rendered, see [ignoring HTML content](#ignoring-html-content) | `array` | Optional, `['head', 'scripts', ...]` `ignoredStyles` | CSS styles from the `style` attribute you don't want rendered, see [ignoring HTML content](#ignoring-html-content) | `array` | Optional `ignoreNodesFunction` | Return true in this custom function to ignore nodes very precisely, see [ignoring HTML content](#ignoring-html-content) | `function` | Optional +`debug` | Prints the parsing result from htmlparser2 and render-html after the initial render | `bool` | Optional, defaults to `false` ## Demo @@ -85,6 +93,8 @@ Feel free to write more advanced examples and submit a pull-request for it, it w This is very useful if you want to make some very specific styling of your HTML content, or even implement custom HTML tags. +### Custom HTML tags + Just pass an object to the `renderers` prop with the tag name as the key, an a function as its value, like so : ```javascript @@ -113,6 +123,21 @@ Your renderers functions receive several arguments that will be very useful to m * `convertedCSSStyles` : conversion of the `style` attribute from CSS to react-native's stylesheet * `passProps` : various useful information : `groupInfo`, `parentTagName`, `parentIsText`... +### Lists prefixes + +The default renderer of the `