]> git.immae.eu Git - perso/Immae/Projets/packagist/ludivine-ckeditor-component.git/blame - sources/plugins/link/plugin.js
Update to 4.7.3
[perso/Immae/Projets/packagist/ludivine-ckeditor-component.git] / sources / plugins / link / plugin.js
CommitLineData
c63493c8
IB
1/**\r
2 * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.\r
3 * For licensing, see LICENSE.md or http://ckeditor.com/license\r
4 */\r
5\r
6'use strict';\r
7\r
8( function() {\r
9 CKEDITOR.plugins.add( 'link', {\r
10 requires: 'dialog,fakeobjects',\r
11 // jscs:disable maximumLineLength\r
1794320d 12 lang: 'af,ar,az,bg,bn,bs,ca,cs,cy,da,de,de-ch,el,en,en-au,en-ca,en-gb,eo,es,es-mx,et,eu,fa,fi,fo,fr,fr-ca,gl,gu,he,hi,hr,hu,id,is,it,ja,ka,km,ko,ku,lt,lv,mk,mn,ms,nb,nl,no,oc,pl,pt,pt-br,ro,ru,si,sk,sl,sq,sr,sr-latn,sv,th,tr,tt,ug,uk,vi,zh,zh-cn', // %REMOVE_LINE_CORE%\r
c63493c8
IB
13 // jscs:enable maximumLineLength\r
14 icons: 'anchor,anchor-rtl,link,unlink', // %REMOVE_LINE_CORE%\r
15 hidpi: true, // %REMOVE_LINE_CORE%\r
16 onLoad: function() {\r
17 // Add the CSS styles for anchor placeholders.\r
18 var iconPath = CKEDITOR.getUrl( this.path + 'images' + ( CKEDITOR.env.hidpi ? '/hidpi' : '' ) + '/anchor.png' ),\r
19 baseStyle = 'background:url(' + iconPath + ') no-repeat %1 center;border:1px dotted #00f;background-size:16px;';\r
20\r
21 var template = '.%2 a.cke_anchor,' +\r
22 '.%2 a.cke_anchor_empty' +\r
23 ',.cke_editable.%2 a[name]' +\r
24 ',.cke_editable.%2 a[data-cke-saved-name]' +\r
25 '{' +\r
26 baseStyle +\r
27 'padding-%1:18px;' +\r
28 // Show the arrow cursor for the anchor image (FF at least).\r
29 'cursor:auto;' +\r
30 '}' +\r
31 '.%2 img.cke_anchor' +\r
32 '{' +\r
33 baseStyle +\r
34 'width:16px;' +\r
35 'min-height:15px;' +\r
36 // The default line-height on IE.\r
37 'height:1.15em;' +\r
38 // Opera works better with "middle" (even if not perfect)\r
39 'vertical-align:text-bottom;' +\r
40 '}';\r
41\r
42 // Styles with contents direction awareness.\r
43 function cssWithDir( dir ) {\r
44 return template.replace( /%1/g, dir == 'rtl' ? 'right' : 'left' ).replace( /%2/g, 'cke_contents_' + dir );\r
45 }\r
46\r
47 CKEDITOR.addCss( cssWithDir( 'ltr' ) + cssWithDir( 'rtl' ) );\r
48 },\r
49\r
50 init: function( editor ) {\r
51 var allowed = 'a[!href]',\r
52 required = 'a[href]';\r
53\r
54 if ( CKEDITOR.dialog.isTabEnabled( editor, 'link', 'advanced' ) )\r
55 allowed = allowed.replace( ']', ',accesskey,charset,dir,id,lang,name,rel,tabindex,title,type,download]{*}(*)' );\r
56 if ( CKEDITOR.dialog.isTabEnabled( editor, 'link', 'target' ) )\r
57 allowed = allowed.replace( ']', ',target,onclick]' );\r
58\r
59 // Add the link and unlink buttons.\r
60 editor.addCommand( 'link', new CKEDITOR.dialogCommand( 'link', {\r
61 allowedContent: allowed,\r
62 requiredContent: required\r
63 } ) );\r
64 editor.addCommand( 'anchor', new CKEDITOR.dialogCommand( 'anchor', {\r
65 allowedContent: 'a[!name,id]',\r
66 requiredContent: 'a[name]'\r
67 } ) );\r
68 editor.addCommand( 'unlink', new CKEDITOR.unlinkCommand() );\r
69 editor.addCommand( 'removeAnchor', new CKEDITOR.removeAnchorCommand() );\r
70\r
71 editor.setKeystroke( CKEDITOR.CTRL + 76 /*L*/, 'link' );\r
72\r
73 if ( editor.ui.addButton ) {\r
74 editor.ui.addButton( 'Link', {\r
75 label: editor.lang.link.toolbar,\r
76 command: 'link',\r
77 toolbar: 'links,10'\r
78 } );\r
79 editor.ui.addButton( 'Unlink', {\r
80 label: editor.lang.link.unlink,\r
81 command: 'unlink',\r
82 toolbar: 'links,20'\r
83 } );\r
84 editor.ui.addButton( 'Anchor', {\r
85 label: editor.lang.link.anchor.toolbar,\r
86 command: 'anchor',\r
87 toolbar: 'links,30'\r
88 } );\r
89 }\r
90\r
91 CKEDITOR.dialog.add( 'link', this.path + 'dialogs/link.js' );\r
92 CKEDITOR.dialog.add( 'anchor', this.path + 'dialogs/anchor.js' );\r
93\r
94 editor.on( 'doubleclick', function( evt ) {\r
1794320d
IB
95 // If the link has descendants and the last part of it is also a part of a word partially\r
96 // unlinked, clicked element may be a descendant of the link, not the link itself (http://dev.ckeditor.com/ticket/11956).\r
97 // The evt.data.element.getAscendant( 'img', 1 ) condition allows opening anchor dialog if the anchor is empty (#501).\r
98 var element = evt.data.element.getAscendant( { a: 1, img: 1 }, true );\r
c63493c8 99\r
1794320d 100 if ( element && !element.isReadOnly() ) {\r
c63493c8
IB
101 if ( element.is( 'a' ) ) {\r
102 evt.data.dialog = ( element.getAttribute( 'name' ) && ( !element.getAttribute( 'href' ) || !element.getChildCount() ) ) ? 'anchor' : 'link';\r
103\r
104 // Pass the link to be selected along with event data.\r
105 evt.data.link = element;\r
106 } else if ( CKEDITOR.plugins.link.tryRestoreFakeAnchor( editor, element ) ) {\r
107 evt.data.dialog = 'anchor';\r
108 }\r
109 }\r
110 }, null, null, 0 );\r
111\r
112 // If event was cancelled, link passed in event data will not be selected.\r
113 editor.on( 'doubleclick', function( evt ) {\r
1794320d 114 // Make sure both links and anchors are selected (http://dev.ckeditor.com/ticket/11822).\r
c63493c8
IB
115 if ( evt.data.dialog in { link: 1, anchor: 1 } && evt.data.link )\r
116 editor.getSelection().selectElement( evt.data.link );\r
117 }, null, null, 20 );\r
118\r
119 // If the "menu" plugin is loaded, register the menu items.\r
120 if ( editor.addMenuItems ) {\r
121 editor.addMenuItems( {\r
122 anchor: {\r
123 label: editor.lang.link.anchor.menu,\r
124 command: 'anchor',\r
125 group: 'anchor',\r
126 order: 1\r
127 },\r
128\r
129 removeAnchor: {\r
130 label: editor.lang.link.anchor.remove,\r
131 command: 'removeAnchor',\r
132 group: 'anchor',\r
133 order: 5\r
134 },\r
135\r
136 link: {\r
137 label: editor.lang.link.menu,\r
138 command: 'link',\r
139 group: 'link',\r
140 order: 1\r
141 },\r
142\r
143 unlink: {\r
144 label: editor.lang.link.unlink,\r
145 command: 'unlink',\r
146 group: 'link',\r
147 order: 5\r
148 }\r
149 } );\r
150 }\r
151\r
152 // If the "contextmenu" plugin is loaded, register the listeners.\r
153 if ( editor.contextMenu ) {\r
154 editor.contextMenu.addListener( function( element ) {\r
155 if ( !element || element.isReadOnly() )\r
156 return null;\r
157\r
158 var anchor = CKEDITOR.plugins.link.tryRestoreFakeAnchor( editor, element );\r
159\r
160 if ( !anchor && !( anchor = CKEDITOR.plugins.link.getSelectedLink( editor ) ) )\r
161 return null;\r
162\r
163 var menu = {};\r
164\r
165 if ( anchor.getAttribute( 'href' ) && anchor.getChildCount() )\r
166 menu = { link: CKEDITOR.TRISTATE_OFF, unlink: CKEDITOR.TRISTATE_OFF };\r
167\r
168 if ( anchor && anchor.hasAttribute( 'name' ) )\r
169 menu.anchor = menu.removeAnchor = CKEDITOR.TRISTATE_OFF;\r
170\r
171 return menu;\r
172 } );\r
173 }\r
174\r
175 this.compiledProtectionFunction = getCompiledProtectionFunction( editor );\r
176 },\r
177\r
178 afterInit: function( editor ) {\r
179 // Empty anchors upcasting to fake objects.\r
180 editor.dataProcessor.dataFilter.addRules( {\r
181 elements: {\r
182 a: function( element ) {\r
183 if ( !element.attributes.name )\r
184 return null;\r
185\r
186 if ( !element.children.length )\r
187 return editor.createFakeParserElement( element, 'cke_anchor', 'anchor' );\r
188\r
189 return null;\r
190 }\r
191 }\r
192 } );\r
193\r
194 var pathFilters = editor._.elementsPath && editor._.elementsPath.filters;\r
195 if ( pathFilters ) {\r
196 pathFilters.push( function( element, name ) {\r
197 if ( name == 'a' ) {\r
198 if ( CKEDITOR.plugins.link.tryRestoreFakeAnchor( editor, element ) || ( element.getAttribute( 'name' ) && ( !element.getAttribute( 'href' ) || !element.getChildCount() ) ) )\r
199 return 'anchor';\r
200 }\r
201 } );\r
202 }\r
203 }\r
204 } );\r
205\r
206 // Loads the parameters in a selected link to the link dialog fields.\r
207 var javascriptProtocolRegex = /^javascript:/,\r
208 emailRegex = /^mailto:([^?]+)(?:\?(.+))?$/,\r
209 emailSubjectRegex = /subject=([^;?:@&=$,\/]*)/i,\r
210 emailBodyRegex = /body=([^;?:@&=$,\/]*)/i,\r
211 anchorRegex = /^#(.*)$/,\r
212 urlRegex = /^((?:http|https|ftp|news):\/\/)?(.*)$/,\r
213 selectableTargets = /^(_(?:self|top|parent|blank))$/,\r
214 encodedEmailLinkRegex = /^javascript:void\(location\.href='mailto:'\+String\.fromCharCode\(([^)]+)\)(?:\+'(.*)')?\)$/,\r
215 functionCallProtectedEmailLinkRegex = /^javascript:([^(]+)\(([^)]+)\)$/,\r
216 popupRegex = /\s*window.open\(\s*this\.href\s*,\s*(?:'([^']*)'|null)\s*,\s*'([^']*)'\s*\)\s*;\s*return\s*false;*\s*/,\r
217 popupFeaturesRegex = /(?:^|,)([^=]+)=(\d+|yes|no)/gi;\r
218\r
219 var advAttrNames = {\r
220 id: 'advId',\r
221 dir: 'advLangDir',\r
222 accessKey: 'advAccessKey',\r
223 // 'data-cke-saved-name': 'advName',\r
224 name: 'advName',\r
225 lang: 'advLangCode',\r
226 tabindex: 'advTabIndex',\r
227 title: 'advTitle',\r
228 type: 'advContentType',\r
229 'class': 'advCSSClasses',\r
230 charset: 'advCharset',\r
231 style: 'advStyles',\r
232 rel: 'advRel'\r
233 };\r
234\r
235 function unescapeSingleQuote( str ) {\r
236 return str.replace( /\\'/g, '\'' );\r
237 }\r
238\r
239 function escapeSingleQuote( str ) {\r
240 return str.replace( /'/g, '\\$&' );\r
241 }\r
242\r
243 function protectEmailAddressAsEncodedString( address ) {\r
244 var charCode,\r
245 length = address.length,\r
246 encodedChars = [];\r
247\r
248 for ( var i = 0; i < length; i++ ) {\r
249 charCode = address.charCodeAt( i );\r
250 encodedChars.push( charCode );\r
251 }\r
252\r
253 return 'String.fromCharCode(' + encodedChars.join( ',' ) + ')';\r
254 }\r
255\r
256 function protectEmailLinkAsFunction( editor, email ) {\r
257 var plugin = editor.plugins.link,\r
258 name = plugin.compiledProtectionFunction.name,\r
259 params = plugin.compiledProtectionFunction.params,\r
260 paramName, paramValue, retval;\r
261\r
262 retval = [ name, '(' ];\r
263 for ( var i = 0; i < params.length; i++ ) {\r
264 paramName = params[ i ].toLowerCase();\r
265 paramValue = email[ paramName ];\r
266\r
267 i > 0 && retval.push( ',' );\r
268 retval.push( '\'', paramValue ? escapeSingleQuote( encodeURIComponent( email[ paramName ] ) ) : '', '\'' );\r
269 }\r
270 retval.push( ')' );\r
271 return retval.join( '' );\r
272 }\r
273\r
274 function getCompiledProtectionFunction( editor ) {\r
275 var emailProtection = editor.config.emailProtection || '',\r
276 compiledProtectionFunction;\r
277\r
278 // Compile the protection function pattern.\r
279 if ( emailProtection && emailProtection != 'encode' ) {\r
280 compiledProtectionFunction = {};\r
281\r
282 emailProtection.replace( /^([^(]+)\(([^)]+)\)$/, function( match, funcName, params ) {\r
283 compiledProtectionFunction.name = funcName;\r
284 compiledProtectionFunction.params = [];\r
285 params.replace( /[^,\s]+/g, function( param ) {\r
286 compiledProtectionFunction.params.push( param );\r
287 } );\r
288 } );\r
289 }\r
290\r
291 return compiledProtectionFunction;\r
292 }\r
293\r
294 /**\r
295 * Set of Link plugin helpers.\r
296 *\r
297 * @class\r
298 * @singleton\r
299 */\r
300 CKEDITOR.plugins.link = {\r
301 /**\r
302 * Get the surrounding link element of the current selection.\r
303 *\r
304 * CKEDITOR.plugins.link.getSelectedLink( editor );\r
305 *\r
306 * // The following selections will all return the link element.\r
307 *\r
308 * <a href="#">li^nk</a>\r
309 * <a href="#">[link]</a>\r
310 * text[<a href="#">link]</a>\r
311 * <a href="#">li[nk</a>]\r
312 * [<b><a href="#">li]nk</a></b>]\r
313 * [<a href="#"><b>li]nk</b></a>\r
314 *\r
315 * @since 3.2.1\r
316 * @param {CKEDITOR.editor} editor\r
1794320d
IB
317 * @param {Boolean} [returnMultiple=false] Indicates whether the function should return only the first selected link or all of them.\r
318 * @returns {CKEDITOR.dom.element/CKEDITOR.dom.element[]/null} A single link element or an array of link\r
319 * elements relevant to the current selection.\r
c63493c8 320 */\r
1794320d
IB
321 getSelectedLink: function( editor, returnMultiple ) {\r
322 var selection = editor.getSelection(),\r
323 selectedElement = selection.getSelectedElement(),\r
324 ranges = selection.getRanges(),\r
325 links = [],\r
326 link,\r
327 range,\r
328 i;\r
329\r
330 if ( !returnMultiple && selectedElement && selectedElement.is( 'a' ) ) {\r
c63493c8 331 return selectedElement;\r
1794320d
IB
332 }\r
333\r
334 for ( i = 0; i < ranges.length; i++ ) {\r
335 range = selection.getRanges()[ i ];\r
c63493c8 336\r
1794320d
IB
337 // Skip bogus to cover cases of multiple selection inside tables (#tp2245).\r
338 range.shrink( CKEDITOR.SHRINK_TEXT, false, { skipBogus: true } );\r
339 link = editor.elementPath( range.getCommonAncestor() ).contains( 'a', 1 );\r
c63493c8 340\r
1794320d
IB
341 if ( link && returnMultiple ) {\r
342 links.push( link );\r
343 } else if ( link ) {\r
344 return link;\r
345 }\r
c63493c8 346 }\r
1794320d
IB
347\r
348 return returnMultiple ? links : null;\r
c63493c8
IB
349 },\r
350\r
351 /**\r
352 * Collects anchors available in the editor (i.e. used by the Link plugin).\r
353 * Note that the scope of search is different for inline (the "global" document) and\r
354 * classic (`iframe`-based) editors (the "inner" document).\r
355 *\r
356 * @since 4.3.3\r
357 * @param {CKEDITOR.editor} editor\r
358 * @returns {CKEDITOR.dom.element[]} An array of anchor elements.\r
359 */\r
360 getEditorAnchors: function( editor ) {\r
361 var editable = editor.editable(),\r
362\r
363 // The scope of search for anchors is the entire document for inline editors\r
1794320d 364 // and editor's editable for classic editor/divarea (http://dev.ckeditor.com/ticket/11359).\r
c63493c8
IB
365 scope = ( editable.isInline() && !editor.plugins.divarea ) ? editor.document : editable,\r
366\r
367 links = scope.getElementsByTag( 'a' ),\r
368 imgs = scope.getElementsByTag( 'img' ),\r
369 anchors = [],\r
370 i = 0,\r
371 item;\r
372\r
373 // Retrieve all anchors within the scope.\r
374 while ( ( item = links.getItem( i++ ) ) ) {\r
375 if ( item.data( 'cke-saved-name' ) || item.hasAttribute( 'name' ) ) {\r
376 anchors.push( {\r
377 name: item.data( 'cke-saved-name' ) || item.getAttribute( 'name' ),\r
378 id: item.getAttribute( 'id' )\r
379 } );\r
380 }\r
381 }\r
382 // Retrieve all "fake anchors" within the scope.\r
383 i = 0;\r
384\r
385 while ( ( item = imgs.getItem( i++ ) ) ) {\r
386 if ( ( item = this.tryRestoreFakeAnchor( editor, item ) ) ) {\r
387 anchors.push( {\r
388 name: item.getAttribute( 'name' ),\r
389 id: item.getAttribute( 'id' )\r
390 } );\r
391 }\r
392 }\r
393\r
394 return anchors;\r
395 },\r
396\r
397 /**\r
398 * Opera and WebKit do not make it possible to select empty anchors. Fake\r
399 * elements must be used for them.\r
400 *\r
401 * @readonly\r
402 * @deprecated 4.3.3 It is set to `true` in every browser.\r
403 * @property {Boolean}\r
404 */\r
405 fakeAnchor: true,\r
406\r
407 /**\r
1794320d 408 * For browsers that do not support CSS3 `a[name]:empty()`. Note that IE9 is included because of http://dev.ckeditor.com/ticket/7783.\r
c63493c8
IB
409 *\r
410 * @readonly\r
411 * @deprecated 4.3.3 It is set to `false` in every browser.\r
412 * @property {Boolean} synAnchorSelector\r
413 */\r
414\r
415 /**\r
416 * For browsers that have editing issues with an empty anchor.\r
417 *\r
418 * @readonly\r
419 * @deprecated 4.3.3 It is set to `false` in every browser.\r
420 * @property {Boolean} emptyAnchorFix\r
421 */\r
422\r
423 /**\r
424 * Returns an element representing a real anchor restored from a fake anchor.\r
425 *\r
426 * @param {CKEDITOR.editor} editor\r
427 * @param {CKEDITOR.dom.element} element\r
428 * @returns {CKEDITOR.dom.element} Restored anchor element or nothing if the\r
429 * passed element was not a fake anchor.\r
430 */\r
431 tryRestoreFakeAnchor: function( editor, element ) {\r
432 if ( element && element.data( 'cke-real-element-type' ) && element.data( 'cke-real-element-type' ) == 'anchor' ) {\r
433 var link = editor.restoreRealElement( element );\r
434 if ( link.data( 'cke-saved-name' ) )\r
435 return link;\r
436 }\r
437 },\r
438\r
439 /**\r
440 * Parses attributes of the link element and returns an object representing\r
441 * the current state (data) of the link. This data format is a plain object accepted\r
442 * e.g. by the Link dialog window and {@link #getLinkAttributes}.\r
443 *\r
444 * **Note:** Data model format produced by the parser must be compatible with the Link\r
445 * plugin dialog because it is passed directly to {@link CKEDITOR.dialog#setupContent}.\r
446 *\r
447 * @since 4.4\r
448 * @param {CKEDITOR.editor} editor\r
449 * @param {CKEDITOR.dom.element} element\r
450 * @returns {Object} An object of link data.\r
451 */\r
452 parseLinkAttributes: function( editor, element ) {\r
453 var href = ( element && ( element.data( 'cke-saved-href' ) || element.getAttribute( 'href' ) ) ) || '',\r
454 compiledProtectionFunction = editor.plugins.link.compiledProtectionFunction,\r
455 emailProtection = editor.config.emailProtection,\r
456 javascriptMatch, emailMatch, anchorMatch, urlMatch,\r
457 retval = {};\r
458\r
459 if ( ( javascriptMatch = href.match( javascriptProtocolRegex ) ) ) {\r
460 if ( emailProtection == 'encode' ) {\r
461 href = href.replace( encodedEmailLinkRegex, function( match, protectedAddress, rest ) {\r
1794320d 462 // Without it 'undefined' is appended to e-mails without subject and body (http://dev.ckeditor.com/ticket/9192).\r
c63493c8
IB
463 rest = rest || '';\r
464\r
465 return 'mailto:' +\r
466 String.fromCharCode.apply( String, protectedAddress.split( ',' ) ) +\r
467 unescapeSingleQuote( rest );\r
468 } );\r
469 }\r
470 // Protected email link as function call.\r
471 else if ( emailProtection ) {\r
472 href.replace( functionCallProtectedEmailLinkRegex, function( match, funcName, funcArgs ) {\r
473 if ( funcName == compiledProtectionFunction.name ) {\r
474 retval.type = 'email';\r
475 var email = retval.email = {};\r
476\r
477 var paramRegex = /[^,\s]+/g,\r
478 paramQuoteRegex = /(^')|('$)/g,\r
479 paramsMatch = funcArgs.match( paramRegex ),\r
480 paramsMatchLength = paramsMatch.length,\r
481 paramName, paramVal;\r
482\r
483 for ( var i = 0; i < paramsMatchLength; i++ ) {\r
484 paramVal = decodeURIComponent( unescapeSingleQuote( paramsMatch[ i ].replace( paramQuoteRegex, '' ) ) );\r
485 paramName = compiledProtectionFunction.params[ i ].toLowerCase();\r
486 email[ paramName ] = paramVal;\r
487 }\r
488 email.address = [ email.name, email.domain ].join( '@' );\r
489 }\r
490 } );\r
491 }\r
492 }\r
493\r
494 if ( !retval.type ) {\r
495 if ( ( anchorMatch = href.match( anchorRegex ) ) ) {\r
496 retval.type = 'anchor';\r
497 retval.anchor = {};\r
498 retval.anchor.name = retval.anchor.id = anchorMatch[ 1 ];\r
499 }\r
500 // Protected email link as encoded string.\r
501 else if ( ( emailMatch = href.match( emailRegex ) ) ) {\r
502 var subjectMatch = href.match( emailSubjectRegex ),\r
503 bodyMatch = href.match( emailBodyRegex );\r
504\r
505 retval.type = 'email';\r
506 var email = ( retval.email = {} );\r
507 email.address = emailMatch[ 1 ];\r
508 subjectMatch && ( email.subject = decodeURIComponent( subjectMatch[ 1 ] ) );\r
509 bodyMatch && ( email.body = decodeURIComponent( bodyMatch[ 1 ] ) );\r
510 }\r
511 // urlRegex matches empty strings, so need to check for href as well.\r
512 else if ( href && ( urlMatch = href.match( urlRegex ) ) ) {\r
513 retval.type = 'url';\r
514 retval.url = {};\r
515 retval.url.protocol = urlMatch[ 1 ];\r
516 retval.url.url = urlMatch[ 2 ];\r
517 }\r
518 }\r
519\r
520 // Load target and popup settings.\r
521 if ( element ) {\r
522 var target = element.getAttribute( 'target' );\r
523\r
524 // IE BUG: target attribute is an empty string instead of null in IE if it's not set.\r
525 if ( !target ) {\r
526 var onclick = element.data( 'cke-pa-onclick' ) || element.getAttribute( 'onclick' ),\r
527 onclickMatch = onclick && onclick.match( popupRegex );\r
528\r
529 if ( onclickMatch ) {\r
530 retval.target = {\r
531 type: 'popup',\r
532 name: onclickMatch[ 1 ]\r
533 };\r
534\r
535 var featureMatch;\r
536 while ( ( featureMatch = popupFeaturesRegex.exec( onclickMatch[ 2 ] ) ) ) {\r
1794320d 537 // Some values should remain numbers (http://dev.ckeditor.com/ticket/7300)\r
c63493c8
IB
538 if ( ( featureMatch[ 2 ] == 'yes' || featureMatch[ 2 ] == '1' ) && !( featureMatch[ 1 ] in { height: 1, width: 1, top: 1, left: 1 } ) )\r
539 retval.target[ featureMatch[ 1 ] ] = true;\r
540 else if ( isFinite( featureMatch[ 2 ] ) )\r
541 retval.target[ featureMatch[ 1 ] ] = featureMatch[ 2 ];\r
542 }\r
543 }\r
544 } else {\r
545 retval.target = {\r
546 type: target.match( selectableTargets ) ? target : 'frame',\r
547 name: target\r
548 };\r
549 }\r
550\r
551 var download = element.getAttribute( 'download' );\r
552 if ( download !== null ) {\r
553 retval.download = true;\r
554 }\r
555\r
556 var advanced = {};\r
557\r
558 for ( var a in advAttrNames ) {\r
559 var val = element.getAttribute( a );\r
560\r
561 if ( val )\r
562 advanced[ advAttrNames[ a ] ] = val;\r
563 }\r
564\r
565 var advName = element.data( 'cke-saved-name' ) || advanced.advName;\r
566\r
567 if ( advName )\r
568 advanced.advName = advName;\r
569\r
570 if ( !CKEDITOR.tools.isEmpty( advanced ) )\r
571 retval.advanced = advanced;\r
572 }\r
573\r
574 return retval;\r
575 },\r
576\r
577 /**\r
578 * Converts link data produced by {@link #parseLinkAttributes} into an object which consists\r
579 * of attributes to be set (with their values) and an array of attributes to be removed.\r
580 * This method can be used to compose or to update any link element with the given data.\r
581 *\r
582 * @since 4.4\r
583 * @param {CKEDITOR.editor} editor\r
584 * @param {Object} data Data in {@link #parseLinkAttributes} format.\r
585 * @returns {Object} An object consisting of two keys, i.e.:\r
586 *\r
587 * {\r
588 * // Attributes to be set.\r
589 * set: {\r
590 * href: 'http://foo.bar',\r
591 * target: 'bang'\r
592 * },\r
593 * // Attributes to be removed.\r
594 * removed: [\r
595 * 'id', 'style'\r
596 * ]\r
597 * }\r
598 *\r
599 */\r
600 getLinkAttributes: function( editor, data ) {\r
601 var emailProtection = editor.config.emailProtection || '',\r
602 set = {};\r
603\r
604 // Compose the URL.\r
605 switch ( data.type ) {\r
606 case 'url':\r
607 var protocol = ( data.url && data.url.protocol !== undefined ) ? data.url.protocol : 'http://',\r
608 url = ( data.url && CKEDITOR.tools.trim( data.url.url ) ) || '';\r
609\r
610 set[ 'data-cke-saved-href' ] = ( url.indexOf( '/' ) === 0 ) ? url : protocol + url;\r
611\r
612 break;\r
613 case 'anchor':\r
614 var name = ( data.anchor && data.anchor.name ),\r
615 id = ( data.anchor && data.anchor.id );\r
616\r
617 set[ 'data-cke-saved-href' ] = '#' + ( name || id || '' );\r
618\r
619 break;\r
620 case 'email':\r
621 var email = data.email,\r
622 address = email.address,\r
623 linkHref;\r
624\r
625 switch ( emailProtection ) {\r
626 case '':\r
627 case 'encode':\r
628 var subject = encodeURIComponent( email.subject || '' ),\r
629 body = encodeURIComponent( email.body || '' ),\r
630 argList = [];\r
631\r
632 // Build the e-mail parameters first.\r
633 subject && argList.push( 'subject=' + subject );\r
634 body && argList.push( 'body=' + body );\r
635 argList = argList.length ? '?' + argList.join( '&' ) : '';\r
636\r
637 if ( emailProtection == 'encode' ) {\r
638 linkHref = [\r
639 'javascript:void(location.href=\'mailto:\'+', // jshint ignore:line\r
640 protectEmailAddressAsEncodedString( address )\r
641 ];\r
642 // parameters are optional.\r
643 argList && linkHref.push( '+\'', escapeSingleQuote( argList ), '\'' );\r
644\r
645 linkHref.push( ')' );\r
646 } else {\r
647 linkHref = [ 'mailto:', address, argList ];\r
648 }\r
649\r
650 break;\r
651 default:\r
652 // Separating name and domain.\r
653 var nameAndDomain = address.split( '@', 2 );\r
654 email.name = nameAndDomain[ 0 ];\r
655 email.domain = nameAndDomain[ 1 ];\r
656\r
657 linkHref = [ 'javascript:', protectEmailLinkAsFunction( editor, email ) ]; // jshint ignore:line\r
658 }\r
659\r
660 set[ 'data-cke-saved-href' ] = linkHref.join( '' );\r
661 break;\r
662 }\r
663\r
664 // Popups and target.\r
665 if ( data.target ) {\r
666 if ( data.target.type == 'popup' ) {\r
667 var onclickList = [\r
668 'window.open(this.href, \'', data.target.name || '', '\', \''\r
669 ],\r
670 featureList = [\r
671 'resizable', 'status', 'location', 'toolbar', 'menubar', 'fullscreen', 'scrollbars', 'dependent'\r
672 ],\r
673 featureLength = featureList.length,\r
674 addFeature = function( featureName ) {\r
675 if ( data.target[ featureName ] )\r
676 featureList.push( featureName + '=' + data.target[ featureName ] );\r
677 };\r
678\r
679 for ( var i = 0; i < featureLength; i++ )\r
680 featureList[ i ] = featureList[ i ] + ( data.target[ featureList[ i ] ] ? '=yes' : '=no' );\r
681\r
682 addFeature( 'width' );\r
683 addFeature( 'left' );\r
684 addFeature( 'height' );\r
685 addFeature( 'top' );\r
686\r
687 onclickList.push( featureList.join( ',' ), '\'); return false;' );\r
688 set[ 'data-cke-pa-onclick' ] = onclickList.join( '' );\r
689 }\r
690 else if ( data.target.type != 'notSet' && data.target.name ) {\r
691 set.target = data.target.name;\r
692 }\r
693 }\r
694\r
695 // Force download attribute.\r
696 if ( data.download ) {\r
697 set.download = '';\r
698 }\r
699\r
700 // Advanced attributes.\r
701 if ( data.advanced ) {\r
702 for ( var a in advAttrNames ) {\r
703 var val = data.advanced[ advAttrNames[ a ] ];\r
704\r
705 if ( val )\r
706 set[ a ] = val;\r
707 }\r
708\r
709 if ( set.name )\r
710 set[ 'data-cke-saved-name' ] = set.name;\r
711 }\r
712\r
1794320d 713 // Browser need the "href" fro copy/paste link to work. (http://dev.ckeditor.com/ticket/6641)\r
c63493c8
IB
714 if ( set[ 'data-cke-saved-href' ] )\r
715 set.href = set[ 'data-cke-saved-href' ];\r
716\r
717 var removed = {\r
718 target: 1,\r
719 onclick: 1,\r
720 'data-cke-pa-onclick': 1,\r
721 'data-cke-saved-name': 1,\r
722 'download': 1\r
723 };\r
724\r
725 if ( data.advanced )\r
726 CKEDITOR.tools.extend( removed, advAttrNames );\r
727\r
728 // Remove all attributes which are not currently set.\r
729 for ( var s in set )\r
730 delete removed[ s ];\r
731\r
732 return {\r
733 set: set,\r
734 removed: CKEDITOR.tools.objectKeys( removed )\r
735 };\r
736 },\r
737\r
738\r
739 /**\r
740 * Determines whether an element should have a "Display Text" field in the Link dialog.\r
741 *\r
742 * @since 4.5.11\r
743 * @param {CKEDITOR.dom.element/null} element Selected element, `null` if none selected or if a ranged selection\r
744 * is made.\r
745 * @param {CKEDITOR.editor} editor The editor instance for which the check is performed.\r
746 * @returns {Boolean}\r
747 */\r
748 showDisplayTextForElement: function( element, editor ) {\r
749 var undesiredElements = {\r
1794320d
IB
750 img: 1,\r
751 table: 1,\r
752 tbody: 1,\r
753 thead: 1,\r
754 tfoot: 1,\r
755 input: 1,\r
756 select: 1,\r
757 textarea: 1\r
758 },\r
759 selection = editor.getSelection();\r
c63493c8
IB
760\r
761 // Widget duck typing, we don't want to show display text for widgets.\r
762 if ( editor.widgets && editor.widgets.focused ) {\r
763 return false;\r
764 }\r
765\r
1794320d
IB
766 if ( selection && selection.getRanges().length > 1 ) {\r
767 return false;\r
768 }\r
769\r
c63493c8
IB
770 return !element || !element.getName || !element.is( undesiredElements );\r
771 }\r
772 };\r
773\r
774 // TODO Much probably there's no need to expose these as public objects.\r
775\r
776 CKEDITOR.unlinkCommand = function() {};\r
777 CKEDITOR.unlinkCommand.prototype = {\r
778 exec: function( editor ) {\r
1794320d
IB
779 // IE/Edge removes link from selection while executing "unlink" command when cursor\r
780 // is right before/after link's text. Therefore whole link must be selected and the\r
781 // position of cursor must be restored to its initial state after unlinking. (http://dev.ckeditor.com/ticket/13062)\r
782 if ( CKEDITOR.env.ie ) {\r
783 var range = editor.getSelection().getRanges()[ 0 ],\r
784 link = ( range.getPreviousEditableNode() && range.getPreviousEditableNode().getAscendant( 'a', true ) ) ||\r
785 ( range.getNextEditableNode() && range.getNextEditableNode().getAscendant( 'a', true ) ),\r
786 bookmark;\r
787\r
788 if ( range.collapsed && link ) {\r
789 bookmark = range.createBookmark();\r
790 range.selectNodeContents( link );\r
791 range.select();\r
792 }\r
793 }\r
794\r
c63493c8
IB
795 var style = new CKEDITOR.style( { element: 'a', type: CKEDITOR.STYLE_INLINE, alwaysRemoveElement: 1 } );\r
796 editor.removeStyle( style );\r
1794320d
IB
797\r
798 if ( bookmark ) {\r
799 range.moveToBookmark( bookmark );\r
800 range.select();\r
801 }\r
c63493c8
IB
802 },\r
803\r
804 refresh: function( editor, path ) {\r
805 // Despite our initial hope, document.queryCommandEnabled() does not work\r
806 // for this in Firefox. So we must detect the state by element paths.\r
807\r
808 var element = path.lastElement && path.lastElement.getAscendant( 'a', true );\r
809\r
810 if ( element && element.getName() == 'a' && element.getAttribute( 'href' ) && element.getChildCount() )\r
811 this.setState( CKEDITOR.TRISTATE_OFF );\r
812 else\r
813 this.setState( CKEDITOR.TRISTATE_DISABLED );\r
814 },\r
815\r
816 contextSensitive: 1,\r
817 startDisabled: 1,\r
1794320d
IB
818 requiredContent: 'a[href]',\r
819 editorFocus: 1\r
c63493c8
IB
820 };\r
821\r
822 CKEDITOR.removeAnchorCommand = function() {};\r
823 CKEDITOR.removeAnchorCommand.prototype = {\r
824 exec: function( editor ) {\r
825 var sel = editor.getSelection(),\r
826 bms = sel.createBookmarks(),\r
827 anchor;\r
828 if ( sel && ( anchor = sel.getSelectedElement() ) && ( !anchor.getChildCount() ? CKEDITOR.plugins.link.tryRestoreFakeAnchor( editor, anchor ) : anchor.is( 'a' ) ) )\r
829 anchor.remove( 1 );\r
830 else {\r
831 if ( ( anchor = CKEDITOR.plugins.link.getSelectedLink( editor ) ) ) {\r
832 if ( anchor.hasAttribute( 'href' ) ) {\r
833 anchor.removeAttributes( { name: 1, 'data-cke-saved-name': 1 } );\r
834 anchor.removeClass( 'cke_anchor' );\r
835 } else {\r
836 anchor.remove( 1 );\r
837 }\r
838 }\r
839 }\r
840 sel.selectBookmarks( bms );\r
841 },\r
842 requiredContent: 'a[name]'\r
843 };\r
844\r
845 CKEDITOR.tools.extend( CKEDITOR.config, {\r
846 /**\r
847 * Whether to show the Advanced tab in the Link dialog window.\r
848 *\r
849 * @cfg {Boolean} [linkShowAdvancedTab=true]\r
850 * @member CKEDITOR.config\r
851 */\r
852 linkShowAdvancedTab: true,\r
853\r
854 /**\r
855 * Whether to show the Target tab in the Link dialog window.\r
856 *\r
857 * @cfg {Boolean} [linkShowTargetTab=true]\r
858 * @member CKEDITOR.config\r
859 */\r
860 linkShowTargetTab: true\r
861\r
862 /**\r
863 * Whether JavaScript code is allowed as a `href` attribute in an anchor tag.\r
864 * With this option enabled it is possible to create links like:\r
865 *\r
866 * <a href="javascript:alert('Hello world!')">hello world</a>\r
867 *\r
868 * By default JavaScript links are not allowed and will not pass\r
869 * the Link dialog window validation.\r
870 *\r
871 * @since 4.4.1\r
872 * @cfg {Boolean} [linkJavaScriptLinksAllowed=false]\r
873 * @member CKEDITOR.config\r
874 */\r
875 } );\r
876} )();\r