summaryrefslogtreecommitdiff
path: root/common/content/find.js
blob: 794476accf6ca142e052c55dca9c38e49e0c45c1 (plain)
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/***** B/GIN 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.

(c) 2006-2008: Martin Stubenschrott <stubenschrott@gmx.net>

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 *****/

// TODO: proper backwards search - implement our own component?
//     : implement our own highlighter?
//     : frameset pages
//     : <ESC> should cancel search highlighting in 'incsearch' mode and jump
//       back to the presearch page location - can probably use the same
//       solution as marks
//     : 'linksearch' searches should highlight link matches only
//     : changing any search settings should also update the search state including highlighting
//     : incremental searches shouldn't permanently update search modifiers

// make sure you only create this object when the "liberator" object is ready
function Search() //{{{
{
    ////////////////////////////////////////////////////////////////////////////////
    ////////////////////// PRIVATE SECTION /////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////{{{

    // FIXME:
    //var self = this;               // needed for callbacks since "this" is the "liberator" object in a callback
    var found = false;               // true if the last search was successful
    var backwards = false;           // currently searching backwards
    var searchString = "";           // current search string (without modifiers)
    var searchPattern = "";          // current search string (includes modifiers)
    var lastSearchPattern = "";      // the last searched pattern (includes modifiers)
    var lastSearchString = "";       // the last searched string (without modifiers)
    var lastSearchBackwards = false; // like "backwards", but for the last search, so if you cancel a search with <esc> this is not set
    var caseSensitive = false;       // search string is case sensitive
    var linksOnly = false;           // search is limited to link text only

    // Event handlers for search - closure is needed
    liberator.registerCallback("change", modes.SEARCH_FORWARD, function (command) { search.searchKeyPressed(command); });
    liberator.registerCallback("submit", modes.SEARCH_FORWARD, function (command) { search.searchSubmitted(command); });
    liberator.registerCallback("cancel", modes.SEARCH_FORWARD, function () { search.searchCanceled(); });
    // TODO: allow advanced myModes in register/triggerCallback
    liberator.registerCallback("change", modes.SEARCH_BACKWARD, function (command) { search.searchKeyPressed(command); });
    liberator.registerCallback("submit", modes.SEARCH_BACKWARD, function (command) { search.searchSubmitted(command); });
    liberator.registerCallback("cancel", modes.SEARCH_BACKWARD, function () { search.searchCanceled(); });

    // set searchString, searchPattern, caseSensitive, linksOnly
    function processUserPattern(pattern)
    {
        //// strip off pattern terminator and offset
        //if (backwards)
        //    pattern = pattern.replace(/\?.*/, "");
        //else
        //    pattern = pattern.replace(/\/.*/, "");

        searchPattern = pattern;

        // links only search - \l wins if both modifiers specified
        if (/\\l/.test(pattern))
            linksOnly = true;
        else if (/\L/.test(pattern))
            linksOnly = false;
        else if (options["linksearch"])
            linksOnly = true;
        else
            linksOnly = false;

        // strip links-only modifiers
        pattern = pattern.replace(/(\\)?\\[lL]/g, function ($0, $1) { return $1 ? $0 : ""; });

        // case sensitivity - \c wins if both modifiers specified
        if (/\c/.test(pattern))
            caseSensitive = false;
        else if (/\C/.test(pattern))
            caseSensitive = true;
        else if (options["ignorecase"] && options["smartcase"] && /[A-Z]/.test(pattern))
            caseSensitive = true;
        else if (options["ignorecase"])
            caseSensitive = false;
        else
            caseSensitive = true;

        // strip case-sensitive modifiers
        pattern = pattern.replace(/(\\)?\\[cC]/g, function ($0, $1) { return $1 ? $0 : ""; });

        // remove any modifer escape \
        pattern = pattern.replace(/\\(\\[cClL])/g, "$1");

        searchString = pattern;
    }

    /* Stolen from toolkit.jar in Firefox, for the time being. The private
     * methods were unstable, and changed. The new version is not remotely
     * compatible with what we do.
     *   The following only applies to this object, and may not be
     * necessary, or accurate, but, just in case:
     *   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):
     *       Blake Ross <blake@cs.stanford.edu> (Original Author)
     *       Masayuki Nakano <masayuki@d-toybox.com>
     *       Ben Basson <contact@cusser.net>
     *       Jason Barnabe <jason_barnabe@fastmail.fm>
     *       Asaf Romano <mano@mozilla.com>
     *       Ehsan Akhgari <ehsan.akhgari@gmail.com>
     *       Graeme McCutcheon <graememcc_firefox@graeme-online.co.uk>
     */
    var highlightObj = {
        search: function (aWord, matchCase)
        {
            var finder = Cc["@mozilla.org/embedcomp/rangefind;1"]
                                   .createInstance()
                                   .QueryInterface(Ci.nsIFind);
            if (matchCase !== undefined)
                finder.caseSensitive = matchCase;

            var range;
            while ((range = finder.Find(aWord,
                                        this.searchRange,
                                        this.startPt,
                                        this.endPt)))
                yield range;
        },

        highlightDoc: function highlightDoc(win, aWord)
        {
            Array.forEach(win.frames, function (frame) highlightObj.highlightDoc(frame, aWord));

            var doc = win.document;
            if (!doc || !(doc instanceof HTMLDocument))
                return;

            if (!aWord)
            {
                let elems = highlightObj.getSpans(doc);
                for (let i = elems.snapshotLength; --i >= 0;)
                {
                    let elem = elems.snapshotItem(i);
                    let docfrag = doc.createDocumentFragment();
                    let next = elem.nextSibling;
                    let parent = elem.parentNode;

                    let child;
                    while (child = elem.firstChild)
                        docfrag.appendChild(child);

                    parent.removeChild(elem);
                    parent.insertBefore(docfrag, next);
                    parent.normalize();
                }
                return;
            }

            var baseNode = <span highlight="Search"/>
            baseNode = util.xmlToDom(baseNode, window.content.document);

            var body = doc.body;
            var count = body.childNodes.length;
            this.searchRange = doc.createRange();
            this.startPt = doc.createRange();
            this.endPt = doc.createRange();

            this.searchRange.setStart(body, 0);
            this.searchRange.setEnd(body, count);

            this.startPt.setStart(body, 0);
            this.startPt.setEnd(body, 0);
            this.endPt.setStart(body, count);
            this.endPt.setEnd(body, count);

            liberator.interrupted = false;
            let n = 0;
            for (let retRange in this.search(aWord, caseSensitive))
            {
                // Highlight
                var nodeSurround = baseNode.cloneNode(true);
                var node = this.highlight(retRange, nodeSurround);
                this.startPt = node.ownerDocument.createRange();
                this.startPt.setStart(node, node.childNodes.length);
                this.startPt.setEnd(node, node.childNodes.length);
                if (n++ % 20 == 0)
                    liberator.threadYield(true);
                if (liberator.interrupted)
                    break;
            }
        },

        highlight: function highlight(aRange, aNode)
        {
            var startContainer = aRange.startContainer;
            var startOffset = aRange.startOffset;
            var endOffset = aRange.endOffset;
            var docfrag = aRange.extractContents();
            var before = startContainer.splitText(startOffset);
            var parent = before.parentNode;
            aNode.appendChild(docfrag);
            parent.insertBefore(aNode, before);
            return aNode;
        },

        getSpans: function (doc) buffer.evaluateXPath("//*[@liberator:highlight='Search']", doc)
    };

    /////////////////////////////////////////////////////////////////////////////}}}
    ////////////////////// OPTIONS /////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////{{{

    options.add(["hlsearch", "hls"],
        "Highlight previous search pattern matches",
        "boolean", "false",
        {
            setter: function (value)
            {
                if (value)
                    search.highlight();
                else
                    search.clear();

                return value;
            }
        });

    options.add(["ignorecase", "ic"],
        "Ignore case in search patterns",
        "boolean", true);

    options.add(["incsearch", "is"],
        "Show where the search pattern matches as it is typed",
        "boolean", true);

    options.add(["linksearch", "lks"],
        "Limit the search to hyperlink text",
        "boolean", false);

    options.add(["smartcase", "scs"],
        "Override the 'ignorecase' option if the pattern contains uppercase characters",
        "boolean", true);

    /////////////////////////////////////////////////////////////////////////////}}}
    ////////////////////// MAPPINGS ////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////{{{

    var myModes = config.browserModes;
    myModes = myModes.concat([modes.CARET]);

    mappings.add(myModes,
        ["/"], "Search forward for a pattern",
        function () { search.openSearchDialog(modes.SEARCH_FORWARD); });

    mappings.add(myModes,
        ["?"], "Search backwards for a pattern",
        function () { search.openSearchDialog(modes.SEARCH_BACKWARD); });

    mappings.add(myModes,
        ["n"], "Find next",
        function () { search.findAgain(false); });

    mappings.add(myModes,
        ["N"], "Find previous",
        function () { search.findAgain(true); });

    mappings.add(myModes.concat([modes.CARET, modes.TEXTAREA]), ["*"],
        "Find word under cursor",
        function ()
        {
            found = false;
            search.searchSubmitted(buffer.getCurrentWord(), false);
            search.findAgain();
        });

    mappings.add(myModes.concat([modes.CARET, modes.TEXTAREA]), ["#"],
        "Find word under cursor backwards",
        function ()
        {
            found = false;
            search.searchSubmitted(buffer.getCurrentWord(), true);
            search.findAgain();
        });

    /////////////////////////////////////////////////////////////////////////////}}}
    ////////////////////// COMMANDS ////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////{{{

    commands.add(["noh[lsearch]"],
        "Remove the search highlighting",
        function () { search.clear(); },
        { argCount: "0" });

    /////////////////////////////////////////////////////////////////////////////}}}
    ////////////////////// PUBLIC SECTION //////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////{{{

    return {

        // Called when the search dialog is asked for
        // If you omit "mode", it will default to forward searching
        openSearchDialog: function (mode)
        {
            if (mode == modes.SEARCH_BACKWARD)
            {
                commandline.open("?", "", modes.SEARCH_BACKWARD);
                backwards = true;
            }
            else
            {
                commandline.open("/", "", modes.SEARCH_FORWARD);
                backwards = false;
            }

            // TODO: focus the top of the currently visible screen
        },

        // Finds text in a page
        // TODO: backwards seems impossible i fear :(
        find: function (str, backwards)
        {
            let fastFind = getBrowser().fastFind;

            processUserPattern(str);

            fastFind.caseSensitive = caseSensitive;
            found = fastFind.find(searchString, linksOnly) != Ci.nsITypeAheadFind.FIND_NOTFOUND;

            if (!found)
                setTimeout(function () liberator.echoerr("E486: Pattern not found: " + searchPattern, commandline.FORCE_SINGLELINE), 0);

            return found;
        },

        // Called when the current search needs to be repeated
        findAgain: function (reverse)
        {
            // this hack is needed to make n/N work with the correct string, if
            // we typed /foo<esc> after the original search.  Since searchString is
            // readonly we have to call find() again to update it.
            if (getBrowser().fastFind.searchString != lastSearchString)
                this.find(lastSearchString, false);

            let up = reverse ? !lastSearchBackwards : lastSearchBackwards;
            let result = getBrowser().fastFind.findAgain(up, linksOnly);

            if (result == Ci.nsITypeAheadFind.FIND_NOTFOUND)
            {
                liberator.echoerr("E486: Pattern not found: " + lastSearchPattern, commandline.FORCE_SINGLELINE);
            }
            else if (result == Ci.nsITypeAheadFind.FIND_WRAPPED)
            {
                // hack needed, because wrapping causes a "scroll" event which clears
                // our command line
                setTimeout(function () {
                    if (up)
                        commandline.echo("search hit TOP, continuing at BOTTOM",
                            commandline.HL_WARNINGMSG, commandline.APPEND_TO_MESSAGES | commandline.FORCE_SINGLELINE);
                    else
                        commandline.echo("search hit BOTTOM, continuing at TOP",
                            commandline.HL_WARNINGMSG, commandline.APPEND_TO_MESSAGES | commandline.FORCE_SINGLELINE);
                }, 0);
            }
            else
            {
                commandline.echo((up ? "?" : "/") + lastSearchPattern, null, commandline.FORCE_SINGLELINE);

                if (options["hlsearch"])
                    this.highlight(lastSearchString);
            }
        },

        // Called when the user types a key in the search dialog. Triggers a find attempt if 'incsearch' is set
        searchKeyPressed: function (command)
        {
            if (options["incsearch"])
                this.find(command, backwards);
        },

        // Called when the enter key is pressed to trigger a search
        // use forcedBackward if you call this function directly
        searchSubmitted: function (command, forcedBackward)
        {
            if (typeof forcedBackward === "boolean")
                backwards = forcedBackward;

            // use the last pattern if none specified
            if (!command)
                command = lastSearchPattern;

            if (!options["incsearch"] || !found)
            {
                this.clear();
                this.find(command, backwards);
            }

            lastSearchBackwards = backwards;
            //lastSearchPattern = command.replace(backwards ? /\?.*/ : /\/.*/, ""); // XXX
            lastSearchPattern = command;
            lastSearchString = searchString;

            // TODO: move to find() when reverse incremental searching is kludged in
            // need to find again for reverse searching
            if (backwards)
                setTimeout(function () { search.findAgain(false); }, 0);

            if (options["hlsearch"])
                this.highlight(searchString);

            modes.reset();
        },

        // Called when the search is canceled - for example if someone presses
        // escape while typing a search
        searchCanceled: function ()
        {
            this.clear();
            // TODO: code to reposition the document to the place before search started
        },

        // FIXME: thunderbird incompatible
        // this is not dependent on the value of 'hlsearch'
        highlight: function (text)
        {
            if (config.name == "Muttator")
                return;

            // already highlighted?
            if (highlightObj.getSpans(content.document).snapshotLength > 0)
                return;

            if (!text)
                text = lastSearchString;

            highlightObj.highlightDoc(window.content, text);

            // recreate selection since _highlightDoc collapses the selection backwards
            getBrowser().fastFind.findAgain(false, linksOnly);

            // TODO: remove highlighting from non-link matches (HTML - A/AREA with href attribute; XML - Xlink [type="simple"])
        },

        clear: function ()
        {
            highlightObj.highlightDoc(window.content);
            // need to manually collapse the selection if the document is not
            // highlighted
            getBrowser().fastFind.collapseSelection();
        }

    };
    //}}}
}; //}}}

// vim: set fdm=marker sw=4 ts=4 et: