Skip to content

Commit

Permalink
Should not coerce children prop on custom elements to a string. Fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
jimfb committed Oct 30, 2015
1 parent 7619214 commit 3769cb3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/renderers/dom/shared/ReactDOMComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var registrationNameModules = EventPluginRegistry.registrationNameModules;
// For quickly matching children type, to test if can be treated as content.
var CONTENT_TYPES = {'string': true, 'number': true};

var CHILDREN = keyOf({children: null});
var STYLE = keyOf({style: null});
var HTML = keyOf({__html: null});

Expand Down Expand Up @@ -742,9 +743,11 @@ ReactDOMComponent.Mixin = {
}
propValue = CSSPropertyOperations.createMarkupForStyles(propValue, this);
}
var markup = null;
var markup = '';
if (this._tag != null && isCustomComponent(this._tag, props)) {
markup = DOMPropertyOperations.createMarkupForCustomAttribute(propKey, propValue);
if (propKey !== CHILDREN || typeof propValue === 'string' || typeof propValue === 'number') {
markup = DOMPropertyOperations.createMarkupForCustomAttribute(propKey, propValue);
}
} else {
markup = DOMPropertyOperations.createMarkupForProperty(propKey, propValue);
}
Expand Down Expand Up @@ -1015,6 +1018,9 @@ ReactDOMComponent.Mixin = {
deleteListener(this._rootNodeID, propKey);
}
} else if (isCustomComponent(this._tag, nextProps)) {
if (propKey === CHILDREN && typeof nextProp !== 'string' && typeof nextProp !== 'number') {
nextProp = null;
}
DOMPropertyOperations.setValueForAttribute(
getNode(this),
propKey,
Expand Down
18 changes: 18 additions & 0 deletions src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,24 @@ describe('ReactDOMComponent', function() {
expect(stubStyle.display).toEqual('');
});

it('should skip child object attribute on web components', function() {
var container = document.createElement('div');

// Test intial render to null
ReactDOM.render(<my-component children={['foo']} />, container);
expect(container.firstChild.hasAttribute('children')).toBe(false);

ReactDOM.render(<my-component children={'foo'} />, container);
expect(container.firstChild.hasAttribute('children')).toBe(true);

ReactDOM.render(<my-component children={5} />, container);
expect(container.firstChild.hasAttribute('children')).toBe(true);

// Test updates to null
ReactDOM.render(<my-component children={['foo']} />, container);
expect(container.firstChild.hasAttribute('children')).toBe(false);
});

it('should remove attributes', function() {
var container = document.createElement('div');
ReactDOM.render(<img height="17" />, container);
Expand Down

0 comments on commit 3769cb3

Please sign in to comment.