Skip to content

Commit 11c3227

Browse files
committed
Fix infinite recursion (bug unfixed - see #131)
1 parent 196aa8e commit 11c3227

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

plugins/auto-close-brackets.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ codeInput.plugins.AutoCloseBrackets = class extends codeInput.Plugin {
1919

2020
/* Add keystroke events */
2121
afterElementsAdded(codeInput) {
22-
codeInput.textareaElement.addEventListener('keydown', (event) => { this.checkBackspace(codeInput, event) });
22+
codeInput.pluginData.autoCloseBrackets = { automatedKeypresses: false};
23+
codeInput.textareaElement.addEventListener('keydown', (event) => { this.checkBackspace(codeInput, event); });
2324
codeInput.textareaElement.addEventListener('beforeinput', (event) => { this.checkBrackets(codeInput, event); });
2425
}
2526

2627
/* Deal with the automatic creation of closing bracket when opening brackets are typed, and the ability to "retype" a closing
2728
bracket where one has already been placed. */
2829
checkBrackets(codeInput, event) {
30+
if(codeInput.pluginData.autoCloseBrackets.automatedKeypresses) return;
2931
if(event.data == codeInput.textareaElement.value[codeInput.textareaElement.selectionStart]) {
3032
// Check if a closing bracket is typed
3133
for(let openingBracket in this.bracketPairs) {
@@ -41,14 +43,20 @@ codeInput.plugins.AutoCloseBrackets = class extends codeInput.Plugin {
4143
// Opening bracket typed; Create bracket pair
4244
let closingBracket = this.bracketPairs[event.data];
4345
// Insert the closing bracket
46+
// automatedKeypresses property to prevent keypresses being captured
47+
// by this plugin during automated input as some browsers
48+
// (e.g. GNOME Web) do.
49+
codeInput.pluginData.autoCloseBrackets.automatedKeypresses = true;
4450
document.execCommand("insertText", false, closingBracket);
51+
codeInput.pluginData.autoCloseBrackets.automatedKeypresses = false;
4552
// Move caret before the inserted closing bracket
4653
codeInput.textareaElement.selectionStart = codeInput.textareaElement.selectionEnd -= 1;
4754
}
4855
}
4956

5057
/* Deal with cases where a backspace deleting an opening bracket deletes the closing bracket straight after it as well */
5158
checkBackspace(codeInput, event) {
59+
if(codeInput.pluginData.autoCloseBrackets.automatedKeypresses) return;
5260
if(event.key == "Backspace" && codeInput.textareaElement.selectionStart == codeInput.textareaElement.selectionEnd) {
5361
let closingBracket = this.bracketPairs[codeInput.textareaElement.value[codeInput.textareaElement.selectionStart-1]];
5462
if(closingBracket != undefined && codeInput.textareaElement.value[codeInput.textareaElement.selectionStart] == closingBracket) {
@@ -58,4 +66,4 @@ codeInput.plugins.AutoCloseBrackets = class extends codeInput.Plugin {
5866
}
5967
}
6068
}
61-
}
69+
}

plugins/indent.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@ codeInput.plugins.Indent = class extends codeInput.Plugin {
8181
let indentationWidthPx = testIndentationWidthSpan.offsetWidth;
8282
codeInput.removeChild(testIndentationWidthPre);
8383

84-
codeInput.pluginData.indent = {indentationWidthPx: indentationWidthPx};
84+
codeInput.pluginData.indent = {automatedKeypresses: false, indentationWidthPx: indentationWidthPx};
8585
}
8686

8787
/* Deal with the Tab key causing indentation, and Tab+Selection indenting / Shift+Tab+Selection unindenting lines, and the mechanism through which Tab can be used to switch focus instead (accessibility). */
8888
checkTab(codeInput, event) {
89+
if(codeInput.pluginData.indent.automatedKeypresses) return;
8990
if(!this.tabIndentationEnabled) return;
9091
if(this.escTabToChangeFocus) {
9192
// Accessibility - allow Tab for keyboard navigation when Esc pressed right before it.
@@ -116,7 +117,12 @@ codeInput.plugins.Indent = class extends codeInput.Plugin {
116117

117118
if(!event.shiftKey && inputElement.selectionStart == inputElement.selectionEnd) {
118119
// Just place a tab/spaces here.
120+
// automatedKeypresses property to prevent keypresses being captured
121+
// by this plugin during automated input as some browsers
122+
// (e.g. GNOME Web) do.
123+
codeInput.pluginData.indent.automatedKeypresses = true;
119124
document.execCommand("insertText", false, this.indentation);
125+
codeInput.pluginData.indent.automatedKeypresses = false;
120126

121127
} else {
122128
let lines = inputElement.value.split("\n");
@@ -147,7 +153,12 @@ codeInput.plugins.Indent = class extends codeInput.Plugin {
147153
// Add tab at start
148154
inputElement.selectionStart = letterI;
149155
inputElement.selectionEnd = letterI;
156+
// automatedKeypresses property to prevent keypresses being captured
157+
// by this plugin during automated input as some browsers
158+
// (e.g. GNOME Web) do.
159+
codeInput.pluginData.indent.f = true;
150160
document.execCommand("insertText", false, this.indentation);
161+
codeInput.pluginData.indent.automatedKeypresses = false;
151162

152163
// Change selection
153164
if(selectionStartI > letterI) { // Indented outside selection
@@ -191,6 +202,7 @@ codeInput.plugins.Indent = class extends codeInput.Plugin {
191202

192203
/* Deal with new lines retaining indentation */
193204
checkEnter(codeInput, event) {
205+
if(codeInput.pluginData.indent.automatedKeypresses) return;
194206
if(event.key != "Enter") {
195207
return;
196208
}
@@ -270,11 +282,16 @@ codeInput.plugins.Indent = class extends codeInput.Plugin {
270282
// save the current cursor position
271283
let selectionStartI = inputElement.selectionStart;
272284

285+
// automatedKeypresses property to prevent keypresses being captured
286+
// by this plugin during automated input as some browsers
287+
// (e.g. GNOME Web) do.
288+
codeInput.pluginData.indent.automatedKeypresses = true;
273289
if(bracketThreeLinesTriggered) {
274290
document.execCommand("insertText", false, "\n" + furtherIndentation); // Write indented line
275291
numberIndents += 1; // Reflects the new indent
276292
}
277293
document.execCommand("insertText", false, "\n" + newLine); // Write new line, including auto-indentation
294+
codeInput.pluginData.indent.automatedKeypresses = false;
278295

279296
// move cursor to new position
280297
inputElement.selectionStart = selectionStartI + numberIndents*this.indentationNumChars + 1; // count the indent level and the newline character
@@ -294,6 +311,7 @@ codeInput.plugins.Indent = class extends codeInput.Plugin {
294311

295312
/* Deal with one 'tab' of spaces-based-indentation being deleted by each backspace, rather than one space */
296313
checkBackspace(codeInput, event) {
314+
if(codeInput.pluginData.indent.automatedKeypresses) return;
297315
if(event.key != "Backspace" || this.indentationNumChars == 1) {
298316
return; // Normal backspace when indentation of 1
299317
}

0 commit comments

Comments
 (0)