Skip to content

Commit 8656326

Browse files
adrienvergevalorkin
authored andcommitted
fix(typeahead): Fix crash on Firefox and contenteditable input (#2057)
When using the typeahead directive on a `contenteditable` input, crashes can happen on Firefox: TypeError n.target.innerText is undefined According to many sources [1] [2] [3], `innerText` should only be used on old browsers where `textContent` in not defined (e.g. Internet Explorer). On newer browsers that abide by standards, `textContent` is the right property to use to avoid problems. [1]: https://stackoverflow.com/a/22990890 [2]: https://teamtreehouse.com/community/firefox-innertext-undefined-teachers-note [3]: https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent
1 parent 2bf9ad8 commit 8656326

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/typeahead/typeahead.directive.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,15 @@ export class TypeaheadDirective implements OnInit, OnDestroy {
118118
}
119119

120120
// For `<input>`s, use the `value` property. For others that don't have a
121-
// `value` (such as `<span contenteditable="true">`, use `innerText`.
121+
// `value` (such as `<span contenteditable="true">`), use either
122+
// `textContent` or `innerText` (depending on which one is supported, i.e.
123+
// Firefox or IE).
122124
const value = e.target.value !== undefined
123125
? e.target.value
124-
: e.target.innerText;
125-
if (value.trim().length >= this.typeaheadMinLength) {
126+
: e.target.textContent !== undefined
127+
? e.target.textContent
128+
: e.target.innerText;
129+
if (value && value.trim().length >= this.typeaheadMinLength) {
126130
this.typeaheadLoading.emit(true);
127131
this.keyUpEventEmitter.emit(e.target.value);
128132
} else {

0 commit comments

Comments
 (0)