Ticket #2768: 2768_5.patch

File 2768_5.patch, 4.7 KB (added by Martin Kou, 15 years ago)
  • _source/plugins/styles/plugin.js

     
    417417                range.enlarge( CKEDITOR.ENLARGE_ELEMENT );
    418418
    419419                var bookmark = range.createBookmark( true ),
    420                         startNode = range.document.getById( bookmark.startNode ),
    421                         startPath = new CKEDITOR.dom.elementPath( startNode.getParent() );
     420                        startNode = range.document.getById( bookmark.startNode );
    422421
    423422                if ( range.collapsed )
    424423                {
    425424                        /*
    426425                         * If the range is collapsed, try to remove the style from all ancestor
    427                          * elements, until either a block boundary is reached, or the style is
    428                          * removed.
     426                         * elements, until a block boundary is reached.
    429427                         */
     428                        var startPath = new CKEDITOR.dom.elementPath( startNode.getParent() );
    430429                        for ( var i = 0, element ; i < startPath.elements.length && ( element = startPath.elements[i] )  ; i++ )
    431430                        {
    432431                                if ( element == startPath.block || element == startPath.blockLimit )
     
    434433
    435434                                if ( this.checkElementRemovable( element ) )
    436435                                {
     436                                        /*
     437                                         * Before removing the style node, there may be a sibling to the style node
     438                                         * that's exactly the same to the one to be removed. To the user, it makes
     439                                         * no difference that they're separate entities in the DOM tree. So, merge
     440                                         * them before removal.
     441                                         */
     442                                        mergeSiblings( element );
    437443                                        removeFromElement( this, element );
    438                                         break;
    439444                                }
    440445                        }
    441446                }
     
    446451                         * node via DFS and remove the styles one-by-one.
    447452                         */
    448453                        var endNode = range.document.getById( bookmark.endNode ),
    449                                 endPath = new CKEDITOR.dom.elementPath( endNode.getParent() );
    450                                 currentNode = startNode;
     454                                me = this;
    451455
    452                         // Find out the ancestor that needs to be broken down at startNode and endNode.
    453                         var breakStart = null, breakEnd = null;
    454                         for ( var i = 0 ; i < startPath.elements.length ; i++ )
     456                        /*
     457                         * Find out the style ancestor that needs to be broken down at startNode
     458                         * and endNode.
     459                         */
     460                        function breakNodes()
    455461                        {
    456                                 if ( this.checkElementRemovable( startPath.elements[ i ] ) )
     462                                var startPath = new CKEDITOR.dom.elementPath( startNode.getParent() ),
     463                                        endPath = new CKEDITOR.dom.elementPath( endNode.getParent() ),
     464                                        breakStart = null,
     465                                        breakEnd = null;
     466                                for ( var i = 0 ; i < startPath.elements.length ; i++ )
    457467                                {
    458                                         breakStart = startPath.elements[ i ];
    459                                         break;
     468                                        var element = startPath.elements[ i ];
     469
     470                                        if ( element == startPath.block || element == startPath.blockLimit )
     471                                                break;
     472
     473                                        if ( me.checkElementRemovable( element ) )
     474                                                breakStart = element;
    460475                                }
    461                         }
    462                         for ( var i = 0 ; i < endPath.elements.length ; i++ )
    463                         {
    464                                 if ( this.checkElementRemovable( endPath.elements[ i ] ) )
     476                                for ( var i = 0 ; i < endPath.elements.length ; i++ )
    465477                                {
    466                                         breakEnd = endPath.elements[ i ];
    467                                         break;
     478                                        var element = endPath.elements[ i ];
     479
     480                                        if ( element == endPath.block || element == endPath.blockLimit )
     481                                                break;
     482
     483                                        if ( me.checkElementRemovable( element ) )
     484                                                breakEnd = element;
    468485                                }
     486
     487                                if ( breakEnd )
     488                                        endNode.breakParent( breakEnd );
     489                                if ( breakStart )
     490                                        startNode.breakParent( breakStart );
    469491                        }
     492                        breakNodes();
    470493
    471                         if ( breakEnd )
    472                                 endNode.breakParent( breakEnd );
    473                         if ( breakStart )
    474                                 startNode.breakParent( breakStart );
    475 
    476494                        // Now, do the DFS walk.
    477                         while ( ( currentNode = currentNode.getNextSourceNode() ) && !currentNode.equals( endNode ) )
     495                        var currentNode = startNode.getNext();
     496                        while ( !currentNode.equals( endNode ) )
    478497                        {
    479                                 if ( currentNode.type == CKEDITOR.NODE_ELEMENT )
     498                                /*
     499                                 * Need to get the next node first because removeFromElement() can remove
     500                                 * the current node from DOM tree.
     501                                 */
     502                                var nextNode = currentNode.getNextSourceNode();
     503                                if ( currentNode.type == CKEDITOR.NODE_ELEMENT && this.checkElementRemovable( currentNode ) )
     504                                {
    480505                                        removeFromElement( this, currentNode );
     506
     507                                        /*
     508                                         * removeFromElement() may have merged the next node with something before
     509                                         * the startNode via mergeSiblings(). In that case, the nextNode would
     510                                         * contain startNode and we'll have to call breakNodes() again and also
     511                                         * reassign the nextNode to something after startNode.
     512                                         */
     513                                        if ( nextNode.type == CKEDITOR.NODE_ELEMENT && nextNode.contains( startNode ) )
     514                                        {
     515                                                breakNodes();
     516                                                nextNode = startNode.getNext();
     517                                        }
     518                                }
     519                                currentNode = nextNode;
    481520                        }
    482521                }
    483522               
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy