Skip to content

fix(attributes-to-props): don't convert value of option tag #790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .size-limit.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"path": "dist/html-react-parser.min.js",
"limit": "10.541 KB"
"limit": "10.553 KB"
}
]
6 changes: 5 additions & 1 deletion lib/attributes-to-props.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export type Props = Record<string, string> & {
* 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;
4 changes: 3 additions & 1 deletion lib/attributes-to-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/dom-to-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions test/attributes-to-props.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down