Bug 650345 - Implement minimal UI for Find in the Source Editor. r=rcampbell

This commit is contained in:
Mihai Sucan
2012-01-16 18:51:44 +02:00
parent b341a509ed
commit c7c4670445
15 changed files with 885 additions and 27 deletions

View File

@@ -12,4 +12,5 @@ browser.jar:
content/browser/orion.js (sourceeditor/orion/orion.js)
content/browser/orion.css (sourceeditor/orion/orion.css)
content/browser/orion-mozilla.css (sourceeditor/orion/mozilla.css)
content/browser/source-editor-overlay.xul (sourceeditor/source-editor-overlay.xul)

View File

@@ -45,6 +45,7 @@
]>
<?xml-stylesheet href="chrome://global/skin/global.css" type="text/css"?>
<?xul-overlay href="chrome://global/content/editMenuOverlay.xul"?>
<?xul-overlay href="chrome://browser/content/source-editor-overlay.xul"?>
<window id="main-window"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
@@ -58,6 +59,7 @@
<script type="application/javascript" src="chrome://browser/content/scratchpad.js"/>
<commandset id="editMenuCommands"/>
<commandset id="sourceEditorCommands"/>
<commandset id="sp-commandset">
<command id="sp-cmd-newWindow" oncommand="Scratchpad.openScratchpad();"/>
@@ -142,6 +144,28 @@
key="&webConsoleCmd.commandkey;"
command="sp-cmd-webConsole"
modifiers="accel,shift"/>
<key id="key_find"
key="&findCmd.key;"
command="cmd_find"
modifiers="accel"/>
#ifdef XP_MACOSX
<key id="key_findAgain"
key="&findAgainCmd.key;"
command="cmd_findAgain"
modifiers="accel"/>
<key id="key_findPrevious"
key="&findPreviousCmd.key;"
command="cmd_findPrevious"
modifiers="accel,shift"/>
#else
<key id="key_findAgain"
keycode="VK_F3"
command="cmd_findAgain"/>
<key id="key_findPrevious"
keycode="VK_F3"
command="cmd_findPrevious"
modifiers="shift"/>
#endif
</keyset>
@@ -223,23 +247,17 @@
key="key_selectAll"
accesskey="&selectAllCmd.accesskey;"
command="cmd_selectAll"/>
<!-- TODO: bug 650345 - implement search and replace
<menuseparator/>
<menuitem id="sp-menu-find"
label="&findOnCmd.label;"
accesskey="&findOnCmd.accesskey;"
label="&findCmd.label;"
accesskey="&findCmd.accesskey;"
key="key_find"
disabled="true"
command="cmd_find"/>
<menuitem id="sp-menu-findAgain"
label="&findAgainCmd.label;"
accesskey="&findAgainCmd.accesskey;"
key="key_findAgain"
disabled="true"
command="cmd_findAgain"/>
<menuseparator id="sp-execute-separator"/>
-->
</menupopup>
</menu>

View File

@@ -62,6 +62,7 @@ _BROWSER_TEST_FILES = \
browser_scratchpad_bug_653427_confirm_close.js \
browser_scratchpad_bug684546_reset_undo.js \
browser_scratchpad_bug690552_display_outputs_errors.js \
browser_scratchpad_bug650345_find_ui.js \
libs:: $(_BROWSER_TEST_FILES)
$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/browser/$(relativesrcdir)

View File

@@ -0,0 +1,97 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
function test()
{
waitForExplicitFinish();
gBrowser.selectedTab = gBrowser.addTab();
gBrowser.selectedBrowser.addEventListener("load", function browserLoad() {
gBrowser.selectedBrowser.removeEventListener("load", browserLoad, true);
openScratchpad(runTests);
}, true);
content.location = "data:text/html,<p>test the Find feature in Scratchpad";
}
function runTests(aWindow, aScratchpad)
{
let editor = aScratchpad.editor;
let text = "foobar bug650345\nBug650345 bazbaz\nfoobar omg\ntest";
editor.setText(text);
let needle = "foobar";
editor.setSelection(0, needle.length);
let oldPrompt = Services.prompt;
Services.prompt = {
prompt: function() { return true; },
};
let findKey = "F";
info("test Ctrl/Cmd-" + findKey + " (find)");
EventUtils.synthesizeKey(findKey, {accelKey: true}, aWindow);
let selection = editor.getSelection();
let newIndex = text.indexOf(needle, needle.length);
is(selection.start, newIndex, "selection.start is correct");
is(selection.end, newIndex + needle.length, "selection.end is correct");
info("test cmd_find");
aWindow.goDoCommand("cmd_find");
selection = editor.getSelection();
is(selection.start, 0, "selection.start is correct");
is(selection.end, needle.length, "selection.end is correct");
let findNextKey = Services.appinfo.OS == "Darwin" ? "G" : "VK_F3";
let findNextKeyOptions = Services.appinfo.OS == "Darwin" ?
{accelKey: true} : {};
info("test " + findNextKey + " (findNext)");
EventUtils.synthesizeKey(findNextKey, findNextKeyOptions, aWindow);
selection = editor.getSelection();
is(selection.start, newIndex, "selection.start is correct");
is(selection.end, newIndex + needle.length, "selection.end is correct");
info("test cmd_findAgain");
aWindow.goDoCommand("cmd_findAgain");
selection = editor.getSelection();
is(selection.start, 0, "selection.start is correct");
is(selection.end, needle.length, "selection.end is correct");
let findPreviousKey = Services.appinfo.OS == "Darwin" ? "G" : "VK_F3";
let findPreviousKeyOptions = Services.appinfo.OS == "Darwin" ?
{accelKey: true, shiftKey: true} : {shiftKey: true};
info("test " + findPreviousKey + " (findPrevious)");
EventUtils.synthesizeKey(findPreviousKey, findPreviousKeyOptions, aWindow);
selection = editor.getSelection();
is(selection.start, newIndex, "selection.start is correct");
is(selection.end, newIndex + needle.length, "selection.end is correct");
info("test cmd_findPrevious");
aWindow.goDoCommand("cmd_findPrevious");
selection = editor.getSelection();
is(selection.start, 0, "selection.start is correct");
is(selection.end, needle.length, "selection.end is correct");
needle = "BAZbaz";
newIndex = text.toLowerCase().indexOf(needle.toLowerCase());
Services.prompt = {
prompt: function(aWindow, aTitle, aMessage, aValue) {
aValue.value = needle;
return true;
},
};
info("test Ctrl/Cmd-" + findKey + " (find) with a custom value");
EventUtils.synthesizeKey(findKey, {accelKey: true}, aWindow);
selection = editor.getSelection();
is(selection.start, newIndex, "selection.start is correct");
is(selection.end, newIndex + needle.length, "selection.end is correct");
Services.prompt = oldPrompt;
finish();
}

View File

@@ -52,6 +52,7 @@ EXTRA_JS_MODULES = \
source-editor.jsm \
source-editor-orion.jsm \
source-editor-textarea.jsm \
source-editor-ui.jsm \
$(NULL)
include $(topsrcdir)/config/rules.mk

View File

@@ -44,6 +44,7 @@ const Ci = Components.interfaces;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource:///modules/source-editor-ui.jsm");
XPCOMUtils.defineLazyServiceGetter(this, "clipboardHelper",
"@mozilla.org/widget/clipboardhelper;1",
@@ -126,6 +127,8 @@ function SourceEditor() {
Services.prefs.getBoolPref(SourceEditor.PREFS.EXPAND_TAB);
this._onOrionSelection = this._onOrionSelection.bind(this);
this.ui = new SourceEditorUI(this);
}
SourceEditor.prototype = {
@@ -143,6 +146,13 @@ SourceEditor.prototype = {
_tabSize: null,
_iframeWindow: null,
/**
* The Source Editor user interface manager.
* @type object
* An instance of the SourceEditorUI.
*/
ui: null,
/**
* The editor container element.
* @type nsIDOMElement
@@ -204,6 +214,7 @@ SourceEditor.prototype = {
this.parentElement = aElement;
this._config = aConfig;
this._onReadyCallback = aCallback;
this.ui.init();
},
/**
@@ -272,11 +283,21 @@ SourceEditor.prototype = {
this._dragAndDrop = new TextDND(this._view, this._undoStack);
this._view.setAction("undo", this.undo.bind(this));
this._view.setAction("redo", this.redo.bind(this));
this._view.setAction("tab", this._doTab.bind(this));
this._view.setAction("Unindent Lines", this._doUnindentLines.bind(this));
this._view.setAction("enter", this._doEnter.bind(this));
let actions = {
"undo": [this.undo, this],
"redo": [this.redo, this],
"tab": [this._doTab, this],
"Unindent Lines": [this._doUnindentLines, this],
"enter": [this._doEnter, this],
"Find...": [this.ui.find, this.ui],
"Find Next Occurrence": [this.ui.findNext, this.ui],
"Find Previous Occurrence": [this.ui.findPrevious, this.ui],
};
for (let name in actions) {
let action = actions[name];
this._view.setAction(name, action[0].bind(action[1]));
}
let keys = (config.keys || []).concat(DEFAULT_KEYBINDINGS);
keys.forEach(function(aKey) {
@@ -296,6 +317,7 @@ SourceEditor.prototype = {
*/
_onOrionLoad: function SE__onOrionLoad()
{
this.ui.onReady();
if (this._onReadyCallback) {
this._onReadyCallback(this);
this._onReadyCallback = null;
@@ -883,6 +905,9 @@ SourceEditor.prototype = {
this._onOrionSelection = null;
this._view.destroy();
this.ui.destroy();
this.ui = null;
this.parentElement.removeChild(this._iframe);
this.parentElement = null;
this._iframeWindow = null;
@@ -896,5 +921,6 @@ SourceEditor.prototype = {
this._view = null;
this._model = null;
this._config = null;
this._lastFind = null;
},
};

View File

@@ -0,0 +1,46 @@
<?xml version="1.0"?>
<!-- ***** BEGIN LICENSE BLOCK *****
- Version: MPL 1.1/GPL 2.0/LGPL 2.1
-
- The contents of this file are subject to the Mozilla Public License Version
- 1.1 (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
- http://www.mozilla.org/MPL/
-
- Software distributed under the License is distributed on an "AS IS" basis,
- WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- for the specific language governing rights and limitations under the
- License.
-
- The Original Code is Source Editor.
-
- The Initial Developer of the Original Code is
- The Mozilla Foundation.
- Portions created by the Initial Developer are Copyright (C) 2012
- the Initial Developer. All Rights Reserved.
-
- Contributor(s):
- Mihai Sucan <mihai.sucan@gmail.com> (original author)
-
- Alternatively, the contents of this file may be used under the terms of
- either the GNU General Public License Version 2 or later (the "GPL"), or
- the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
- in which case the provisions of the GPL or the LGPL are applicable instead
- of those above. If you wish to allow use of your version of this file only
- under the terms of either the GPL or the LGPL, and not to allow others to
- use your version of this file under the terms of the MPL, indicate your
- decision by deleting the provisions above and replace them with the notice
- and other provisions required by the GPL or the LGPL. If you do not delete
- the provisions above, a recipient may use your version of this file under
- the terms of any one of the MPL, the GPL or the LGPL.
-
- ***** END LICENSE BLOCK ***** -->
<overlay id="sourceEditorOverlay"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<commandset id="sourceEditorCommands">
<command id="cmd_find" oncommand="goDoCommand('cmd_find')"/>
<command id="cmd_findAgain" oncommand="goDoCommand('cmd_findAgain')" disabled="true"/>
<command id="cmd_findPrevious" oncommand="goDoCommand('cmd_findPrevious')" disabled="true"/>
</commandset>
</overlay>

View File

@@ -44,9 +44,23 @@ const Ci = Components.interfaces;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource:///modules/source-editor-ui.jsm");
const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
/**
* Default key bindings in the textarea editor.
*/
const DEFAULT_KEYBINDINGS = [
{
_action: "_doTab",
keyCode: Ci.nsIDOMKeyEvent.DOM_VK_TAB,
shiftKey: false,
accelKey: false,
altKey: false,
},
];
var EXPORTED_SYMBOLS = ["SourceEditor"];
/**
@@ -69,6 +83,8 @@ function SourceEditor() {
this._listeners = {};
this._lastSelection = {};
this.ui = new SourceEditorUI(this);
}
SourceEditor.prototype = {
@@ -80,6 +96,13 @@ SourceEditor.prototype = {
_expandTab: null,
_tabSize: null,
/**
* The Source Editor user interface manager.
* @type object
* An instance of the SourceEditorUI.
*/
ui: null,
/**
* The editor container element.
* @type nsIDOMElement
@@ -136,7 +159,7 @@ SourceEditor.prototype = {
this._textbox.readOnly = aConfig.readOnly;
// Make sure that the SourceEditor Selection events are fired properly.
// Also make sure that the Tab key inserts spaces when expandTab is true.
// Also make sure that the configured keyboard bindings work.
this._textbox.addEventListener("select", this._onSelect.bind(this), false);
this._textbox.addEventListener("keypress", this._onKeyPress.bind(this), false);
this._textbox.addEventListener("keyup", this._onSelect.bind(this), false);
@@ -161,29 +184,61 @@ SourceEditor.prototype = {
this._config = aConfig;
for each (let key in DEFAULT_KEYBINDINGS) {
for (let prop in key) {
if (prop == "accelKey") {
let newProp = Services.appinfo.OS == "Darwin" ? "metaKey" : "ctrlKey";
key[newProp] = key[prop];
delete key[prop];
break;
}
}
}
this.ui.init();
this.ui.onReady();
if (aCallback) {
aCallback(this);
}
},
/**
* The textbox keypress event handler allows users to indent code using the
* Tab key.
* The textbox keypress event handler calls the configured action for keyboard
* event.
*
* @private
* @param nsIDOMEvent aEvent
* The DOM object for the event.
* @see DEFAULT_KEYBINDINGS
*/
_onKeyPress: function SE__onKeyPress(aEvent)
{
if (aEvent.keyCode != aEvent.DOM_VK_TAB || aEvent.shiftKey ||
aEvent.metaKey || aEvent.ctrlKey || aEvent.altKey) {
return;
for each (let key in DEFAULT_KEYBINDINGS) {
let matched = true;
for (let prop in key) {
if (prop.charAt(0) != "_" && aEvent[prop] !== key[prop]) {
matched = false;
break;
}
}
if (matched) {
let context = key._context ? this[key._context] : this;
context[key._action].call(context);
aEvent.preventDefault();
break;
}
}
},
aEvent.preventDefault();
let caret = this.getCaretOffset();
/**
* The Tab keypress event handler. This allows the user to indent the code
* with spaces, when expandTab is true.
*/
_doTab: function SE__doTab()
{
let selection = this.getSelection();
let caret = selection.start;
let indent = "\t";
if (this._expandTab) {
@@ -201,8 +256,8 @@ SourceEditor.prototype = {
indent = (new Array(spaces + 1)).join(" ");
}
this.setText(indent, caret, caret);
this.setCaretOffset(caret + indent.length);
this.setText(indent, selection.start, selection.end);
this.setCaretOffset(selection.start + indent.length);
},
/**
@@ -616,8 +671,8 @@ SourceEditor.prototype = {
aColumn = aColumn || 0;
let text = this._textbox.value;
let i = 0, n = text.length, c0, c1;
let line = 0, col = 0;
let i = -1, n = text.length, c0, c1;
let line = 0, col = -1;
while (i < n) {
c1 = text.charAt(i++);
if (line < aLine && (c1 == "\r" || (c0 != "\r" && c1 == "\n"))) {
@@ -709,6 +764,10 @@ SourceEditor.prototype = {
}
this._editor.removeEditActionListener(this._editActionListener);
this.ui.destroy();
this.ui = null;
this.parentElement.removeChild(this._textbox);
this.parentElement = null;
this._editor = null;
@@ -717,6 +776,7 @@ SourceEditor.prototype = {
this._listeners = null;
this._lastSelection = null;
this._editActionListener = null;
this._lastFind = null;
},
};

View File

@@ -0,0 +1,255 @@
/* vim:set ts=2 sw=2 sts=2 et tw=80:
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Source Editor component.
*
* The Initial Developer of the Original Code is
* The Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Mihai Sucan <mihai.sucan@gmail.com> (original author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK *****/
"use strict";
const Cu = Components.utils;
Cu.import("resource://gre/modules/Services.jsm");
var EXPORTED_SYMBOLS = ["SourceEditorUI"];
/**
* The Source Editor component user interface.
*/
function SourceEditorUI(aEditor)
{
this.editor = aEditor;
}
SourceEditorUI.prototype = {
/**
* Initialize the user interface. This is called by the SourceEditor.init()
* method.
*/
init: function SEU_init()
{
this._ownerWindow = this.editor.parentElement.ownerDocument.defaultView;
},
/**
* The UI onReady function is executed once the Source Editor completes
* initialization and it is ready for usage. Currently this code sets up the
* nsIController.
*/
onReady: function SEU_onReady()
{
if (this._ownerWindow.controllers) {
this._controller = new SourceEditorController(this.editor);
this._ownerWindow.controllers.insertControllerAt(0, this._controller);
}
},
/**
* The "find" command UI. This displays a prompt that allows the user to input
* the string to search for in the code. By default the current selection is
* used as a search string, or the last search string.
*/
find: function SEU_find()
{
let str = {value: this.editor.getSelectedText()};
if (!str.value && this.editor.lastFind) {
str.value = this.editor.lastFind.str;
}
let result = Services.prompt.prompt(this._ownerWindow,
SourceEditorUI.strings.GetStringFromName("findCmd.promptTitle"),
SourceEditorUI.strings.GetStringFromName("findCmd.promptMessage"),
str, null, {});
if (result && str.value) {
let start = this.editor.getSelection().end;
let pos = this.editor.find(str.value, {ignoreCase: true, start: start});
if (pos == -1) {
this.editor.find(str.value, {ignoreCase: true});
}
this._onFind();
}
return true;
},
/**
* Find the next occurrence of the last search string.
*/
findNext: function SEU_findNext()
{
let lastFind = this.editor.lastFind;
if (lastFind) {
this.editor.findNext(true);
this._onFind();
}
return true;
},
/**
* Find the previous occurrence of the last search string.
*/
findPrevious: function SEU_findPrevious()
{
let lastFind = this.editor.lastFind;
if (lastFind) {
this.editor.findPrevious(true);
this._onFind();
}
return true;
},
/**
* This executed after each find/findNext/findPrevious operation.
* @private
*/
_onFind: function SEU__onFind()
{
let lastFind = this.editor.lastFind;
if (lastFind && lastFind.index > -1) {
this.editor.setSelection(lastFind.index, lastFind.index + lastFind.str.length);
}
if (this._ownerWindow.goUpdateCommand) {
this._ownerWindow.goUpdateCommand("cmd_findAgain");
this._ownerWindow.goUpdateCommand("cmd_findPrevious");
}
},
/**
* Destroy the SourceEditorUI instance. This is called by the
* SourceEditor.destroy() method.
*/
destroy: function SEU_destroy()
{
this._ownerWindow = null;
this.editor = null;
this._controller = null;
},
};
/**
* The Source Editor nsIController implements features that need to be available
* from XUL commands.
*
* @constructor
* @param object aEditor
* SourceEditor object instance for which the controller is instanced.
*/
function SourceEditorController(aEditor)
{
this._editor = aEditor;
}
SourceEditorController.prototype = {
/**
* Check if a command is supported by the controller.
*
* @param string aCommand
* The command name you want to check support for.
* @return boolean
* True if the command is supported, false otherwise.
*/
supportsCommand: function SEC_supportsCommand(aCommand)
{
let result;
switch (aCommand) {
case "cmd_find":
case "cmd_findAgain":
case "cmd_findPrevious":
result = true;
break;
default:
result = false;
break;
}
return result;
},
/**
* Check if a command is enabled or not.
*
* @param string aCommand
* The command name you want to check if it is enabled or not.
* @return boolean
* True if the command is enabled, false otherwise.
*/
isCommandEnabled: function SEC_isCommandEnabled(aCommand)
{
let result;
switch (aCommand) {
case "cmd_find":
result = true;
break;
case "cmd_findAgain":
case "cmd_findPrevious":
result = this._editor.lastFind && this._editor.lastFind.lastFound != -1;
break;
default:
result = false;
break;
}
return result;
},
/**
* Perform a command.
*
* @param string aCommand
* The command name you want to execute.
* @return void
*/
doCommand: function SEC_doCommand(aCommand)
{
switch (aCommand) {
case "cmd_find":
this._editor.ui.find();
break;
case "cmd_findAgain":
this._editor.ui.findNext();
break;
case "cmd_findPrevious":
this._editor.ui.findPrevious();
break;
}
},
onEvent: function() { }
};

View File

@@ -41,12 +41,18 @@
const Cu = Components.utils;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource:///modules/source-editor-ui.jsm");
const PREF_EDITOR_COMPONENT = "devtools.editor.component";
const SOURCEEDITOR_L10N = "chrome://browser/locale/devtools/sourceeditor.properties";
var component = Services.prefs.getCharPref(PREF_EDITOR_COMPONENT);
var obj = {};
try {
if (component == "ui") {
throw new Error("The UI editor component is not available.");
}
Cu.import("resource:///modules/source-editor-" + component + ".jsm", obj);
} catch (ex) {
Cu.reportError(ex);
@@ -66,6 +72,10 @@ var EXPORTED_SYMBOLS = ["SourceEditor"];
// Add the constants used by all SourceEditors.
XPCOMUtils.defineLazyGetter(SourceEditorUI, "strings", function() {
return Services.strings.createBundle(SOURCEEDITOR_L10N);
});
/**
* Known SourceEditor preferences.
*/
@@ -142,3 +152,161 @@ SourceEditor.EVENTS = {
SELECTION: "Selection",
};
/**
* Extend a destination object with properties from a source object.
*
* @param object aDestination
* @param object aSource
*/
function extend(aDestination, aSource)
{
for (let name in aSource) {
if (!aDestination.hasOwnProperty(name)) {
aDestination[name] = aSource[name];
}
}
}
/**
* Add methods common to all components.
*/
extend(SourceEditor.prototype, {
_lastFind: null,
/**
* Find a string in the editor.
*
* @param string aString
* The string you want to search for. If |aString| is not given the
* currently selected text is used.
* @param object [aOptions]
* Optional find options:
* - start: (integer) offset to start searching from. Default: 0 if
* backwards is false. If backwards is true then start = text.length.
* - ignoreCase: (boolean) tells if you want the search to be case
* insensitive or not. Default: false.
* - backwards: (boolean) tells if you want the search to go backwards
* from the given |start| offset. Default: false.
* @return integer
* The offset where the string was found.
*/
find: function SE_find(aString, aOptions)
{
if (typeof(aString) != "string") {
return -1;
}
aOptions = aOptions || {};
let str = aOptions.ignoreCase ? aString.toLowerCase() : aString;
let text = this.getText();
if (aOptions.ignoreCase) {
text = text.toLowerCase();
}
let index = aOptions.backwards ?
text.lastIndexOf(str, aOptions.start) :
text.indexOf(str, aOptions.start);
let lastFoundIndex = index;
if (index == -1 && this.lastFind && this.lastFind.index > -1 &&
this.lastFind.str === aString &&
this.lastFind.ignoreCase === !!aOptions.ignoreCase) {
lastFoundIndex = this.lastFind.index;
}
this._lastFind = {
str: aString,
index: index,
lastFound: lastFoundIndex,
ignoreCase: !!aOptions.ignoreCase,
};
return index;
},
/**
* Find the next occurrence of the last search operation.
*
* @param boolean aWrap
* Tells if you want to restart the search from the beginning of the
* document if the string is not found.
* @return integer
* The offset where the string was found.
*/
findNext: function SE_findNext(aWrap)
{
if (!this.lastFind && this.lastFind.lastFound == -1) {
return -1;
}
let options = {
start: this.lastFind.lastFound + this.lastFind.str.length,
ignoreCase: this.lastFind.ignoreCase,
};
let index = this.find(this.lastFind.str, options);
if (index == -1 && aWrap) {
options.start = 0;
index = this.find(this.lastFind.str, options);
}
return index;
},
/**
* Find the previous occurrence of the last search operation.
*
* @param boolean aWrap
* Tells if you want to restart the search from the end of the
* document if the string is not found.
* @return integer
* The offset where the string was found.
*/
findPrevious: function SE_findPrevious(aWrap)
{
if (!this.lastFind && this.lastFind.lastFound == -1) {
return -1;
}
let options = {
start: this.lastFind.lastFound - this.lastFind.str.length,
ignoreCase: this.lastFind.ignoreCase,
backwards: true,
};
let index;
if (options.start > 0) {
index = this.find(this.lastFind.str, options);
} else {
index = this._lastFind.index = -1;
}
if (index == -1 && aWrap) {
options.start = this.getCharCount() - 1;
index = this.find(this.lastFind.str, options);
}
return index;
},
});
/**
* Retrieve the last find operation result. This object holds the following
* properties:
* - str: the last search string.
* - index: stores the result of the most recent find operation. This is the
* index in the text where |str| was found or -1 otherwise.
* - lastFound: tracks the index where |str| was last found, throughout
* multiple find operations. This can be -1 if |str| was never found in the
* document.
* - ignoreCase: tells if the search was case insensitive or not.
* @type object
*/
Object.defineProperty(SourceEditor.prototype, "lastFind", {
get: function() { return this._lastFind; },
enumerable: true,
configurable: true,
});

View File

@@ -53,6 +53,7 @@ _BROWSER_TEST_FILES = \
browser_bug684546_reset_undo.js \
browser_bug695035_middle_click_paste.js \
browser_bug687160_line_api.js \
browser_bug650345_find.js \
head.js \
libs:: $(_BROWSER_TEST_FILES)

View File

@@ -0,0 +1,147 @@
/* vim: set ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
Cu.import("resource:///modules/source-editor.jsm");
let testWin;
let editor;
function test()
{
waitForExplicitFinish();
const windowUrl = "data:text/xml,<?xml version='1.0'?>" +
"<window xmlns='http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul'" +
" title='test for bug 650345' width='600' height='500'><hbox flex='1'/></window>";
const windowFeatures = "chrome,titlebar,toolbar,centerscreen,resizable,dialog=no";
testWin = Services.ww.openWindow(null, windowUrl, "_blank", windowFeatures, null);
testWin.addEventListener("load", function onWindowLoad() {
testWin.removeEventListener("load", onWindowLoad, false);
waitForFocus(initEditor, testWin);
}, false);
}
function initEditor()
{
let hbox = testWin.document.querySelector("hbox");
editor = new SourceEditor();
editor.init(hbox, {}, editorLoaded);
}
function editorLoaded()
{
editor.focus();
let text = "foobar bug650345\nBug650345 bazbaz\nfoobar omg\ntest";
editor.setText(text);
let needle = "foobar";
is(editor.find(), -1, "find() works");
ok(!editor.lastFind, "no editor.lastFind yet");
is(editor.find(needle), 0, "find('" + needle + "') works");
is(editor.lastFind.str, needle, "lastFind.str is correct");
is(editor.lastFind.index, 0, "lastFind.index is correct");
is(editor.lastFind.lastFound, 0, "lastFind.lastFound is correct");
is(editor.lastFind.ignoreCase, false, "lastFind.ignoreCase is correct");
let newIndex = text.indexOf(needle, needle.length);
is(editor.findNext(), newIndex, "findNext() works");
is(editor.lastFind.str, needle, "lastFind.str is correct");
is(editor.lastFind.index, newIndex, "lastFind.index is correct");
is(editor.lastFind.lastFound, newIndex, "lastFind.lastFound is correct");
is(editor.lastFind.ignoreCase, false, "lastFind.ignoreCase is correct");
is(editor.findNext(), -1, "findNext() works again");
is(editor.lastFind.index, -1, "lastFind.index is correct");
is(editor.lastFind.lastFound, newIndex, "lastFind.lastFound is correct");
is(editor.findPrevious(), 0, "findPrevious() works");
is(editor.lastFind.index, 0, "lastFind.index is correct");
is(editor.lastFind.lastFound, 0, "lastFind.lastFound is correct");
is(editor.findPrevious(), -1, "findPrevious() works again");
is(editor.lastFind.index, -1, "lastFind.index is correct");
is(editor.lastFind.lastFound, 0, "lastFind.lastFound is correct");
is(editor.findNext(), newIndex, "findNext() works");
is(editor.lastFind.index, newIndex, "lastFind.index is correct");
is(editor.lastFind.lastFound, newIndex, "lastFind.lastFound is correct");
is(editor.findNext(true), 0, "findNext(true) works");
is(editor.lastFind.index, 0, "lastFind.index is correct");
is(editor.lastFind.lastFound, 0, "lastFind.lastFound is correct");
is(editor.findNext(true), newIndex, "findNext(true) works again");
is(editor.lastFind.index, newIndex, "lastFind.index is correct");
is(editor.lastFind.lastFound, newIndex, "lastFind.lastFound is correct");
is(editor.findPrevious(true), 0, "findPrevious(true) works");
is(editor.lastFind.index, 0, "lastFind.index is correct");
is(editor.lastFind.lastFound, 0, "lastFind.lastFound is correct");
is(editor.findPrevious(true), newIndex, "findPrevious(true) works again");
is(editor.lastFind.index, newIndex, "lastFind.index is correct");
is(editor.lastFind.lastFound, newIndex, "lastFind.lastFound is correct");
needle = "error";
is(editor.find(needle), -1, "find('" + needle + "') works");
is(editor.lastFind.str, needle, "lastFind.str is correct");
is(editor.lastFind.index, -1, "lastFind.index is correct");
is(editor.lastFind.lastFound, -1, "lastFind.lastFound is correct");
is(editor.lastFind.ignoreCase, false, "lastFind.ignoreCase is correct");
is(editor.findNext(), -1, "findNext() works");
is(editor.lastFind.str, needle, "lastFind.str is correct");
is(editor.lastFind.index, -1, "lastFind.index is correct");
is(editor.lastFind.lastFound, -1, "lastFind.lastFound is correct");
is(editor.findNext(true), -1, "findNext(true) works");
is(editor.findPrevious(), -1, "findPrevious() works");
is(editor.findPrevious(true), -1, "findPrevious(true) works");
needle = "bug650345";
newIndex = text.indexOf(needle);
is(editor.find(needle), newIndex, "find('" + needle + "') works");
is(editor.findNext(), -1, "findNext() works");
is(editor.findNext(true), newIndex, "findNext(true) works");
is(editor.findPrevious(), -1, "findPrevious() works");
is(editor.findPrevious(true), newIndex, "findPrevious(true) works");
is(editor.lastFind.index, newIndex, "lastFind.index is correct");
is(editor.lastFind.lastFound, newIndex, "lastFind.lastFound is correct");
is(editor.find(needle, {ignoreCase: 1}), newIndex,
"find('" + needle + "', {ignoreCase: 1}) works");
is(editor.lastFind.ignoreCase, true, "lastFind.ignoreCase is correct");
let newIndex2 = text.toLowerCase().indexOf(needle, newIndex + needle.length);
is(editor.findNext(), newIndex2, "findNext() works");
is(editor.findNext(), -1, "findNext() works");
is(editor.lastFind.index, -1, "lastFind.index is correct");
is(editor.lastFind.lastFound, newIndex2, "lastFind.lastFound is correct");
is(editor.findNext(true), newIndex, "findNext(true) works");
is(editor.findPrevious(), -1, "findPrevious() works");
is(editor.findPrevious(true), newIndex2, "findPrevious(true) works");
is(editor.findPrevious(), newIndex, "findPrevious() works again");
needle = "foobar";
newIndex = text.indexOf(needle, 2);
is(editor.find(needle, {start: 2}), newIndex,
"find('" + needle + "', {start:2}) works");
is(editor.findNext(), -1, "findNext() works");
is(editor.findNext(true), 0, "findNext(true) works");
editor.destroy();
testWin.close();
testWin = editor = null;
waitForFocus(finish, window);
}

View File

@@ -67,6 +67,22 @@
<!ENTITY selectAllCmd.key "A">
<!ENTITY selectAllCmd.accesskey "A">
<!ENTITY findCmd.label "Find…">
<!ENTITY findCmd.key "F">
<!ENTITY findCmd.accesskey "F">
<!ENTITY findAgainCmd.label "Find Again…">
<!-- LOCALIZATION NOTE (findAgainCmd.key): This key is used only on Macs.
- Windows and Linux builds use the F3 key which is not localizable on purpose.
-->
<!ENTITY findAgainCmd.key "G">
<!ENTITY findAgainCmd.accesskey "g">
<!-- LOCALIZATION NOTE (findPreviousCmd.key): This key is used only on Macs.
- Windows and Linux builds use the Shift-F3 key which is not localizable on
- purpose.
-->
<!ENTITY findPreviousCmd.key "G">
<!ENTITY run.label "Run">
<!ENTITY run.accesskey "R">
<!ENTITY run.key "r">

View File

@@ -0,0 +1,20 @@
# LOCALIZATION NOTE These strings are used inside the Source Editor component.
# This component is used whenever source code is displayed for the purpose of
# being edited, inside the Firefox developer tools - current examples are the
# Scratchpad and the Style Editor tools.
# LOCALIZATION NOTE The correct localization of this file might be to keep it
# in English, or another language commonly spoken among web developers.
# You want to make that choice consistent across the developer tools.
# A good criteria is the language in which you'd find the best documentation
# on web development on the web.
# LOCALIZATION NOTE (findCmd.promptTitle): This is the dialog title used
# when the user wants to search for a string in the code. You can
# access this feature by pressing Ctrl-F on Windows/Linux or Cmd-F on Mac.
findCmd.promptTitle=Find…
# LOCALIZATION NOTE (gotoLineCmd.promptMessage): This is the message shown when
# the user wants to search for a string in the code. You can
# access this feature by pressing Ctrl-F on Windows/Linux or Cmd-F on Mac.
findCmd.promptMessage=Search for:

View File

@@ -27,6 +27,7 @@
locale/browser/devtools/styleinspector.properties (%chrome/browser/devtools/styleinspector.properties)
locale/browser/devtools/styleinspector.dtd (%chrome/browser/devtools/styleinspector.dtd)
locale/browser/devtools/webConsole.dtd (%chrome/browser/devtools/webConsole.dtd)
locale/browser/devtools/sourceeditor.properties (%chrome/browser/devtools/sourceeditor.properties)
locale/browser/openLocation.dtd (%chrome/browser/openLocation.dtd)
locale/browser/openLocation.properties (%chrome/browser/openLocation.properties)
* locale/browser/pageInfo.dtd (%chrome/browser/pageInfo.dtd)