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 4fb39ce commit 42211a9
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 @@ -51,6 +51,7 @@ var registrationNameModules = ReactBrowserEventEmitter.registrationNameModules;
var CONTENT_TYPES = {'string': true, 'number': true};

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

var ELEMENT_NODE_TYPE = 1;

Expand Down Expand Up @@ -688,9 +689,11 @@ ReactDOMComponent.Mixin = {
}
propValue = CSSPropertyOperations.createMarkupForStyles(propValue);
}
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 @@ -970,6 +973,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 @@ -198,6 +198,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 42211a9

Please sign in to comment.