Skip to content

Commit bb4bf49

Browse files
authoredJan 5, 2023
fix(attributes-to-props): don't convert value of option tag (#790)
* fix(attributes-to-props): don't convert value of option tag Fixes #625 * fix(attributes-to-props): fix issues with tests * fix(attributes-to-props): increase size limit * fix(attributes-to-props): improve jsdoc documentation * fix(attributes-to-props): remove lock file * fix(attributes-to-props): fix type issues
1 parent 0062056 commit bb4bf49

5 files changed

+16
-4
lines changed
 

‎.size-limit.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
33
"path": "dist/html-react-parser.min.js",
4-
"limit": "10.541 KB"
4+
"limit": "10.553 KB"
55
}
66
]

‎lib/attributes-to-props.d.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ export type Props = Record<string, string> & {
1010
* Converts HTML/SVG DOM attributes to React props.
1111
*
1212
* @param attributes - HTML/SVG DOM attributes.
13+
* @param nodeName - DOM node name.
1314
* @returns - React props.
1415
*/
15-
export default function attributesToProps(attributes: Attributes): Props;
16+
export default function attributesToProps(
17+
attributes: Attributes,
18+
nodeName?: string
19+
): Props;

‎lib/attributes-to-props.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ var utilities = require('./utilities');
55
* Converts HTML/SVG DOM attributes to React props.
66
*
77
* @param {object} [attributes={}] - HTML/SVG DOM attributes.
8+
* @param {string} [nodeName] - DOM node name.
89
* @returns - React props.
910
*/
10-
module.exports = function attributesToProps(attributes) {
11+
module.exports = function attributesToProps(attributes, nodeName) {
1112
attributes = attributes || {};
1213

1314
var valueOnlyInputs = {
@@ -43,6 +44,7 @@ module.exports = function attributesToProps(attributes) {
4344
// https://reactjs.org/docs/uncontrolled-components.html
4445
if (
4546
(propName === 'checked' || propName === 'value') &&
47+
nodeName !== 'option' &&
4648
!inputIsValueOnly
4749
) {
4850
propName = getPropName('default' + attributeNameLowerCased);

‎lib/dom-to-react.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function domToReact(nodes, options) {
7676
if (skipAttributesToProps(node)) {
7777
setStyleProp(props.style, props);
7878
} else if (props) {
79-
props = attributesToProps(props);
79+
props = attributesToProps(props, node.name);
8080
}
8181

8282
children = null;

‎test/attributes-to-props.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ describe('attributesToProps with HTML attribute', () => {
186186
expect(attributesToProps(attributes)).toEqual(props);
187187
}
188188
);
189+
190+
it('preserves value of option element', () => {
191+
expect(attributesToProps({ value: 'foo' }, 'option')).toEqual({
192+
value: 'foo'
193+
});
194+
});
189195
});
190196

191197
describe('attributesToProps with SVG attribute', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.