2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
6 CKEDITOR.plugins.add( 'link',
\r
8 init : function( editor )
\r
10 // Add the link and unlink buttons.
\r
11 editor.addCommand( 'link', new CKEDITOR.dialogCommand( 'link' ) );
\r
12 editor.addCommand( 'anchor', new CKEDITOR.dialogCommand( 'anchor' ) );
\r
13 editor.addCommand( 'unlink', new CKEDITOR.unlinkCommand() );
\r
14 editor.ui.addButton( 'Link',
\r
16 label : editor.lang.link.toolbar,
\r
19 editor.ui.addButton( 'Unlink',
\r
21 label : editor.lang.unlink,
\r
24 editor.ui.addButton( 'Anchor',
\r
26 label : editor.lang.anchor.toolbar,
\r
29 CKEDITOR.dialog.add( 'link', this.path + 'dialogs/link.js' );
\r
30 CKEDITOR.dialog.add( 'anchor', this.path + 'dialogs/anchor.js' );
\r
32 // Add the CSS styles for anchor placeholders.
\r
34 var side = ( editor.lang.dir == 'rtl' ? 'right' : 'left' );
\r
36 'background:url(' + CKEDITOR.getUrl( this.path + 'images/anchor.gif' ) + ') no-repeat ' + side + ' center;' +
\r
37 'border:1px dotted #00f;';
\r
40 'a.cke_anchor,a.cke_anchor_empty' +
\r
41 // IE6 breaks with the following selectors.
\r
42 ( ( CKEDITOR.env.ie && CKEDITOR.env.version < 7 ) ? '' :
\r
43 ',a[name],a[data-cke-saved-name]' ) +
\r
46 'padding-' + side + ':18px;' +
\r
47 // Show the arrow cursor for the anchor image (FF at least).
\r
50 ( CKEDITOR.env.ie ? (
\r
51 'a.cke_anchor_empty' +
\r
53 // Make empty anchor selectable on IE.
\r
54 'display:inline-block;' +
\r
61 'min-height:15px;' +
\r
62 // The default line-height on IE.
\r
64 // Opera works better with "middle" (even if not perfect)
\r
65 'vertical-align:' + ( CKEDITOR.env.opera ? 'middle' : 'text-bottom' ) + ';' +
\r
68 // Register selection change handler for the unlink button.
\r
69 editor.on( 'selectionChange', function( evt )
\r
71 if ( editor.readOnly )
\r
75 * Despite our initial hope, document.queryCommandEnabled() does not work
\r
76 * for this in Firefox. So we must detect the state by element paths.
\r
78 var command = editor.getCommand( 'unlink' ),
\r
79 element = evt.data.path.lastElement && evt.data.path.lastElement.getAscendant( 'a', true );
\r
80 if ( element && element.getName() == 'a' && element.getAttribute( 'href' ) && element.getChildCount() )
\r
81 command.setState( CKEDITOR.TRISTATE_OFF );
\r
83 command.setState( CKEDITOR.TRISTATE_DISABLED );
\r
86 editor.on( 'doubleclick', function( evt )
\r
88 var element = CKEDITOR.plugins.link.getSelectedLink( editor ) || evt.data.element;
\r
90 if ( !element.isReadOnly() )
\r
92 if ( element.is( 'a' ) )
\r
94 evt.data.dialog = ( element.getAttribute( 'name' ) && ( !element.getAttribute( 'href' ) || !element.getChildCount() ) ) ? 'anchor' : 'link';
\r
95 editor.getSelection().selectElement( element );
\r
97 else if ( CKEDITOR.plugins.link.tryRestoreFakeAnchor( editor, element ) )
\r
98 evt.data.dialog = 'anchor';
\r
102 // If the "menu" plugin is loaded, register the menu items.
\r
103 if ( editor.addMenuItems )
\r
105 editor.addMenuItems(
\r
109 label : editor.lang.anchor.menu,
\r
110 command : 'anchor',
\r
116 label : editor.lang.link.menu,
\r
124 label : editor.lang.unlink,
\r
125 command : 'unlink',
\r
132 // If the "contextmenu" plugin is loaded, register the listeners.
\r
133 if ( editor.contextMenu )
\r
135 editor.contextMenu.addListener( function( element, selection )
\r
137 if ( !element || element.isReadOnly() )
\r
140 var anchor = CKEDITOR.plugins.link.tryRestoreFakeAnchor( editor, element );
\r
142 if ( !anchor && !( anchor = CKEDITOR.plugins.link.getSelectedLink( editor ) ) )
\r
147 if ( anchor.getAttribute( 'href' ) && anchor.getChildCount() )
\r
148 menu = { link : CKEDITOR.TRISTATE_OFF, unlink : CKEDITOR.TRISTATE_OFF };
\r
150 if ( anchor && anchor.hasAttribute( 'name' ) )
\r
151 menu.anchor = CKEDITOR.TRISTATE_OFF;
\r
158 afterInit : function( editor )
\r
160 // Register a filter to displaying placeholders after mode change.
\r
162 var dataProcessor = editor.dataProcessor,
\r
163 dataFilter = dataProcessor && dataProcessor.dataFilter,
\r
164 htmlFilter = dataProcessor && dataProcessor.htmlFilter,
\r
165 pathFilters = editor._.elementsPath && editor._.elementsPath.filters;
\r
169 dataFilter.addRules(
\r
173 a : function( element )
\r
175 var attributes = element.attributes;
\r
176 if ( !attributes.name )
\r
179 var isEmpty = !element.children.length;
\r
181 if ( CKEDITOR.plugins.link.synAnchorSelector )
\r
183 // IE needs a specific class name to be applied
\r
184 // to the anchors, for appropriate styling.
\r
185 var ieClass = isEmpty ? 'cke_anchor_empty' : 'cke_anchor';
\r
186 var cls = attributes[ 'class' ];
\r
187 if ( attributes.name && ( !cls || cls.indexOf( ieClass ) < 0 ) )
\r
188 attributes[ 'class' ] = ( cls || '' ) + ' ' + ieClass;
\r
190 if ( isEmpty && CKEDITOR.plugins.link.emptyAnchorFix )
\r
192 attributes.contenteditable = 'false';
\r
193 attributes[ 'data-cke-editable' ] = 1;
\r
196 else if ( CKEDITOR.plugins.link.fakeAnchor && isEmpty )
\r
197 return editor.createFakeParserElement( element, 'cke_anchor', 'anchor' );
\r
205 if ( CKEDITOR.plugins.link.emptyAnchorFix && htmlFilter )
\r
207 htmlFilter.addRules(
\r
211 a : function( element )
\r
213 delete element.attributes.contenteditable;
\r
221 pathFilters.push( function( element, name )
\r
225 if ( CKEDITOR.plugins.link.tryRestoreFakeAnchor( editor, element ) ||
\r
226 ( element.getAttribute( 'name' ) && ( !element.getAttribute( 'href' ) || !element.getChildCount() ) ) )
\r
235 requires : [ 'fakeobjects' ]
\r
238 CKEDITOR.plugins.link =
\r
241 * Get the surrounding link element of current selection.
\r
243 * @example CKEDITOR.plugins.link.getSelectedLink( editor );
\r
245 * The following selection will all return the link element.
\r
247 * <a href="#">li^nk</a>
\r
248 * <a href="#">[link]</a>
\r
249 * text[<a href="#">link]</a>
\r
250 * <a href="#">li[nk</a>]
\r
251 * [<b><a href="#">li]nk</a></b>]
\r
252 * [<a href="#"><b>li]nk</b></a>
\r
255 getSelectedLink : function( editor )
\r
259 var selection = editor.getSelection();
\r
260 if ( selection.getType() == CKEDITOR.SELECTION_ELEMENT )
\r
262 var selectedElement = selection.getSelectedElement();
\r
263 if ( selectedElement.is( 'a' ) )
\r
264 return selectedElement;
\r
267 var range = selection.getRanges( true )[ 0 ];
\r
268 range.shrink( CKEDITOR.SHRINK_TEXT );
\r
269 var root = range.getCommonAncestor();
\r
270 return root.getAscendant( 'a', true );
\r
272 catch( e ) { return null; }
\r
275 // Opera and WebKit don't make it possible to select empty anchors. Fake
\r
276 // elements must be used for them.
\r
277 fakeAnchor : CKEDITOR.env.opera || CKEDITOR.env.webkit,
\r
279 // For browsers that don't support CSS3 a[name]:empty(), note IE9 is included because of #7783.
\r
280 synAnchorSelector : CKEDITOR.env.ie,
\r
282 // For browsers that have editing issue with empty anchor.
\r
283 emptyAnchorFix : CKEDITOR.env.ie && CKEDITOR.env.version < 8,
\r
285 tryRestoreFakeAnchor : function( editor, element )
\r
287 if ( element && element.data( 'cke-real-element-type' ) && element.data( 'cke-real-element-type' ) == 'anchor' )
\r
289 var link = editor.restoreRealElement( element );
\r
290 if ( link.data( 'cke-saved-name' ) )
\r
296 CKEDITOR.unlinkCommand = function(){};
\r
297 CKEDITOR.unlinkCommand.prototype =
\r
300 exec : function( editor )
\r
303 * execCommand( 'unlink', ... ) in Firefox leaves behind <span> tags at where
\r
304 * the <a> was, so again we have to remove the link ourselves. (See #430)
\r
306 * TODO: Use the style system when it's complete. Let's use execCommand()
\r
307 * as a stopgap solution for now.
\r
309 var selection = editor.getSelection(),
\r
310 bookmarks = selection.createBookmarks(),
\r
311 ranges = selection.getRanges(),
\r
315 for ( var i = 0 ; i < ranges.length ; i++ )
\r
317 rangeRoot = ranges[i].getCommonAncestor( true );
\r
318 element = rangeRoot.getAscendant( 'a', true );
\r
321 ranges[i].selectNodeContents( element );
\r
324 selection.selectRanges( ranges );
\r
325 editor.document.$.execCommand( 'unlink', false, null );
\r
326 selection.selectBookmarks( bookmarks );
\r
329 startDisabled : true
\r
332 CKEDITOR.tools.extend( CKEDITOR.config,
\r
334 linkShowAdvancedTab : true,
\r
335 linkShowTargetTab : true
\r