Skip to content

Commit 8f0a8a7

Browse files
yasushi.andomhevery
yasushi.ando
authored andcommitted
fix(input): treat <input> with no type as type="text"
make <input ng-model="myModel" /> acceptable
1 parent 6351681 commit 8f0a8a7

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

lib/core_dom/selector.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ DirectiveSelector directiveSelectorFactory(DirectiveMap directives) {
254254
String nodeName = element.tagName.toLowerCase();
255255
Map<String, String> attrs = {};
256256

257+
// Set default attribute
258+
if (nodeName == 'input' && !element.attributes.containsKey('type')) {
259+
element.attributes['type'] = 'text';
260+
}
261+
257262
// Select node
258263
partialSelection = elementSelector.selectNode(directiveRefs, partialSelection, element, nodeName);
259264

test/directive/ng_model_spec.dart

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,74 @@ describe('ng-model', () {
200200
}));
201201
});
202202

203+
describe('no type attribute', () {
204+
it('should be set "text" as default value for "type" attribute', inject(() {
205+
_.compile('<input ng-model="model">');
206+
_.rootScope.$digest();
207+
expect((_.rootElement as dom.InputElement).attributes['type']).toEqual('text');
208+
}));
209+
210+
it('should update input value from model', inject(() {
211+
_.compile('<input ng-model="model">');
212+
_.rootScope.$digest();
213+
214+
expect((_.rootElement as dom.InputElement).value).toEqual('');
215+
216+
_.rootScope.$apply('model = "misko"');
217+
expect((_.rootElement as dom.InputElement).value).toEqual('misko');
218+
}));
219+
220+
it('should render null as the empty string', inject(() {
221+
_.compile('<input ng-model="model">');
222+
_.rootScope.$digest();
223+
224+
expect((_.rootElement as dom.InputElement).value).toEqual('');
225+
226+
_.rootScope.$apply('model = null');
227+
expect((_.rootElement as dom.InputElement).value).toEqual('');
228+
}));
229+
230+
it('should update model from the input value', inject(() {
231+
_.compile('<input ng-model="model" probe="p">');
232+
Probe probe = _.rootScope.p;
233+
var ngModel = probe.directive(NgModel);
234+
InputElement inputElement = probe.element;
235+
236+
inputElement.value = 'abc';
237+
_.triggerEvent(inputElement, 'change');
238+
expect(_.rootScope.model).toEqual('abc');
239+
240+
inputElement.value = 'def';
241+
var input = probe.directive(InputTextLikeDirective);
242+
input.processValue();
243+
expect(_.rootScope.model).toEqual('def');
244+
}));
245+
246+
it('should write to input only if value is different', inject(() {
247+
var scope = _.rootScope;
248+
var element = new dom.InputElement();
249+
var model = new NgModel(scope, new NodeAttrs(new DivElement()), element, new NgNullForm());
250+
dom.querySelector('body').append(element);
251+
var input = new InputTextLikeDirective(element, model, scope);
252+
253+
element.value = 'abc';
254+
element.selectionStart = 1;
255+
element.selectionEnd = 2;
256+
257+
model.render('abc');
258+
259+
expect(element.value).toEqual('abc');
260+
expect(element.selectionStart).toEqual(1);
261+
expect(element.selectionEnd).toEqual(2);
262+
263+
model.render('xyz');
264+
265+
expect(element.value).toEqual('xyz');
266+
expect(element.selectionStart).toEqual(3);
267+
expect(element.selectionEnd).toEqual(3);
268+
}));
269+
});
270+
203271
describe('type="checkbox"', () {
204272
it('should update input value from model', inject((Scope scope) {
205273
var element = _.compile('<input type="checkbox" ng-model="model">');

0 commit comments

Comments
 (0)