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