annotate findWord.js @ 117:4be71cc75f64

on click now
author ht
date Sat, 23 Mar 2019 18:09:20 +0000
parents 45b039236dc0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
115
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
1 // Thanks to https://stackoverflow.com/a/30586239 for word-finder
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
2 $(function() {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
3 // Get the HTML in #hoverText - just a wrapper for convenience
117
4be71cc75f64 on click now
ht
parents: 116
diff changeset
4 //var $hoverText = $("#hoverText");
115
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
5
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
6 // Get the full word the cursor is over regardless of span breaks
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
7 function getFullWord(event) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
8 var i, begin, end, range, textNode, offset;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
9
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
10 // Internet Explorer
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
11 if (document.body.createTextRange) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
12 try {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
13 range = document.body.createTextRange();
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
14 range.moveToPoint(event.clientX, event.clientY);
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
15 range.select();
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
16 range = getTextRangeBoundaryPosition(range, true);
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
17
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
18 textNode = range.node;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
19 offset = range.offset;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
20 } catch(e) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
21 return ""; // Sigh, IE
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
22 }
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
23 }
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
24
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
25 // Firefox, Safari
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
26 // REF: https://developer.mozilla.org/en-US/docs/Web/API/Document/caretPositionFromPoint
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
27 else if (document.caretPositionFromPoint) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
28 range = document.caretPositionFromPoint(event.clientX, event.clientY);
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
29 textNode = range.offsetNode;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
30 offset = range.offset;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
31
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
32 // Chrome
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
33 // REF: https://developer.mozilla.org/en-US/docs/Web/API/document/caretRangeFromPoint
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
34 } else if (document.caretRangeFromPoint) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
35 range = document.caretRangeFromPoint(event.clientX, event.clientY);
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
36 textNode = range.startContainer;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
37 offset = range.startOffset;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
38 }
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
39
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
40 // Only act on text nodes
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
41 if (!textNode || textNode.nodeType !== Node.TEXT_NODE) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
42 return "";
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
43 }
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
44
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
45 var data = textNode.textContent;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
46
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
47 // Sometimes the offset can be at the 'length' of the data.
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
48 // It might be a bug with this 'experimental' feature
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
49 // Compensate for this below
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
50 if (offset >= data.length) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
51 offset = data.length - 1;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
52 }
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
53
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
54 // Ignore the cursor on spaces - these aren't words
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
55 if (isW(data[offset])) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
56 return "";
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
57 }
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
58
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
59 // Scan behind the current character until whitespace is found, or beginning
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
60 i = begin = end = offset;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
61 while (i > 0 && !isW(data[i - 1])) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
62 i--;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
63 }
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
64 begin = i;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
65
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
66 // Scan ahead of the current character until whitespace is found, or end
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
67 i = offset;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
68 while (i < data.length - 1 && !isW(data[i + 1])) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
69 i++;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
70 }
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
71 end = i;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
72
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
73 // This is our temporary word
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
74 var word = data.substring(begin, end + 1);
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
75
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
76 // Demo only
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
77 showBridge(null, null, null);
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
78
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
79 // If at a node boundary, cross over and see what
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
80 // the next word is and check if this should be added to our temp word
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
81 if (end === data.length - 1 || begin === 0) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
82
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
83 var nextNode = getNextNode(textNode);
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
84 var prevNode = getPrevNode(textNode);
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
85
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
86 // Get the next node text
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
87 if (end == data.length - 1 && nextNode) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
88 var nextText = nextNode.textContent;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
89
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
90 // Demo only
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
91 showBridge(word, nextText, null);
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
92
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
93 // Add the letters from the next text block until a whitespace, or end
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
94 i = 0;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
95 while (i < nextText.length && !isW(nextText[i])) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
96 word += nextText[i++];
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
97 }
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
98
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
99 } else if (begin === 0 && prevNode) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
100 // Get the previous node text
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
101 var prevText = prevNode.textContent;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
102
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
103 // Demo only
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
104 // HST: showBridge(word, null, prevText);
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
105
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
106 // Add the letters from the next text block until a whitespace, or end
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
107 i = prevText.length - 1;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
108 while (i >= 0 && !isW(prevText[i])) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
109 word = prevText[i--] + word;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
110 }
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
111 }
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
112 }
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
113 return word;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
114 }
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
115
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
116 // Return the word the cursor is over
117
4be71cc75f64 on click now
ht
parents: 116
diff changeset
117 $("p").click(function(e) {
115
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
118 var word = getFullWord(e);
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
119 if (word !== "") {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
120 var res=$("#result");
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
121 var pageX = event.pageX;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
122 var pageY = event.pageY;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
123 //var parentOffset = jQuery('ul li.large', this).parent().offset();
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
124 //var relX = pageX - parentOffset.left;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
125 //var relY = pageY - parentOffset.top;
116
45b039236dc0 cleaner
ht
parents: 115
diff changeset
126 res.css("visibility","visible");
117
4be71cc75f64 on click now
ht
parents: 116
diff changeset
127 res.css("left",(pageX-10) + "px").css("top",(pageY-15) + "px");
115
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
128 res.text(word);
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
129 }
116
45b039236dc0 cleaner
ht
parents: 115
diff changeset
130 });
117
4be71cc75f64 on click now
ht
parents: 116
diff changeset
131 $("#result").mousemove(function(e) {
116
45b039236dc0 cleaner
ht
parents: 115
diff changeset
132 $("#result").css("visibility","hidden");
45b039236dc0 cleaner
ht
parents: 115
diff changeset
133 });
117
4be71cc75f64 on click now
ht
parents: 116
diff changeset
134 $("#result").click(function(e) {
4be71cc75f64 on click now
ht
parents: 116
diff changeset
135 alert(e.target.innerHTML + " clicked");
4be71cc75f64 on click now
ht
parents: 116
diff changeset
136 $("#result").css("visibility","hidden");
4be71cc75f64 on click now
ht
parents: 116
diff changeset
137 });
4be71cc75f64 on click now
ht
parents: 116
diff changeset
138 // $("p").mousemove(function(e) {
4be71cc75f64 on click now
ht
parents: 116
diff changeset
139 // $("#result").css("visibility","hidden");
4be71cc75f64 on click now
ht
parents: 116
diff changeset
140 // });
115
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
141 });
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
142
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
143 // Helper functions
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
144
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
145 // Whitespace checker
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
146 function isW(s) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
147 return /[ \f\n\r\t\v\u00A0\u2028\u2029]/.test(s);
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
148 }
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
149
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
150 // Barrier nodes are BR, DIV, P, PRE, TD, TR, ...
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
151 function isBarrierNode(node) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
152 return node ? /^(BR|DIV|P|PRE|TD|TR|TABLE)$/i.test(node.nodeName) : true;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
153 }
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
154
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
155 // Try to find the next adjacent node
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
156 function getNextNode(node) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
157 var n = null;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
158 // Does this node have a sibling?
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
159 if (node.nextSibling) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
160 n = node.nextSibling;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
161
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
162 // Doe this node's container have a sibling?
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
163 } else if (node.parentNode && node.parentNode.nextSibling) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
164 n = node.parentNode.nextSibling;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
165 }
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
166 return isBarrierNode(n) ? null : n;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
167 }
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
168
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
169 // Try to find the prev adjacent node
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
170 function getPrevNode(node) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
171 var n = null;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
172
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
173 // Does this node have a sibling?
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
174 if (node.previousSibling) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
175 n = node.previousSibling;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
176
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
177 // Doe this node's container have a sibling?
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
178 } else if (node.parentNode && node.parentNode.previousSibling) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
179 n = node.parentNode.previousSibling;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
180 }
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
181 return isBarrierNode(n) ? null : n;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
182 }
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
183
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
184 // REF: http://stackoverflow.com/questions/3127369/how-to-get-selected-textnode-in-contenteditable-div-in-ie
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
185 function getChildIndex(node) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
186 var i = 0;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
187 while( (node = node.previousSibling) ) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
188 i++;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
189 }
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
190 return i;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
191 }
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
192
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
193 // All this code just to make this work with IE, OTL
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
194 // REF: http://stackoverflow.com/questions/3127369/how-to-get-selected-textnode-in-contenteditable-div-in-ie
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
195 function getTextRangeBoundaryPosition(textRange, isStart) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
196 var workingRange = textRange.duplicate();
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
197 workingRange.collapse(isStart);
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
198 var containerElement = workingRange.parentElement();
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
199 var workingNode = document.createElement("span");
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
200 var comparison, workingComparisonType = isStart ?
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
201 "StartToStart" : "StartToEnd";
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
202
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
203 var boundaryPosition, boundaryNode;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
204
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
205 // Move the working range through the container's children, starting at
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
206 // the end and working backwards, until the working range reaches or goes
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
207 // past the boundary we're interested in
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
208 do {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
209 containerElement.insertBefore(workingNode, workingNode.previousSibling);
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
210 workingRange.moveToElementText(workingNode);
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
211 } while ( (comparison = workingRange.compareEndPoints(
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
212 workingComparisonType, textRange)) > 0 && workingNode.previousSibling);
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
213
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
214 // We've now reached or gone past the boundary of the text range we're
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
215 // interested in so have identified the node we want
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
216 boundaryNode = workingNode.nextSibling;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
217 if (comparison == -1 && boundaryNode) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
218 // This must be a data node (text, comment, cdata) since we've overshot.
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
219 // The working range is collapsed at the start of the node containing
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
220 // the text range's boundary, so we move the end of the working range
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
221 // to the boundary point and measure the length of its text to get
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
222 // the boundary's offset within the node
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
223 workingRange.setEndPoint(isStart ? "EndToStart" : "EndToEnd", textRange);
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
224
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
225 boundaryPosition = {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
226 node: boundaryNode,
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
227 offset: workingRange.text.length
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
228 };
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
229 } else {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
230 // We've hit the boundary exactly, so this must be an element
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
231 boundaryPosition = {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
232 node: containerElement,
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
233 offset: getChildIndex(workingNode)
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
234 };
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
235 }
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
236
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
237 // Clean up
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
238 workingNode.parentNode.removeChild(workingNode);
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
239
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
240 return boundaryPosition;
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
241 }
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
242
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
243 // DEMO-ONLY code - this shows how the word is recombined across boundaries
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
244 function showBridge(word, nextText, prevText) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
245 if (nextText) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
246 $("#bridge").html("<span class=\"word\">" + word + "</span> | " + nextText.substring(0, 20) + "...").show();
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
247 } else if (prevText) {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
248 $("#bridge").html("..." + prevText.substring(prevText.length - 20, prevText.length) + " | <span class=\"word\">" + word + "</span>").show();
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
249 } else {
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
250 $("#bridge").hide();
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
251 }
b3d53c87d7ea towards dictionary lookup support
ht
parents:
diff changeset
252 }