Merge pull request #5536 from zpao/keys-on-prototypes

Don't use `key` when defined on String, Number prototypes
This commit is contained in:
Paul O’Shannessy
2015-11-30 23:33:26 -08:00
2 changed files with 36 additions and 1 deletions
@@ -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);
+3 -1
View File
@@ -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);
}