git ssb

0+

cel / text-node-searcher



Commit a7f543f01ad4471c001726e7a96a4048c7824f8e

Try simplifying

Only search within each text node
Charles Lehner committed on 12/18/2015, 4:53:05 PM
Parent: cd6426ffba8d4fca02f45ca0551179c8564a72fd

Files changed

index.jschanged
index.jsView
@@ -51,117 +51,108 @@
5151 .appendChild(document.createTextNode(str));
5252 }
5353 window.log = log;
5454
55-function getFirstTextNode(container) {
56- return getNextTextNode(container, container);
57-}
58-
59-function getNextTextNode(node, container) {
55 +function getNextTextNode(node, container, wrap) {
6056 do {
6157 if (node.firstChild) {
62- console.log('firstchild')
58 + console.log('firstchild');
6359 node = node.firstChild;
6460 } else if (node.nextSibling) {
65- console.log('nextsib')
61 + console.log('nextsib');
6662 node = node.nextSibling;
6763 } else {
6864 do {
6965 if (node == container)
70- return null;
71- console.log('parent')
66 + return wrap ? getFirstTextNode(container) : null;
67 + console.log('parent');
7268 node = node.parentNode;
7369 } while (!node.nextSibling);
74- console.log('next sib')
70 + console.log('next sib');
7571 node = node.nextSibling;
7672 }
7773 } while (node.nodeType != Node.TEXT_NODE);
78- console.log('ret', node.nodeValue)
7974 return node;
8075 }
8176
82-function nodeValue(node) {
83- return node.nodeValue;
77 +function 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;
8491 }
8592
86-function textNodesToString(nodes) {
87- return nodes.map(nodeValue).join("");
93 +function getFirstTextNode(container) {
94 + return getNextTextNode(container, container);
8895 }
8996
97 +function getLastTextNode(container) {
98 + return getPrevTextNode(container, container);
99 +}
100 +
90101 Searcher.prototype.selectNext = function () {
91102 if (!this.queryLen)
92103 return;
93104
94105 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)
106 + var startNode = sel.focusNode || getFirstTextNode(this.container);
107 + var startOffset = sel.focusOffset;
108 + if (!startNode)
109 + return;
110 + if (!startNode.nodeValue)
111 + console.log('start', startNode);
112 +
113 + for (var node = startNode, str = node.nodeValue.substr(startOffset);
114 + node;
115 + node = getNextTextNode(node, this.container, true),
116 + str = node.nodeValue)
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);
104123 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();
119124 }
120- startNode = textNodes[0];
121- startOffset = i;
122- // m[0].length
123- setSelection(startNode, startOffset,
124- startNode, startOffset + 1);
125125 }
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- */
135126 };
136127
137128 Searcher.prototype.selectPrev = function () {
138-};
129 + if (!this.queryLen)
130 + return;
139131
140-/*
141-function 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;
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.nodeValue)
138 + console.log('end', endNode);
139 +
140 + for (var node = endNode, str = endNode.nodeValue.substr(0, endOffset);
141 + node;
142 + node = getPrevTextNode(node, this.container, true),
143 + str = node.nodeValue)
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;
148151 }
149152 }
150-}
151-*/
153 +};
152154
153-/*
154-var range = document.createRange();
155-
156-range.setStart(startNode, startOffset);
157-range.setEnd(endNode, endOffset);
158-
159-var s = window.getSelection();
160-l.removeAllRanges();
161-s.addRange(range);
162-*/
163-
164155 if (global.module)
165156 module.exports = Searcher;
166157 else
167158 global.Searcher = Searcher;

Built with git-ssb-web