diff --git a/src/utils/ChildMapping.js b/src/utils/ChildMapping.js index cdd33a3e..18f9edf8 100644 --- a/src/utils/ChildMapping.js +++ b/src/utils/ChildMapping.js @@ -1,5 +1,7 @@ import { Children, cloneElement, isValidElement } from 'react'; +const hasOwnProperty = Object.prototype.hasOwnProperty; + /** * Given `this.props.children`, return an object mapping key to child. * @@ -41,7 +43,7 @@ export function mergeChildMappings(prev, next) { next = next || {}; function getValueForKey(key) { - return key in next ? next[key] : prev[key]; + return hasOwnProperty.call(next, key) ? next[key] : prev[key]; } // For each key of `next`, the list of keys to insert before that key in @@ -49,8 +51,8 @@ export function mergeChildMappings(prev, next) { let nextKeysPending = Object.create(null); let pendingKeys = []; - for (let prevKey in prev) { - if (prevKey in next) { + for (let prevKey of Object.keys(prev)) { + if (hasOwnProperty.call(next, prevKey)) { if (pendingKeys.length) { nextKeysPending[prevKey] = pendingKeys; pendingKeys = []; @@ -61,8 +63,8 @@ export function mergeChildMappings(prev, next) { } let i; - let childMapping = {}; - for (let nextKey in next) { + let childMapping = Object.create(null); + for (let nextKey of Object.keys(next)) { if (nextKeysPending[nextKey]) { for (i = 0; i < nextKeysPending[nextKey].length; i++) { let pendingNextKey = nextKeysPending[nextKey][i]; @@ -106,8 +108,8 @@ export function getNextChildMapping(nextProps, prevChildMapping, onExited) { if (!isValidElement(child)) return; - const hasPrev = key in prevChildMapping; - const hasNext = key in nextChildMapping; + const hasPrev = hasOwnProperty.call(prevChildMapping, key); + const hasNext = hasOwnProperty.call(nextChildMapping, key); const prevChild = prevChildMapping[key]; const isLeaving = isValidElement(prevChild) && !prevChild.props.in; diff --git a/test/ChildMapping-test.js b/test/ChildMapping-test.js index 7afb2574..93ae68b7 100644 --- a/test/ChildMapping-test.js +++ b/test/ChildMapping-test.js @@ -105,6 +105,75 @@ describe('ChildMapping', () => { }); }); + it('should ignore inherited keys in the previous mapping', () => { + let prev = Object.create({ inherited: { unexpected: true } }); + prev.one = true; + let next = { two: true }; + + expect(ChildMapping.mergeChildMappings(prev, next)).toEqual({ + one: true, + two: true, + }); + }); + + it('should ignore inherited keys in the next mapping', () => { + let prev = { one: true }; + let next = Object.create({ inherited: { unexpected: true } }); + next.two = true; + + expect(ChildMapping.mergeChildMappings(prev, next)).toEqual({ + one: true, + two: true, + }); + }); + + it('should preserve previous own entries shadowed by the next prototype', () => { + let prev = { one: 'previous', two: true }; + let next = Object.create({ one: 'inherited' }); + next.two = true; + + let merged = ChildMapping.mergeChildMappings(prev, next); + + expect(merged.one).toBe('previous'); + expect(Object.keys(merged)).toEqual(['one', 'two']); + }); + + it('should preserve own entries named after Object.prototype properties', () => { + let prev = Object.create(null); + Object.defineProperty(prev, '__proto__', { + enumerable: true, + value: 'previous', + }); + prev.constructor = 'constructor'; + let next = Object.create(null); + next.hasOwnProperty = 'hasOwnProperty'; + + let merged = ChildMapping.mergeChildMappings(prev, next); + + expect(Object.keys(merged)).toEqual([ + 'hasOwnProperty', + '__proto__', + 'constructor', + ]); + expect(Object.getOwnPropertyDescriptor(merged, '__proto__').value).toBe( + 'previous' + ); + expect(merged.constructor).toBe('constructor'); + expect(merged.hasOwnProperty).toBe('hasOwnProperty'); + }); + + it('should treat a child inherited by the previous mapping as new', () => { + let child =
; + let props = { children: child, enter: true, exit: true }; + let prev = Object.create({ '.$one': { unexpected: true } }); + + let next = ChildMapping.getNextChildMapping(props, prev, () => {}); + + expect(next['.$one'].props.in).toBe(true); + expect(next['.$one'].props.enter).toBe(true); + expect(next['.$one'].props.exit).toBe(true); + }); + it('should support mergeChildMappings with undefined input', () => { let prev = { one: true, diff --git a/test/TransitionGroup-test.js b/test/TransitionGroup-test.js index da07db44..8aee08c1 100644 --- a/test/TransitionGroup-test.js +++ b/test/TransitionGroup-test.js @@ -75,6 +75,43 @@ describe('TransitionGroup', () => { renderStrict(