1 if(!dojo._hasResource["dijit._editor.selection"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dijit._editor.selection"] = true;
3 dojo.provide("dijit._editor.selection");
6 // all of these methods branch internally for IE. This is probably
7 // sub-optimal in terms of runtime performance. We should investigate the
8 // size difference for differentiating at definition time.
10 dojo.mixin(dijit._editor.selection, {
12 // summary: Get the selection type (like dojo.doc.select.type in IE).
13 if(dojo.doc.selection){ //IE
14 return dojo.doc.selection.type.toLowerCase();
18 // Check if the actual selection is a CONTROL (IMG, TABLE, HR, etc...).
21 oSel = dojo.global.getSelection();
22 }catch(e){ /*squelch*/ }
24 if(oSel && oSel.rangeCount==1){
25 var oRange = oSel.getRangeAt(0);
26 if( (oRange.startContainer == oRange.endContainer) &&
27 ((oRange.endOffset - oRange.startOffset) == 1) &&
28 (oRange.startContainer.nodeType != 3 /* text node*/)
37 getSelectedText: function(){
39 // Return the text (no html tags) included in the current selection or null if no text is selected
40 if(dojo.doc.selection){ //IE
41 if(dijit._editor.selection.getType() == 'control'){
44 return dojo.doc.selection.createRange().text;
46 var selection = dojo.global.getSelection();
48 return selection.toString();
54 getSelectedHtml: function(){
56 // Return the html of the current selection or null if unavailable
57 if(dojo.doc.selection){ //IE
58 if(dijit._editor.selection.getType() == 'control'){
61 return dojo.doc.selection.createRange().htmlText;
63 var selection = dojo.global.getSelection();
64 if(selection && selection.rangeCount){
65 var frag = selection.getRangeAt(0).cloneContents();
66 var div = dojo.doc.createElement("div");
67 div.appendChild(frag);
74 getSelectedElement: function(){
76 // Retrieves the selected element (if any), just in the case that
77 // a single element (object like and image or a table) is
79 if(this.getType() == "control"){
80 if(dojo.doc.selection){ //IE
81 var range = dojo.doc.selection.createRange();
82 if(range && range.item){
83 return dojo.doc.selection.createRange().item(0);
86 var selection = dojo.global.getSelection();
87 return selection.anchorNode.childNodes[ selection.anchorOffset ];
93 getParentElement: function(){
95 // Get the parent element of the current selection
96 if(this.getType() == "control"){
97 var p = this.getSelectedElement();
98 if(p){ return p.parentNode; }
100 if(dojo.doc.selection){ //IE
101 return dojo.doc.selection.createRange().parentElement();
103 var selection = dojo.global.getSelection();
105 var node = selection.anchorNode;
107 while(node && (node.nodeType != 1)){ // not an element
108 node = node.parentNode;
118 hasAncestorElement: function(/*String*/tagName /* ... */){
120 // Check whether current selection has a parent element which is
121 // of type tagName (or one of the other specified tagName)
122 return this.getAncestorElement.apply(this, arguments) != null;
125 getAncestorElement: function(/*String*/tagName /* ... */){
127 // Return the parent element of the current selection which is of
128 // type tagName (or one of the other specified tagName)
130 var node = this.getSelectedElement() || this.getParentElement();
131 return this.getParentOfType(node, arguments);
134 isTag: function(/*DomNode*/node, /*Array*/tags){
135 if(node && node.tagName){
136 var _nlc = node.tagName.toLowerCase();
137 for(var i=0; i<tags.length; i++){
138 var _tlc = String(tags[i]).toLowerCase();
147 getParentOfType: function(/*DomNode*/node, /*Array*/tags){
149 if(this.isTag(node, tags).length){
152 node = node.parentNode;
157 collapse: function(/*Boolean*/beginning) {
158 // summary: clear current selection
159 if(window['getSelection']){
160 var selection = dojo.global.getSelection();
161 if(selection.removeAllRanges){ // Mozilla
163 selection.collapseToStart();
165 selection.collapseToEnd();
168 // pulled from WebCore/ecma/kjs_window.cpp, line 2536
169 selection.collapse(beginning);
171 }else if(dojo.doc.selection){ // IE
172 var range = dojo.doc.selection.createRange();
173 range.collapse(beginning);
179 // summary: delete current selection
180 var _s = dojo.doc.selection;
182 if(_s.type.toLowerCase() != "none"){
187 _s = dojo.global.getSelection();
188 _s.deleteFromDocument();
193 selectElementChildren: function(/*DomNode*/element,/*Boolean?*/nochangefocus){
195 // clear previous selection and select the content of the node
196 // (excluding the node itself)
197 var _window = dojo.global;
198 var _document = dojo.doc;
199 element = dojo.byId(element);
200 if(_document.selection && dojo.body().createTextRange){ // IE
201 var range = element.ownerDocument.body.createTextRange();
202 range.moveToElementText(element);
205 range.select(); // IE throws an exception here if the widget is hidden. See #5439
206 }catch(e){ /* squelch */}
208 }else if(_window.getSelection){
209 var selection = _window.getSelection();
210 if(selection.setBaseAndExtent){ // Safari
211 selection.setBaseAndExtent(element, 0, element, element.innerText.length - 1);
212 }else if(selection.selectAllChildren){ // Mozilla
213 selection.selectAllChildren(element);
218 selectElement: function(/*DomNode*/element,/*Boolean?*/nochangefocus){
220 // clear previous selection and select element (including all its children)
221 var range, _document = dojo.doc;
222 element = dojo.byId(element);
223 if(_document.selection && dojo.body().createTextRange){ // IE
225 range = dojo.body().createControlRange();
226 range.addElement(element);
231 this.selectElementChildren(element,nochangefocus);
233 }else if(dojo.global.getSelection){
234 var selection = dojo.global.getSelection();
235 // FIXME: does this work on Safari?
236 if(selection.removeAllRanges){ // Mozilla
237 range = _document.createRange();
238 range.selectNode(element);
239 selection.removeAllRanges();
240 selection.addRange(range);