Ticket #4513: 4513_2.patch

File 4513_2.patch, 5.3 KB (added by Garry Yao, 14 years ago)
  • _source/plugins/link/dialogs/link.js

     
    55
    66CKEDITOR.dialog.add( 'link', function( editor )
    77{
     8        var plugin = CKEDITOR.plugins.link;
    89        // Handles the event when the "Target" selection box is changed.
    910        var targetChanged = function()
    1011        {
     
    9293
    9394        var parseLink = function( editor, element )
    9495        {
    95                 var href = element ? ( element.getAttribute( '_cke_saved_href' ) || element.getAttribute( 'href' ) ) : '',
     96                var href = ( element  && ( element.getAttribute( '_cke_saved_href' ) || element.getAttribute( 'href' ) ) ) || '',
    9697                        emailMatch,
    9798                        anchorMatch,
    9899                        urlMatch,
     
    11271128
    11281129                        var editor = this.getParentEditor(),
    11291130                                selection = editor.getSelection(),
    1130                                 ranges = selection.getRanges(),
    1131                                 element = null,
    1132                                 me = this;
    1133                         // Fill in all the relevant fields if there's already one link selected.
    1134                         if ( ranges.length == 1 )
    1135                         {
     1131                                element = null;
    11361132
    1137                                 var rangeRoot = ranges[0].getCommonAncestor( true );
    1138                                 element = rangeRoot.getAscendant( 'a', true );
    1139                                 if ( element && element.getAttribute( 'href' ) )
    1140                                 {
    1141                                         selection.selectElement( element );
    1142                                 }
    1143                                 else if ( ( element = rangeRoot.getAscendant( 'img', true ) ) &&
    1144                                                  element.getAttribute( '_cke_real_element_type' ) &&
    1145                                                  element.getAttribute( '_cke_real_element_type' ) == 'anchor' )
    1146                                 {
    1147                                         this.fakeObj = element;
    1148                                         element = editor.restoreRealElement( this.fakeObj );
    1149                                         selection.selectElement( this.fakeObj );
    1150                                 }
    1151                                 else
    1152                                         element = null;
    1153                         }
     1133                        // Fill in all the relevant fields if there's already one link selected.
     1134                        if ( ( element = plugin.getSelectedLink( editor ) ) && element.hasAttribute( 'href' ) )
     1135                                selection.selectElement( element );
     1136                        else if ( ( element = selection.getSelectedElement() ) && element.is( 'img' )
     1137                                        && element.getAttribute( '_cke_real_element_type' )
     1138                                        && element.getAttribute( '_cke_real_element_type' ) == 'anchor' )
     1139                        {
     1140                                this.fakeObj = element;
     1141                                element = editor.restoreRealElement( this.fakeObj );
     1142                                selection.selectElement( this.fakeObj );
     1143                        }
     1144                        else
     1145                                element = null;
    11541146
    11551147                        this.setupContent( parseLink.apply( this, [ editor, element ] ) );
    11561148                },
  • _source/plugins/link/plugin.js

     
    107107
    108108                                        if ( !isAnchor )
    109109                                        {
    110                                                 if ( !( element = element.getAscendant( 'a', true ) ) )
     110                                                if ( !( element = CKEDITOR.plugins.link.getSelectedLink( editor ) ) )
    111111                                                        return null;
    112112
    113113                                                isAnchor = ( element.getAttribute( 'name' ) && !element.getAttribute( 'href' ) );
     
    147147        requires : [ 'fakeobjects' ]
    148148} );
    149149
     150CKEDITOR.plugins.link =
     151{
     152        /**
     153         *  Get the link element under current selection.
     154         * @param editor
     155         */
     156        getSelectedLink : function( editor )
     157        {
     158                var selection = editor.getSelection(),
     159                        bookmarks = selection.createBookmarks(),
     160                        ranges = selection.getRanges(),
     161                        range = ranges[0],
     162                        link;
     163
     164                link = range.getSurroundingElement( 'a' );
     165                selection.selectBookmarks( bookmarks );
     166
     167                return link;
     168        }
     169
     170};
     171
    150172CKEDITOR.unlinkCommand = function(){};
    151173CKEDITOR.unlinkCommand.prototype =
    152174{
  • _source/core/dom/range.js

     
    17071707                                return container ;
    17081708
    17091709                        return container.getChild( this.endOffset - 1 ) || container ;
    1710                 }
    1711         };
     1710                },
     1711
     1712                /**
     1713                 *  Retrieve the element that either surround or touch the boundary of the range.
     1714                 *  Note: This method may result in fragile text nodes. 
     1715                 * @param tagName
     1716                 * @example
     1717                 *  var link = range.getSurroundingElement( 'a' );
     1718                 *       <pre>
     1719                 *  <a href="#">li^nk</a>
     1720                 *  <a href="#">[link]</a>
     1721                 *  text[<a href="#">link]</a>
     1722                 *  <a href="#">li[nk</a>]
     1723                 *  [<b><a href="#">li]nk</a></b>]
     1724                 *  [<a href="#"><b>li]nk</b></a>
     1725                 * </pre>
     1726                 */
     1727                getSurroundingElement : function( tagName )
     1728                {
     1729                        var surround;
     1730
     1731                        if( this.collapsed )
     1732                                surround = this.startContainer.getAscendant( tagName, true );
     1733                        else
     1734                        {
     1735                                var walker = new CKEDITOR.dom.walker( this );
     1736
     1737                                walker.evaluator = function( node )
     1738                                {
     1739                                        if ( node.type == CKEDITOR.NODE_TEXT )
     1740                                        {
     1741                                                var ascendant = node.getAscendant( tagName );
     1742                                                if ( ascendant && ( !surround || ascendant.equals( surround ) ) )
     1743                                                {
     1744                                                        surround = ascendant;
     1745                                                        return true;
     1746                                                }
     1747                                                else
     1748                                                        return ( surround = false );
     1749                                        }
     1750                                };
     1751
     1752                                walker.guard = function()
     1753                                {
     1754                                        return surround !== false;
     1755                                };
     1756
     1757                                // Walk through all enclosed text nodes that are parts of  the *same* element.
     1758                                walker.checkForward();
     1759
     1760                        }
     1761                        return surround || null;
     1762                }
     1763        };
    17121764})();
    17131765
    17141766CKEDITOR.POSITION_AFTER_START   = 1;    // <element>^contents</element>         "^text"
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy