]> git.immae.eu Git - perso/Immae/Projets/packagist/ludivine-ckeditor-component.git/blame - sources/plugins/removeformat/plugin.js
Update to 4.7.3
[perso/Immae/Projets/packagist/ludivine-ckeditor-component.git] / sources / plugins / removeformat / 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
6CKEDITOR.plugins.add( 'removeformat', {\r
7 // jscs:disable maximumLineLength\r
1794320d 8 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
9 // jscs:enable maximumLineLength\r
10 icons: 'removeformat', // %REMOVE_LINE_CORE%\r
11 hidpi: true, // %REMOVE_LINE_CORE%\r
12 init: function( editor ) {\r
13 editor.addCommand( 'removeFormat', CKEDITOR.plugins.removeformat.commands.removeformat );\r
14 editor.ui.addButton && editor.ui.addButton( 'RemoveFormat', {\r
15 label: editor.lang.removeformat.toolbar,\r
16 command: 'removeFormat',\r
17 toolbar: 'cleanup,10'\r
18 } );\r
19 }\r
20} );\r
21\r
22CKEDITOR.plugins.removeformat = {\r
23 commands: {\r
24 removeformat: {\r
25 exec: function( editor ) {\r
26 var tagsRegex = editor._.removeFormatRegex || ( editor._.removeFormatRegex = new RegExp( '^(?:' + editor.config.removeFormatTags.replace( /,/g, '|' ) + ')$', 'i' ) );\r
27\r
28 var removeAttributes = editor._.removeAttributes || ( editor._.removeAttributes = editor.config.removeFormatAttributes.split( ',' ) ),\r
29 filter = CKEDITOR.plugins.removeformat.filter,\r
30 ranges = editor.getSelection().getRanges(),\r
31 iterator = ranges.createIterator(),\r
32 isElement = function( element ) {\r
33 return element.type == CKEDITOR.NODE_ELEMENT;\r
34 },\r
35 range;\r
36\r
37 while ( ( range = iterator.getNextRange() ) ) {\r
38 if ( !range.collapsed )\r
39 range.enlarge( CKEDITOR.ENLARGE_ELEMENT );\r
40\r
41 // Bookmark the range so we can re-select it after processing.\r
42 var bookmark = range.createBookmark(),\r
43 // The style will be applied within the bookmark boundaries.\r
44 startNode = bookmark.startNode,\r
45 endNode = bookmark.endNode,\r
46 currentNode;\r
47\r
48 // We need to check the selection boundaries (bookmark spans) to break\r
49 // the code in a way that we can properly remove partially selected nodes.\r
50 // For example, removing a <b> style from\r
51 // <b>This is [some text</b> to show <b>the] problem</b>\r
52 // ... where [ and ] represent the selection, must result:\r
53 // <b>This is </b>[some text to show the]<b> problem</b>\r
54 // The strategy is simple, we just break the partial nodes before the\r
55 // removal logic, having something that could be represented this way:\r
56 // <b>This is </b>[<b>some text</b> to show <b>the</b>]<b> problem</b>\r
57\r
58 var breakParent = function( node ) {\r
59 // Let's start checking the start boundary.\r
60 var path = editor.elementPath( node ),\r
61 pathElements = path.elements;\r
62\r
63 for ( var i = 1, pathElement; pathElement = pathElements[ i ]; i++ ) {\r
64 if ( pathElement.equals( path.block ) || pathElement.equals( path.blockLimit ) )\r
65 break;\r
66\r
67 // If this element can be removed (even partially).\r
68 if ( tagsRegex.test( pathElement.getName() ) && filter( editor, pathElement ) )\r
69 node.breakParent( pathElement );\r
70 }\r
71 };\r
72\r
73 breakParent( startNode );\r
74 if ( endNode ) {\r
75 breakParent( endNode );\r
76\r
77 // Navigate through all nodes between the bookmarks.\r
78 currentNode = startNode.getNextSourceNode( true, CKEDITOR.NODE_ELEMENT );\r
79\r
80 while ( currentNode ) {\r
81 // If we have reached the end of the selection, stop looping.\r
82 if ( currentNode.equals( endNode ) )\r
83 break;\r
84\r
85 if ( currentNode.isReadOnly() ) {\r
86 // In case of non-editable we're skipping to the next sibling *elmenet*.\r
87\r
88 // We need to be aware that endNode can be nested within current non-editable.\r
89 // This condition tests if currentNode (non-editable) contains endNode. If it does\r
90 // then we should break the filtering\r
91 if ( currentNode.getPosition( endNode ) & CKEDITOR.POSITION_CONTAINS ) {\r
92 break;\r
93 }\r
94\r
95 currentNode = currentNode.getNext( isElement );\r
96 continue;\r
97 }\r
98\r
99 // Cache the next node to be processed. Do it now, because\r
100 // currentNode may be removed.\r
101 var nextNode = currentNode.getNextSourceNode( false, CKEDITOR.NODE_ELEMENT ),\r
102 isFakeElement = currentNode.getName() == 'img' && currentNode.data( 'cke-realelement' );\r
103\r
104 // This node must not be a fake element, and must not be read-only.\r
105 if ( !isFakeElement && filter( editor, currentNode ) ) {\r
106 // Remove elements nodes that match with this style rules.\r
107 if ( tagsRegex.test( currentNode.getName() ) )\r
108 currentNode.remove( 1 );\r
109 else {\r
110 currentNode.removeAttributes( removeAttributes );\r
111 editor.fire( 'removeFormatCleanup', currentNode );\r
112 }\r
113 }\r
114\r
115 currentNode = nextNode;\r
116 }\r
117 }\r
118\r
119 range.moveToBookmark( bookmark );\r
120 }\r
121\r
122 // The selection path may not changed, but we should force a selection\r
1794320d 123 // change event to refresh command states, due to the above attribution change. (http://dev.ckeditor.com/ticket/9238)\r
c63493c8
IB
124 editor.forceNextSelectionCheck();\r
125 editor.getSelection().selectRanges( ranges );\r
126 }\r
127 }\r
128 },\r
129\r
130 // Perform the remove format filters on the passed element.\r
131 // @param {CKEDITOR.editor} editor\r
132 // @param {CKEDITOR.dom.element} element\r
133 filter: function( editor, element ) {\r
134 // If editor#addRemoveFotmatFilter hasn't been executed yet value is not initialized.\r
135 var filters = editor._.removeFormatFilters || [];\r
136 for ( var i = 0; i < filters.length; i++ ) {\r
137 if ( filters[ i ]( element ) === false )\r
138 return false;\r
139 }\r
140 return true;\r
141 }\r
142};\r
143\r
144/**\r
145 * Add to a collection of functions to decide whether a specific\r
146 * element should be considered as formatting element and thus\r
147 * could be removed during `removeFormat` command.\r
148 *\r
149 * **Note:** Only available with the existence of `removeformat` plugin.\r
150 *\r
151 * // Don't remove empty span.\r
152 * editor.addRemoveFormatFilter( function( element ) {\r
153 * return !( element.is( 'span' ) && CKEDITOR.tools.isEmpty( element.getAttributes() ) );\r
154 * } );\r
155 *\r
156 * @since 3.3\r
157 * @member CKEDITOR.editor\r
158 * @param {Function} func The function to be called, which will be passed a {CKEDITOR.dom.element} element to test.\r
159 */\r
160CKEDITOR.editor.prototype.addRemoveFormatFilter = function( func ) {\r
161 if ( !this._.removeFormatFilters )\r
162 this._.removeFormatFilters = [];\r
163\r
164 this._.removeFormatFilters.push( func );\r
165};\r
166\r
167/**\r
168 * A comma separated list of elements to be removed when executing the `remove\r
169 * format` command. Note that only inline elements are allowed.\r
170 *\r
171 * @cfg\r
172 * @member CKEDITOR.config\r
173 */\r
174CKEDITOR.config.removeFormatTags = 'b,big,cite,code,del,dfn,em,font,i,ins,kbd,q,s,samp,small,span,strike,strong,sub,sup,tt,u,var';\r
175\r
176/**\r
177 * A comma separated list of elements attributes to be removed when executing\r
178 * the `remove format` command.\r
179 *\r
180 * @cfg\r
181 * @member CKEDITOR.config\r
182 */\r
183CKEDITOR.config.removeFormatAttributes = 'class,style,lang,width,height,align,hspace,valign';\r
184\r
185/**\r
186 * Fired after an element was cleaned by the removeFormat plugin.\r
187 *\r
188 * @event removeFormatCleanup\r
189 * @member CKEDITOR.editor\r
190 * @param {CKEDITOR.editor} editor This editor instance.\r
191 * @param data\r
192 * @param {CKEDITOR.dom.element} data.element The element that was cleaned up.\r
193 */\r