Skip to content

Commit

Permalink
[dom] Normalize setting a node value to null
Browse files Browse the repository at this point in the history
When set a node value to `null`, then a node value returns `"null"` as string
on the legacy IE (IE <= 9). A node value returns an empty string on the modern
browsers (Chrome, Firefox, IE >= 10). This change is to normalize the results
of setting a node value to `null` on any browsers. Fix yui#1603.
  • Loading branch information
okuryu committed Feb 5, 2014
1 parent 3d336a7 commit 408cb79
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/dom/HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ DOM Change History
@VERSION@
------

* [#1603][]: Set a node value to an empty string setting to null. [Ryuichi Okumura]
* [#1469][]: Fix a bug with setStyle() cannot set an opacity to 1. [Ryuichi Okumura]

[#1603]: https://github.com/yui/yui3/issues/1603
[#1469]: https://github.com/yui/yui3/issues/1469

3.14.1
Expand Down
2 changes: 1 addition & 1 deletion src/dom/js/dom-attrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ Y.mix(Y_DOM, {

if (node && node[TAG_NAME]) {
setter = Y_DOM.VALUE_SETTERS[node[TAG_NAME].toLowerCase()];

val = (val === null) ? '' : val;
if (setter) {
setter(node, val);
} else {
Expand Down
39 changes: 39 additions & 0 deletions src/dom/tests/unit/assets/dom-core-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,16 @@ YUI.add('dom-core-test', function(Y) {
node.removeAttribute('value')
},

'value updated from null should be empty string': function() {
var node = document.getElementById('test-text-no-value'),
val = null;

Y.DOM.setValue(node, val);
Assert.areEqual('', Y.DOM.getValue(node));
Y.DOM.setValue(node, '');
node.removeAttribute('value');
},

'textarea from html value should match new value': function() {
var node = document.getElementById('test-textarea-text-value'),
val = 'new textarea test';
Expand All @@ -1436,6 +1446,15 @@ YUI.add('dom-core-test', function(Y) {
Y.DOM.setValue('');
},

'textarea from null should be empty string': function() {
var node = document.getElementById('test-textarea-no-value'),
val = null;

Y.DOM.setValue(node, val);
Assert.areEqual('', Y.DOM.getValue(node));
Y.DOM.setValue('');
},

'button value should match updated value': function() {
var node = document.getElementById('test-button-value'),
val = 'new value';
Expand Down Expand Up @@ -1464,6 +1483,16 @@ YUI.add('dom-core-test', function(Y) {
node.removeAttribute('value')
},

'button value updated from null should be empty string': function() {
var node = document.getElementById('test-button-no-value'),
val = null;

Y.DOM.setValue(node, val);
Assert.areEqual('', Y.DOM.getValue(node));
Y.DOM.setValue(node, '');
node.removeAttribute('value');
},

'option value should match updated value': function() {
var node = document.getElementById('test-option-value'),
val = 'new value';
Expand Down Expand Up @@ -1499,6 +1528,16 @@ YUI.add('dom-core-test', function(Y) {
Assert.areEqual(val, Y.DOM.getValue(node));
Y.DOM.setValue(node, '');
node.removeAttribute('value')
},

'option value updated from null should be null': function() {
var node = document.getElementById('test-option-no-value'),
val = null;

Y.DOM.setValue(node, val);
Assert.areEqual('', Y.DOM.getValue(node));
Y.DOM.setValue(node, '');
node.removeAttribute('value');
}
}));

Expand Down

0 comments on commit 408cb79

Please sign in to comment.