root / trunk / web / dojo / dijit / _editor / range.js
History | View | Annotate | Download (9.26 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dijit._editor.range"]){ |
||
9 | dojo._hasResource["dijit._editor.range"]=true; |
||
10 | dojo.provide("dijit._editor.range");
|
||
11 | dijit.range={}; |
||
12 | dijit.range.getIndex=function(_1,_2){ |
||
13 | var _3=[],_4=[];
|
||
14 | var _5=_2;
|
||
15 | var _6=_1;
|
||
16 | var _7,n;
|
||
17 | while(_1!=_5){
|
||
18 | var i=0; |
||
19 | _7=_1.parentNode; |
||
20 | while((n=_7.childNodes[i++])){
|
||
21 | if(n===_1){
|
||
22 | --i; |
||
23 | break;
|
||
24 | } |
||
25 | } |
||
26 | _3.unshift(i); |
||
27 | _4.unshift(i-_7.childNodes.length); |
||
28 | _1=_7; |
||
29 | } |
||
30 | if(_3.length>0&&_6.nodeType==3){ |
||
31 | n=_6.previousSibling; |
||
32 | while(n&&n.nodeType==3){ |
||
33 | _3[_3.length-1]--;
|
||
34 | n=n.previousSibling; |
||
35 | } |
||
36 | n=_6.nextSibling; |
||
37 | while(n&&n.nodeType==3){ |
||
38 | _4[_4.length-1]++;
|
||
39 | n=n.nextSibling; |
||
40 | } |
||
41 | } |
||
42 | return {o:_3,r:_4}; |
||
43 | }; |
||
44 | dijit.range.getNode=function(_8,_9){ |
||
45 | if(!dojo.isArray(_8)||_8.length==0){ |
||
46 | return _9;
|
||
47 | } |
||
48 | var _a=_9;
|
||
49 | dojo.every(_8,function(i){
|
||
50 | if(i>=0&&i<_a.childNodes.length){ |
||
51 | _a=_a.childNodes[i]; |
||
52 | }else{
|
||
53 | _a=null;
|
||
54 | return false; |
||
55 | } |
||
56 | return true; |
||
57 | }); |
||
58 | return _a;
|
||
59 | }; |
||
60 | dijit.range.getCommonAncestor=function(n1,n2,_b){ |
||
61 | _b=_b||n1.ownerDocument.body; |
||
62 | var _c=function(n){ |
||
63 | var as=[];
|
||
64 | while(n){
|
||
65 | as.unshift(n); |
||
66 | if(n!==_b){
|
||
67 | n=n.parentNode; |
||
68 | }else{
|
||
69 | break;
|
||
70 | } |
||
71 | } |
||
72 | return as;
|
||
73 | }; |
||
74 | var _d=_c(n1);
|
||
75 | var _e=_c(n2);
|
||
76 | var m=Math.min(_d.length,_e.length);
|
||
77 | var _f=_d[0]; |
||
78 | for(var i=1;i<m;i++){ |
||
79 | if(_d[i]===_e[i]){
|
||
80 | _f=_d[i]; |
||
81 | }else{
|
||
82 | break;
|
||
83 | } |
||
84 | } |
||
85 | return _f;
|
||
86 | }; |
||
87 | dijit.range.getAncestor=function(_10,_11,_12){ |
||
88 | _12=_12||_10.ownerDocument.body; |
||
89 | while(_10&&_10!==_12){
|
||
90 | var _13=_10.nodeName.toUpperCase();
|
||
91 | if(_11.test(_13)){
|
||
92 | return _10;
|
||
93 | } |
||
94 | _10=_10.parentNode; |
||
95 | } |
||
96 | return null; |
||
97 | }; |
||
98 | dijit.range.BlockTagNames=/^(?:P|DIV|H1|H2|H3|H4|H5|H6|ADDRESS|PRE|OL|UL|LI|DT|DE)$/;
|
||
99 | dijit.range.getBlockAncestor=function(_14,_15,_16){ |
||
100 | _16=_16||_14.ownerDocument.body; |
||
101 | _15=_15||dijit.range.BlockTagNames; |
||
102 | var _17=null,_18; |
||
103 | while(_14&&_14!==_16){
|
||
104 | var _19=_14.nodeName.toUpperCase();
|
||
105 | if(!_17&&_15.test(_19)){
|
||
106 | _17=_14; |
||
107 | } |
||
108 | if(!_18&&(/^(?:BODY|TD|TH|CAPTION)$/).test(_19)){ |
||
109 | _18=_14; |
||
110 | } |
||
111 | _14=_14.parentNode; |
||
112 | } |
||
113 | return {blockNode:_17,blockContainer:_18||_14.ownerDocument.body}; |
||
114 | }; |
||
115 | dijit.range.atBeginningOfContainer=function(_1a,_1b,_1c){ |
||
116 | var _1d=false; |
||
117 | var _1e=(_1c==0); |
||
118 | if(!_1e&&_1b.nodeType==3){ |
||
119 | if(/^[\s\xA0]+$/.test(_1b.nodeValue.substr(0,_1c))){ |
||
120 | _1e=true;
|
||
121 | } |
||
122 | } |
||
123 | if(_1e){
|
||
124 | var _1f=_1b;
|
||
125 | _1d=true;
|
||
126 | while(_1f&&_1f!==_1a){
|
||
127 | if(_1f.previousSibling){
|
||
128 | _1d=false;
|
||
129 | break;
|
||
130 | } |
||
131 | _1f=_1f.parentNode; |
||
132 | } |
||
133 | } |
||
134 | return _1d;
|
||
135 | }; |
||
136 | dijit.range.atEndOfContainer=function(_20,_21,_22){ |
||
137 | var _23=false; |
||
138 | var _24=(_22==(_21.length||_21.childNodes.length));
|
||
139 | if(!_24&&_21.nodeType==3){ |
||
140 | if(/^[\s\xA0]+$/.test(_21.nodeValue.substr(_22))){ |
||
141 | _24=true;
|
||
142 | } |
||
143 | } |
||
144 | if(_24){
|
||
145 | var _25=_21;
|
||
146 | _23=true;
|
||
147 | while(_25&&_25!==_20){
|
||
148 | if(_25.nextSibling){
|
||
149 | _23=false;
|
||
150 | break;
|
||
151 | } |
||
152 | _25=_25.parentNode; |
||
153 | } |
||
154 | } |
||
155 | return _23;
|
||
156 | }; |
||
157 | dijit.range.adjacentNoneTextNode=function(_26,_27){ |
||
158 | var _28=_26;
|
||
159 | var len=(0-_26.length)||0; |
||
160 | var _29=_27?"nextSibling":"previousSibling"; |
||
161 | while(_28){
|
||
162 | if(_28.nodeType!=3){ |
||
163 | break;
|
||
164 | } |
||
165 | len+=_28.length; |
||
166 | _28=_28[_29]; |
||
167 | } |
||
168 | return [_28,len];
|
||
169 | }; |
||
170 | dijit.range._w3c=Boolean(window["getSelection"]);
|
||
171 | dijit.range.create=function(win){ |
||
172 | if(dijit.range._w3c){
|
||
173 | return (win||dojo.global).document.createRange();
|
||
174 | }else{
|
||
175 | return new dijit.range.W3CRange; |
||
176 | } |
||
177 | }; |
||
178 | dijit.range.getSelection=function(win,_2a){ |
||
179 | if(dijit.range._w3c){
|
||
180 | return win.getSelection();
|
||
181 | }else{
|
||
182 | var s=new dijit.range.ie.selection(win); |
||
183 | if(!_2a){
|
||
184 | s._getCurrentSelection(); |
||
185 | } |
||
186 | return s;
|
||
187 | } |
||
188 | }; |
||
189 | if(!dijit.range._w3c){
|
||
190 | dijit.range.ie={cachedSelection:{},selection:function(win){ |
||
191 | this._ranges=[];
|
||
192 | this.addRange=function(r,_2b){ |
||
193 | this._ranges.push(r);
|
||
194 | if(!_2b){
|
||
195 | r._select(); |
||
196 | } |
||
197 | this.rangeCount=this._ranges.length; |
||
198 | }; |
||
199 | this.removeAllRanges=function(){ |
||
200 | this._ranges=[];
|
||
201 | this.rangeCount=0; |
||
202 | }; |
||
203 | var _2c=function(){ |
||
204 | var r=win.document.selection.createRange();
|
||
205 | var _2d=win.document.selection.type.toUpperCase();
|
||
206 | if(_2d=="CONTROL"){ |
||
207 | return new dijit.range.W3CRange(dijit.range.ie.decomposeControlRange(r)); |
||
208 | }else{
|
||
209 | return new dijit.range.W3CRange(dijit.range.ie.decomposeTextRange(r)); |
||
210 | } |
||
211 | }; |
||
212 | this.getRangeAt=function(i){ |
||
213 | return this._ranges[i]; |
||
214 | }; |
||
215 | this._getCurrentSelection=function(){ |
||
216 | this.removeAllRanges();
|
||
217 | var r=_2c();
|
||
218 | if(r){
|
||
219 | this.addRange(r,true); |
||
220 | } |
||
221 | }; |
||
222 | },decomposeControlRange:function(_2e){ |
||
223 | var _2f=_2e.item(0),_30=_2e.item(_2e.length-1); |
||
224 | var _31=_2f.parentNode,_32=_30.parentNode;
|
||
225 | var _33=dijit.range.getIndex(_2f,_31).o;
|
||
226 | var _34=dijit.range.getIndex(_30,_32).o+1; |
||
227 | return [_31,_33,_32,_34];
|
||
228 | },getEndPoint:function(_35,end){ |
||
229 | var _36=_35.duplicate();
|
||
230 | _36.collapse(!end); |
||
231 | var _37="EndTo"+(end?"End":"Start"); |
||
232 | var _38=_36.parentElement();
|
||
233 | var _39,_3a,_3b;
|
||
234 | if(_38.childNodes.length>0){ |
||
235 | dojo.every(_38.childNodes,function(_3c,i){
|
||
236 | var _3d;
|
||
237 | if(_3c.nodeType!=3){ |
||
238 | _36.moveToElementText(_3c); |
||
239 | if(_36.compareEndPoints(_37,_35)>0){ |
||
240 | if(_3b&&_3b.nodeType==3){ |
||
241 | _39=_3b; |
||
242 | _3d=true;
|
||
243 | }else{
|
||
244 | _39=_38; |
||
245 | _3a=i; |
||
246 | return false; |
||
247 | } |
||
248 | }else{
|
||
249 | if(i==_38.childNodes.length-1){ |
||
250 | _39=_38; |
||
251 | _3a=_38.childNodes.length; |
||
252 | return false; |
||
253 | } |
||
254 | } |
||
255 | }else{
|
||
256 | if(i==_38.childNodes.length-1){ |
||
257 | _39=_3c; |
||
258 | _3d=true;
|
||
259 | } |
||
260 | } |
||
261 | if(_3d&&_39){
|
||
262 | var _3e=dijit.range.adjacentNoneTextNode(_39)[0]; |
||
263 | if(_3e){
|
||
264 | _39=_3e.nextSibling; |
||
265 | }else{
|
||
266 | _39=_38.firstChild; |
||
267 | } |
||
268 | var _3f=dijit.range.adjacentNoneTextNode(_39);
|
||
269 | _3e=_3f[0];
|
||
270 | var _40=_3f[1]; |
||
271 | if(_3e){
|
||
272 | _36.moveToElementText(_3e); |
||
273 | _36.collapse(false);
|
||
274 | }else{
|
||
275 | _36.moveToElementText(_38); |
||
276 | } |
||
277 | _36.setEndPoint(_37,_35); |
||
278 | _3a=_36.text.length-_40; |
||
279 | return false; |
||
280 | } |
||
281 | _3b=_3c; |
||
282 | return true; |
||
283 | }); |
||
284 | }else{
|
||
285 | _39=_38; |
||
286 | _3a=0;
|
||
287 | } |
||
288 | if(!end&&_39.nodeType==1&&_3a==_39.childNodes.length){ |
||
289 | var _41=_39.nextSibling;
|
||
290 | if(_41&&_41.nodeType==3){ |
||
291 | _39=_41; |
||
292 | _3a=0;
|
||
293 | } |
||
294 | } |
||
295 | return [_39,_3a];
|
||
296 | },setEndPoint:function(_42,_43,_44){ |
||
297 | var _45=_42.duplicate(),_46,len;
|
||
298 | if(_43.nodeType!=3){ |
||
299 | if(_44>0){ |
||
300 | _46=_43.childNodes[_44-1];
|
||
301 | if(_46){
|
||
302 | if(_46.nodeType==3){ |
||
303 | _43=_46; |
||
304 | _44=_46.length; |
||
305 | }else{
|
||
306 | if(_46.nextSibling&&_46.nextSibling.nodeType==3){ |
||
307 | _43=_46.nextSibling; |
||
308 | _44=0;
|
||
309 | }else{
|
||
310 | _45.moveToElementText(_46.nextSibling?_46:_43); |
||
311 | var _47=_46.parentNode;
|
||
312 | var _48=_47.insertBefore(_46.ownerDocument.createTextNode(" "),_46.nextSibling); |
||
313 | _45.collapse(false);
|
||
314 | _47.removeChild(_48); |
||
315 | } |
||
316 | } |
||
317 | } |
||
318 | }else{
|
||
319 | _45.moveToElementText(_43); |
||
320 | _45.collapse(true);
|
||
321 | } |
||
322 | } |
||
323 | if(_43.nodeType==3){ |
||
324 | var _49=dijit.range.adjacentNoneTextNode(_43);
|
||
325 | var _4a=_49[0]; |
||
326 | len=_49[1];
|
||
327 | if(_4a){
|
||
328 | _45.moveToElementText(_4a); |
||
329 | _45.collapse(false);
|
||
330 | if(_4a.contentEditable!="inherit"){ |
||
331 | len++; |
||
332 | } |
||
333 | }else{
|
||
334 | _45.moveToElementText(_43.parentNode); |
||
335 | _45.collapse(true);
|
||
336 | } |
||
337 | _44+=len; |
||
338 | if(_44>0){ |
||
339 | if(_45.move("character",_44)!=_44){ |
||
340 | console.error("Error when moving!");
|
||
341 | } |
||
342 | } |
||
343 | } |
||
344 | return _45;
|
||
345 | },decomposeTextRange:function(_4b){ |
||
346 | var _4c=dijit.range.ie.getEndPoint(_4b);
|
||
347 | var _4d=_4c[0],_4e=_4c[1]; |
||
348 | var _4f=_4c[0],_50=_4c[1]; |
||
349 | if(_4b.htmlText.length){
|
||
350 | if(_4b.htmlText==_4b.text){
|
||
351 | _50=_4e+_4b.text.length; |
||
352 | }else{
|
||
353 | _4c=dijit.range.ie.getEndPoint(_4b,true);
|
||
354 | _4f=_4c[0],_50=_4c[1]; |
||
355 | } |
||
356 | } |
||
357 | return [_4d,_4e,_4f,_50];
|
||
358 | },setRange:function(_51,_52,_53,_54,_55,_56){ |
||
359 | var _57=dijit.range.ie.setEndPoint(_51,_52,_53);
|
||
360 | _51.setEndPoint("StartToStart",_57);
|
||
361 | if(!_56){
|
||
362 | var end=dijit.range.ie.setEndPoint(_51,_54,_55);
|
||
363 | } |
||
364 | _51.setEndPoint("EndToEnd",end||_57);
|
||
365 | return _51;
|
||
366 | }}; |
||
367 | dojo.declare("dijit.range.W3CRange",null,{constructor:function(){ |
||
368 | if(arguments.length>0){ |
||
369 | this.setStart(arguments[0][0],arguments[0][1]); |
||
370 | this.setEnd(arguments[0][2],arguments[0][3]); |
||
371 | }else{
|
||
372 | this.commonAncestorContainer=null; |
||
373 | this.startContainer=null; |
||
374 | this.startOffset=0; |
||
375 | this.endContainer=null; |
||
376 | this.endOffset=0; |
||
377 | this.collapsed=true; |
||
378 | } |
||
379 | },_updateInternal:function(){ |
||
380 | if(this.startContainer!==this.endContainer){ |
||
381 | this.commonAncestorContainer=dijit.range.getCommonAncestor(this.startContainer,this.endContainer); |
||
382 | }else{
|
||
383 | this.commonAncestorContainer=this.startContainer; |
||
384 | } |
||
385 | this.collapsed=(this.startContainer===this.endContainer)&&(this.startOffset==this.endOffset); |
||
386 | },setStart:function(_58,_59){ |
||
387 | _59=parseInt(_59); |
||
388 | if(this.startContainer===_58&&this.startOffset==_59){ |
||
389 | return;
|
||
390 | } |
||
391 | delete this._cachedBookmark; |
||
392 | this.startContainer=_58;
|
||
393 | this.startOffset=_59;
|
||
394 | if(!this.endContainer){ |
||
395 | this.setEnd(_58,_59);
|
||
396 | }else{
|
||
397 | this._updateInternal();
|
||
398 | } |
||
399 | },setEnd:function(_5a,_5b){ |
||
400 | _5b=parseInt(_5b); |
||
401 | if(this.endContainer===_5a&&this.endOffset==_5b){ |
||
402 | return;
|
||
403 | } |
||
404 | delete this._cachedBookmark; |
||
405 | this.endContainer=_5a;
|
||
406 | this.endOffset=_5b;
|
||
407 | if(!this.startContainer){ |
||
408 | this.setStart(_5a,_5b);
|
||
409 | }else{
|
||
410 | this._updateInternal();
|
||
411 | } |
||
412 | },setStartAfter:function(_5c,_5d){ |
||
413 | this._setPoint("setStart",_5c,_5d,1); |
||
414 | },setStartBefore:function(_5e,_5f){ |
||
415 | this._setPoint("setStart",_5e,_5f,0); |
||
416 | },setEndAfter:function(_60,_61){ |
||
417 | this._setPoint("setEnd",_60,_61,1); |
||
418 | },setEndBefore:function(_62,_63){ |
||
419 | this._setPoint("setEnd",_62,_63,0); |
||
420 | },_setPoint:function(_64,_65,_66,ext){ |
||
421 | var _67=dijit.range.getIndex(_65,_65.parentNode).o;
|
||
422 | this[_64](_65.parentNode,_67.pop()+ext);
|
||
423 | },_getIERange:function(){ |
||
424 | var r=(this._body||this.endContainer.ownerDocument.body).createTextRange(); |
||
425 | dijit.range.ie.setRange(r,this.startContainer,this.startOffset,this.endContainer,this.endOffset,this.collapsed); |
||
426 | return r;
|
||
427 | },getBookmark:function(_68){ |
||
428 | this._getIERange();
|
||
429 | return this._cachedBookmark; |
||
430 | },_select:function(){ |
||
431 | var r=this._getIERange(); |
||
432 | r.select(); |
||
433 | },deleteContents:function(){ |
||
434 | var r=this._getIERange(); |
||
435 | r.pasteHTML("");
|
||
436 | this.endContainer=this.startContainer; |
||
437 | this.endOffset=this.startOffset; |
||
438 | this.collapsed=true; |
||
439 | },cloneRange:function(){ |
||
440 | var r=new dijit.range.W3CRange([this.startContainer,this.startOffset,this.endContainer,this.endOffset]); |
||
441 | r._body=this._body;
|
||
442 | return r;
|
||
443 | },detach:function(){ |
||
444 | this._body=null; |
||
445 | this.commonAncestorContainer=null; |
||
446 | this.startContainer=null; |
||
447 | this.startOffset=0; |
||
448 | this.endContainer=null; |
||
449 | this.endOffset=0; |
||
450 | this.collapsed=true; |
||
451 | }}); |
||
452 | } |
||
453 | } |