git ssb

0+

cel / text-node-searcher



Tree: cd6426ffba8d4fca02f45ca0551179c8564a72fd

Files: cd6426ffba8d4fca02f45ca0551179c8564a72fd / index.js

3792 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 getFirstTextNode(container) {
56 return getNextTextNode(container, container);
57}
58
59function getNextTextNode(node, container) {
60 do {
61 if (node.firstChild) {
62 console.log('firstchild')
63 node = node.firstChild;
64 } else if (node.nextSibling) {
65 console.log('nextsib')
66 node = node.nextSibling;
67 } else {
68 do {
69 if (node == container)
70 return null;
71 console.log('parent')
72 node = node.parentNode;
73 } while (!node.nextSibling);
74 console.log('next sib')
75 node = node.nextSibling;
76 }
77 } while (node.nodeType != Node.TEXT_NODE);
78 console.log('ret', node.nodeValue)
79 return node;
80}
81
82function nodeValue(node) {
83 return node.nodeValue;
84}
85
86function textNodesToString(nodes) {
87 return nodes.map(nodeValue).join("");
88}
89
90Searcher.prototype.selectNext = function () {
91 if (!this.queryLen)
92 return;
93
94 var sel = window.getSelection();
95 var startNode = sel.extentNode || getFirstTextNode(this.container);
96 var startOffset = sel.extentOffset;
97 var textNodesTextLen = startNode.nodeValue.length - startOffset;
98 var textNodes = [startNode];
99 var lastTextNode = startNode;
100 while (textNodesTextLen < this.queryLen) {
101 // console.log('add another.', textNodesTextLen)
102 lastTextNode = getNextTextNode(lastTextNode, this.container);
103 if (!lastTextNode)
104 return;
105 textNodes.push(lastTextNode);
106 textNodesTextLen += lastTextNode.nodeValue.length;
107 }
108 var str = textNodesToString(textNodes);
109 log(textNodesTextLen, textNodes.length, str);
110 textNodesToString(textNodes);
111 var m = str.search(this.query);
112 if (m) {
113 var i = m.index;
114 var firstNodeLen = textNodes[0].nodeValue.length;
115 while (i > firstNodeLen) {
116 textNodesTextLen -= firstNodeLen;
117 i -= firstNodeLen;
118 textNodes.shift();
119 }
120 startNode = textNodes[0];
121 startOffset = i;
122 // m[0].length
123 setSelection(startNode, startOffset,
124 startNode, startOffset + 1);
125 }
126 // set selection
127
128
129
130 // log(node, offset);
131 /*
132 setSelection(sel.anchorNode, sel.anchorOffset + 1,
133 sel.extentNode, sel.extentOffset + 1);
134 */
135};
136
137Searcher.prototype.selectPrev = function () {
138};
139
140/*
141function Searcher_selectNext(parentNode) {
142 for (var el = parentNode.firstChild; el; el = el.nextSibling) {
143 if (el.firstChild) {
144 Searcher_selectNext(parentNode);
145 } else if (el.nodeType == 3) { // NODE_TEXT
146 var text = el.nodeValue;
147 void text;
148 }
149 }
150}
151*/
152
153/*
154var range = document.createRange();
155
156range.setStart(startNode, startOffset);
157range.setEnd(endNode, endOffset);
158
159var s = window.getSelection();
160l.removeAllRanges();
161s.addRange(range);
162*/
163
164if (global.module)
165 module.exports = Searcher;
166else
167 global.Searcher = Searcher;
168}(this));
169

Built with git-ssb-web