git ssb

0+

cel / text-node-searcher



Tree: 79bc3a2162c5397f0fc094b0cde3038530cdfd28

Files: 79bc3a2162c5397f0fc094b0cde3038530cdfd28 / index.js

3648 bytesRaw
1(function (global) {
2
3function 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
15function 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
25function Searcher(container) {
26 this.container = container;
27}
28
29Searcher.prototype.highlight = function () {
30 // if (this.isHighlighted)
31
32};
33
34Searcher.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
43function log() {
44 var str;
45 try {
46 str = [].slice.call(arguments).join(", ");
47 } catch(e) {
48 str = e.message;
49 }
50 document.body.appendChild(document.createElement("pre"))
51 .appendChild(document.createTextNode(str));
52}
53window.log = log;
54
55function getNextTextNode(node, container, wrap) {
56 do {
57 if (node.firstChild) {
58 console.log('firstchild');
59 node = node.firstChild;
60 } else if (node.nextSibling) {
61 console.log('nextsib');
62 node = node.nextSibling;
63 } else {
64 do {
65 if (node == container)
66 return wrap ? getFirstTextNode(container) : null;
67 console.log('parent');
68 node = node.parentNode;
69 } while (!node.nextSibling);
70 console.log('next sib');
71 node = node.nextSibling;
72 }
73 } while (node.nodeType != Node.TEXT_NODE);
74 return node;
75}
76
77function getPrevTextNode(node, container, wrap) {
78 do {
79 if (node.previousSibling) {
80 console.log('previous sib');
81 node = node.previousSibling;
82 while (node.lastChild)
83 node = node.lastChild;
84 } else if (node.parentNode != container) {
85 node = node.parentNode;
86 } else {
87 return wrap ? getLastTextNode(container) : null;
88 }
89 } while (node.nodeType != Node.TEXT_NODE);
90 return node;
91}
92
93function getFirstTextNode(container) {
94 return getNextTextNode(container, container);
95}
96
97function getLastTextNode(container) {
98 return getPrevTextNode(container, container);
99}
100
101Searcher.prototype.selectNext = function () {
102 if (!this.queryLen)
103 return;
104
105 var sel = window.getSelection();
106 var startNode = sel.focusNode || getFirstTextNode(this.container);
107 var startOffset = sel.focusOffset;
108 if (!startNode)
109 return;
110 if (!startNode.data)
111 console.log('start', startNode);
112
113 for (var node = startNode, str = node.data.substr(startOffset);
114 node;
115 node = getNextTextNode(node, this.container, true),
116 str = node.data)
117 {
118 var m = str.match(this.query);
119 if (m) {
120 var i = m.index;
121 console.log('next node', node, 'index', i);
122 setSelection(node, i, node, i + m[0].length);
123 return;
124 }
125 }
126};
127
128Searcher.prototype.selectPrev = function () {
129 if (!this.queryLen)
130 return;
131
132 var sel = window.getSelection();
133 var endNode = sel.anchorNode || getLastTextNode(this.container);
134 var endOffset = sel.anchorOffset;
135 if (!endNode)
136 return;
137 if (!endNode.data)
138 console.log('end', endNode);
139
140 for (var node = endNode, str = endNode.data.substr(0, endOffset);
141 node;
142 node = getPrevTextNode(node, this.container, true),
143 str = node.data)
144 {
145 var m = str.match(this.query);
146 if (m) {
147 var i = m.index;
148 console.log('prev node', node, 'index', i);
149 setSelection(node, i, node, i + m[0].length);
150 return;
151 }
152 }
153};
154
155if (global.module)
156 module.exports = Searcher;
157else
158 global.Searcher = Searcher;
159}(this));
160

Built with git-ssb-web