Skip to content

Commit d03f1a5

Browse files
committed
Add void element checking to TagElementType
1 parent 8b179c8 commit d03f1a5

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# React HTML Parser
22

3-
A utility library for converting HTML strings into [React](https://facebook.github.io/react/) components. Avoids the use of dangerouslySetInnerHTML and converts standard HTML elements, attributes and inline styles into their React equivalents.
3+
A utility for converting HTML strings into [React](https://facebook.github.io/react/) components. Avoids the use of dangerouslySetInnerHTML and converts standard HTML elements, attributes and inline styles into their React equivalents.
44

55
[Try the Live Demo](https://wrakky.github.io/react-html-parser)
66

src/elementTypes/TagElementType.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import ProcessNodes from '../utils/ProcessNodes';
33
import GeneratePropsFromAttributes from '../utils/GeneratePropsFromAttributes';
44
import TransformTagName from '../utils/TransformTagName';
5+
import isVoidElement from '../utils/isVoidElement';
56

67
/**
78
* Converts any element (excluding style - see StyleElementType - and script) to a react element.
@@ -12,15 +13,18 @@ import TransformTagName from '../utils/TransformTagName';
1213
*/
1314
export default function TagElementType(node, key) {
1415

15-
// If the node has children process them
16-
const children = ProcessNodes(node.children);
17-
1816
// generate props
1917
const props = GeneratePropsFromAttributes(node.attribs, key);
2018

2119
// transform the tag name if needed
2220
const tagName = TransformTagName(node.name);
2321

22+
// If the node is not a void element and has children then process them
23+
let children = null;
24+
if (!isVoidElement(tagName)) {
25+
children = ProcessNodes(node.children);
26+
}
27+
2428
// create and return the element
2529
return React.createElement(tagName, props, children);
2630
}

src/utils/isVoidElement.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const voidElements = [
2+
'area',
3+
'base',
4+
'br',
5+
'col',
6+
'command',
7+
'embed',
8+
'hr',
9+
'img',
10+
'input',
11+
'keygen',
12+
'link',
13+
'meta',
14+
'param',
15+
'source',
16+
'track',
17+
'wbr'
18+
];
19+
20+
export default function isVoidElement(element) {
21+
22+
return voidElements.indexOf(element) >= 0;
23+
24+
}

0 commit comments

Comments
 (0)