Ticket #656: getParentElement.patch

File getParentElement.patch, 3.9 KB (added by mokhet, 14 years ago)
  • htmlarea.js

     
    28082808}; 
    28092809 
    28102810// Returns the deepest node that contains both endpoints of the selection. 
    2811 HTMLArea.prototype.getParentElement = function(sel) { 
    2812   if(typeof sel == 'undefined') 
    2813   { 
    2814     sel = this._getSelection(); 
    2815   } 
    2816   var range = this._createRange(sel); 
    2817   if (HTMLArea.is_ie) { 
     2811if (HTMLArea.is_ie) { 
     2812  HTMLArea.prototype.getParentElement = function(sel) { 
     2813    if(typeof sel == 'undefined') { sel = this._getSelection(); } 
     2814    var range = this._createRange(sel); 
    28182815    switch (sel.type) { 
    2819         case "Text":          
    2820       // try to circumvent a bug in IE: 
    2821       // the parent returned is not always the real parent element     
    2822       var parent = range.parentElement(); 
    2823       while (true) 
    2824       { 
    2825         var TestRange = range.duplicate(); 
    2826         TestRange.moveToElementText(parent); 
    2827         if (TestRange.inRange(range)) break; 
    2828         if ((parent.nodeType != 1) || (parent.tagName.toLowerCase() == 'body')) break; 
    2829         parent = parent.parentElement; 
     2816      case "Text": 
     2817        // try to circumvent a bug in IE: 
     2818        // the parent returned is not always the real parent element 
     2819        var parent = range.parentElement(); 
     2820        while (true) 
     2821        { 
     2822          var TestRange = range.duplicate(); 
     2823          TestRange.moveToElementText(parent); 
     2824          if (TestRange.inRange(range)) break; 
     2825          if ((parent.nodeType != 1) || (parent.tagName.toLowerCase() == 'body')) break; 
     2826          parent = parent.parentElement; 
     2827        } 
     2828        return parent; 
     2829      case "None": 
     2830        // It seems that even for selection of type "None", 
     2831        // there _is_ a parent element and it's value is not 
     2832        // only correct, but very important to us.  MSIE is 
     2833        // certainly the buggiest browser in the world and I 
     2834        // wonder, God, how can Earth stand it? 
     2835        return range.parentElement(); 
     2836      case "Control": 
     2837        return range.item(0); 
     2838      default: 
     2839        return this._doc.body; 
     2840    } 
     2841  }; 
     2842} else { 
     2843  HTMLArea.prototype.getParentElement = function(sel) { 
     2844    if(typeof sel == 'undefined') { sel = this._getSelection(); } 
     2845    var range = this._createRange(sel); 
     2846    try { 
     2847      var p = range.commonAncestorContainer; 
     2848      if (!range.collapsed && range.startContainer == range.endContainer && 
     2849          range.startOffset - range.endOffset <= 1 && range.startContainer.hasChildNodes()) 
     2850        p = range.startContainer.childNodes[range.startOffset]; 
     2851      /* 
     2852      alert(range.startContainer + ":" + range.startOffset + "\n" + 
     2853            range.endContainer + ":" + range.endOffset); 
     2854      */ 
     2855      while (p.nodeType == 3) { 
     2856        p = p.parentNode; 
    28302857      } 
    2831       return parent; 
    2832         case "None": 
    2833       // It seems that even for selection of type "None", 
    2834       // there _is_ a parent element and it's value is not 
    2835       // only correct, but very important to us.  MSIE is 
    2836       // certainly the buggiest browser in the world and I 
    2837       // wonder, God, how can Earth stand it? 
    2838       return range.parentElement(); 
    2839         case "Control": 
    2840       return range.item(0); 
    2841         default: 
    2842       return this._doc.body; 
     2858      return p; 
     2859    } catch (e) { 
     2860      return null; 
    28432861    } 
    2844   } else try { 
    2845     var p = range.commonAncestorContainer; 
    2846     if (!range.collapsed && range.startContainer == range.endContainer && 
    2847         range.startOffset - range.endOffset <= 1 && range.startContainer.hasChildNodes()) 
    2848       p = range.startContainer.childNodes[range.startOffset]; 
    2849     /* 
    2850     alert(range.startContainer + ":" + range.startOffset + "\n" + 
    2851           range.endContainer + ":" + range.endOffset); 
    2852     */ 
    2853     while (p.nodeType == 3) { 
    2854       p = p.parentNode; 
    2855     } 
    2856     return p; 
    2857   } catch (e) { 
    2858     return null; 
    2859   } 
    2860 }; 
     2862  }; 
     2863} 
    28612864 
    28622865// Returns an array with all the ancestor nodes of the selection. 
    28632866HTMLArea.prototype.getAllAncestors = function() {