Ticket #3240: 3240_4.patch
File 3240_4.patch, 5.0 KB (added by , 16 years ago) |
---|
-
_source/core/dom/node.js
424 424 if ( $ == $other ) 425 425 return CKEDITOR.POSITION_IDENTICAL; 426 426 427 // Handle non element nodes (don't support contains nor sourceIndex).428 if ( this.type != CKEDITOR.NODE_ELEMENT || otherNode.type != CKEDITOR.NODE_ELEMENT )427 // Only element nodes support contains and sourceIndex. 428 if ( this.type == CKEDITOR.NODE_ELEMENT && otherNode.type == CKEDITOR.NODE_ELEMENT ) 429 429 { 430 if ( $.parentNode == $other ) 431 return CKEDITOR.POSITION_IS_CONTAINED + CKEDITOR.POSITION_FOLLOWING; 432 else if ( $other.parentNode == $ ) 433 return CKEDITOR.POSITION_CONTAINS + CKEDITOR.POSITION_PRECEDING; 434 else if ( $.parentNode == $other.parentNode ) 435 return this.getIndex() < otherNode.getIndex() ? CKEDITOR.POSITION_PRECEDING : CKEDITOR.POSITION_FOLLOWING; 436 else 430 if ( $.contains ) 437 431 { 438 $ = $.parentNode; 439 $other = $other.parentNode; 432 if ( $.contains( $other ) ) 433 return CKEDITOR.POSITION_CONTAINS + CKEDITOR.POSITION_PRECEDING; 434 435 if ( $other.contains( $ ) ) 436 return CKEDITOR.POSITION_IS_CONTAINED + CKEDITOR.POSITION_FOLLOWING; 440 437 } 441 }442 438 443 if ( $.contains( $other ) ) 444 return CKEDITOR.POSITION_CONTAINS + CKEDITOR.POSITION_PRECEDING; 445 446 if ( $other.contains( $ ) ) 447 return CKEDITOR.POSITION_IS_CONTAINED + CKEDITOR.POSITION_FOLLOWING; 448 449 if ( 'sourceIndex' in $ ) 450 { 451 return ( $.sourceIndex < 0 || $other.sourceIndex < 0 ) ? CKEDITOR.POSITION_DISCONNECTED : 452 ( $.sourceIndex < $other.sourceIndex ) ? CKEDITOR.POSITION_PRECEDING : 453 CKEDITOR.POSITION_FOLLOWING; 439 if ( 'sourceIndex' in $ ) 440 { 441 return ( $.sourceIndex < 0 || $other.sourceIndex < 0 ) ? CKEDITOR.POSITION_DISCONNECTED : 442 ( $.sourceIndex < $other.sourceIndex ) ? CKEDITOR.POSITION_PRECEDING : 443 CKEDITOR.POSITION_FOLLOWING; 444 } 454 445 } 446 447 // For nodes that don't support compareDocumentPosition, contains 448 // or sourceIndex, their "address" is compared. 455 449 456 // WebKit has no support for sourceIndex. 450 var addressOfThis = this.getAddress(), 451 addressOfOther = otherNode.getAddress(), 452 minLevel = Math.min( addressOfThis.length, addressOfOther.length ); 457 453 458 var doc = this.getDocument().$; 454 // Determinate preceed/follow relationship. 455 for ( var i = 0 ; i <= minLevel - 1 ; i++ ) 456 { 457 if ( addressOfThis[ i ] != addressOfOther[ i ] ) 458 { 459 if ( i < minLevel ) 460 { 461 return addressOfThis[ i ] < addressOfOther[ i ] ? 462 CKEDITOR.POSITION_PRECEDING : CKEDITOR.POSITION_FOLLOWING; 463 } 464 break; 465 } 466 } 459 467 460 var range1 = doc.createRange(); 461 var range2 = doc.createRange(); 462 463 range1.selectNode( $ ); 464 range2.selectNode( $other ); 465 466 return range1.compareBoundaryPoints( 1, range2 ) > 0 ? 467 CKEDITOR.POSITION_FOLLOWING : 468 CKEDITOR.POSITION_PRECEDING; 468 // Determinate contains/contained relationship. 469 return ( addressOfThis.length < addressOfOther.length ) ? 470 CKEDITOR.POSITION_CONTAINS + CKEDITOR.POSITION_PRECEDING : 471 CKEDITOR.POSITION_IS_CONTAINED + CKEDITOR.POSITION_FOLLOWING; 469 472 }, 470 473 471 474 /** -
_source/tests/core/dom/node.html
9 9 <script type="text/javascript" src="../../test.js"></script> 10 10 <script type="text/javascript"> 11 11 //<![CDATA[ 12 13 CKEDITOR.test.addTestCase( (function()12 var tc; 13 CKEDITOR.test.addTestCase( tc = (function() 14 14 { 15 15 // Local reference to the "assert" object. 16 16 var assert = CKEDITOR.test.assert; … … 68 68 { 69 69 var node1 = new CKEDITOR.dom.node( document.getElementsByTagName( 'h1' )[0] ); 70 70 var node2 = new CKEDITOR.dom.node( document.getElementsByTagName( 'h1' )[0].firstChild ); 71 72 71 assert.areSame( CKEDITOR.POSITION_IS_CONTAINED + CKEDITOR.POSITION_FOLLOWING, node2.getPosition( node1 ) ); 73 72 }, 74 73 … … 104 103 assert.areSame( CKEDITOR.POSITION_FOLLOWING, node2.getPosition( node1 ) ); 105 104 }, 106 105 106 /** 107 * Test 'preceding' position. 108 */ 109 test_getPosition_3240: function() 110 { 111 var node1 = new CKEDITOR.dom.node( document.getElementsByTagName( 'b' )[0].firstChild ); 112 var node2 = new CKEDITOR.dom.node( document.getElementsByTagName( 'span' )[0].firstChild ); 113 114 assert.areSame( CKEDITOR.POSITION_PRECEDING, node1.getPosition( node2 ) ); 115 }, 116 107 117 name : document.title 108 118 }; 109 119 })() ); … … 115 125 // 116 126 // alert( node1.getPosition( node2 ) ); 117 127 //}; 118 128 //window.onload = tc.test_getPosition6; 119 129 //]]> 120 130 </script> 121 131 </head> … … 124 134 <h1>Title</h1> 125 135 <p><b>Sample</b> <i>Text</i></p> 126 136 </div> 137 <span>Another</span> 127 138 </body> 128 139 </html>