2 Copyright (c) 2003-2013, 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 requires : [ 'fakeobjects', 'dialog' ],
\r
9 init : function( editor )
\r
11 // Add the link and unlink buttons.
\r
12 editor.addCommand( 'link', new CKEDITOR.dialogCommand( 'link' ) );
\r
13 editor.addCommand( 'anchor', new CKEDITOR.dialogCommand( 'anchor' ) );
\r
14 editor.addCommand( 'unlink', new CKEDITOR.unlinkCommand() );
\r
15 editor.addCommand( 'removeAnchor', new CKEDITOR.removeAnchorCommand() );
\r
16 editor.ui.addButton( 'Link',
\r
18 label : editor.lang.link.toolbar,
\r
21 editor.ui.addButton( 'Unlink',
\r
23 label : editor.lang.unlink,
\r
26 editor.ui.addButton( 'Anchor',
\r
28 label : editor.lang.anchor.toolbar,
\r
31 CKEDITOR.dialog.add( 'link', this.path + 'dialogs/link.js' );
\r
32 CKEDITOR.dialog.add( 'anchor', this.path + 'dialogs/anchor.js' );
\r
34 // Add the CSS styles for anchor placeholders.
\r
36 var side = ( editor.lang.dir == 'rtl' ? 'right' : 'left' );
\r
38 'background:url(' + CKEDITOR.getUrl( this.path + 'images/anchor.gif' ) + ') no-repeat ' + side + ' center;' +
\r
39 'border:1px dotted #00f;';
\r
42 'a.cke_anchor,a.cke_anchor_empty' +
\r
43 // IE6 breaks with the following selectors.
\r
44 ( ( CKEDITOR.env.ie && CKEDITOR.env.version < 7 ) ? '' :
\r
45 ',a[name],a[data-cke-saved-name]' ) +
\r
48 'padding-' + side + ':18px;' +
\r
49 // Show the arrow cursor for the anchor image (FF at least).
\r
52 ( CKEDITOR.env.ie ? (
\r
53 'a.cke_anchor_empty' +
\r
55 // Make empty anchor selectable on IE.
\r
56 'display:inline-block;' +
\r
63 'min-height:15px;' +
\r
64 // The default line-height on IE.
\r
66 // Opera works better with "middle" (even if not perfect)
\r
67 'vertical-align:' + ( CKEDITOR.env.opera ? 'middle' : 'text-bottom' ) + ';' +
\r
70 // Register selection change handler for the unlink button.
\r
71 editor.on( 'selectionChange', function( evt )
\r
73 if ( editor.readOnly )
\r
77 * Despite our initial hope, document.queryCommandEnabled() does not work
\r
78 * for this in Firefox. So we must detect the state by element paths.
\r
80 var command = editor.getCommand( 'unlink' ),
\r
81 element = evt.data.path.lastElement && evt.data.path.lastElement.getAscendant( 'a', true );
\r
82 if ( element && element.getName() == 'a' && element.getAttribute( 'href' ) && element.getChildCount() )
\r
83 command.setState( CKEDITOR.TRISTATE_OFF );
\r
85 command.setState( CKEDITOR.TRISTATE_DISABLED );
\r
88 editor.on( 'doubleclick', function( evt )
\r
90 var element = CKEDITOR.plugins.link.getSelectedLink( editor ) || evt.data.element;
\r
92 if ( !element.isReadOnly() )
\r
94 if ( element.is( 'a' ) )
\r
96 evt.data.dialog = ( element.getAttribute( 'name' ) && ( !element.getAttribute( 'href' ) || !element.getChildCount() ) ) ? 'anchor' : 'link';
\r
97 editor.getSelection().selectElement( element );
\r
99 else if ( CKEDITOR.plugins.link.tryRestoreFakeAnchor( editor, element ) )
\r
100 evt.data.dialog = 'anchor';
\r
104 // If the "menu" plugin is loaded, register the menu items.
\r
105 if ( editor.addMenuItems )
\r
107 editor.addMenuItems(
\r
111 label : editor.lang.anchor.menu,
\r
112 command : 'anchor',
\r
119 label : editor.lang.anchor.remove,
\r
120 command : 'removeAnchor',
\r
127 label : editor.lang.link.menu,
\r
135 label : editor.lang.unlink,
\r
136 command : 'unlink',
\r
143 // If the "contextmenu" plugin is loaded, register the listeners.
\r
144 if ( editor.contextMenu )
\r
146 editor.contextMenu.addListener( function( element, selection )
\r
148 if ( !element || element.isReadOnly() )
\r
151 var anchor = CKEDITOR.plugins.link.tryRestoreFakeAnchor( editor, element );
\r
153 if ( !anchor && !( anchor = CKEDITOR.plugins.link.getSelectedLink( editor ) ) )
\r
158 if ( anchor.getAttribute( 'href' ) && anchor.getChildCount() )
\r
159 menu = { link : CKEDITOR.TRISTATE_OFF, unlink : CKEDITOR.TRISTATE_OFF };
\r
161 if ( anchor && anchor.hasAttribute( 'name' ) )
\r
162 menu.anchor = menu.removeAnchor = CKEDITOR.TRISTATE_OFF;
\r
169 afterInit : function( editor )
\r
171 // Register a filter to displaying placeholders after mode change.
\r
173 var dataProcessor = editor.dataProcessor,
\r
174 dataFilter = dataProcessor && dataProcessor.dataFilter,
\r
175 htmlFilter = dataProcessor && dataProcessor.htmlFilter,
\r
176 pathFilters = editor._.elementsPath && editor._.elementsPath.filters;
\r
180 dataFilter.addRules(
\r
184 a : function( element )
\r
186 var attributes = element.attributes;
\r
187 if ( !attributes.name )
\r
190 var isEmpty = !element.children.length;
\r
192 if ( CKEDITOR.plugins.link.synAnchorSelector )
\r
194 // IE needs a specific class name to be applied
\r
195 // to the anchors, for appropriate styling.
\r
196 var ieClass = isEmpty ? 'cke_anchor_empty' : 'cke_anchor';
\r
197 var cls = attributes[ 'class' ];
\r
198 if ( attributes.name && ( !cls || cls.indexOf( ieClass ) < 0 ) )
\r
199 attributes[ 'class' ] = ( cls || '' ) + ' ' + ieClass;
\r
201 if ( isEmpty && CKEDITOR.plugins.link.emptyAnchorFix )
\r
203 attributes.contenteditable = 'false';
\r
204 attributes[ 'data-cke-editable' ] = 1;
\r
207 else if ( CKEDITOR.plugins.link.fakeAnchor && isEmpty )
\r
208 return editor.createFakeParserElement( element, 'cke_anchor', 'anchor' );
\r
216 if ( CKEDITOR.plugins.link.emptyAnchorFix && htmlFilter )
\r
218 htmlFilter.addRules(
\r
222 a : function( element )
\r
224 delete element.attributes.contenteditable;
\r
232 pathFilters.push( function( element, name )
\r
236 if ( CKEDITOR.plugins.link.tryRestoreFakeAnchor( editor, element ) ||
\r
237 ( element.getAttribute( 'name' ) && ( !element.getAttribute( 'href' ) || !element.getChildCount() ) ) )
\r
247 CKEDITOR.plugins.link =
\r
250 * Get the surrounding link element of current selection.
\r
252 * @example CKEDITOR.plugins.link.getSelectedLink( editor );
\r
254 * The following selection will all return the link element.
\r
256 * <a href="#">li^nk</a>
\r
257 * <a href="#">[link]</a>
\r
258 * text[<a href="#">link]</a>
\r
259 * <a href="#">li[nk</a>]
\r
260 * [<b><a href="#">li]nk</a></b>]
\r
261 * [<a href="#"><b>li]nk</b></a>
\r
264 getSelectedLink : function( editor )
\r
268 var selection = editor.getSelection();
\r
269 if ( selection.getType() == CKEDITOR.SELECTION_ELEMENT )
\r
271 var selectedElement = selection.getSelectedElement();
\r
272 if ( selectedElement.is( 'a' ) )
\r
273 return selectedElement;
\r
276 var range = selection.getRanges( true )[ 0 ];
\r
277 range.shrink( CKEDITOR.SHRINK_TEXT );
\r
278 var root = range.getCommonAncestor();
\r
279 return root.getAscendant( 'a', true );
\r
281 catch( e ) { return null; }
\r
284 // Opera and WebKit don't make it possible to select empty anchors. Fake
\r
285 // elements must be used for them.
\r
286 fakeAnchor : CKEDITOR.env.opera || CKEDITOR.env.webkit,
\r
288 // For browsers that don't support CSS3 a[name]:empty(), note IE9 is included because of #7783.
\r
289 synAnchorSelector : CKEDITOR.env.ie,
\r
291 // For browsers that have editing issue with empty anchor.
\r
292 emptyAnchorFix : CKEDITOR.env.ie && CKEDITOR.env.version < 8,
\r
294 tryRestoreFakeAnchor : function( editor, element )
\r
296 if ( element && element.data( 'cke-real-element-type' ) && element.data( 'cke-real-element-type' ) == 'anchor' )
\r
298 var link = editor.restoreRealElement( element );
\r
299 if ( link.data( 'cke-saved-name' ) )
\r
305 CKEDITOR.unlinkCommand = function(){};
\r
306 CKEDITOR.unlinkCommand.prototype =
\r
309 exec : function( editor )
\r
312 * execCommand( 'unlink', ... ) in Firefox leaves behind <span> tags at where
\r
313 * the <a> was, so again we have to remove the link ourselves. (See #430)
\r
315 * TODO: Use the style system when it's complete. Let's use execCommand()
\r
316 * as a stopgap solution for now.
\r
318 var selection = editor.getSelection(),
\r
319 bookmarks = selection.createBookmarks(),
\r
320 ranges = selection.getRanges(),
\r
324 for ( var i = 0 ; i < ranges.length ; i++ )
\r
326 rangeRoot = ranges[i].getCommonAncestor( true );
\r
327 element = rangeRoot.getAscendant( 'a', true );
\r
330 ranges[i].selectNodeContents( element );
\r
333 selection.selectRanges( ranges );
\r
334 editor.document.$.execCommand( 'unlink', false, null );
\r
335 selection.selectBookmarks( bookmarks );
\r
338 startDisabled : true
\r
341 CKEDITOR.removeAnchorCommand = function(){};
\r
342 CKEDITOR.removeAnchorCommand.prototype =
\r
345 exec : function( editor )
\r
347 var sel = editor.getSelection(),
\r
348 bms = sel.createBookmarks(),
\r
350 if ( sel && ( anchor = sel.getSelectedElement() ) && ( CKEDITOR.plugins.link.fakeAnchor && !anchor.getChildCount() ? CKEDITOR.plugins.link.tryRestoreFakeAnchor( editor, anchor ) : anchor.is( 'a' ) ) )
\r
351 anchor.remove( 1 );
\r
354 if ( ( anchor = CKEDITOR.plugins.link.getSelectedLink( editor ) ) )
\r
356 if ( anchor.hasAttribute( 'href' ) )
\r
358 anchor.removeAttributes( { name : 1, 'data-cke-saved-name' : 1 } );
\r
359 anchor.removeClass( 'cke_anchor' );
\r
362 anchor.remove( 1 );
\r
365 sel.selectBookmarks( bms );
\r
369 CKEDITOR.tools.extend( CKEDITOR.config,
\r
371 linkShowAdvancedTab : true,
\r
372 linkShowTargetTab : true
\r