Skip to content
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

Merge adjacent ReactTextComponents in flattenChildren #1989

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 2 additions & 4 deletions src/browser/server/__tests__/ReactServerRendering-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ describe('ReactServerRendering', function() {
'<div ' + ID_ATTRIBUTE_NAME + '="[^"]+" ' +
ReactMarkupChecksum.CHECKSUM_ATTR_NAME + '="[^"]+">' +
'<span ' + ID_ATTRIBUTE_NAME + '="[^"]+">' +
'<span ' + ID_ATTRIBUTE_NAME + '="[^"]+">My name is </span>' +
'<span ' + ID_ATTRIBUTE_NAME + '="[^"]+">child</span>' +
'<span ' + ID_ATTRIBUTE_NAME + '="[^"]+">My name is child</span>' +
'</span>' +
'</div>'
);
Expand Down Expand Up @@ -146,8 +145,7 @@ describe('ReactServerRendering', function() {
expect(response).toMatch(
'<span ' + ID_ATTRIBUTE_NAME + '="[^"]+" ' +
ReactMarkupChecksum.CHECKSUM_ATTR_NAME + '="[^"]+">' +
'<span ' + ID_ATTRIBUTE_NAME + '="[^"]+">Component name: </span>' +
'<span ' + ID_ATTRIBUTE_NAME + '="[^"]+">TestComponent</span>' +
'<span ' + ID_ATTRIBUTE_NAME + '="[^"]+">Component name: TestComponent</span>' +
'</span>'
);
expect(lifecycle).toEqual(
Expand Down
80 changes: 40 additions & 40 deletions src/core/__tests__/ReactMultiChildText-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,31 +128,31 @@ describe('ReactMultiChildText', function() {

// two adjacent values
[true, 0], ['0'],
[0, 0], ['0', '0'],
[1.2, 0], ['1.2', '0'],
[0, ''], ['0', ''],
['foo', 0], ['foo', '0'],
[0, 0], ['00'],
[1.2, 0], ['1.20'],
[0, ''], ['0'],
['foo', 0], ['foo0'],
[0, <div />], ['0', <div />],

[true, 1.2], ['1.2'],
[1.2, 0], ['1.2', '0'],
[1.2, 1.2], ['1.2', '1.2'],
[1.2, ''], ['1.2', ''],
['foo', 1.2], ['foo', '1.2'],
[1.2, 0], ['1.20'],
[1.2, 1.2], ['1.21.2'],
[1.2, ''], ['1.2'],
['foo', 1.2], ['foo1.2'],
[1.2, <div />], ['1.2', <div />],

[true, ''], [''],
['', 0], ['', '0'],
[1.2, ''], ['1.2', ''],
['', ''], ['', ''],
['foo', ''], ['foo', ''],
['', 0], ['0'],
[1.2, ''], ['1.2'],
['', ''], [''],
['foo', ''], ['foo'],
['', <div />], ['', <div />],

[true, 'foo'], ['foo'],
['foo', 0], ['foo', '0'],
[1.2, 'foo'], ['1.2', 'foo'],
['foo', ''], ['foo', ''],
['foo', 'foo'], ['foo', 'foo'],
['foo', 0], ['foo0'],
[1.2, 'foo'], ['1.2foo'],
['foo', ''], ['foo'],
['foo', 'foo'], ['foofoo'],
['foo', <div />], ['foo', <div />],

// values separated by an element
Expand All @@ -161,48 +161,48 @@ describe('ReactMultiChildText', function() {
['', <div />, ''], ['', <div />, ''],
['foo', <div />, 'foo'], ['foo', <div />, 'foo'],

[true, 1.2, <div />, '', 'foo'], ['1.2', <div />, '', 'foo'],
[1.2, '', <div />, 'foo', true], ['1.2', '', <div />, 'foo'],
['', 'foo', <div />, true, 1.2], ['', 'foo', <div />, '1.2'],
[true, 1.2, <div />, '', 'foo'], ['1.2', <div />, 'foo'],
[1.2, '', <div />, 'foo', true], ['1.2', <div />, 'foo'],
['', 'foo', <div />, true, 1.2], ['foo', <div />, '1.2'],

[true, 1.2, '', <div />, 'foo', true, 1.2], ['1.2', '', <div />, 'foo', '1.2'],
['', 'foo', true, <div />, 1.2, '', 'foo'], ['', 'foo', <div />, '1.2', '', 'foo'],
[true, 1.2, '', <div />, 'foo', true, 1.2], ['1.2', <div />, 'foo1.2'],
['', 'foo', true, <div />, 1.2, '', 'foo'], ['foo', <div />, '1.2foo'],

// values inside arrays
[[true], [true]], [],
[[1.2], [1.2]], ['1.2', '1.2'],
[[''], ['']], ['', ''],
[['foo'], ['foo']], ['foo', 'foo'],
[[1.2], [1.2]], ['1.21.2'],
[[''], ['']], [''],
[['foo'], ['foo']], ['foofoo'],
[[<div />], [<div />]], [<div />, <div />],

[[true, 1.2, <div />], '', 'foo'], ['1.2', <div />, '', 'foo'],
[1.2, '', [<div />, 'foo', true]], ['1.2', '', <div />, 'foo'],
['', ['foo', <div />, true], 1.2], ['', 'foo', <div />, '1.2'],
[[true, 1.2, <div />], '', 'foo'], ['1.2', <div />, 'foo'],
[1.2, '', [<div />, 'foo', true]], ['1.2', <div />, 'foo'],
['', ['foo', <div />, true], 1.2], ['foo', <div />, '1.2'],

[true, [1.2, '', <div />, 'foo'], true, 1.2], ['1.2', '', <div />, 'foo', '1.2'],
['', 'foo', [true, <div />, 1.2, ''], 'foo'], ['', 'foo', <div />, '1.2', '', 'foo'],
[true, [1.2, '', <div />, 'foo'], true, 1.2], ['1.2', <div />, 'foo1.2'],
['', 'foo', [true, <div />, 1.2, ''], 'foo'], ['foo', <div />, '1.2foo'],

// values inside objects
[{a: true}, {a: true}], [],
[{a: 1.2}, {a: 1.2}], ['1.2', '1.2'],
[{a: ''}, {a: ''}], ['', ''],
[{a: 'foo'}, {a: 'foo'}], ['foo', 'foo'],
[{a: 1.2}, {a: 1.2}], ['1.21.2'],
[{a: ''}, {a: ''}], [''],
[{a: 'foo'}, {a: 'foo'}], ['foofoo'],
[{a: <div />}, {a: <div />}], [<div />, <div />],

[{a: true, b: 1.2, c: <div />}, '', 'foo'], ['1.2', <div />, '', 'foo'],
[1.2, '', {a: <div />, b: 'foo', c: true}], ['1.2', '', <div />, 'foo'],
['', {a: 'foo', b: <div />, c: true}, 1.2], ['', 'foo', <div />, '1.2'],
[{a: true, b: 1.2, c: <div />}, '', 'foo'], ['1.2', <div />, 'foo'],
[1.2, '', {a: <div />, b: 'foo', c: true}], ['1.2', <div />, 'foo'],
['', {a: 'foo', b: <div />, c: true}, 1.2], ['foo', <div />, '1.2'],

[true, {a: 1.2, b: '', c: <div />, d: 'foo'}, true, 1.2], ['1.2', '', <div />, 'foo', '1.2'],
['', 'foo', {a: true, b: <div />, c: 1.2, d: ''}, 'foo'], ['', 'foo', <div />, '1.2', '', 'foo'],
[true, {a: 1.2, b: '', c: <div />, d: 'foo'}, true, 1.2], ['1.2', <div />, 'foo1.2'],
['', 'foo', {a: true, b: <div />, c: 1.2, d: ''}, 'foo'], ['foo', <div />, '1.2foo'],

// values inside elements
[<div>{true}{1.2}{<div />}</div>, '', 'foo'], [<div />, '', 'foo'],
[1.2, '', <div>{<div />}{'foo'}{true}</div>], ['1.2', '', <div />],
[<div>{true}{1.2}{<div />}</div>, '', 'foo'], [<div />, 'foo'],
[1.2, '', <div>{<div />}{'foo'}{true}</div>], ['1.2', <div />],
['', <div>{'foo'}{<div />}{true}</div>, 1.2], ['', <div />, '1.2'],

[true, <div>{1.2}{''}{<div />}{'foo'}</div>, true, 1.2], [<div />, '1.2'],
['', 'foo', <div>{true}{<div />}{1.2}{''}</div>, 'foo'], ['', 'foo', <div />, 'foo']
['', 'foo', <div>{true}{<div />}{1.2}{''}</div>, 'foo'], ['foo', <div />, 'foo']
]);
});

Expand Down
23 changes: 22 additions & 1 deletion src/utils/flattenChildren.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@

"use strict";

var ReactTextComponent = require('ReactTextComponent');

var traverseAllChildren = require('traverseAllChildren');
var warning = require('warning');

var previousName = null;

/**
* @param {function} traverseContext Context passed through traversal.
* @param {?ReactComponent} child React child component.
Expand All @@ -38,7 +42,23 @@ function flattenSingleChildIntoContext(traverseContext, child, name) {
name
);
if (keyUnique && child != null) {
result[name] = child;
var type = typeof child;
var normalizedValue;

if (type === 'string' || type === 'number') {
var previousValue = result[previousName];
if (previousValue && previousValue.type === ReactTextComponent.type) {
normalizedValue = ReactTextComponent(previousValue.props + child);
name = previousName;
} else {
normalizedValue = ReactTextComponent('' + child);
}
} else {
normalizedValue = child;
}

result[name] = normalizedValue;
previousName = name;
}
}

Expand All @@ -53,6 +73,7 @@ function flattenChildren(children) {
}
var result = {};
traverseAllChildren(children, flattenSingleChildIntoContext, result);
previousName = null;
return result;
}

Expand Down
53 changes: 23 additions & 30 deletions src/utils/traverseAllChildren.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,39 +127,32 @@ var traverseAllChildrenImpl =
// All of the above are perceived as null.
callback(traverseContext, null, storageName, indexSoFar);
subtreeCount = 1;
} else if (ReactDescriptor.isValidDescriptor(children)) {
} else if (type === 'string' || type === 'number' ||
ReactDescriptor.isValidDescriptor(children)) {
callback(traverseContext, children, storageName, indexSoFar);
subtreeCount = 1;
} else {
if (type === 'object') {
invariant(
!children || children.nodeType !== 1,
'traverseAllChildren(...): Encountered an invalid child; DOM ' +
'elements are not valid children of React components.'
);
for (var key in children) {
if (children.hasOwnProperty(key)) {
subtreeCount += traverseAllChildrenImpl(
children[key],
(
nameSoFar + (nameSoFar ? SUBSEPARATOR : SEPARATOR) +
wrapUserProvidedKey(key) + SUBSEPARATOR +
getComponentKey(children[key], 0)
),
indexSoFar + subtreeCount,
callback,
traverseContext
);
}
} else if (type === 'object') {
invariant(
!children || children.nodeType !== 1,
'traverseAllChildren(...): Encountered an invalid child; DOM ' +
'elements are not valid children of React components.'
);
for (var key in children) {
if (children.hasOwnProperty(key)) {
var nextName = (
nameSoFar + (nameSoFar ? SUBSEPARATOR : SEPARATOR) +
wrapUserProvidedKey(key) + SUBSEPARATOR +
getComponentKey(children[key], 0)
);
var nextIndex = indexSoFar + subtreeCount;
subtreeCount += traverseAllChildrenImpl(
children[key],
nextName,
nextIndex,
callback,
traverseContext
);
}
} else if (type === 'string') {
var normalizedText = ReactTextComponent(children);
callback(traverseContext, normalizedText, storageName, indexSoFar);
subtreeCount += 1;
} else if (type === 'number') {
var normalizedNumber = ReactTextComponent('' + children);
callback(traverseContext, normalizedNumber, storageName, indexSoFar);
subtreeCount += 1;
}
}
}
Expand Down