Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/utils/ChildMapping.js
Original file line number Diff line number Diff line change
@@ -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.
*
Expand Down Expand Up @@ -41,16 +43,16 @@ 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
// the combined list
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 = [];
Expand All @@ -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];
Expand Down Expand Up @@ -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;
Expand Down
69 changes: 69 additions & 0 deletions test/ChildMapping-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <div key="one" />;
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,
Expand Down
37 changes: 37 additions & 0 deletions test/TransitionGroup-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,43 @@ describe('TransitionGroup', () => {
renderStrict(<TransitionGroup />, container);
});

it('should ignore enumerable prototype properties added after mounting', () => {
const { rerender } = render(<TransitionGroup />, { container });
// React also checks inherited propTypes and DOM attributes in this test.
const consoleError = jest
.spyOn(console, 'error')
.mockImplementation(() => {});
const property = 'unexpectedTransitionChild';
const previousDescriptor = Object.getOwnPropertyDescriptor(
Object.prototype,
property
);

try {
Object.defineProperty(Object.prototype, property, {
configurable: true,
enumerable: true,
writable: true,
value: { unexpected: true },
});

rerender(
<TransitionGroup>
<Child key="child" />
</TransitionGroup>
);
} finally {
if (previousDescriptor) {
Object.defineProperty(Object.prototype, property, previousDescriptor);
} else {
delete Object.prototype[property];
}
consoleError.mockRestore();
}

expect(container.querySelectorAll('span')).toHaveLength(1);
});

it('should handle transitioning correctly', () => {
function Parent({ count = 1 }) {
let children = [];
Expand Down