git ssb

0+

cel / text-node-searcher



Commit 92ca0b89c1929fc689ca0acc47d9f44252a3f1fb

Fix wrapping behavior

Charles Lehner committed on 12/19/2015, 4:22:39 AM
Parent: 2eaf7a5bf2fb270f40ecd2aea5f6453eb7330591

Files changed

index.jschanged
index.jsView
@@ -52,42 +52,34 @@
5252 function shouldDescendInto(node) {
5353 return node.nodeName != "SCRIPT" && node.nodeName != "STYLE";
5454 }
5555
56-function getNextTextNode(node, container, wrap) {
57- outer: do {
56 +function getNextTextNode(node, container) {
57 + do {
5858 if (shouldDescendInto(node) && node.firstChild) {
5959 node = node.firstChild;
60- } else if (node.nextSibling) {
61- node = node.nextSibling;
6260 } else {
63- do {
64- if (node == container) {
65- if (!wrap)
66- return null;
67- wrap = false;
68- continue outer;
69- }
61 + while (!node.nextSibling) {
7062 node = node.parentNode;
71- if (!node)
72- node = container;
73- } while (!node.nextSibling);
63 + if (node == container || !node)
64 + return null;
65 + }
7466 node = node.nextSibling;
7567 }
7668 } while (node.nodeType != node.TEXT_NODE);
7769 return node;
7870 }
7971
80-function getPreviousTextNode(node, container, wrap) {
72 +function getPreviousTextNode(node, container) {
73 + if (node == container) {
74 + while (node.lastChild && shouldDescendInto(node))
75 + node = node.lastChild;
76 + if (node.nodeType == node.TEXT_NODE)
77 + return node;
78 + }
8179 do {
82- if (!node) {
83- node = container;
84- } else if (node == container) {
85- if (!wrap)
86- return null;
87- while (shouldDescendInto(node) && node.lastChild)
88- node = node.lastChild;
89- wrap = false;
80 + if (!node || node == container) {
81 + return null;
9082 } else if (node.previousSibling) {
9183 node = node.previousSibling;
9284 while (shouldDescendInto(node) && node.lastChild)
9385 node = node.lastChild;
@@ -113,16 +105,16 @@
113105 var sel = window.getSelection();
114106 var startNode = sel.focusNode;
115107 var startOffset = 0;
116108 if (!startNode || !this.container.contains(startNode))
117- startNode = getNextTextNode(this.container, this.container, true);
109 + startNode = getNextTextNode(this.container, this.container);
118110 else if (startNode.nodeType != startNode.TEXT_NODE)
119- startNode = getNextTextNode(startNode, this.container, true);
111 + startNode = getNextTextNode(startNode, this.container);
120112 else
121113 startOffset = sel.focusOffset;
122114
123- for (var node = startNode; node;
124- node = getNextTextNode(node, this.container, true)) {
115 + var wrapped = false;
116 + for (var node = startNode; node;) {
125117 var str = node.data;
126118 this.query.lastIndex = startOffset;
127119 if (startOffset)
128120 startOffset = 0;
@@ -131,8 +123,15 @@
131123 setSelection(node, m.index, node, m.index + m[0].length);
132124 node.parentNode.scrollIntoView(false);
133125 return;
134126 }
127 + node = getNextTextNode(node, this.container);
128 + if (!node) {
129 + if (wrapped)
130 + return;
131 + wrapped = true;
132 + node = getNextTextNode(this.container, this.container);
133 + }
135134 }
136135 };
137136
138137 TextNodeSearcher.prototype.selectPrevious = function () {
@@ -142,27 +141,34 @@
142141 var sel = window.getSelection();
143142 var endNode = sel.anchorNode;
144143 var endOffset = 0;
145144 if (!endNode || !this.container.contains(endNode))
146- endNode = getPreviousTextNode(endNode, this.container, true);
145 + endNode = getPreviousTextNode(this.container, this.container);
147146 else if (endNode.nodeType != endNode.TEXT_NODE)
148- endNode = getPreviousTextNode(this.container, this.container, true);
147 + endNode = getPreviousTextNode(endNode, this.container);
149148 else
150149 endOffset = sel.anchorOffset;
151150
152- for (var node = endNode; node;
153- node = getPreviousTextNode(node, this.container, true)) {
151 + var wrapped = false;
152 + for (var node = endNode; node;) {
154153 var str = node.data;
155154 if (endOffset < Infinity) {
156155 str = node.data.substr(0, endOffset);
157156 endOffset = Infinity;
158157 }
159158 var m = matchLast(this.query, str);
160159 if (m) {
161160 setSelection(node, m.index, node, m.index + m[0].length);
162- node.parentNode.scrollIntoView(true);
161 + node.parentNode.scrollIntoView(false);
163162 return;
164163 }
164 + node = getPreviousTextNode(node, this.container);
165 + if (!node) {
166 + if (wrapped)
167 + return;
168 + wrapped = true;
169 + node = getPreviousTextNode(this.container, this.container);
170 + }
165171 }
166172 };
167173
168174 if (typeof module != "undefined")

Built with git-ssb-web