Files: 0cbc09f8c35d3f52a860beea7d78733855882fd0 / index.js
1800 bytesRaw
1 | (function (global) { |
2 | |
3 | function addAccents(str) { |
4 | // http://www.the-art-of-web.com/javascript/search-highlight/ |
5 | return str |
6 | // .replace(/([ao])e/ig, "$1") |
7 | .replace(/e/ig, "[eèéêë]") |
8 | .replace(/a/ig, "([aàâä]|ae)") |
9 | .replace(/i/ig, "[iîï]") |
10 | .replace(/o/ig, "([oôö]|oe)") |
11 | .replace(/u/ig, "[uùûü]") |
12 | .replace(/y/ig, "[yÿ]"); |
13 | } |
14 | |
15 | function setSelection(startNode, startOffset, endNode, endOffset) { |
16 | var range = document.createRange(); |
17 | range.setStart(startNode, startOffset); |
18 | range.setEnd(endNode, endOffset); |
19 | |
20 | var sel = window.getSelection(); |
21 | sel.removeAllRanges(); |
22 | sel.addRange(range); |
23 | } |
24 | |
25 | function Searcher(container) { |
26 | this.container = container; |
27 | } |
28 | |
29 | Searcher.prototype.highlight = function () { |
30 | // if (this.isHighlighted) |
31 | |
32 | }; |
33 | |
34 | Searcher.prototype.setQuery = function (str) { |
35 | if (str == this.queryStr) |
36 | return; |
37 | |
38 | this.queryStr = str; |
39 | this.queryLen = str.length; |
40 | this.query = new RegExp(addAccents(str), "i"); |
41 | }; |
42 | |
43 | Searcher.prototype.selectNext = function () { |
44 | // Searcher_selectNext(this.container); |
45 | var sel = window.getSelection(); |
46 | // var range = sel.getRangeAt(0); |
47 | setSelection(sel.anchorNode, sel.anchorOffset + 1, |
48 | sel.extentNode, sel.extentOffset + 1); |
49 | }; |
50 | |
51 | Searcher.prototype.selectPrev = function () { |
52 | }; |
53 | |
54 | /* |
55 | function Searcher_selectNext(parentNode) { |
56 | for (var el = parentNode.firstChild; el; el = el.nextSibling) { |
57 | if (el.firstChild) { |
58 | Searcher_selectNext(parentNode); |
59 | } else if (el.nodeType == 3) { // NODE_TEXT |
60 | var text = el.nodeValue; |
61 | void text; |
62 | } |
63 | } |
64 | } |
65 | */ |
66 | |
67 | /* |
68 | var range = document.createRange(); |
69 | |
70 | range.setStart(startNode, startOffset); |
71 | range.setEnd(endNode, endOffset); |
72 | |
73 | var s = window.getSelection(); |
74 | l.removeAllRanges(); |
75 | s.addRange(range); |
76 | */ |
77 | |
78 | if (global.module) |
79 | module.exports = Searcher; |
80 | else |
81 | global.Searcher = Searcher; |
82 | }(this)); |
83 |
Built with git-ssb-web