update to set value property with empty string; add test; update ReactDOMComponent test
everdimension committed
Mar 10, 2016 at 20:48 UTC
28a97dbd74e3ab7a87786e8b4a3e8cea63579977
3 files changed
+13
-8
src/renderers/dom/shared/DOMPropertyOperations.js
+3
-6
@@ -174,16 +174,13 @@ var DOMPropertyOperations = {
174
var propName = propertyInfo.propertyName;
175
// Must explicitly cast values for HAS_SIDE_EFFECTS-properties to the
176
// property type before comparing; only `value` does and is string.
177
+ // Must set `value` property if it is not null and not yet set.
178
if (!propertyInfo.hasSideEffects ||
178
- ('' + node[propName]) !== ('' + value)) {
179
+ ('' + node[propName]) !== ('' + value) ||
180
+ !node.hasAttribute(propertyInfo.attributeName)) {
181
// Contrary to `setAttribute`, object properties are properly
182
// `toString`ed by IE8/9.
183
node[propName] = value;
182
- } else if (node.nodeName && node.nodeName.toLowerCase() === 'option' &&
183
- !node.hasAttribute('value')) {
184
- // set empty "value" attribute to OPTION element
185
- // if it is provided, but not yet set
186
- node[propName] = value;
184
}
185
} else {
186
var attributeName = propertyInfo.attributeName;
src/renderers/dom/shared/__tests__/DOMPropertyOperations-test.js
+8
@@ -221,6 +221,14 @@ describe('DOMPropertyOperations', function() {
221
expect(stubNode.getAttribute('role')).toBe('<html>');
222
});
223
224
+ it('should not remove empty attributes for special properties', function() {
225
+ stubNode = document.createElement('input');
226
+ DOMPropertyOperations.setValueForProperty(stubNode, 'value', '');
227
+ // JSDOM does not behave correctly for attributes/properties
228
+ //expect(stubNode.getAttribute('value')).toBe('');
229
+ expect(stubNode.value).toBe('');
230
+ });
231
+
232
it('should remove for falsey boolean properties', function() {
233
DOMPropertyOperations.setValueForProperty(
234
stubNode,
src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js
+2
-2
@@ -763,10 +763,10 @@ describe('ReactDOMComponent', function() {
763
expect(nodeValueSetter.mock.calls.length).toBe(2);
764
765
ReactDOM.render(<div value="" />, container);
766
- expect(nodeValueSetter.mock.calls.length).toBe(2);
766
+ expect(nodeValueSetter.mock.calls.length).toBe(3);
767
768
ReactDOM.render(<div />, container);
769
- expect(nodeValueSetter.mock.calls.length).toBe(2);
769
+ expect(nodeValueSetter.mock.calls.length).toBe(3);
770
});
771
772
it('should not incur unnecessary DOM mutations for boolean properties', function() {