src/cm/media/js/client/c_selection.js
author Simon Descarpentries <sid@sopinspace.com>
Thu, 31 Oct 2013 11:49:30 +0100
changeset 558 5ba711a2bd06
parent 556 69503659fe8f
child 600 fda73ac53450
permissions -rw-r--r--
Add comments, fix missing compressed js
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     1
getWrapperAncestor = function(elt) {
341
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
     2
  var parent = elt ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
     3
  while (parent != null) {
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
     4
      if (CY.DOM.hasClass(parent, 'c-s')) 
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
     5
        return parent ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
     6
      parent = parent.parentNode ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
     7
  }
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
     8
  return null ; 
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     9
}
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    10
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    11
hasWrapperAncestor = function(elt) {
341
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    12
  return (getWrapperAncestor(elt) != null) ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    13
/*  var parent = elt ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    14
  while (parent != null) {
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    15
      if (CY.DOM.hasClass(parent, 'c-s')) 
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    16
        return true ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    17
      parent = parent.parentNode ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    18
  }
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    19
  return false ;*/ 
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    20
}
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    21
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    22
// returns null or :
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    23
// {'text' : textcontent, 'start': {'elt':startNode, 'nbChar':startOffset(==number of characters to selection start in the start node},
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    24
// 'end': ....}
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    25
// the text attribute is informational having it empty doesn't mean selection is empty !!
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    26
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    27
// when selection starts/ends in/on a non textual element (<hr/> for example) we very often have anchorNode/focusNode == body elt
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    28
// TODO adapt this body case by considering offset ( cf. http://www.w3.org/TR/DOM-Level-2-Traversal-Range/ranges.html)
556
69503659fe8f [c_selection.js] If safari_mobile, get current selection from a previously created global variable
Simon Descarpentries <sid@sopinspace.com>
parents: 532
diff changeset
    29
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    30
getSelectionInfo  = function () {
341
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    31
  var startNode = null, endNode = null, startOffset = 0, endOffset = 0, text = '' ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    32
  
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    33
  if (window.getSelection) { // everything else than IE
558
5ba711a2bd06 Add comments, fix missing compressed js
Simon Descarpentries <sid@sopinspace.com>
parents: 556
diff changeset
    34
	// SID: if on safari_mobile get the previous meaningfull selection from a global variable set in text_view_comments.html
556
69503659fe8f [c_selection.js] If safari_mobile, get current selection from a previously created global variable
Simon Descarpentries <sid@sopinspace.com>
parents: 532
diff changeset
    35
    var userSelection = safari_mobile ? storedSelection : window.getSelection ();
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    36
341
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    37
    if (userSelection.rangeCount > 0) {
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    38
      var range = userSelection.getRangeAt(0) ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    39
      text = range.toString() ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    40
      if (text != "")  {
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    41
        
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    42
        // selection occured from right to left ? :
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    43
        var r1 = document.createRange() ;r1.setStart(userSelection.anchorNode, userSelection.anchorOffset) ;r1.collapse(true) ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    44
        var r2 = document.createRange() ;r2.setEnd(userSelection.focusNode, userSelection.focusOffset) ;r2.collapse(false) ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    45
        var leftToRight = (r2.compareBoundaryPoints(2, r1) == 1) ; // 2 is for END_TO_END
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    46
//        CY.log("leftToRight : " + leftToRight) ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    47
        startNode = (leftToRight) ? userSelection.anchorNode.parentNode : userSelection.focusNode.parentNode ;  
397
eba2e55eb437 Fix selection of amath elements
gibus
parents: 341
diff changeset
    48
        // GIB: when selecting amath, we should go up in the dom to find the accurate start/end Nodes
eba2e55eb437 Fix selection of amath elements
gibus
parents: 341
diff changeset
    49
        if (startNode.nodeName == 'mi' || startNode.nodeName == 'mo') {
eba2e55eb437 Fix selection of amath elements
gibus
parents: 341
diff changeset
    50
          startNode = startNode.parentElement.parentElement.parentElement.parentElement;
eba2e55eb437 Fix selection of amath elements
gibus
parents: 341
diff changeset
    51
        }
341
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    52
        innerStartNode = (leftToRight) ? userSelection.anchorNode : userSelection.focusNode ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    53
        endNode = (leftToRight) ? userSelection.focusNode.parentNode : userSelection.anchorNode.parentNode;
397
eba2e55eb437 Fix selection of amath elements
gibus
parents: 341
diff changeset
    54
        // GIB: when selecting amath, we should go up in the dom to find the accurate start/end Nodes
eba2e55eb437 Fix selection of amath elements
gibus
parents: 341
diff changeset
    55
        if (endNode.nodeName == 'mi' || endNode.nodeName == 'mo') {
eba2e55eb437 Fix selection of amath elements
gibus
parents: 341
diff changeset
    56
          endNode = endNode.parentElement.parentElement.parentElement.parentElement;
eba2e55eb437 Fix selection of amath elements
gibus
parents: 341
diff changeset
    57
        }
341
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    58
        innerEndNode = (leftToRight) ? userSelection.focusNode : userSelection.anchorNode;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    59
          
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    60
        startOffset = (leftToRight) ? userSelection.anchorOffset : userSelection.focusOffset;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    61
        endOffset = (leftToRight) ? userSelection.focusOffset : userSelection.anchorOffset ;
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    62
341
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    63
        if (!hasWrapperAncestor(endNode) && hasWrapperAncestor(startNode)){
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    64
          var r3 = document.createRange() ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    65
          r3.setStart(innerStartNode, startOffset) ;
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    66
341
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    67
          var csStartAncestor = getWrapperAncestor(startNode) ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    68
          var next = csStartAncestor ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    69
          r3.setEndAfter(next) ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    70
          
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    71
          var ind = parseInt(csStartAncestor.id.substring('sv_'.length)) ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    72
          while(r3.toString().length < range.toString().length) {
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    73
              ind++ ;
532
0bad3613f59d Reverse to YUI 3.0.0 since with YUI.3.10.3, comment content including words 'paragraph' or 'section' do not show up on Firefox, this is weird and has to be investigated.
gibus
parents: 525
diff changeset
    74
              var node = CY.get("#sv_"+ind) ;
341
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    75
              if (node) {
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    76
                next = CY.Node.getDOMNode(node) ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    77
                r3.setEndAfter(next) ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    78
              }
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    79
              else 
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    80
                break ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    81
          }
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    82
          endNode = next.lastChild ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    83
          endOffset = CY.DOM.getText(endNode).length ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    84
        }
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    85
        else if (!hasWrapperAncestor(startNode) && hasWrapperAncestor(endNode)){
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    86
          var r3 = document.createRange() ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    87
          r3.setEnd(innerEndNode, endOffset) ;
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    88
341
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    89
          var csEndAncestor = getWrapperAncestor(endNode) ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    90
          var prev = csEndAncestor ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    91
          r3.setStartBefore(prev) ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    92
          
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    93
          var ind = parseInt(csEndAncestor.id.substring('sv_'.length)) ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    94
          while(r3.toString().length < range.toString().length) {
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    95
              ind-- ;
532
0bad3613f59d Reverse to YUI 3.0.0 since with YUI.3.10.3, comment content including words 'paragraph' or 'section' do not show up on Firefox, this is weird and has to be investigated.
gibus
parents: 525
diff changeset
    96
              var node = CY.get("#sv_"+ind) ;
341
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    97
              if (node) {
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    98
                prev = CY.Node.getDOMNode(node) ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
    99
                r3.setStartBefore(prev) ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   100
              }
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   101
              else 
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   102
                break ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   103
          }
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   104
          startNode = prev.firstChild ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   105
          startOffset = 0 ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   106
        }
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   107
        else if (!hasWrapperAncestor(startNode) && !hasWrapperAncestor(endNode)){
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   108
          var textLength = text.length ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   109
          
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   110
          // gather nodes with id sv_xxxx as candidates for start ancestor
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   111
          var startNodeInds = [] ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   112
          for (var ind = 0 ;  ; ind++) {
532
0bad3613f59d Reverse to YUI 3.0.0 since with YUI.3.10.3, comment content including words 'paragraph' or 'section' do not show up on Firefox, this is weird and has to be investigated.
gibus
parents: 525
diff changeset
   113
            var svNode = CY.get("#sv_"+ind) ;
341
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   114
            if (svNode == null) {
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   115
              break;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   116
            }
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   117
            else {
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   118
              var svText = svNode.get("text") ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   119
              if (text.indexOf(svText) == 0) {
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   120
                startNodeInds.push(ind) ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   121
              }
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   122
            }
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   123
          }
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   124
          
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   125
          // gather nodes with id sv_xxxx as candidates for end ancestor
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   126
          var endNodeInds = [] ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   127
          for (var ind = 0 ;  ; ind++) {
532
0bad3613f59d Reverse to YUI 3.0.0 since with YUI.3.10.3, comment content including words 'paragraph' or 'section' do not show up on Firefox, this is weird and has to be investigated.
gibus
parents: 525
diff changeset
   128
            var svNode = CY.get("#sv_"+ind) ;
341
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   129
            if (svNode == null) {
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   130
              break;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   131
            }
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   132
            else {
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   133
              var svText = svNode.get("text") ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   134
              if (text.indexOf(svText) == (textLength - svText.length)) { // i.e. the selection exactly ends with svText
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   135
                endNodeInds.push(ind) ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   136
              }
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   137
            }
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   138
          }
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   139
341
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   140
          var stop = false ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   141
          for (var i = 0 ; i < startNodeInds.length ; i++) {
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   142
            for (var j = 0 ; j < endNodeInds.length ; j++) {
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   143
              var r4 = document.createRange() ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   144
              
532
0bad3613f59d Reverse to YUI 3.0.0 since with YUI.3.10.3, comment content including words 'paragraph' or 'section' do not show up on Firefox, this is weird and has to be investigated.
gibus
parents: 525
diff changeset
   145
              var s = CY.Node.getDOMNode(CY.get("#sv_"+startNodeInds[i])) ; var e = CY.Node.getDOMNode(CY.get("#sv_"+endNodeInds[j])) ;
341
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   146
              
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   147
              r4.setStartBefore(s) ; r4.setEndAfter(CY.Node.getDOMNode(e)) ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   148
              
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   149
              // does r4 starts after range start and r4 ends before range end ? 
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   150
              if ((-1 < r4.compareBoundaryPoints(0, range)) && (1 > r4.compareBoundaryPoints(2, range))) { 
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   151
                startNode = s.firstChild ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   152
                startOffset = 0 ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   153
                endNode = e.lastChild ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   154
                endOffset = CY.DOM.getText(e).length ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   155
                
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   156
                stop = true ; 
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   157
                break ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   158
              }
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   159
            }
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   160
            if (stop)
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   161
              break ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   162
          }             
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   163
        }       
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   164
        
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   165
        r1.detach() ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   166
        r2.detach() ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   167
      }
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   168
      else 
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   169
        return null ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   170
    }
556
69503659fe8f [c_selection.js] If safari_mobile, get current selection from a previously created global variable
Simon Descarpentries <sid@sopinspace.com>
parents: 532
diff changeset
   171
    else
341
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   172
      return null ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   173
    
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   174
  }
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   175
  else if (document.selection) { // IE case
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   176
    var rng = document.selection.createRange();
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   177
    if (rng.text.length == 0) 
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   178
      return null ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   179
    var el = rng.parentElement();
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   180
341
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   181
    // duplicate the range and collapse it to its start, to ask IE the parent element of the start textNode.    
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   182
    var rngStart = rng.duplicate();
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   183
    var rngEnd = rng.duplicate();
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   184
341
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   185
    rngStart.collapse(true); // collapse to start
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   186
    rngEnd.collapse(false);  // collapse to end
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   187
    
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   188
    startNode = rngStart.parentElement() ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   189
    while(rngStart.moveStart('character', -1) != 0) {
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   190
      if (rngStart.parentElement() != startNode)
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   191
        break ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   192
      startOffset++ ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   193
    }
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   194
    endNode = rngEnd.parentElement() ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   195
    while(rngEnd.moveEnd('character', -1) != 0) {
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   196
      if (rngEnd.parentElement() != endNode)
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   197
        break ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   198
      endOffset++ ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   199
    }
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   200
    
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   201
    text = rng.text ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   202
  }
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   203
  
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   204
  if (!hasWrapperAncestor(startNode) || !hasWrapperAncestor(endNode)){
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   205
    // CY.log('no wrapper on one end') ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   206
    return null ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   207
  }
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   208
  return {'text' : text, 'start' : {'elt':startNode, 'offset':startOffset}, 'end' : {'elt':endNode, 'offset':endOffset}} ;
053551f213fb Coding style for js: expand tabs
gibus
parents: 0
diff changeset
   209
}