Skip to content

Commit

Permalink
Emit warning when assigning to read only properties in client
Browse files Browse the repository at this point in the history
  • Loading branch information
josepharhar committed Sep 28, 2021
1 parent af292bc commit 1a093e5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/react-dom/src/__tests__/DOMPropertyOperations-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,28 @@ describe('DOMPropertyOperations', () => {
expect(customElement.getAttribute('textContent')).toBe(null);
expect(customElement.hasChildNodes()).toBe(false);
});

// @gate enableCustomElementPropertySupport
it('Assigning to read-only properties should emit warnings for custom elements', () => {
const readOnlyProperties = [
'isContentEditable',
'offsetParent',
'offsetTop',
'offsetLeft',
'offsetWidth',
'offsetHeight'
];
for (const readOnlyProperty of readOnlyProperties) {
const container = document.createElement('div');
const props = {};
props[readOnlyProperty] = 'foo';
expect(() => {
ReactDOM.render(React.createElement('my-custom-element', props), container);
}).toErrorDev(
`Assigning to the read-only property \`${readOnlyProperty}\` won't have any effect on the element.`
);
}
});
});

describe('deleteValueForProperty', () => {
Expand Down
14 changes: 14 additions & 0 deletions packages/react-dom/src/client/DOMPropertyOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,20 @@ export function setValueForProperty(
}
}

if (__DEV__ && enableCustomElementPropertySupport && isCustomComponentTag) {
const readOnlyProperties = [
'isContentEditable',
'offsetParent',
'offsetTop',
'offsetLeft',
'offsetWidth',
'offsetHeight'
];
if (readOnlyProperties.includes(name)) {
console.error("Assigning to the read-only property `%s` won't have any effect on the element.", name);
}
}

if (shouldRemoveAttribute(name, value, propertyInfo, isCustomComponentTag)) {
value = null;
}
Expand Down

0 comments on commit 1a093e5

Please sign in to comment.