@recursive-robot/react-jsx-parser 1.31.0 → 1.32.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2017 Troy Alford
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Troy Alford
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,149 +1,149 @@
1
- # react-jsx-parser [![CircleCI][circle-ci-badge]](https://circleci.com/gh/TroyAlford/react-jsx-parser) [![Version][npm-version]][npm-link] [![NPM Downloads][npm-downloads]][npm-link] [![License][npm-license]](https://github.com/TroyAlford/react-jsx-parser/blob/master/LICENSE)
2
-
3
- [circle-ci-badge]: https://circleci.com/gh/TroyAlford/react-jsx-parser.svg?style=svg
4
- [npm-version]: https://img.shields.io/npm/v/react-jsx-parser.svg
5
- [npm-downloads]: https://img.shields.io/npm/dt/react-jsx-parser.svg
6
- [npm-license]: https://img.shields.io/npm/l/react-jsx-parser.svg
7
- [npm-link]: https://www.npmjs.com/package/react-jsx-parser
8
-
9
- A React component which can parse JSX and output rendered React Components.
10
-
11
- ## Basic Usage - Injecting JSX as a String
12
- ```javascript
13
- import React from 'react'
14
- import JsxParser from 'react-jsx-parser'
15
- import Library from 'some-library-of-components'
16
-
17
- class InjectableComponent extends Component {
18
- static defaultProps = {
19
- eventHandler: () => {}
20
- }
21
- // ... inner workings of InjectableComponent
22
- }
23
-
24
- const MyComponent = () => (
25
- <JsxParser
26
- bindings={{
27
- foo: 'bar',
28
- myEventHandler: () => { /* ... do stuff ... */ },
29
- }}
30
- components={{ InjectableComponent, Library }}
31
- jsx={`
32
- <h1>Header</h1>
33
- <InjectableComponent eventHandler={myEventHandler} truthyProp />
34
- <Library.SomeComponent someProp={foo} calc={1 + 1} stringProp="foo" />
35
- <Library.DataFetcher>((data) => <div>{data.name}</div>)</Library.DataFetcher>
36
- `}
37
- />
38
- )
39
- ```
40
-
41
- Because `InjectableComponent` is passed into the `JsxParser.props.components` prop, it is treated as a known element
42
- type, and created using `React.createElement(...)` when parsed out of the JSX. You can also pass in a whole collection
43
- of components, as shown by the `Library` binding, and then access the individual items with `LibraryName.ComponentName`.
44
-
45
- Finally, a note about property bindings. The `JsxParser` can handle several types of binding:
46
- - implicit `true` bindings, such as `<InjectableComponent truthyProp />` (equivalent to `truthyProp={true}`)
47
- - string-value binding, such as `stringProp="foo"`
48
- - expression-binding, such as `calc={1 + 1}`
49
- - named-value binding, such as `eventHandler={myEventHandler}` (note that this requires a match in `bindings`)
50
- - simple [single statement arrow expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#basic_syntax) `(item) => <p>{item.name}</p>`
51
-
52
- The component does support inline arrow function declarations, such as:
53
- - `onKeyPress={event => { /* do stuff */}}`
54
- - Function or arrow functions with bodies `() => { return <p>This will not work</p> }`
55
-
56
- ## Advanced Usage - Injecting Dynamic JSX
57
- ```javascript
58
- // Import desired set of components
59
- import { ComponentA, ComponentB } from 'somePackage/Components'
60
- import ComponentC from 'somePackage/ComponentC'
61
- import ComponentD from 'somePackage/ComponentD'
62
- ...
63
- // Load an HTML or XML fragment from a remote API
64
- const dynamicHtml = loadRemoteData()
65
- ...
66
- // Within your component's render method, bind these components and the fragment as props
67
- <JsxParser
68
- bindings={bindings}
69
- components={{ ComponentA, ComponentB, ComponentC, ComponentD }}
70
- jsx={dynamicHtml}
71
- />
72
- ```
73
-
74
- Any `ComponentA`, `ComponentB`, `ComponentC` or `ComponentD` tags in the dynamically loaded XML/HTML fragment will be rendered as React components. Any unrecognized tags will be handled by `React`.
75
-
76
- _Note:_ Non-standard tags may throw errors and warnings, but will typically be rendered in a reasonable way.
77
-
78
- ## Advanced Usage - HTML & Self-Closing Tags
79
- When rendering HTML, standards-adherent editors will render `img`, `hr`, `br`, and other
80
- [void elements](https://www.w3.org/TR/2011/WD-html-markup-20110113/syntax.html#void-elements) with no trailing `/>`. While this is valid HTML, it is _not_ valid JSX. If you wish to opt-in to a more HTML-like parsing style, set the `autoCloseVoidElements` prop to `true`.
81
-
82
- ### Example:
83
- ```jsx
84
- // <hr> has no closing tag, which is valid HTML, but not valid JSX
85
- <JsxParser jsx="<hr><div className='foo'>Foo</div>" />
86
- // Renders: null
87
-
88
- // <hr></hr> is invalid HTML, but valid JSX
89
- <JsxParser jsx="<hr></hr><div className='foo'>Foo</div>" />
90
- // Renders: <hr><div class='foo'>Foo</div>
91
-
92
- // This is valid HTML, and the `autoCloseVoidElements` prop allows it to render
93
- <JsxParser autoCloseVoidElements jsx="<hr><div className='foo'>Foo</div>" />
94
- // Renders: <hr><div class='foo'>Foo</div>
95
-
96
- // This would work in a browser, but will no longer parse with `autoCloseVoidElements`
97
- <JsxParser autoCloseVoidElements jsx="<hr></hr><div className='foo'>Foo</div>" />
98
- // Renders: null
99
- ```
100
-
101
- ## PropTypes / Settings
102
- ```javascript
103
- JsxParser.defaultProps = {
104
- allowUnknownElements: true, // by default, allow unrecognized elements
105
- // if false, unrecognized elements like <foo> are omitted and reported via onError
106
-
107
- autoCloseVoidElements: false, // by default, unclosed void elements will not parse. See examples
108
-
109
- bindings: {}, // by default, do not add any additional bindings
110
-
111
- blacklistedAttrs: [/^on.+/i], // default: removes `on*` attributes (onClick, onChange, etc.)
112
- // any attribute name which matches any of these RegExps will be omitted entirely
113
-
114
- blacklistedTags: ['script'], // by default, removes all <script> tags
115
-
116
- className: '', // space-delimited classes to add to wrapper (ignored if renderInWrapper=false)
117
-
118
- components: {}, // an object map of component tag-names to their definitions - see above
119
- // components must extend React.Component, React.PureComponent, or be a Function
120
-
121
- componentsOnly: false, // non-component HTML tags are allowed by default, omitted if true
122
-
123
- disableFragments: false, // if true, React <Fragment />s will not be used.
124
- // Note: This introduces subtle errors with regard to white-space, and is provided only for
125
- // backward compatibility with React 15.x
126
-
127
- disableKeyGeneration: false, // if true, rendering will not automatically generate `key` props.
128
- // Note: This may result in the "Child elements should have a unique 'key' prop " React error.
129
-
130
- jsx: '', // the jsx string to be parsed & rendered
131
-
132
- onError: () => {}, // if specified, any rendering errors are reported via this method
133
-
134
- showWarnings: false, // if true showWarnings, rendering errors are output with console.warn
135
-
136
- renderError: undefined, // if specified, this function can be used to render errors as a fallback
137
-
138
- renderInWrapper: true, // if false, the HTML output will have no <div> wrapper
139
-
140
- renderUnrecognized: tagName => null, // unrecognized tags are rendered via this method
141
- }
142
- ```
143
-
144
- ## Older Browser Support
145
-
146
- If your application needs to support older browsers, like `IE11`, import from `react-jsx-parser/dist/es5/react-jsx-parser.min.js`,
147
- which transpiles the `acorn-jsx` dependency down to ES5, and also adds additional polyfill support for code used in this package.
148
-
149
- **Note**: <u>not</u> recommended for implementations which only support modern browsers, as the ES5 version is roughly 30% larger.
1
+ # react-jsx-parser [![CircleCI][circle-ci-badge]](https://circleci.com/gh/TroyAlford/react-jsx-parser) [![Version][npm-version]][npm-link] [![NPM Downloads][npm-downloads]][npm-link] [![License][npm-license]](https://github.com/TroyAlford/react-jsx-parser/blob/master/LICENSE)
2
+
3
+ [circle-ci-badge]: https://circleci.com/gh/TroyAlford/react-jsx-parser.svg?style=svg
4
+ [npm-version]: https://img.shields.io/npm/v/react-jsx-parser.svg
5
+ [npm-downloads]: https://img.shields.io/npm/dt/react-jsx-parser.svg
6
+ [npm-license]: https://img.shields.io/npm/l/react-jsx-parser.svg
7
+ [npm-link]: https://www.npmjs.com/package/react-jsx-parser
8
+
9
+ A React component which can parse JSX and output rendered React Components.
10
+
11
+ ## Basic Usage - Injecting JSX as a String
12
+ ```javascript
13
+ import React from 'react'
14
+ import JsxParser from 'react-jsx-parser'
15
+ import Library from 'some-library-of-components'
16
+
17
+ class InjectableComponent extends Component {
18
+ static defaultProps = {
19
+ eventHandler: () => {}
20
+ }
21
+ // ... inner workings of InjectableComponent
22
+ }
23
+
24
+ const MyComponent = () => (
25
+ <JsxParser
26
+ bindings={{
27
+ foo: 'bar',
28
+ myEventHandler: () => { /* ... do stuff ... */ },
29
+ }}
30
+ components={{ InjectableComponent, Library }}
31
+ jsx={`
32
+ <h1>Header</h1>
33
+ <InjectableComponent eventHandler={myEventHandler} truthyProp />
34
+ <Library.SomeComponent someProp={foo} calc={1 + 1} stringProp="foo" />
35
+ <Library.DataFetcher>((data) => <div>{data.name}</div>)</Library.DataFetcher>
36
+ `}
37
+ />
38
+ )
39
+ ```
40
+
41
+ Because `InjectableComponent` is passed into the `JsxParser.props.components` prop, it is treated as a known element
42
+ type, and created using `React.createElement(...)` when parsed out of the JSX. You can also pass in a whole collection
43
+ of components, as shown by the `Library` binding, and then access the individual items with `LibraryName.ComponentName`.
44
+
45
+ Finally, a note about property bindings. The `JsxParser` can handle several types of binding:
46
+ - implicit `true` bindings, such as `<InjectableComponent truthyProp />` (equivalent to `truthyProp={true}`)
47
+ - string-value binding, such as `stringProp="foo"`
48
+ - expression-binding, such as `calc={1 + 1}`
49
+ - named-value binding, such as `eventHandler={myEventHandler}` (note that this requires a match in `bindings`)
50
+ - simple [single statement arrow expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#basic_syntax) `(item) => <p>{item.name}</p>`
51
+
52
+ The component does support inline arrow function declarations, such as:
53
+ - `onKeyPress={event => { /* do stuff */}}`
54
+ - Function or arrow functions with bodies `() => { return <p>This will not work</p> }`
55
+
56
+ ## Advanced Usage - Injecting Dynamic JSX
57
+ ```javascript
58
+ // Import desired set of components
59
+ import { ComponentA, ComponentB } from 'somePackage/Components'
60
+ import ComponentC from 'somePackage/ComponentC'
61
+ import ComponentD from 'somePackage/ComponentD'
62
+ ...
63
+ // Load an HTML or XML fragment from a remote API
64
+ const dynamicHtml = loadRemoteData()
65
+ ...
66
+ // Within your component's render method, bind these components and the fragment as props
67
+ <JsxParser
68
+ bindings={bindings}
69
+ components={{ ComponentA, ComponentB, ComponentC, ComponentD }}
70
+ jsx={dynamicHtml}
71
+ />
72
+ ```
73
+
74
+ Any `ComponentA`, `ComponentB`, `ComponentC` or `ComponentD` tags in the dynamically loaded XML/HTML fragment will be rendered as React components. Any unrecognized tags will be handled by `React`.
75
+
76
+ _Note:_ Non-standard tags may throw errors and warnings, but will typically be rendered in a reasonable way.
77
+
78
+ ## Advanced Usage - HTML & Self-Closing Tags
79
+ When rendering HTML, standards-adherent editors will render `img`, `hr`, `br`, and other
80
+ [void elements](https://www.w3.org/TR/2011/WD-html-markup-20110113/syntax.html#void-elements) with no trailing `/>`. While this is valid HTML, it is _not_ valid JSX. If you wish to opt-in to a more HTML-like parsing style, set the `autoCloseVoidElements` prop to `true`.
81
+
82
+ ### Example:
83
+ ```jsx
84
+ // <hr> has no closing tag, which is valid HTML, but not valid JSX
85
+ <JsxParser jsx="<hr><div className='foo'>Foo</div>" />
86
+ // Renders: null
87
+
88
+ // <hr></hr> is invalid HTML, but valid JSX
89
+ <JsxParser jsx="<hr></hr><div className='foo'>Foo</div>" />
90
+ // Renders: <hr><div class='foo'>Foo</div>
91
+
92
+ // This is valid HTML, and the `autoCloseVoidElements` prop allows it to render
93
+ <JsxParser autoCloseVoidElements jsx="<hr><div className='foo'>Foo</div>" />
94
+ // Renders: <hr><div class='foo'>Foo</div>
95
+
96
+ // This would work in a browser, but will no longer parse with `autoCloseVoidElements`
97
+ <JsxParser autoCloseVoidElements jsx="<hr></hr><div className='foo'>Foo</div>" />
98
+ // Renders: null
99
+ ```
100
+
101
+ ## PropTypes / Settings
102
+ ```javascript
103
+ JsxParser.defaultProps = {
104
+ allowUnknownElements: true, // by default, allow unrecognized elements
105
+ // if false, unrecognized elements like <foo> are omitted and reported via onError
106
+
107
+ autoCloseVoidElements: false, // by default, unclosed void elements will not parse. See examples
108
+
109
+ bindings: {}, // by default, do not add any additional bindings
110
+
111
+ blacklistedAttrs: [/^on.+/i], // default: removes `on*` attributes (onClick, onChange, etc.)
112
+ // any attribute name which matches any of these RegExps will be omitted entirely
113
+
114
+ blacklistedTags: ['script'], // by default, removes all <script> tags
115
+
116
+ className: '', // space-delimited classes to add to wrapper (ignored if renderInWrapper=false)
117
+
118
+ components: {}, // an object map of component tag-names to their definitions - see above
119
+ // components must extend React.Component, React.PureComponent, or be a Function
120
+
121
+ componentsOnly: false, // non-component HTML tags are allowed by default, omitted if true
122
+
123
+ disableFragments: false, // if true, React <Fragment />s will not be used.
124
+ // Note: This introduces subtle errors with regard to white-space, and is provided only for
125
+ // backward compatibility with React 15.x
126
+
127
+ disableKeyGeneration: false, // if true, rendering will not automatically generate `key` props.
128
+ // Note: This may result in the "Child elements should have a unique 'key' prop " React error.
129
+
130
+ jsx: '', // the jsx string to be parsed & rendered
131
+
132
+ onError: () => {}, // if specified, any rendering errors are reported via this method
133
+
134
+ showWarnings: false, // if true showWarnings, rendering errors are output with console.warn
135
+
136
+ renderError: undefined, // if specified, this function can be used to render errors as a fallback
137
+
138
+ renderInWrapper: true, // if false, the HTML output will have no <div> wrapper
139
+
140
+ renderUnrecognized: tagName => null, // unrecognized tags are rendered via this method
141
+ }
142
+ ```
143
+
144
+ ## Older Browser Support
145
+
146
+ If your application needs to support older browsers, like `IE11`, import from `react-jsx-parser/dist/es5/react-jsx-parser.min.js`,
147
+ which transpiles the `acorn-jsx` dependency down to ES5, and also adds additional polyfill support for code used in this package.
148
+
149
+ **Note**: <u>not</u> recommended for implementations which only support modern browsers, as the ES5 version is roughly 30% larger.