]>
git.immae.eu Git - perso/Immae/Projets/packagist/piedsjaloux-ckeditor-component.git/blob - sources/plugins/removeformat/plugin.js
2 * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
3 * For licensing, see LICENSE.md or http://ckeditor.com/license
6 CKEDITOR
.plugins
.add( 'removeformat', {
7 // jscs:disable maximumLineLength
8 lang: 'af,ar,bg,bn,bs,ca,cs,cy,da,de,de-ch,el,en,en-au,en-ca,en-gb,eo,es,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,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%
9 // jscs:enable maximumLineLength
10 icons: 'removeformat', // %REMOVE_LINE_CORE%
11 hidpi: true, // %REMOVE_LINE_CORE%
12 init: function( editor
) {
13 editor
.addCommand( 'removeFormat', CKEDITOR
.plugins
.removeformat
.commands
.removeformat
);
14 editor
.ui
.addButton
&& editor
.ui
.addButton( 'RemoveFormat', {
15 label: editor
.lang
.removeformat
.toolbar
,
16 command: 'removeFormat',
22 CKEDITOR
.plugins
.removeformat
= {
25 exec: function( editor
) {
26 var tagsRegex
= editor
._
.removeFormatRegex
|| ( editor
._
.removeFormatRegex
= new RegExp( '^(?:' + editor
.config
.removeFormatTags
.replace( /,/g
, '|' ) + ')$', 'i' ) );
28 var removeAttributes
= editor
._
.removeAttributes
|| ( editor
._
.removeAttributes
= editor
.config
.removeFormatAttributes
.split( ',' ) ),
29 filter
= CKEDITOR
.plugins
.removeformat
.filter
,
30 ranges
= editor
.getSelection().getRanges(),
31 iterator
= ranges
.createIterator(),
32 isElement = function( element
) {
33 return element
.type
== CKEDITOR
.NODE_ELEMENT
;
37 while ( ( range
= iterator
.getNextRange() ) ) {
38 if ( !range
.collapsed
)
39 range
.enlarge( CKEDITOR
.ENLARGE_ELEMENT
);
41 // Bookmark the range so we can re-select it after processing.
42 var bookmark
= range
.createBookmark(),
43 // The style will be applied within the bookmark boundaries.
44 startNode
= bookmark
.startNode
,
45 endNode
= bookmark
.endNode
,
48 // We need to check the selection boundaries (bookmark spans) to break
49 // the code in a way that we can properly remove partially selected nodes.
50 // For example, removing a <b> style from
51 // <b>This is [some text</b> to show <b>the] problem</b>
52 // ... where [ and ] represent the selection, must result:
53 // <b>This is </b>[some text to show the]<b> problem</b>
54 // The strategy is simple, we just break the partial nodes before the
55 // removal logic, having something that could be represented this way:
56 // <b>This is </b>[<b>some text</b> to show <b>the</b>]<b> problem</b>
58 var breakParent = function( node
) {
59 // Let's start checking the start boundary.
60 var path
= editor
.elementPath( node
),
61 pathElements
= path
.elements
;
63 for ( var i
= 1, pathElement
; pathElement
= pathElements
[ i
]; i
++ ) {
64 if ( pathElement
.equals( path
.block
) || pathElement
.equals( path
.blockLimit
) )
67 // If this element can be removed (even partially).
68 if ( tagsRegex
.test( pathElement
.getName() ) && filter( editor
, pathElement
) )
69 node
.breakParent( pathElement
);
73 breakParent( startNode
);
75 breakParent( endNode
);
77 // Navigate through all nodes between the bookmarks.
78 currentNode
= startNode
.getNextSourceNode( true, CKEDITOR
.NODE_ELEMENT
);
80 while ( currentNode
) {
81 // If we have reached the end of the selection, stop looping.
82 if ( currentNode
.equals( endNode
) )
85 if ( currentNode
.isReadOnly() ) {
86 // In case of non-editable we're skipping to the next sibling *elmenet*.
88 // We need to be aware that endNode can be nested within current non-editable.
89 // This condition tests if currentNode (non-editable) contains endNode. If it does
90 // then we should break the filtering
91 if ( currentNode
.getPosition( endNode
) & CKEDITOR
.POSITION_CONTAINS
) {
95 currentNode
= currentNode
.getNext( isElement
);
99 // Cache the next node to be processed. Do it now, because
100 // currentNode may be removed.
101 var nextNode
= currentNode
.getNextSourceNode( false, CKEDITOR
.NODE_ELEMENT
),
102 isFakeElement
= currentNode
.getName() == 'img' && currentNode
.data( 'cke-realelement' );
104 // This node must not be a fake element, and must not be read-only.
105 if ( !isFakeElement
&& filter( editor
, currentNode
) ) {
106 // Remove elements nodes that match with this style rules.
107 if ( tagsRegex
.test( currentNode
.getName() ) )
108 currentNode
.remove( 1 );
110 currentNode
.removeAttributes( removeAttributes
);
111 editor
.fire( 'removeFormatCleanup', currentNode
);
115 currentNode
= nextNode
;
119 range
.moveToBookmark( bookmark
);
122 // The selection path may not changed, but we should force a selection
123 // change event to refresh command states, due to the above attribution change. (#9238)
124 editor
.forceNextSelectionCheck();
125 editor
.getSelection().selectRanges( ranges
);
130 // Perform the remove format filters on the passed element.
131 // @param {CKEDITOR.editor} editor
132 // @param {CKEDITOR.dom.element} element
133 filter: function( editor
, element
) {
134 // If editor#addRemoveFotmatFilter hasn't been executed yet value is not initialized.
135 var filters
= editor
._
.removeFormatFilters
|| [];
136 for ( var i
= 0; i
< filters
.length
; i
++ ) {
137 if ( filters
[ i
]( element
) === false )
145 * Add to a collection of functions to decide whether a specific
146 * element should be considered as formatting element and thus
147 * could be removed during `removeFormat` command.
149 * **Note:** Only available with the existence of `removeformat` plugin.
151 * // Don't remove empty span.
152 * editor.addRemoveFormatFilter( function( element ) {
153 * return !( element.is( 'span' ) && CKEDITOR.tools.isEmpty( element.getAttributes() ) );
157 * @member CKEDITOR.editor
158 * @param {Function} func The function to be called, which will be passed a {CKEDITOR.dom.element} element to test.
160 CKEDITOR
.editor
.prototype.addRemoveFormatFilter = function( func
) {
161 if ( !this._
.removeFormatFilters
)
162 this._
.removeFormatFilters
= [];
164 this._
.removeFormatFilters
.push( func
);
168 * A comma separated list of elements to be removed when executing the `remove
169 * format` command. Note that only inline elements are allowed.
172 * @member CKEDITOR.config
174 CKEDITOR
.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';
177 * A comma separated list of elements attributes to be removed when executing
178 * the `remove format` command.
181 * @member CKEDITOR.config
183 CKEDITOR
.config
.removeFormatAttributes
= 'class,style,lang,width,height,align,hspace,valign';
186 * Fired after an element was cleaned by the removeFormat plugin.
188 * @event removeFormatCleanup
189 * @member CKEDITOR.editor
190 * @param {CKEDITOR.editor} editor This editor instance.
192 * @param {CKEDITOR.dom.element} data.element The element that was cleaned up.