dijit/_editor/selection.js

  • Provides:

    • dijit._editor.selection
  • dijit._editor.selection.getType

    • type
      Function
    • source: [view]
        if(dojo.isIE < 9){
         return dojo.doc.selection.type.toLowerCase();
        }else{
         var stype = "text";


         // Check if the actual selection is a CONTROL (IMG, TABLE, HR, etc...).
         var oSel;
         try{
          oSel = dojo.global.getSelection();
         }catch(e){ /*squelch*/ }


         if(oSel && oSel.rangeCount == 1){
          var oRange = oSel.getRangeAt(0);
          if( (oRange.startContainer == oRange.endContainer) &&
           ((oRange.endOffset - oRange.startOffset) == 1) &&
           (oRange.startContainer.nodeType != 3 /* text node*/)
          ){
           stype = "control";
          }
         }
         return stype; //String
        }
    • summary
      Get the selection type (like dojo.doc.select.type in IE).
    • returns
      String
  • dijit._editor.selection.getSelectedText

    • type
      Function
    • source: [view]
        if(dojo.isIE < 9){
         if(dijit._editor.selection.getType() == 'control'){
          return null;
         }
         return dojo.doc.selection.createRange().text;
        }else{
         var selection = dojo.global.getSelection();
         if(selection){
          return selection.toString(); //String
         }
        }
        return '';
    • summary
      Return the text (no html tags) included in the current selection or null if no text is selected
    • returns
      String
  • dijit._editor.selection.getSelectedHtml

    • type
      Function
    • source: [view]
        if(dojo.isIE < 9){
         if(dijit._editor.selection.getType() == 'control'){
          return null;
         }
         return dojo.doc.selection.createRange().htmlText;
        }else{
         var selection = dojo.global.getSelection();
         if(selection && selection.rangeCount){
          var i;
          var html = "";
          for(i = 0; i < selection.rangeCount; i++){
           //Handle selections spanning ranges, such as Opera
           var frag = selection.getRangeAt(i).cloneContents();
           var div = dojo.doc.createElement("div");
           div.appendChild(frag);
           html += div.innerHTML;
          }
          return html; //String
         }
         return null;
        }
    • summary
      Return the html text of the current selection or null if unavailable
    • returns
      String
  • dijit._editor.selection.getSelectedElement

    • type
      Function
    • source: [view]
        if(dijit._editor.selection.getType() == "control"){
         if(dojo.isIE < 9){
          var range = dojo.doc.selection.createRange();
          if(range && range.item){
           return dojo.doc.selection.createRange().item(0);
          }
         }else{
          var selection = dojo.global.getSelection();
          return selection.anchorNode.childNodes[ selection.anchorOffset ];
         }
        }
        return null;
    • summary
      Retrieves the selected element (if any), just in the case that
      a single element (object like and image or a table) is
      selected.
  • dijit._editor.selection.getParentElement

    • type
      Function
    • source: [view]
        if(dijit._editor.selection.getType() == "control"){
         var p = this.getSelectedElement();
         if(p){ return p.parentNode; }
        }else{
         if(dojo.isIE < 9){
          var r = dojo.doc.selection.createRange();
          r.collapse(true);
          return r.parentElement();
         }else{
          var selection = dojo.global.getSelection();
          if(selection){
           var node = selection.anchorNode;
           while(node && (node.nodeType != 1)){ // not an element
            node = node.parentNode;
           }
           return node;
          }
         }
        }
        return null;
    • summary
      Get the parent element of the current selection
  • dijit._editor.selection.hasAncestorElement

    • type
      Function
    • parameters:
      • tagName: (typeof String )
        String
        The tag name to determine if it has an ancestor of.
    • source: [view]
        return this.getAncestorElement.apply(this, arguments) != null; //Boolean
    • summary
      Check whether current selection has a  parent element which is
      of type tagName (or one of the other specified tagName)
    • returns
      Boolean
    • chains:
      • this.getAncestorElement: (call)
  • dijit._editor.selection.getAncestorElement

    • type
      Function
    • parameters:
      • tagName: (typeof String )
        String
        The tag name to determine if it has an ancestor of.
    • source: [view]
        var node = this.getSelectedElement() || this.getParentElement();
        return this.getParentOfType(node, arguments); //DOMNode
    • summary
      Return the parent element of the current selection which is of
      type tagName (or one of the other specified tagName)
    • returns
      DOMNode
  • dijit._editor.selection.isTag

    • type
      Function
    • parameters:
      • node: (typeof DomNode)
        The node to inspect.
      • tags: (typeof String[])
        An array of tag name strings to check to see if the node matches.
    • source: [view]
        if(node && node.tagName){
         var _nlc = node.tagName.toLowerCase();
         for(var i=0; i    var _tlc = String(tags[i]).toLowerCase();
          if(_nlc == _tlc){
           return _tlc; // String
          }
         }
        }
        return "";
    • summary
      Function to determine if a node is one of an array of tags.
    • tags:
    • returns
      String
  • dijit._editor.selection.getParentOfType

    • type
      Function
    • parameters:
      • node: (typeof DomNode)
        The node to inspect.
      • tags: (typeof String[])
        An array of tag name strings to check to see if the node matches.
    • source: [view]
        while(node){
         if(this.isTag(node, tags).length){
          return node; // DOMNode
         }
         node = node.parentNode;
        }
        return null;
    • summary
      Function to locate a parent node that matches one of a set of tags
    • tags:
    • returns
      DOMNode
  • dijit._editor.selection.collapse

    • type
      Function
    • parameters:
      • beginning: (typeof Boolean)
        Boolean to indicate whether to collapse the cursor to the beginning of the selection or end.
    • source: [view]
        if(window.getSelection){
         var selection = dojo.global.getSelection();
         if(selection.removeAllRanges){ // Mozilla
          if(beginning){
           selection.collapseToStart();
          }else{
           selection.collapseToEnd();
          }
         }else{ // Safari
          // pulled from WebCore/ecma/kjs_window.cpp, line 2536
          selection.collapse(beginning);
         }
        }else if(dojo.isIE){ // IE
         var range = dojo.doc.selection.createRange();
         range.collapse(beginning);
         range.select();
        }
    • summary
      Function to collapse (clear), the current selection
  • dijit._editor.selection.remove

    • type
      Function
    • source: [view]
        var sel = dojo.doc.selection;
        if(dojo.isIE < 9){
         if(sel.type.toLowerCase() != "none"){
          sel.clear();
         }
         return sel; //Selection
        }else{
         sel = dojo.global.getSelection();
         sel.deleteFromDocument();
         return sel; //Selection
        }
    • summary
      Function to delete the currently selected content from the document.
    • returns
      Selection
  • dijit._editor.selection.selectElementChildren

    • type
      Function
    • parameters:
      • element: (typeof DomNode)
        DOMNode
        The element you wish to select the children content of.
      • nochangefocus: (typeof Boolean)
        Boolean to indicate if the foxus should change or not.
    • source: [view]
        var win = dojo.global;
        var doc = dojo.doc;
        var range;
        element = dojo.byId(element);
        if(doc.selection && dojo.isIE < 9 && dojo.body().createTextRange){ // IE
         range = element.ownerDocument.body.createTextRange();
         range.moveToElementText(element);
         if(!nochangefocus){
          try{
           range.select(); // IE throws an exception here if the widget is hidden. See #5439
          }catch(e){ /* squelch */}
         }
        }else if(win.getSelection){
         var selection = dojo.global.getSelection();
         if(dojo.isOpera){
          //Opera's selectAllChildren doesn't seem to work right
          //against nodes and possibly others ... so
          //we use the W3C range API
          if(selection.rangeCount){
           range = selection.getRangeAt(0);
          }else{
           range = doc.createRange();
          }
          range.setStart(element, 0);
          range.setEnd(element,(element.nodeType == 3)?element.length:element.childNodes.length);
          selection.addRange(range);
         }else{
          selection.selectAllChildren(element);
         }
        }
    • summary
      clear previous selection and select the content of the node
      (excluding the node itself)
  • dijit._editor.selection.selectElement

    • type
      Function
    • parameters:
      • element: (typeof DomNode)
        DOMNode
        The element to select.
      • nochangefocus: (typeof Boolean)
        Boolean indicating if the focus should be changed.  IE only.
    • source: [view]
        var range;
        var doc = dojo.doc;
        var win = dojo.global;
        element = dojo.byId(element);
        if(dojo.isIE < 9 && dojo.body().createTextRange){
         try{
          var tg = element.tagName ? element.tagName.toLowerCase() : "";
          if(tg === "img" || tg === "table"){
           range = dojo.body().createControlRange();
          }else{
           range = dojo.body().createRange();
          }
          range.addElement(element);
          if(!nochangefocus){
           range.select();
          }
         }catch(e){
          this.selectElementChildren(element,nochangefocus);
         }
        }else if(dojo.global.getSelection){
         var selection = win.getSelection();
         range = doc.createRange();
         if(selection.removeAllRanges){ // Mozilla
          // FIXME: does this work on Safari?
          if(dojo.isOpera){
           //Opera works if you use the current range on
           //the selection if present.
           if(selection.getRangeAt(0)){
            range = selection.getRangeAt(0);
           }
          }
          range.selectNode(element);
          selection.removeAllRanges();
          selection.addRange(range);
         }
        }
    • summary
      clear previous selection and select element (including all its children)
  • dijit._editor.selection.inSelection

    • type
      Function
    • parameters:
      • node: (typeof )
    • source: [view]
        if(node){
         var newRange;
         var doc = dojo.doc;
         var range;


         if(dojo.global.getSelection){
          //WC3
          var sel = dojo.global.getSelection();
          if(sel && sel.rangeCount > 0){
           range = sel.getRangeAt(0);
          }
          if(range && range.compareBoundaryPoints && doc.createRange){
           try{
            newRange = doc.createRange();
            newRange.setStart(node, 0);
            if(range.compareBoundaryPoints(range.START_TO_END, newRange) === 1){
             return true;
            }
           }catch(e){ /* squelch */}
          }
         }else if(doc.selection){
          // Probably IE, so we can't use the range object as the pseudo
          // range doesn't implement the boundry checking, we have to
          // use IE specific crud.
          range = doc.selection.createRange();
          try{
           newRange = node.ownerDocument.body.createControlRange();
           if(newRange){
            newRange.addElement(node);
           }
          }catch(e1){
           try{
            newRange = node.ownerDocument.body.createTextRange();
            newRange.moveToElementText(node);
           }catch(e2){/* squelch */}
          }
          if(range && newRange){
           // We can finally compare similar to W3C
           if(range.compareEndPoints("EndToStart", newRange) === 1){
            return true;
           }
          }
         }
        }
        return false; // boolean
    • summary
      This function determines if 'node' is
      in the current selection.
    • tags:
    • returns
      boolean
  • dijit._editor.selection

    • type
      Object
    • summary
  • dijit._editor

    • type
      Object
    • summary
  • dijit

    • type
      Object
    • summary