Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Highlighting text that matches the selection |
michael@0 | 2 | // |
michael@0 | 3 | // Defines an option highlightSelectionMatches, which, when enabled, |
michael@0 | 4 | // will style strings that match the selection throughout the |
michael@0 | 5 | // document. |
michael@0 | 6 | // |
michael@0 | 7 | // The option can be set to true to simply enable it, or to a |
michael@0 | 8 | // {minChars, style, showToken} object to explicitly configure it. |
michael@0 | 9 | // minChars is the minimum amount of characters that should be |
michael@0 | 10 | // selected for the behavior to occur, and style is the token style to |
michael@0 | 11 | // apply to the matches. This will be prefixed by "cm-" to create an |
michael@0 | 12 | // actual CSS class name. showToken, when enabled, will cause the |
michael@0 | 13 | // current token to be highlighted when nothing is selected. |
michael@0 | 14 | |
michael@0 | 15 | (function(mod) { |
michael@0 | 16 | if (typeof exports == "object" && typeof module == "object") // CommonJS |
michael@0 | 17 | mod(require("../../lib/codemirror")); |
michael@0 | 18 | else if (typeof define == "function" && define.amd) // AMD |
michael@0 | 19 | define(["../../lib/codemirror"], mod); |
michael@0 | 20 | else // Plain browser env |
michael@0 | 21 | mod(CodeMirror); |
michael@0 | 22 | })(function(CodeMirror) { |
michael@0 | 23 | "use strict"; |
michael@0 | 24 | |
michael@0 | 25 | var DEFAULT_MIN_CHARS = 2; |
michael@0 | 26 | var DEFAULT_TOKEN_STYLE = "matchhighlight"; |
michael@0 | 27 | var DEFAULT_DELAY = 100; |
michael@0 | 28 | |
michael@0 | 29 | function State(options) { |
michael@0 | 30 | if (typeof options == "object") { |
michael@0 | 31 | this.minChars = options.minChars; |
michael@0 | 32 | this.style = options.style; |
michael@0 | 33 | this.showToken = options.showToken; |
michael@0 | 34 | this.delay = options.delay; |
michael@0 | 35 | } |
michael@0 | 36 | if (this.style == null) this.style = DEFAULT_TOKEN_STYLE; |
michael@0 | 37 | if (this.minChars == null) this.minChars = DEFAULT_MIN_CHARS; |
michael@0 | 38 | if (this.delay == null) this.delay = DEFAULT_DELAY; |
michael@0 | 39 | this.overlay = this.timeout = null; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | CodeMirror.defineOption("highlightSelectionMatches", false, function(cm, val, old) { |
michael@0 | 43 | if (old && old != CodeMirror.Init) { |
michael@0 | 44 | var over = cm.state.matchHighlighter.overlay; |
michael@0 | 45 | if (over) cm.removeOverlay(over); |
michael@0 | 46 | clearTimeout(cm.state.matchHighlighter.timeout); |
michael@0 | 47 | cm.state.matchHighlighter = null; |
michael@0 | 48 | cm.off("cursorActivity", cursorActivity); |
michael@0 | 49 | } |
michael@0 | 50 | if (val) { |
michael@0 | 51 | cm.state.matchHighlighter = new State(val); |
michael@0 | 52 | highlightMatches(cm); |
michael@0 | 53 | cm.on("cursorActivity", cursorActivity); |
michael@0 | 54 | } |
michael@0 | 55 | }); |
michael@0 | 56 | |
michael@0 | 57 | function cursorActivity(cm) { |
michael@0 | 58 | var state = cm.state.matchHighlighter; |
michael@0 | 59 | clearTimeout(state.timeout); |
michael@0 | 60 | state.timeout = setTimeout(function() {highlightMatches(cm);}, state.delay); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | function highlightMatches(cm) { |
michael@0 | 64 | cm.operation(function() { |
michael@0 | 65 | var state = cm.state.matchHighlighter; |
michael@0 | 66 | if (state.overlay) { |
michael@0 | 67 | cm.removeOverlay(state.overlay); |
michael@0 | 68 | state.overlay = null; |
michael@0 | 69 | } |
michael@0 | 70 | if (!cm.somethingSelected() && state.showToken) { |
michael@0 | 71 | var re = state.showToken === true ? /[\w$]/ : state.showToken; |
michael@0 | 72 | var cur = cm.getCursor(), line = cm.getLine(cur.line), start = cur.ch, end = start; |
michael@0 | 73 | while (start && re.test(line.charAt(start - 1))) --start; |
michael@0 | 74 | while (end < line.length && re.test(line.charAt(end))) ++end; |
michael@0 | 75 | if (start < end) |
michael@0 | 76 | cm.addOverlay(state.overlay = makeOverlay(line.slice(start, end), re, state.style)); |
michael@0 | 77 | return; |
michael@0 | 78 | } |
michael@0 | 79 | if (cm.getCursor("head").line != cm.getCursor("anchor").line) return; |
michael@0 | 80 | var selection = cm.getSelections()[0].replace(/^\s+|\s+$/g, ""); |
michael@0 | 81 | if (selection.length >= state.minChars) |
michael@0 | 82 | cm.addOverlay(state.overlay = makeOverlay(selection, false, state.style)); |
michael@0 | 83 | }); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | function boundariesAround(stream, re) { |
michael@0 | 87 | return (!stream.start || !re.test(stream.string.charAt(stream.start - 1))) && |
michael@0 | 88 | (stream.pos == stream.string.length || !re.test(stream.string.charAt(stream.pos))); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | function makeOverlay(query, hasBoundary, style) { |
michael@0 | 92 | return {token: function(stream) { |
michael@0 | 93 | if (stream.match(query) && |
michael@0 | 94 | (!hasBoundary || boundariesAround(stream, hasBoundary))) |
michael@0 | 95 | return style; |
michael@0 | 96 | stream.next(); |
michael@0 | 97 | stream.skipTo(query.charAt(0)) || stream.skipToEnd(); |
michael@0 | 98 | }}; |
michael@0 | 99 | } |
michael@0 | 100 | }); |