diff --git a/.size-limit.json b/.size-limit.json index 99900b46..a8fb2325 100644 --- a/.size-limit.json +++ b/.size-limit.json @@ -1,6 +1,6 @@ [ { "path": "dist/html-react-parser.min.js", - "limit": "10.541 KB" + "limit": "10.553 KB" } ] diff --git a/lib/attributes-to-props.d.ts b/lib/attributes-to-props.d.ts index 02fd8441..b6d95ce3 100644 --- a/lib/attributes-to-props.d.ts +++ b/lib/attributes-to-props.d.ts @@ -10,6 +10,10 @@ export type Props = Record & { * Converts HTML/SVG DOM attributes to React props. * * @param attributes - HTML/SVG DOM attributes. + * @param nodeName - DOM node name. * @returns - React props. */ -export default function attributesToProps(attributes: Attributes): Props; +export default function attributesToProps( + attributes: Attributes, + nodeName?: string +): Props; diff --git a/lib/attributes-to-props.js b/lib/attributes-to-props.js index 706a7471..0d5c8f77 100644 --- a/lib/attributes-to-props.js +++ b/lib/attributes-to-props.js @@ -5,9 +5,10 @@ var utilities = require('./utilities'); * Converts HTML/SVG DOM attributes to React props. * * @param {object} [attributes={}] - HTML/SVG DOM attributes. + * @param {string} [nodeName] - DOM node name. * @returns - React props. */ -module.exports = function attributesToProps(attributes) { +module.exports = function attributesToProps(attributes, nodeName) { attributes = attributes || {}; var valueOnlyInputs = { @@ -43,6 +44,7 @@ module.exports = function attributesToProps(attributes) { // https://reactjs.org/docs/uncontrolled-components.html if ( (propName === 'checked' || propName === 'value') && + nodeName !== 'option' && !inputIsValueOnly ) { propName = getPropName('default' + attributeNameLowerCased); diff --git a/lib/dom-to-react.js b/lib/dom-to-react.js index 54cc77b3..4f46d65c 100644 --- a/lib/dom-to-react.js +++ b/lib/dom-to-react.js @@ -76,7 +76,7 @@ function domToReact(nodes, options) { if (skipAttributesToProps(node)) { setStyleProp(props.style, props); } else if (props) { - props = attributesToProps(props); + props = attributesToProps(props, node.name); } children = null; diff --git a/test/attributes-to-props.test.js b/test/attributes-to-props.test.js index d369f5e2..ab9d164c 100644 --- a/test/attributes-to-props.test.js +++ b/test/attributes-to-props.test.js @@ -186,6 +186,12 @@ describe('attributesToProps with HTML attribute', () => { expect(attributesToProps(attributes)).toEqual(props); } ); + + it('preserves value of option element', () => { + expect(attributesToProps({ value: 'foo' }, 'option')).toEqual({ + value: 'foo' + }); + }); }); describe('attributesToProps with SVG attribute', () => {