From 01c77852406850ffc138874403ebe5830eb30390 Mon Sep 17 00:00:00 2001 From: Girish Sharma Date: Tue, 25 Mar 2014 20:43:44 +0530 Subject: [PATCH] Bug 913955 - Avoid race conditions and inserting in middle of text in inplace-editor, r=mratcliffe --- browser/devtools/shared/inplace-editor.js | 22 ++++++++++++++++--- ...s_property_completion_existing_property.js | 5 ++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/browser/devtools/shared/inplace-editor.js b/browser/devtools/shared/inplace-editor.js index c1771c635bc..45a6871e4e2 100644 --- a/browser/devtools/shared/inplace-editor.js +++ b/browser/devtools/shared/inplace-editor.js @@ -1008,6 +1008,11 @@ InplaceEditor.prototype = { * true if you don't want to automatically insert the first suggestion */ _maybeSuggestCompletion: function(aNoAutoInsert) { + // Input can be null in cases when you intantaneously switch out of it. + if (!this.input) { + return; + } + let preTimeoutQuery = this.input.value; // Since we are calling this method from a keypress event handler, the // |input.value| does not include currently typed character. Thus we perform // this method async. @@ -1019,10 +1024,12 @@ InplaceEditor.prototype = { if (this.contentType == CONTENT_TYPES.PLAIN_TEXT) { return; } - + if (!this.input) { + return; + } let input = this.input; - // Input can be null in cases when you intantaneously switch out of it. - if (!input) { + // The length of input.value should be increased by 1 + if (input.value.length - preTimeoutQuery.length > 1) { return; } let query = input.value.slice(0, input.selectionStart); @@ -1030,6 +1037,15 @@ InplaceEditor.prototype = { if (query == null) { return; } + // If nothing is selected and there is a non-space character after the + // cursor, do not autocomplete. + if (input.selectionStart == input.selectionEnd && + input.selectionStart < input.value.length && + input.value.slice(input.selectionStart)[0] != " ") { + // This emit is mainly to make the test flow simpler. + this.emit("after-suggest", "nothing to autocomplete"); + return; + } let list = []; if (this.contentType == CONTENT_TYPES.CSS_PROPERTY) { list = CSSPropertyList; diff --git a/browser/devtools/styleinspector/test/browser_bug893965_css_property_completion_existing_property.js b/browser/devtools/styleinspector/test/browser_bug893965_css_property_completion_existing_property.js index 74174279bef..4044d515636 100644 --- a/browser/devtools/styleinspector/test/browser_bug893965_css_property_completion_existing_property.js +++ b/browser/devtools/styleinspector/test/browser_bug893965_css_property_completion_existing_property.js @@ -48,6 +48,9 @@ let testData = [ ["VK_BACK_SPACE", "", -1, 0], ["f", "fill", 0, MAX_ENTRIES], ["i", "fill", 0, 4], + ["VK_LEFT", "fill", -1, 0], + ["VK_LEFT", "fill", -1, 0], + ["i", "fiill", -1, 0], ["VK_ESCAPE", null, -1, 0], ]; @@ -90,7 +93,7 @@ function checkStateAndMoveOn(index) { info("pressing key " + key + " to get result: [" + testData[index].slice(1) + "] for state " + state); - if (/(right|back_space|escape)/ig.test(key)) { + if (/(left|right|back_space|escape)/ig.test(key)) { info("added event listener for right|back_space|escape keys"); editor.input.addEventListener("keypress", function onKeypress() { if (editor.input) {