Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 70489c2

Browse files
devversionThomasBurleson
authored andcommitted
fix(list): fix converted space when target is content editable.
As this is a feature which is depending on the browsers behaviour, we need to dispatch native events to the editable element, otherwise the test won't fail when the `editableContent` check is removed. Fixes #5406 Closes #7161
1 parent ad019c8 commit 70489c2

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/components/list/list.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ function mdListItemDirective($mdAria, $mdConstant, $mdUtil, $timeout) {
282282

283283
if (!hasClick && !proxies.length) {
284284
firstChild && firstChild.addEventListener('keypress', function(e) {
285-
if (e.target.nodeName != 'INPUT' && e.target.nodeName != 'TEXTAREA') {
285+
if (e.target.nodeName != 'INPUT' && e.target.nodeName != 'TEXTAREA' && !e.target.isContentEditable) {
286286
var keyCode = e.which || e.keyCode;
287287
if (keyCode == $mdConstant.KEY_CODE.SPACE) {
288288
if (firstChild) {

src/components/list/list.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,30 @@ describe('mdListItem directive', function() {
8888
expect($rootScope.modelVal).toBeFalsy();
8989
}));
9090

91+
it('should not convert spacebar keypress for editable elements', inject(function($mdConstant) {
92+
var listItem = setup('<md-list-item><div contenteditable="true"></div></md-list-item>');
93+
var editableEl = listItem.find('div');
94+
var onClickSpy = jasmine.createSpy('onClickSpy');
95+
96+
// We need to append our element to the DOM because the browser won't detect `contentEditable` when the element
97+
// is hidden in the DOM. See the related issue for chromium:
98+
// https://code.google.com/p/chromium/issues/detail?id=313082
99+
document.body.appendChild(listItem[0]);
100+
101+
editableEl.on('click', onClickSpy);
102+
103+
// We need to dispatch the keypress natively, because otherwise the `keypress` won't be triggered in the list.
104+
var event = document.createEvent('Event');
105+
event.keyCode = $mdConstant.KEY_CODE.SPACE;
106+
event.initEvent('keypress', true, true);
107+
108+
editableEl[0].dispatchEvent(event);
109+
110+
expect(onClickSpy).not.toHaveBeenCalled();
111+
112+
document.body.removeChild(listItem[0]);
113+
}));
114+
91115
xit('should not convert spacebar keypress for text inputs', inject(function($mdConstant) {
92116

93117
var listItem = setup('<md-list-item><input ng-keypress="pressed = true" type="text"></md-list-item>');

0 commit comments

Comments
 (0)