Ticket #3240: 3240_4.patch

File 3240_4.patch, 5.0 KB (added by Frederico Caldeira Knabben, 15 years ago)
  • _source/core/dom/node.js

     
    424424                        if ( $ == $other )
    425425                                return CKEDITOR.POSITION_IDENTICAL;
    426426
    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 )
    429429                        {
    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 )
    437431                                {
    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;
    440437                                }
    441                         }
    442438
    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                                }
    454445                        }
     446                       
     447                        // For nodes that don't support compareDocumentPosition, contains
     448                        // or sourceIndex, their "address" is compared.
    455449
    456                         // WebKit has no support for sourceIndex.
     450                        var addressOfThis = this.getAddress(),
     451                                addressOfOther = otherNode.getAddress(),
     452                                minLevel = Math.min( addressOfThis.length, addressOfOther.length );
    457453
    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                                }
    459467
    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;
    469472                },
    470473
    471474                /**
  • _source/tests/core/dom/node.html

     
    99        <script type="text/javascript" src="../../test.js"></script>
    1010        <script type="text/javascript">
    1111        //<![CDATA[
    12 
    13 CKEDITOR.test.addTestCase( (function()
     12var tc;
     13CKEDITOR.test.addTestCase( tc = (function()
    1414{
    1515        // Local reference to the "assert" object.
    1616        var assert = CKEDITOR.test.assert;
     
    6868                {
    6969                        var node1 = new CKEDITOR.dom.node( document.getElementsByTagName( 'h1' )[0] );
    7070                        var node2 = new CKEDITOR.dom.node( document.getElementsByTagName( 'h1' )[0].firstChild );
    71 
    7271                        assert.areSame( CKEDITOR.POSITION_IS_CONTAINED + CKEDITOR.POSITION_FOLLOWING, node2.getPosition( node1 ) );
    7372                },
    7473
     
    104103                        assert.areSame( CKEDITOR.POSITION_FOLLOWING, node2.getPosition( node1 ) );
    105104                },
    106105
     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
    107117                name : document.title
    108118        };
    109119})() );
     
    115125//
    116126//      alert( node1.getPosition( node2 ) );
    117127//};
    118 
     128//window.onload =  tc.test_getPosition6;
    119129        //]]>
    120130        </script>
    121131</head>
     
    124134                <h1>Title</h1>
    125135                <p><b>Sample</b> <i>Text</i></p>
    126136        </div>
     137        <span>Another</span>
    127138</body>
    128139</html>
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy