Bug 713612 - add closing curly bracket to avoid disrupting the rest of the CSS. r=rcampbell

This commit is contained in:
Cedric Vivier 2012-01-25 03:04:00 +01:00
parent 1ad43e3919
commit 4515a46e4e
2 changed files with 53 additions and 2 deletions

View File

@ -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);
}

View File

@ -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) ;
},