Source

Firefox Enhanced Find Extension / content / smartfindbar.xml

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<?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 mozilla.org viewsource frontend.
  -
  - The Initial Developer of the Original Code is
  - Netscape Communications Corporation.
  - Portions created by the Initial Developer are Copyright (C) 2003
  - the Initial Developer. All Rights Reserved.
  -
  - Contributor(s): Tomaz Nolêto <tnoleto@gmail.com>
  -
  - 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 ***** -->

<!DOCTYPE bindings [
<!ENTITY % findBarDTD SYSTEM "chrome://global/locale/findbar.dtd" >
%findBarDTD;
<!ENTITY % globalDTD SYSTEM "chrome://global/locale/global.dtd">
%globalDTD;
<!ENTITY % smartfindDTD SYSTEM "chrome://smartfind/locale/smartfind.dtd">
%smartfindDTD;
]>

<bindings id="smartfindBindings"
    xmlns="http://www.mozilla.org/xbl"
    xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
    xmlns:xbl="http://www.mozilla.org/xbl">

    <binding id="smartfindbar-textbox"
        extends="chrome://global/content/bindings/findbar.xml#findbar-textbox">

        <implementation>
            <method name="_handleEnter">
                <parameter name="aEvent"/>
                <body><![CDATA[
                    if (this.findbar._findMode == this.findbar.FIND_NORMAL) {
                        var findString = this.findbar._findField;
                        if (!findString.value)
                            return;
                        if (aEvent.ctrlKey) {
                            this.findbar.getElement("highlight").click();
                            return;
                        }
                        this.findbar.onFindAgainCommand(aEvent.shiftKey);
                    }
                    else {
                        // We need to keep a reference to _foundLink because
                        // _finishFAYT resets it to null.
                        var tmpLink = this._findbar._foundLink;
                        if (tmpLink && this.findbar._finishFAYT(aEvent))
                            this.findbar._dispatchKeypressEvent(tmpLink, aEvent);

                        // Add <ENTER> support for similatiry search if in QUICK SEARCH mode
                        this.findbar.onFindAgainCommand(aEvent.shiftKey);
                        aEvent.preventDefault();
                        aEvent.stopPropagation();
                    }
                ]]></body>
            </method>
        </implementation>

        <handlers>
            <handler event="keypress">
                <![CDATA[
                    var win = this.findbar._currentWindow ||
                         this.findbar.browser.contentWindow;

                    switch (event.keyCode) {
                        case KeyEvent.DOM_VK_RETURN:
                            //this._handleEnter(event);
                            //event.preventDefault();
                            //event.stopPropagation();
                            break;
                        case KeyEvent.DOM_VK_TAB:
                            this._handleTab(event);
                            break;
                        case KeyEvent.DOM_VK_PAGE_UP:
                            win.scrollByPages(-1);
                            event.preventDefault();
                            break;
                        case KeyEvent.DOM_VK_PAGE_DOWN:
                            win.scrollByPages(1);
                            event.preventDefault();
                            break;
                        case KeyEvent.DOM_VK_UP:
                            win.scrollByLines(-1);
                            event.preventDefault();
                            break;
                        case KeyEvent.DOM_VK_DOWN:
                            win.scrollByLines(1);
                            event.preventDefault();
                            break;
                    }
            ]]></handler>
        </handlers>
    </binding>

    <binding id="smartfindbar"
        extends="chrome://global/content/bindings/findbar.xml#findbar">

        <resources>
            <stylesheet src="chrome://global/skin/findBar.css"/>
        </resources>

        <content hidden="true">
            <xul:hbox anonid="findbar-container" class="findbar-container" flex="1" align="center">
                <xul:toolbarbutton anonid="find-closebutton"
                    class="findbar-closebutton"
                    tooltiptext="&findCloseButton.tooltip;"
                    oncommand="close();"/>
                <xul:label anonid="find-label" class="findbar-find-fast" control="findbar-textbox"/>
                <xul:hbox anonid="find-field-container"
                    class="find-field-container findbar-find-fast">
                <xul:textbox class="findbar-textbox" anonid="findbar-textbox" oncommand="onFindAgainCommand(false);"/>
            </xul:hbox>
            <xul:hbox anonid="find-buttons-container" class="find-buttons-container">
                <xul:toolbarbutton anonid="find-next"
                    class="findbar-find-next tabbable"
                    label="&next.label;"
                    accesskey="&next.accesskey;"
                    tooltiptext="&next.tooltip;"
                    oncommand="onFindAgainCommand(false);"
                    disabled="true"
                    chromedir="&locale.dir;"
                    xbl:inherits="accesskey=findnextaccesskey"/>
                <xul:toolbarbutton anonid="find-previous"
                    class="findbar-find-previous tabbable"
                    label="&previous.label;"
                    accesskey="&previous.accesskey;"
                    tooltiptext="&previous.tooltip;"
                    oncommand="onFindAgainCommand(true);"
                    disabled="true"
                    chromedir="&locale.dir;"
                    xbl:inherits="accesskey=findpreviousaccesskey"/>
            </xul:hbox>
            <xul:toolbarbutton anonid="highlight"
                class="findbar-highlight tabbable"
                label="&highlight.label;"
                accesskey="&highlight.accesskey;"
                tooltiptext="&highlight.tooltiptext;"
                oncommand="toggleHighlight(this.checked);"
                type="checkbox"
                disabled="true"
                xbl:inherits="accesskey=highlightaccesskey"/>
            <xul:checkbox anonid="find-case-sensitive"
                oncommand="_setCaseSensitivity(this.checked);"
                label="&caseSensitiveCheckbox.label;"
                accesskey="&caseSensitiveCheckbox.accesskey;"
                xbl:inherits="accesskey=matchcaseaccesskey"/>
            <xul:checkbox anonid="find-similar"
                oncommand="_setSimilar(this.checked)"
                label="&smartfindSimilarCheckbox.label;"
                accesskey="&smartfindSimilarCheckbox.accesskey;"/>
            <xul:checkbox anonid="find-regex"
                oncommand="_setRegex(this.checked)"
                label="&smartfindRegexCheckbox.label;"
                accesskey="&smartfindRegexCheckbox.accesskey;"/>
            <xul:label anonid="match-case-status" class="findbar-find-fast"/>
            <xul:image anonid="find-status-icon" class="findbar-find-fast find-status-icon"/>
            <xul:description anonid="find-status" class="findbar-find-fast findbar-find-status"
                control="findbar-textbox">
            </xul:description>
            </xul:hbox>
        </content>

        <implementation implements="nsIDOMEventListener">

            <constructor><![CDATA[
                // These elements are accessed frequently and are therefore cached
                this._findField = this.getElement("findbar-textbox");
                this._findStatusIcon = this.getElement("find-status-icon");
                this._findStatusDesc = this.getElement("find-status");

                var prefsvc =
                Components.classes["@mozilla.org/preferences-service;1"]
                            .getService(Components.interfaces.nsIPrefBranch2);

                this._quickFindTimeoutLength =
                    prefsvc.getIntPref("accessibility.typeaheadfind.timeout");
                this._flashFindBar =
                prefsvc.getIntPref("accessibility.typeaheadfind.flashBar");

                prefsvc.addObserver("accessibility.typeaheadfind",
                                    this._observer, false);
                prefsvc.addObserver("accessibility.typeaheadfind.linksonly",
                                    this._observer, false);
                prefsvc.addObserver("accessibility.typeaheadfind.casesensitive",
                                    this._observer, false);

                this._useTypeAheadFind =
                    prefsvc.getBoolPref("accessibility.typeaheadfind");
                this._typeAheadLinksOnly =
                    prefsvc.getBoolPref("accessibility.typeaheadfind.linksonly");
                this._typeAheadCaseSensitive =
                    prefsvc.getIntPref("accessibility.typeaheadfind.casesensitive");

                // similarity
                this._similar =
                    prefsvc.getBoolPref("extensions.smartfind.enabled");
                this._similarLevel =
                    prefsvc.getIntPref("extensions.smartfind.similarity_level");

                // Regex
                this._regex =
                    prefsvc.getBoolPref("extensions.smartfind.regex.enabled");

                // Convenience
                this.nsITypeAheadFind = Components.interfaces.nsITypeAheadFind;
                this.nsISelectionController = Components.interfaces.nsISelectionController;
            ]]></constructor>

            <method name="_setSimilar">
                <parameter name="aSimilar"/>
                <body><![CDATA[
                    var prefsvc = Components.classes["@mozilla.org/preferences-service;1"]
                        .getService(Components.interfaces.nsIPrefBranch2);

                    prefsvc.setBoolPref("extensions.smartfind.enabled", aSimilar ? true : false);
                    // We cannot do both a regular expression search and
                    // a similar search.
                    if (this._similar = aSimilar) {
                        this.getElement("find-regex").checked = false;
                    }
                ]]></body>
            </method>

            <method name="_setRegex">
                <parameter name="aRegex"/>
                <body><![CDATA[
                    var prefsvc = Components.classes["@mozilla.org/preferences-service;1"]
                        .getService(Components.interfaces.nsIPrefBranch2);

                    prefsvc.setBoolPref("extensions.smartfind.regex.enabled", aRegex ? true : false);
                    // We cannot do both a regular expression search and
                    // a similar search.
                    if (this._regex = aRegex) {
                        this.getElement("find-similar").checked = false;
                    }
                ]]></body>
            </method>

            <method name="_setSimilarLevel">
                <parameter name="aValue"/>
                <body><![CDATA[
                    var prefsvc = Components.classes["@mozilla.org/preferences-service;1"]
                        .getService(Components.interfaces.nsIPrefBranch2);

                    prefsvc.setIntPref("extensions.smartfind.similarity_level", aValue);
                    this._similarLevel = aValue;
                ]]></body>
            </method>

            <method name="_find">
                <parameter name="aValue"/>
                <body><![CDATA[
                var val = aValue || this._findField.value

                this._enableFindButtons(val);
                if (this.getElement("highlight").checked)
                    this._setHighlightTimeout();

                this._updateCaseSensitivity(val);

                var fastFind = this.browser.fastFind;
                var res = fastFind.find(val, this._findMode == this.FIND_LINKS);
                this._updateFoundLink(res);
                if(this._similar)
                    this._noSimilar = false;
                this._updateStatusUI(res, false);

                if (this._findMode != this.FIND_NORMAL)
                    this._setFindCloseTimeout();

                return res;
                ]]></body>
            </method>

            <method name="onFindAgainCommand">
                <parameter name="aFindPrevious"/>
                <body><![CDATA[
                    var findString = this._browser.fastFind.searchString || this._findField.value;
                    if (!findString) {
                        this.startFind();
                        return;
                    }

                    var res;
                    // Ensure the stored SearchString is in sync with what we want to find
                    if (this._findField.value != this._browser.fastFind.searchString &&
                        !this.hidden) {
                        res = this._find(this._findField.value);
                    }
                    else {
                        res = this._findAgain(aFindPrevious);
                        // If SearchString was not found, try a similar string if enabled
                        if (res == 1 && this._similar) {
                            findString = getSimilarTerms(this.browser.contentWindow.document,
                                this._browser.fastFind.searchString, this._similarLevel);

                            if (findString != "") {
                                res = this._find(findString);

                                // Change entry background color when finding similar word
                                this._findField.setAttribute("status", "foundsimilar");

                                // Set findbar entry and SearchString to be the similar string found.
                                this._findField.value = findString;
                                this._noSimilar = false;
                            }
                            else { // no similar term found !
                                if (!this._noSimilar) {
                                    this._noSimilar = true;
                                    this._updateStatusUI(res, aFindPrevious);
                                }
                            }
                        }
                    }

                    if (res == this.nsITypeAheadFind.FIND_NOTFOUND) {
                        if (this.open()) {
                            if (this._findMode != this.FIND_NORMAL)
                                this._setFindCloseTimeout();
                            this._findField.focus();
                            this._findField.focus();
                            if (!this._similar)
                                this._updateStatusUI(res, aFindPrevious);
                        }
                    }
                ]]></body>
            </method>
            <method name="_updateStatusUI">
                <parameter name="res"/>
                <parameter name="aFindPrevious"/>
                <body><![CDATA[
                    switch (res) {
                        case this.nsITypeAheadFind.FIND_WRAPPED:
                            this._findStatusIcon.setAttribute("status", "wrapped");
                            this._findStatusDesc.textContent =
                              aFindPrevious ? this._wrappedToBottomStr : this._wrappedToTopStr;
                            this._findField.removeAttribute("status");
                            break;
                        case this.nsITypeAheadFind.FIND_NOTFOUND:
                            this._findStatusIcon.setAttribute("status", "notfound");
                            if (this._similar)
                                if(this._noSimilar)
                                    this._findStatusDesc.textContent = this._notSimilarySearchStr;
                                else
                                    this._findStatusDesc.textContent = this._adviceSimilarySearchStr;
                            else
                              this._findStatusDesc.textContent = this._notFoundStr;
                            this._findField.setAttribute("status", "notfound");
                            break;
                        case this.nsITypeAheadFind.FIND_FOUND:
                        default:
                        this._findStatusIcon.removeAttribute("status");
                            this._findStatusDesc.textContent = "";
                            this._findField.removeAttribute("status");
                            break;
                    }
                ]]></body>
            </method>


            <method name="open">
                <parameter name="aMode"/>
                <body><![CDATA[
                    if (aMode != undefined)
                        this._findMode = aMode;

                    if (!this._notFoundStr) {
                        var stringsBundle =
                            Components.classes["@mozilla.org/intl/stringbundle;1"]
                            .getService(Components.interfaces.nsIStringBundleService)
                            .createBundle("chrome://global/locale/findbar.properties");
                        this._notFoundStr =
                            stringsBundle.GetStringFromName("NotFound");
                        this._wrappedToTopStr =
                            stringsBundle.GetStringFromName("WrappedToTop");
                        this._wrappedToBottomStr =
                            stringsBundle.GetStringFromName("WrappedToBottom");
                        this._normalFindStr =
                            stringsBundle.GetStringFromName("NormalFindLabel");
                        this._fastFindStr =
                            stringsBundle.GetStringFromName("FastFindLabel");
                        this._fastFindLinksStr =
                            stringsBundle.GetStringFromName("FastFindLinksLabel");
                        this._caseSensitiveStr =
                            stringsBundle.GetStringFromName("CaseSensitive");
                    }

                    stringsBundle = Components.classes["@mozilla.org/intl/stringbundle;1"]
                                    .getService(Components.interfaces.nsIStringBundleService)
                                    .createBundle("chrome://smartfind/locale/smartfind.properties");

                    this._adviceSimilarySearchStr=
                        stringsBundle.GetStringFromName("EnterForSimilarity");

                    this._notSimilarySearchStr=
                        stringsBundle.GetStringFromName("NoSimilar");

                    this._updateFindUI();
                    if (this.hidden) {
                        this.hidden = false;
                        this._updateStatusUI(this.nsITypeAheadFind.FIND_FOUND);
                        return true;
                    }
                    return false;
                ]]></body>
            </method>
        </implementation>
    </binding>
</bindings>