git ssb

0+

cel / text-node-searcher



Commit cd6426ffba8d4fca02f45ca0551179c8564a72fd

Do stuff

Charles Lehner committed on 12/18/2015, 4:22:52 PM
Parent: 0cbc09f8c35d3f52a860beea7d78733855882fd0

Files changed

example.htmlchanged
index.jschanged
example.htmlView
@@ -31,16 +31,26 @@
3131 elit, dictum in tortor eu, congue semper augue. Phasellus interdum
3232 malesuada lorem.</p>
3333
3434 <script>
35- window.onerror = function (e) {
36- document.body.appendChild(document.createElement("blockquote"))
37- .appendChild(document.createTextNode(e));
35 + function toStr(obj) {
36 + var str = '';
37 + for (var key in obj) {
38 + if (str) str += ', ';
39 + str += key + ': ' + obj[key];
40 + }
41 + return '{' + str + '}';
3842 }
43 + window.addEventListener("error", function (e) {
44 + log(e.message, e.filename, e.lineno + ":" + e.colno)
45 + }, false);
46 + // document.body.appendChild(document.createElement("blockquote"))
47 + // .appendChild(document.createTextNode(e.toString()));
3948 </script>
4049 <script src="index.js"></script>
4150 <script>
4251 var searcher = new Searcher(document.body);
52 + searcher.setQuery("dolor");
4353 document.getElementById("search").onkeypress = function () {
4454 searcher.setQuery(this.value);
4555 // searcher.highlight();
4656 };
index.jsView
@@ -39,14 +39,100 @@
3939 this.queryLen = str.length;
4040 this.query = new RegExp(addAccents(str), "i");
4141 };
4242
43 +function 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 +}
53 +window.log = log;
54 +
55 +function getFirstTextNode(container) {
56 + return getNextTextNode(container, container);
57 +}
58 +
59 +function 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 +
82 +function nodeValue(node) {
83 + return node.nodeValue;
84 +}
85 +
86 +function textNodesToString(nodes) {
87 + return nodes.map(nodeValue).join("");
88 +}
89 +
4390 Searcher.prototype.selectNext = function () {
44- // Searcher_selectNext(this.container);
91 + if (!this.queryLen)
92 + return;
93 +
4594 var sel = window.getSelection();
46- // var range = sel.getRangeAt(0);
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 + /*
47132 setSelection(sel.anchorNode, sel.anchorOffset + 1,
48133 sel.extentNode, sel.extentOffset + 1);
134 + */
49135 };
50136
51137 Searcher.prototype.selectPrev = function () {
52138 };

Built with git-ssb-web