-
Notifications
You must be signed in to change notification settings - Fork 47.5k
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
Warn against custom non-lowercase attributes #10699
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -909,25 +909,25 @@ describe('ReactDOMServerIntegration', () => { | |
}); | ||
|
||
itRenders('unknown data- attributes with casing', async render => { | ||
const e = await render(<div data-fooBar="true" />); | ||
expect(e.getAttribute('data-fooBar')).toBe('true'); | ||
const e = await render(<div data-fooBar="true" />, 1); | ||
expect(e.getAttribute('data-foobar')).toBe('true'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing these calls wasn't essential but I figured I'd emphasize they are normalized by the browser anyway. |
||
}); | ||
|
||
itRenders('unknown data- attributes with boolean true', async render => { | ||
const e = await render(<div data-fooBar={true} />); | ||
expect(e.getAttribute('data-fooBar')).toBe('true'); | ||
const e = await render(<div data-foobar={true} />); | ||
expect(e.getAttribute('data-foobar')).toBe('true'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests were not specifically about casing so I fixed them. |
||
}); | ||
|
||
itRenders('unknown data- attributes with boolean false', async render => { | ||
const e = await render(<div data-fooBar={false} />); | ||
expect(e.getAttribute('data-fooBar')).toBe('false'); | ||
const e = await render(<div data-foobar={false} />); | ||
expect(e.getAttribute('data-foobar')).toBe('false'); | ||
}); | ||
|
||
itRenders( | ||
'no unknown data- attributes with casing and null value', | ||
async render => { | ||
const e = await render(<div data-fooBar={null} />); | ||
expect(e.hasAttribute('data-fooBar')).toBe(false); | ||
const e = await render(<div data-fooBar={null} />, 1); | ||
expect(e.hasAttribute('data-foobar')).toBe(false); | ||
}, | ||
); | ||
|
||
|
@@ -972,8 +972,8 @@ describe('ReactDOMServerIntegration', () => { | |
}); | ||
|
||
itRenders('cased custom attributes', async render => { | ||
const e = await render(<div fooBar="test" />); | ||
expect(e.getAttribute('fooBar')).toBe('test'); | ||
const e = await render(<div fooBar="test" />, 1); | ||
expect(e.getAttribute('foobar')).toBe('test'); | ||
}); | ||
}); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,10 @@ if (__DEV__) { | |
var warnedProperties = {}; | ||
var hasOwnProperty = Object.prototype.hasOwnProperty; | ||
var EVENT_NAME_REGEX = /^on[A-Z]/; | ||
var ARIA_NAME_REGEX = /^aria-/i; | ||
var rARIA = new RegExp('^(aria)-[' + DOMProperty.ATTRIBUTE_NAME_CHAR + ']*$'); | ||
var rARIACamel = new RegExp( | ||
'^(aria)[A-Z][' + DOMProperty.ATTRIBUTE_NAME_CHAR + ']*$', | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copy pasta from ARIA hook. Catches more cases which ensures we don't fire two warnings instead of one. |
||
var possibleStandardNames = require('possibleStandardNames'); | ||
|
||
var validateProperty = function(tagName, name, value, debugID) { | ||
|
@@ -91,7 +94,7 @@ if (__DEV__) { | |
} | ||
|
||
// Let the ARIA attribute hook validate ARIA attributes | ||
if (ARIA_NAME_REGEX.test(name)) { | ||
if (rARIA.test(name) || rARIACamel.test(name)) { | ||
return true; | ||
} | ||
|
||
|
@@ -155,6 +158,8 @@ if (__DEV__) { | |
return true; | ||
} | ||
|
||
const isReserved = DOMProperty.isReservedProp(name); | ||
|
||
// Known attributes should match the casing specified in the property config. | ||
if (possibleStandardNames.hasOwnProperty(lowerCasedName)) { | ||
var standardName = possibleStandardNames[lowerCasedName]; | ||
|
@@ -169,6 +174,22 @@ if (__DEV__) { | |
warnedProperties[name] = true; | ||
return true; | ||
} | ||
} else if (!isReserved && name !== lowerCasedName) { | ||
// Unknown attributes should have lowercase casing since that's how they | ||
// will be cased anyway with server rendering. | ||
warning( | ||
false, | ||
'React does not recognize the `%s` prop on a DOM element. If you ' + | ||
'intentionally wanted it to appear in the DOM as a custom ' + | ||
'attribute, spell it as the lowercase `%s` instead. ' + | ||
'If you passed it accidentally from a parent component, remove ' + | ||
'it from the DOM element.%s', | ||
name, | ||
lowerCasedName, | ||
getStackAddendum(debugID), | ||
); | ||
warnedProperties[name] = true; | ||
return true; | ||
} | ||
|
||
if (typeof value === 'boolean') { | ||
|
@@ -186,7 +207,7 @@ if (__DEV__) { | |
|
||
// Now that we've validated casing, do not validate | ||
// data types for reserved props | ||
if (DOMProperty.isReservedProp(name)) { | ||
if (isReserved) { | ||
return true; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it’s important to mention this since people will often get this warning due to spreading.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like practically this is going to make the unknown DOM warning permanent (modulo some cases). Granted it's probably not what the user intends it seems a bit heavy handed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or perhaps maybe just that it's going to give the impression that everything is fine when they aren't getting this warning, while
foo="[Object object]"
still gets passedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The react prop name can still have casing, as long as it's passed as lowercase to the HTML element. This doesn't allow spreading of attributes, but you can do it at least.
I think it's important to communicate what this is protecting against. I don't know that this should live in the warning, but I do agree that a developer not familiar with the problem-space could see this as an arbitrary restriction.
We could turn it into a "React's got your back" kind of thing if we effectively communicate it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about custom elements (or does that not hit here)? Chance of a false positive? If we are trying to get out of folks way about passing what they want to the DOM this feels a bit arbitrary as you say.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sigh didn't see the original issue, sorry for the noise :P