Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: TroyAlford/react-jsx-parser
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: develop
Choose a base ref
...
head repository: fcaldarelli/react-jsx-parser
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: develop
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 3 commits
  • 2 files changed
  • 1 contributor

Commits on Oct 18, 2023

  1. update

    fcaldarelli committed Oct 18, 2023
    Copy the full SHA
    df2602f View commit details
  2. Update

    fcaldarelli committed Oct 18, 2023
    Copy the full SHA
    f19fddc View commit details
  3. Update

    fcaldarelli committed Oct 18, 2023
    Copy the full SHA
    c7577ab View commit details
Showing with 9 additions and 14 deletions.
  1. +1 −1 package.json
  2. +8 −13 source/components/JsxParser.tsx
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
"bugs": "https://github.com/TroyAlford/react-jsx-parser/issues",
"description": "A React component which can parse JSX and output rendered React Components",
"files": [
"dist/"
"source/"
],
"keywords": [
"react",
21 changes: 8 additions & 13 deletions source/components/JsxParser.tsx
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import { canHaveChildren, canHaveWhitespace } from '../constants/specialTags'
import { randomHash } from '../helpers/hash'
import { parseStyle } from '../helpers/parseStyle'
import { resolvePath } from '../helpers/resolvePath'
import { Text, View } from 'react-native'

type ParsedJSX = JSX.Element | boolean | string
type ParsedTree = ParsedJSX | ParsedJSX[] | null
@@ -37,7 +38,7 @@ export default class JsxParser extends React.Component<TProps> {
allowUnknownElements: true,
autoCloseVoidElements: false,
bindings: {},
blacklistedAttrs: [/^on.+/i],
blacklistedAttrs: [], /* [/^on.+/i], */
blacklistedTags: ['script'],
className: '',
components: {},
@@ -58,7 +59,7 @@ export default class JsxParser extends React.Component<TProps> {
const parser = Acorn.Parser.extend(AcornJSX.default({
autoCloseVoidElements: this.props.autoCloseVoidElements,
}))
const wrappedJsx = `<root>${jsx}</root>`
const wrappedJsx = `<Text>${jsx}</Text>`
let parsed: AcornJSX.Expression[] = []
try {
// @ts-ignore - AcornJsx doesn't have typescript typings
@@ -91,7 +92,7 @@ export default class JsxParser extends React.Component<TProps> {
const key = this.props.disableKeyGeneration ? undefined : randomHash()
return this.props.disableFragments
? expression.value
: <Fragment key={key}>{expression.value}</Fragment>
: <Text key={key}>{expression.value}</Text>
case 'ArrayExpression':
return expression.elements.map(ele => this.#parseExpression(ele, scope)) as ParsedTree
case 'BinaryExpression':
@@ -241,9 +242,6 @@ export default class JsxParser extends React.Component<TProps> {
const blacklistedTags = (this.props.blacklistedTags || [])
.map(tag => tag.trim().toLowerCase()).filter(Boolean)

if (/^(html|head|body)$/i.test(name)) {
return childNodes.map(c => this.#parseElement(c, scope)) as JSX.Element[]
}
const tagName = name.trim().toLowerCase()
if (blacklistedTags.indexOf(tagName) !== -1) {
onError!(new Error(`The tag <${name}> is blacklisted, and will not be rendered.`))
@@ -256,7 +254,7 @@ export default class JsxParser extends React.Component<TProps> {
return this.props.renderUnrecognized!(name)
}

if (!allowUnknownElements && document.createElement(name) instanceof HTMLUnknownElement) {
if (!allowUnknownElements) {
onError!(new Error(`The tag <${name}> is unrecognized in this browser, and will not be rendered.`))
return this.props.renderUnrecognized!(name)
}
@@ -333,16 +331,13 @@ export default class JsxParser extends React.Component<TProps> {
}

render = (): JSX.Element => {
const jsx = (this.props.jsx || '').trim().replace(/<!DOCTYPE([^>]*)>/g, '')
const jsx = (this.props.jsx || '').trim();
this.ParsedChildren = this.#parseJSX(jsx)
const className = [...new Set(['jsx-parser', ...String(this.props.className).split(' ')])]
.filter(Boolean)
.join(' ')

return (
this.props.renderInWrapper
? <div className={className}>{this.ParsedChildren}</div>
: <>{this.ParsedChildren}</>
? <Text>{this.ParsedChildren}</Text>
: <Text>{this.ParsedChildren}</Text>
)
}
}