mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Merge pull request #5536 from zpao/keys-on-prototypes
Don't use `key` when defined on String, Number prototypes
This commit is contained in:
@@ -476,6 +476,39 @@ describe('traverseAllChildren', function() {
|
||||
}
|
||||
});
|
||||
|
||||
it('should allow extension of native prototypes', function() {
|
||||
/*eslint-disable no-extend-native */
|
||||
String.prototype.key = 'react';
|
||||
Number.prototype.key = 'rocks';
|
||||
/*eslint-enable no-extend-native */
|
||||
|
||||
var instance = (
|
||||
<div>
|
||||
{'a'}
|
||||
{13}
|
||||
</div>
|
||||
);
|
||||
|
||||
var traverseFn = jasmine.createSpy();
|
||||
|
||||
traverseAllChildren(instance.props.children, traverseFn, null);
|
||||
expect(traverseFn.calls.length).toBe(2);
|
||||
|
||||
expect(traverseFn).toHaveBeenCalledWith(
|
||||
null,
|
||||
'a',
|
||||
'.0'
|
||||
);
|
||||
expect(traverseFn).toHaveBeenCalledWith(
|
||||
null,
|
||||
13,
|
||||
'.1'
|
||||
);
|
||||
|
||||
delete String.prototype.key;
|
||||
delete Number.prototype.key;
|
||||
});
|
||||
|
||||
it('should throw on object', function() {
|
||||
expect(function() {
|
||||
traverseAllChildren({a: 1, b: 2}, function() {}, null);
|
||||
|
||||
@@ -47,7 +47,9 @@ function userProvidedKeyEscaper(match) {
|
||||
* @return {string}
|
||||
*/
|
||||
function getComponentKey(component, index) {
|
||||
if (component && component.key != null) {
|
||||
// Do some typechecking here since we call this blindly. We want to ensure
|
||||
// that we don't block potential future ES APIs.
|
||||
if (component && typeof component === 'object' && component.key != null) {
|
||||
// Explicit key
|
||||
return wrapUserProvidedKey(component.key);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user