Fixed time zone calculation issue
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / 3rdParty / codemirror / addon / search / match-highlighter.js
1 // Highlighting text that matches the selection
2 //
3 // Defines an option highlightSelectionMatches, which, when enabled,
4 // will style strings that match the selection throughout the
5 // document.
6 //
7 // The option can be set to true to simply enable it, or to a
8 // {minChars, style} object to explicitly configure it. minChars is
9 // the minimum amount of characters that should be selected for the
10 // behavior to occur, and style is the token style to apply to the
11 // matches. This will be prefixed by "cm-" to create an actual CSS
12 // class name.
13
14 (function() {
15 var DEFAULT_MIN_CHARS = 2;
16 var DEFAULT_TOKEN_STYLE = "matchhighlight";
17
18 function State(options) {
19 this.minChars = typeof options == "object" && options.minChars || DEFAULT_MIN_CHARS;
20 this.style = typeof options == "object" && options.style || DEFAULT_TOKEN_STYLE;
21 this.overlay = null;
22 }
23
24 CodeMirror.defineOption("highlightSelectionMatches", false, function(cm, val, old) {
25 var prev = old && old != CodeMirror.Init;
26 if (val && !prev) {
27 cm.state.matchHighlighter = new State(val);
28 cm.on("cursorActivity", highlightMatches);
29 } else if (!val && prev) {
30 var over = cm.state.matchHighlighter.overlay;
31 if (over) cm.removeOverlay(over);
32 cm.state.matchHighlighter = null;
33 cm.off("cursorActivity", highlightMatches);
34 }
35 });
36
37 function highlightMatches(cm) {
38 cm.operation(function() {
39 var state = cm.state.matchHighlighter;
40 if (state.overlay) {
41 cm.removeOverlay(state.overlay);
42 state.overlay = null;
43 }
44
45 if (!cm.somethingSelected()) return;
46 var selection = cm.getSelection().replace(/^\s+|\s+$/g, "");
47 if (selection.length < state.minChars) return;
48
49 cm.addOverlay(state.overlay = makeOverlay(selection, state.style));
50 });
51 }
52
53 function makeOverlay(query, style) {
54 return {token: function(stream) {
55 if (stream.match(query)) return style;
56 stream.next();
57 stream.skipTo(query.charAt(0)) || stream.skipToEnd();
58 }};
59 }
60 })();