From 4515a46e4eda848037bbcf876b589b487513abca Mon Sep 17 00:00:00 2001 From: Cedric Vivier Date: Wed, 25 Jan 2012 03:04:00 +0100 Subject: [PATCH] Bug 713612 - add closing curly bracket to avoid disrupting the rest of the CSS. r=rcampbell --- browser/devtools/styleeditor/StyleEditor.jsm | 47 +++++++++++++++++++ .../test/browser_styleeditor_new.js | 8 +++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/browser/devtools/styleeditor/StyleEditor.jsm b/browser/devtools/styleeditor/StyleEditor.jsm index 092e2bd8983..06cf249ae9d 100644 --- a/browser/devtools/styleeditor/StyleEditor.jsm +++ b/browser/devtools/styleeditor/StyleEditor.jsm @@ -229,6 +229,8 @@ StyleEditor.prototype = { }; sourceEditor.init(aElement, config, function onSourceEditorReady() { + setupBracketCompletion(sourceEditor); + sourceEditor.addEventListener(SourceEditor.EVENTS.TEXT_CHANGED, function onTextChanged(aEvent) { this.updateStyleSheet(); @@ -1132,3 +1134,48 @@ function repeat(aText, aCount) { return (new Array(aCount + 1)).join(aText); } + +/** + * Set up bracket completion on a given SourceEditor. + * This automatically closes the following CSS brackets: "{", "(", "[" + * + * @param SourceEditor aSourceEditor + */ +function setupBracketCompletion(aSourceEditor) +{ + let editorElement = aSourceEditor.editorElement; + let pairs = { + 123: { // { + closeString: "}", + closeKeyCode: Ci.nsIDOMKeyEvent.DOM_VK_CLOSE_BRACKET + }, + 40: { // ( + closeString: ")", + closeKeyCode: Ci.nsIDOMKeyEvent.DOM_VK_0 + }, + 91: { // [ + closeString: "]", + closeKeyCode: Ci.nsIDOMKeyEvent.DOM_VK_CLOSE_BRACKET + }, + }; + + editorElement.addEventListener("keypress", function onKeyPress(aEvent) { + let pair = pairs[aEvent.charCode]; + if (!pair) { + return true; + } + + // We detected an open bracket, sending closing character + let keyCode = pair.closeKeyCode; + let charCode = pair.closeString.charCodeAt(0); + let modifiers = 0; + let utils = editorElement.ownerDocument.defaultView. + QueryInterface(Ci.nsIInterfaceRequestor). + getInterface(Ci.nsIDOMWindowUtils); + let handled = utils.sendKeyEvent("keydown", keyCode, 0, modifiers); + utils.sendKeyEvent("keypress", 0, charCode, modifiers, !handled); + utils.sendKeyEvent("keyup", keyCode, 0, modifiers); + // and rewind caret + aSourceEditor.setCaretOffset(aSourceEditor.getCaretOffset() - 1); + }, false); +} diff --git a/browser/devtools/styleeditor/test/browser_styleeditor_new.js b/browser/devtools/styleeditor/test/browser_styleeditor_new.js index c40215d0bdb..fea602970b6 100644 --- a/browser/devtools/styleeditor/test/browser_styleeditor_new.js +++ b/browser/devtools/styleeditor/test/browser_styleeditor_new.js @@ -5,7 +5,7 @@ const TESTCASE_URI = TEST_BASE + "simple.html"; const TRANSITION_CLASS = "moz-styleeditor-transitioning"; - +const TESTCASE_CSS_SOURCE = "body{background-color:red;"; function test() { @@ -79,9 +79,13 @@ function testEditorAdded(aChrome, aEditor) is(computedStyle.backgroundColor, "rgb(255, 255, 255)", "content's background color is initially white"); - for each (let c in "body{background-color:red;}") { + for each (let c in TESTCASE_CSS_SOURCE) { EventUtils.synthesizeKey(c, {}, gChromeWindow); } + + is(aEditor.sourceEditor.getText(), TESTCASE_CSS_SOURCE + "}", + "rule bracket has been auto-closed"); + }, gChromeWindow) ; },