From 7681c69e2e20798529efb448c1a0d2dda1cbb2b5 Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Fri, 20 Mar 2015 20:51:22 -0400 Subject: [PATCH] [BUGFIX beta] Undefined doesn't set an attribute via attributeBindings. --- .../tests/helpers/bind_attr_test.js | 4 +- .../tests/helpers/input_test.js | 38 +++++++++++++++++++ .../ember-views/lib/attr_nodes/attr_node.js | 6 +++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/packages/ember-htmlbars/tests/helpers/bind_attr_test.js b/packages/ember-htmlbars/tests/helpers/bind_attr_test.js index 20edc33f2b0..56e951700d3 100644 --- a/packages/ember-htmlbars/tests/helpers/bind_attr_test.js +++ b/packages/ember-htmlbars/tests/helpers/bind_attr_test.js @@ -584,7 +584,7 @@ QUnit.test("src attribute bound to undefined is not present", function() { runAppend(view); - ok(!view.element.hasAttribute('src'), "src attribute not present"); + ok(!view.element.firstChild.hasAttribute('src'), "src attribute not present"); }); QUnit.test("src attribute bound to null is not present", function() { @@ -597,7 +597,7 @@ QUnit.test("src attribute bound to null is not present", function() { runAppend(view); - ok(!view.element.hasAttribute('src'), "src attribute not present"); + ok(!view.element.firstChild.hasAttribute('src'), "src attribute not present"); }); QUnit.test('specifying `
` is [DEPRECATED]', function() { diff --git a/packages/ember-htmlbars/tests/helpers/input_test.js b/packages/ember-htmlbars/tests/helpers/input_test.js index 266af06d3fa..764c162edf7 100644 --- a/packages/ember-htmlbars/tests/helpers/input_test.js +++ b/packages/ember-htmlbars/tests/helpers/input_test.js @@ -350,3 +350,41 @@ QUnit.test("checkbox name is updated", function() { QUnit.test("checkbox checked property is updated", function() { equal(view.$('input').prop('checked'), false, "the checkbox isn't checked yet"); }); + +QUnit.module("{{input type='text'}} - null/undefined values", { + teardown() { + runDestroy(view); + } +}); + +QUnit.test("placeholder attribute bound to undefined is not present", function() { + view = View.extend({ + controller: {}, + template: compile('{{input placeholder=someThingNotThere}}') + }).create(); + + runAppend(view); + + ok(!view.element.childNodes[1].hasAttribute('placeholder'), "attribute not present"); + + run(null, set, view, 'controller.someThingNotThere', 'foo'); + + equal(view.element.childNodes[1].placeholder, 'foo', "attribute is present"); +}); + +QUnit.test("placeholder attribute bound to null is not present", function() { + view = View.extend({ + controller: { + someNullProperty: null + }, + template: compile('{{input placeholder=someNullProperty}}') + }).create(); + + runAppend(view); + + ok(!view.element.childNodes[1].hasAttribute('placeholder'), "attribute not present"); + + run(null, set, view, 'controller.someNullProperty', 'foo'); + + equal(view.element.childNodes[1].placeholder, 'foo', "attribute is present"); +}); diff --git a/packages/ember-views/lib/attr_nodes/attr_node.js b/packages/ember-views/lib/attr_nodes/attr_node.js index 60d53f81269..dd1ad181107 100644 --- a/packages/ember-views/lib/attr_nodes/attr_node.js +++ b/packages/ember-views/lib/attr_nodes/attr_node.js @@ -49,12 +49,18 @@ AttrNode.prototype.render = function render(buffer) { if (this.isDestroying) { return; } + var value = read(this.attrValue); if (this.attrName === 'value' && (value === null || value === undefined)) { value = ''; } + if (value === undefined) { + value = null; + } + + // If user is typing in a value we don't want to rerender and loose cursor position. if (this.hasRenderedInitially && this.attrName === 'value' && this._morph.element.value === value) { this.lastValue = value;