git ssb

0+

cel / text-node-searcher



Commit 039e38b052d8968548692f64b0458301b8cfe09c

Fix things

Escape regexp chars
Fix iteration edge cases
Charles Lehner committed on 12/18/2015, 8:17:52 PM
Parent: bccf846503be4782b7827153f6920c3500a14584

Files changed

example.htmlchanged
index.jschanged
example.htmlView
@@ -10,8 +10,10 @@
1010 <input type="button" id="prev-btn" value="&larr;"/>
1111 <input type="button" id="next-btn" value="&rarr;"/>
1212 </div>
1313
14 + <p>[brackets] (parens) $ ^ [a-z] ? * . + {3}</p>
15 +
1416 <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum
1517 id mauris odio. Morbi efficitur tellus tellus, sit amet viverra nibh
1618 condimentum ac. Morbi lectus urna, hendrerit sed dui sit amet,
1719 efficitur porta orci. Curabitur et mauris varius, suscipit metus
index.jsView
@@ -22,8 +22,12 @@
2222 .replace(/u/ig, "[uùûü]")
2323 .replace(/y/ig, "[yÿ]");
2424 }
2525
26 +function quoteRegex(str) {
27 + return str.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
28 +}
29 +
2630 function setSelection(startNode, startOffset, endNode, endOffset) {
2731 var range = document.createRange();
2832 range.setStart(startNode, startOffset);
2933 range.setEnd(endNode, endOffset);
@@ -41,9 +45,9 @@
4145 if (str == this.queryStr)
4246 return;
4347
4448 this.queryStr = str;
45- this.query = new RegExp(addAccents(str), "ig");
49 + this.query = new RegExp(addAccents(quoteRegex(str)), "ig");
4650 };
4751
4852 function shouldDescendInto(node) {
4953 return node.nodeName != "SCRIPT" && node.nodeName != "STYLE";
@@ -104,10 +108,11 @@
104108
105109 var sel = window.getSelection();
106110 var startNode = sel.focusNode;
107111 var startOffset = 0;
108- if (!startNode || startNode.nodeType != Node.TEXT_NODE ||
109- !this.container.contains(startNode))
112 + if (!startNode || !this.container.contains(startNode))
113 + startNode = getNextTextNode(this.container, this.container, true);
114 + else if (startNode.nodeType != Node.TEXT_NODE)
110115 startNode = getNextTextNode(startNode, this.container, true);
111116 else
112117 startOffset = sel.focusOffset;
113118
@@ -131,11 +136,12 @@
131136
132137 var sel = window.getSelection();
133138 var endNode = sel.anchorNode;
134139 var endOffset = 0;
135- if (!endNode || endNode.nodeType != Node.TEXT_NODE ||
136- !this.container.contains(endNode))
140 + if (!endNode || !this.container.contains(endNode))
137141 endNode = getPreviousTextNode(endNode, this.container, true);
142 + else if (endNode.nodeType != Node.TEXT_NODE)
143 + endNode = getPreviousTextNode(this.container, this.container, true);
138144 else
139145 endOffset = sel.anchorOffset;
140146
141147 for (var node = endNode; node;

Built with git-ssb-web