Commit 92ca0b89c1929fc689ca0acc47d9f44252a3f1fb
Fix wrapping behavior
Charles Lehner committed on 12/19/2015, 4:22:39 AMParent: 2eaf7a5bf2fb270f40ecd2aea5f6453eb7330591
Files changed
index.js | changed |
index.js | |||
---|---|---|---|
@@ -52,42 +52,34 @@ | |||
52 | 52 … | function shouldDescendInto(node) { | |
53 | 53 … | return node.nodeName != "SCRIPT" && node.nodeName != "STYLE"; | |
54 | 54 … | } | |
55 | 55 … | ||
56 | -function getNextTextNode(node, container, wrap) { | ||
57 | - outer: do { | ||
56 … | +function getNextTextNode(node, container) { | ||
57 … | + do { | ||
58 | 58 … | if (shouldDescendInto(node) && node.firstChild) { | |
59 | 59 … | node = node.firstChild; | |
60 | - } else if (node.nextSibling) { | ||
61 | - node = node.nextSibling; | ||
62 | 60 … | } else { | |
63 | - do { | ||
64 | - if (node == container) { | ||
65 | - if (!wrap) | ||
66 | - return null; | ||
67 | - wrap = false; | ||
68 | - continue outer; | ||
69 | - } | ||
61 … | + while (!node.nextSibling) { | ||
70 | 62 … | node = node.parentNode; | |
71 | - if (!node) | ||
72 | - node = container; | ||
73 | - } while (!node.nextSibling); | ||
63 … | + if (node == container || !node) | ||
64 … | + return null; | ||
65 … | + } | ||
74 | 66 … | node = node.nextSibling; | |
75 | 67 … | } | |
76 | 68 … | } while (node.nodeType != node.TEXT_NODE); | |
77 | 69 … | return node; | |
78 | 70 … | } | |
79 | 71 … | ||
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 … | + } | ||
81 | 79 … | 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; | ||
90 | 82 … | } else if (node.previousSibling) { | |
91 | 83 … | node = node.previousSibling; | |
92 | 84 … | while (shouldDescendInto(node) && node.lastChild) | |
93 | 85 … | node = node.lastChild; | |
@@ -113,16 +105,16 @@ | |||
113 | 105 … | var sel = window.getSelection(); | |
114 | 106 … | var startNode = sel.focusNode; | |
115 | 107 … | var startOffset = 0; | |
116 | 108 … | if (!startNode || !this.container.contains(startNode)) | |
117 | - startNode = getNextTextNode(this.container, this.container, true); | ||
109 … | + startNode = getNextTextNode(this.container, this.container); | ||
118 | 110 … | else if (startNode.nodeType != startNode.TEXT_NODE) | |
119 | - startNode = getNextTextNode(startNode, this.container, true); | ||
111 … | + startNode = getNextTextNode(startNode, this.container); | ||
120 | 112 … | else | |
121 | 113 … | startOffset = sel.focusOffset; | |
122 | 114 … | ||
123 | - for (var node = startNode; node; | ||
124 | - node = getNextTextNode(node, this.container, true)) { | ||
115 … | + var wrapped = false; | ||
116 … | + for (var node = startNode; node;) { | ||
125 | 117 … | var str = node.data; | |
126 | 118 … | this.query.lastIndex = startOffset; | |
127 | 119 … | if (startOffset) | |
128 | 120 … | startOffset = 0; | |
@@ -131,8 +123,15 @@ | |||
131 | 123 … | setSelection(node, m.index, node, m.index + m[0].length); | |
132 | 124 … | node.parentNode.scrollIntoView(false); | |
133 | 125 … | return; | |
134 | 126 … | } | |
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 … | + } | ||
135 | 134 … | } | |
136 | 135 … | }; | |
137 | 136 … | ||
138 | 137 … | TextNodeSearcher.prototype.selectPrevious = function () { | |
@@ -142,27 +141,34 @@ | |||
142 | 141 … | var sel = window.getSelection(); | |
143 | 142 … | var endNode = sel.anchorNode; | |
144 | 143 … | var endOffset = 0; | |
145 | 144 … | if (!endNode || !this.container.contains(endNode)) | |
146 | - endNode = getPreviousTextNode(endNode, this.container, true); | ||
145 … | + endNode = getPreviousTextNode(this.container, this.container); | ||
147 | 146 … | else if (endNode.nodeType != endNode.TEXT_NODE) | |
148 | - endNode = getPreviousTextNode(this.container, this.container, true); | ||
147 … | + endNode = getPreviousTextNode(endNode, this.container); | ||
149 | 148 … | else | |
150 | 149 … | endOffset = sel.anchorOffset; | |
151 | 150 … | ||
152 | - for (var node = endNode; node; | ||
153 | - node = getPreviousTextNode(node, this.container, true)) { | ||
151 … | + var wrapped = false; | ||
152 … | + for (var node = endNode; node;) { | ||
154 | 153 … | var str = node.data; | |
155 | 154 … | if (endOffset < Infinity) { | |
156 | 155 … | str = node.data.substr(0, endOffset); | |
157 | 156 … | endOffset = Infinity; | |
158 | 157 … | } | |
159 | 158 … | var m = matchLast(this.query, str); | |
160 | 159 … | if (m) { | |
161 | 160 … | setSelection(node, m.index, node, m.index + m[0].length); | |
162 | - node.parentNode.scrollIntoView(true); | ||
161 … | + node.parentNode.scrollIntoView(false); | ||
163 | 162 … | return; | |
164 | 163 … | } | |
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 … | + } | ||
165 | 171 … | } | |
166 | 172 … | }; | |
167 | 173 … | ||
168 | 174 … | if (typeof module != "undefined") |
Built with git-ssb-web