-
Notifications
You must be signed in to change notification settings - Fork 47.8k
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
Allow custom attributes by default #7311
Changes from 18 commits
afae07c
3388f4f
9f7a696
fd94756
f870980
ab64ef3
7717ed7
2b402a4
1681e7f
b5736c7
976ca82
17d3c49
40dee5a
b26384d
d672771
a871d55
c748e84
16dd18a
e68bf4d
83e46aa
facfa87
82e05e0
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 |
---|---|---|
|
@@ -13,6 +13,18 @@ | |
|
||
var invariant = require('fbjs/lib/invariant'); | ||
|
||
var RESERVED_PROPS = { | ||
children: true, | ||
dangerouslySetInnerHTML: true, | ||
autoFocus: true, | ||
defaultValue: true, | ||
defaultChecked: true, | ||
innerHTML: true, | ||
suppressContentEditableWarning: true, | ||
onFocusIn: true, | ||
onFocusOut: true, | ||
}; | ||
|
||
function checkMask(value, bitmask) { | ||
return (value & bitmask) === bitmask; | ||
} | ||
|
@@ -32,11 +44,6 @@ var DOMPropertyInjection = { | |
* Inject some specialized knowledge about the DOM. This takes a config object | ||
* with the following properties: | ||
* | ||
* isCustomAttribute: function that given an attribute name will return true | ||
* if it can be inserted into the DOM verbatim. Useful for data-* or aria-* | ||
* attributes where it's impossible to enumerate all of the possible | ||
* attribute names, | ||
* | ||
* Properties: object mapping DOM property name to one of the | ||
* DOMPropertyInjection constants or null. If your attribute isn't in here, | ||
* it won't get written to the DOM. | ||
|
@@ -64,12 +71,6 @@ var DOMPropertyInjection = { | |
var DOMPropertyNames = domPropertyConfig.DOMPropertyNames || {}; | ||
var DOMMutationMethods = domPropertyConfig.DOMMutationMethods || {}; | ||
|
||
if (domPropertyConfig.isCustomAttribute) { | ||
DOMProperty._isCustomAttributeFunctions.push( | ||
domPropertyConfig.isCustomAttribute, | ||
); | ||
} | ||
|
||
for (var propName in Properties) { | ||
invariant( | ||
!DOMProperty.properties.hasOwnProperty(propName), | ||
|
@@ -117,6 +118,9 @@ var DOMPropertyInjection = { | |
|
||
if (DOMAttributeNames.hasOwnProperty(propName)) { | ||
var attributeName = DOMAttributeNames[propName]; | ||
|
||
DOMProperty.aliases[attributeName] = true; | ||
|
||
propertyInfo.attributeName = attributeName; | ||
if (__DEV__) { | ||
DOMProperty.getPossibleStandardName[attributeName] = propName; | ||
|
@@ -196,6 +200,13 @@ var DOMProperty = { | |
*/ | ||
properties: {}, | ||
|
||
/** | ||
* Some attributes are aliased for easier use within React. We don't | ||
* allow direct use of these attributes. See DOMAttributeNames in | ||
* HTMLPropertyConfig and SVGPropertyConfig. | ||
*/ | ||
aliases: {}, | ||
|
||
/** | ||
* Mapping from lowercase property names to the properly cased version, used | ||
* to warn in the case of missing properties. Available only in __DEV__. | ||
|
@@ -208,22 +219,48 @@ var DOMProperty = { | |
getPossibleStandardName: __DEV__ ? {autofocus: 'autoFocus'} : null, | ||
|
||
/** | ||
* All of the isCustomAttribute() functions that have been injected. | ||
*/ | ||
_isCustomAttributeFunctions: [], | ||
|
||
/** | ||
* Checks whether a property name is a custom attribute. | ||
* Checks whether a property name is a writeable attribute. | ||
* @method | ||
*/ | ||
isCustomAttribute: function(attributeName) { | ||
for (var i = 0; i < DOMProperty._isCustomAttributeFunctions.length; i++) { | ||
var isCustomAttributeFn = DOMProperty._isCustomAttributeFunctions[i]; | ||
if (isCustomAttributeFn(attributeName)) { | ||
shouldSetAttribute: function(name, value) { | ||
if ( | ||
DOMProperty.isReservedProp(name) || | ||
DOMProperty.aliases.hasOwnProperty(name) | ||
) { | ||
return false; | ||
} | ||
|
||
let type = typeof value; | ||
|
||
if ( | ||
value === null || | ||
type === 'undefined' || | ||
DOMProperty.properties.hasOwnProperty(name) | ||
) { | ||
return true; | ||
} | ||
|
||
switch (type) { | ||
case 'boolean': | ||
case 'number': | ||
case 'string': | ||
return true; | ||
} | ||
default: | ||
return false; | ||
} | ||
return false; | ||
}, | ||
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. @gaearon Just to make sure I interpreted your comment correctly, custom attribute logic follows:
A nice side-effect of this is that it gets around needing to know about event registration names for server side rendering. However I would love to allow numbers, strings, and boolean values for this check. If we did that, we could remove the 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. Just got your comment about numbers and booleans. I'll update the code. 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. I think this sounds right. |
||
|
||
/** | ||
* Checks to see if a property name is within the list of properties | ||
* reserved for internal React operations. These properties should | ||
* not be set on an HTML element. | ||
* | ||
* @private | ||
* @param {string} name | ||
* @return {boolean} If the name is within reserved props | ||
*/ | ||
isReservedProp(name) { | ||
return RESERVED_PROPS.hasOwnProperty(name); | ||
}, | ||
|
||
injection: DOMPropertyInjection, | ||
|
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.
Is it not obvious to me why this is necessary. Do we have a test verifying this change? Can you add a comment as to why we do this?
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.
And is this only necessary for custom tags? What about custom attributes? If I do
<div myAttribute="" />
or<div data-MyAttribute="">
, does this also mess up server validation?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.
Interestingly, it looks there's a related todo a earlier in the document:
https://github.com/facebook/react/blob/master/src/renderers/dom/fiber/ReactDOMFiberComponent.js#L931
Accessing the attribute name from the DOM is always going to return a lower cased name, so we need to do this for regular DOM elements too. For consistency, and until we close the thread on this, I've added an adjustment and test in 83e46aa.
I think we really just need to decide if we care about allowing custom attributes to have custom casing. If not, we should add that logic to
shouldSetAttribute
and probably make a separate warning along the lines of "Custom attributes must be lowercase".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'd be in favor if just warning when custom attributes use casing.