diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-02-13 20:42:25 -0500 |
---|---|---|
committer | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-02-13 20:50:31 -0500 |
commit | 483f7401b749a38673e2abbdbb9d8d16c889d075 (patch) | |
tree | 0403205031a4b59dfea761d2b8403ee28709ef10 /ext | |
parent | db1da33321a2517cc230c78f2b5c1ce9532aca0c (diff) |
Validate bounds of Display.definitions before using
Diffstat (limited to 'ext')
-rw-r--r-- | ext/mixed/js/display.js | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 5b868df3..00f6d512 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -179,13 +179,15 @@ class Display { e.preventDefault(); const link = e.currentTarget; const entry = link.closest('.entry'); - const definitionIndex = this.entryIndexFind(entry); + const index = this.entryIndexFind(entry); + if (index < 0 || index >= this.definitions.length) { return; } + const expressionIndex = Display.indexOf(entry.querySelectorAll('.term-expression .action-play-audio'), link); this.audioPlay( - this.definitions[definitionIndex], + this.definitions[index], // expressionIndex is used in audioPlay to detect result output mode Math.max(expressionIndex, this.options.general.resultOutputMode === 'merge' ? 0 : -1), - definitionIndex + index ); } @@ -193,6 +195,8 @@ class Display { e.preventDefault(); const link = e.currentTarget; const index = this.entryIndexFind(link); + if (index < 0 || index >= this.definitions.length) { return; } + this.noteAdd(this.definitions[index], link.dataset.mode); } @@ -512,6 +516,8 @@ class Display { } autoPlayAudio() { + if (this.definitions.length === 0) { return; } + this.audioPlay(this.definitions[0], this.firstExpressionIndex, 0); } @@ -611,9 +617,12 @@ class Display { } noteTryAdd(mode) { - const button = this.adderButtonFind(this.index, mode); + const index = this.index; + if (index < 0 || index >= this.definitions.length) { return; } + + const button = this.adderButtonFind(index, mode); if (button !== null && !button.classList.contains('disabled')) { - this.noteAdd(this.definitions[this.index], mode); + this.noteAdd(this.definitions[index], mode); } } @@ -915,9 +924,12 @@ Display._onKeyDownHandlers = new Map([ ['P', (self, e) => { if (e.altKey) { - const entry = self.getEntry(self.index); + const index = self.index; + if (index < 0 || index >= self.definitions.length) { return; } + + const entry = self.getEntry(index); if (entry !== null && entry.dataset.type === 'term') { - self.audioPlay(self.definitions[self.index], self.firstExpressionIndex, self.index); + self.audioPlay(self.definitions[index], self.firstExpressionIndex, index); } return true; } |