selection.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. if (!dojo._hasResource["dijit._editor.selection"]) { // _hasResource checks
  2. // added by build. Do
  3. // not use _hasResource
  4. // directly in your
  5. // code.
  6. dojo._hasResource["dijit._editor.selection"] = true;
  7. dojo.provide("dijit._editor.selection");
  8. // FIXME:
  9. // all of these methods branch internally for IE. This is probably
  10. // sub-optimal in terms of runtime performance. We should investigate the
  11. // size difference for differentiating at definition time.
  12. dojo.mixin(dijit._editor.selection, {
  13. getType : function() {
  14. // summary: Get the selection type (like document.select.type in
  15. // IE).
  16. if (dojo.doc["selection"]) { // IE
  17. return dojo.doc.selection.type.toLowerCase();
  18. } else {
  19. var stype = "text";
  20. // Check if the actual selection is a CONTROL (IMG, TABLE, HR,
  21. // etc...).
  22. var oSel;
  23. try {
  24. oSel = dojo.global.getSelection();
  25. } catch (e) { /* squelch */
  26. }
  27. if (oSel && oSel.rangeCount == 1) {
  28. var oRange = oSel.getRangeAt(0);
  29. if ((oRange.startContainer == oRange.endContainer)
  30. && ((oRange.endOffset - oRange.startOffset) == 1)
  31. && (oRange.startContainer.nodeType != 3 /* text node */)) {
  32. stype = "control";
  33. }
  34. }
  35. return stype;
  36. }
  37. },
  38. getSelectedText : function() {
  39. // summary:
  40. // Return the text (no html tags) included in the current selection
  41. // or null if no text is selected
  42. if (dojo.doc["selection"]) { // IE
  43. if (dijit._editor.selection.getType() == 'control') {
  44. return null;
  45. }
  46. return dojo.doc.selection.createRange().text;
  47. } else {
  48. var selection = dojo.global.getSelection();
  49. if (selection) {
  50. return selection.toString();
  51. }
  52. }
  53. },
  54. getSelectedHtml : function() {
  55. // summary:
  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') {
  59. return null;
  60. }
  61. return dojo.doc.selection.createRange().htmlText;
  62. } else {
  63. var selection = dojo.global.getSelection();
  64. if (selection && selection.rangeCount) {
  65. var frag = selection.getRangeAt(0).cloneContents();
  66. var div = document.createElement("div");
  67. div.appendChild(frag);
  68. return div.innerHTML;
  69. }
  70. return null;
  71. }
  72. },
  73. getSelectedElement : function() {
  74. // summary:
  75. // Retrieves the selected element (if any), just in the case that
  76. // a single element (object like and image or a table) is
  77. // selected.
  78. if (this.getType() == "control") {
  79. if (dojo.doc["selection"]) { // IE
  80. var range = dojo.doc.selection.createRange();
  81. if (range && range.item) {
  82. return dojo.doc.selection.createRange().item(0);
  83. }
  84. } else {
  85. var selection = dojo.global.getSelection();
  86. return selection.anchorNode.childNodes[selection.anchorOffset];
  87. }
  88. }
  89. },
  90. getParentElement : function() {
  91. // summary:
  92. // Get the parent element of the current selection
  93. if (this.getType() == "control") {
  94. var p = this.getSelectedElement();
  95. if (p) {
  96. return p.parentNode;
  97. }
  98. } else {
  99. if (dojo.doc["selection"]) { // IE
  100. return dojo.doc.selection.createRange().parentElement();
  101. } else {
  102. var selection = dojo.global.getSelection();
  103. if (selection) {
  104. var node = selection.anchorNode;
  105. while (node && (node.nodeType != 1)) { // not an
  106. // element
  107. node = node.parentNode;
  108. }
  109. return node;
  110. }
  111. }
  112. }
  113. },
  114. hasAncestorElement : function(/* String */tagName /* ... */) {
  115. // summary:
  116. // Check whether current selection has a parent element which is
  117. // of type tagName (or one of the other specified tagName)
  118. return (this.getAncestorElement.apply(this, arguments) != null);
  119. },
  120. getAncestorElement : function(/* String */tagName /* ... */) {
  121. // summary:
  122. // Return the parent element of the current selection which is of
  123. // type tagName (or one of the other specified tagName)
  124. var node = this.getSelectedElement() || this.getParentElement();
  125. return this.getParentOfType(node, arguments);
  126. },
  127. isTag : function(/* DomNode */node, /* Array */tags) {
  128. if (node && node.tagName) {
  129. var _nlc = node.tagName.toLowerCase();
  130. for (var i = 0; i < tags.length; i++) {
  131. var _tlc = String(tags[i]).toLowerCase();
  132. if (_nlc == _tlc) {
  133. return _tlc;
  134. }
  135. }
  136. }
  137. return "";
  138. },
  139. getParentOfType : function(/* DomNode */node, /* Array */tags) {
  140. while (node) {
  141. if (this.isTag(node, tags).length) {
  142. return node;
  143. }
  144. node = node.parentNode;
  145. }
  146. return null;
  147. },
  148. remove : function() {
  149. // summary: delete current selection
  150. var _s = dojo.doc.selection;
  151. if (_s) { // IE
  152. if (_s.type.toLowerCase() != "none") {
  153. _s.clear();
  154. }
  155. return _s;
  156. } else {
  157. _s = dojo.global.getSelection();
  158. _s.deleteFromDocument();
  159. return _s;
  160. }
  161. },
  162. selectElementChildren : function(/* DomNode */element,/* Boolean? */
  163. nochangefocus) {
  164. // summary:
  165. // clear previous selection and select the content of the node
  166. // (excluding the node itself)
  167. var _window = dojo.global;
  168. var _document = dojo.doc;
  169. element = dojo.byId(element);
  170. if (_document.selection && dojo.body().createTextRange) { // IE
  171. var range = element.ownerDocument.body.createTextRange();
  172. range.moveToElementText(element);
  173. if (!nochangefocus) {
  174. range.select();
  175. }
  176. } else if (_window["getSelection"]) {
  177. var selection = _window.getSelection();
  178. if (selection["setBaseAndExtent"]) { // Safari
  179. selection.setBaseAndExtent(element, 0, element,
  180. element.innerText.length - 1);
  181. } else if (selection["selectAllChildren"]) { // Mozilla
  182. selection.selectAllChildren(element);
  183. }
  184. }
  185. },
  186. selectElement : function(/* DomNode */element,/* Boolean? */nochangefocus) {
  187. // summary:
  188. // clear previous selection and select element (including all its
  189. // children)
  190. var _document = dojo.doc;
  191. element = dojo.byId(element);
  192. if (_document.selection && dojo.body().createTextRange) { // IE
  193. try {
  194. var range = dojo.body().createControlRange();
  195. range.addElement(element);
  196. if (!nochangefocus) {
  197. range.select();
  198. }
  199. } catch (e) {
  200. this.selectElementChildren(element, nochangefocus);
  201. }
  202. } else if (dojo.global["getSelection"]) {
  203. var selection = dojo.global.getSelection();
  204. // FIXME: does this work on Safari?
  205. if (selection["removeAllRanges"]) { // Mozilla
  206. var range = _document.createRange();
  207. range.selectNode(element);
  208. selection.removeAllRanges();
  209. selection.addRange(range);
  210. }
  211. }
  212. }
  213. });
  214. }