diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2016-02-19 23:38:52 +0100 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2016-02-19 23:38:52 +0100 |
commit | 3332bebe4da6dfa0fe3e4b2abddc84b1cc62f8f5 (patch) | |
tree | a4f77655fe55b79606e7d3416504686a1ab8b058 /sources/plugins/removeformat/plugin.js | |
download | piedsjaloux-ckeditor-component-3332bebe4da6dfa0fe3e4b2abddc84b1cc62f8f5.tar.gz piedsjaloux-ckeditor-component-3332bebe4da6dfa0fe3e4b2abddc84b1cc62f8f5.tar.zst piedsjaloux-ckeditor-component-3332bebe4da6dfa0fe3e4b2abddc84b1cc62f8f5.zip |
Initial commit4.5.7
Diffstat (limited to 'sources/plugins/removeformat/plugin.js')
-rw-r--r-- | sources/plugins/removeformat/plugin.js | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/sources/plugins/removeformat/plugin.js b/sources/plugins/removeformat/plugin.js new file mode 100644 index 0000000..044f54a --- /dev/null +++ b/sources/plugins/removeformat/plugin.js | |||
@@ -0,0 +1,193 @@ | |||
1 | /** | ||
2 | * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. | ||
3 | * For licensing, see LICENSE.md or http://ckeditor.com/license | ||
4 | */ | ||
5 | |||
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', | ||
17 | toolbar: 'cleanup,10' | ||
18 | } ); | ||
19 | } | ||
20 | } ); | ||
21 | |||
22 | CKEDITOR.plugins.removeformat = { | ||
23 | commands: { | ||
24 | removeformat: { | ||
25 | exec: function( editor ) { | ||
26 | var tagsRegex = editor._.removeFormatRegex || ( editor._.removeFormatRegex = new RegExp( '^(?:' + editor.config.removeFormatTags.replace( /,/g, '|' ) + ')$', 'i' ) ); | ||
27 | |||
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; | ||
34 | }, | ||
35 | range; | ||
36 | |||
37 | while ( ( range = iterator.getNextRange() ) ) { | ||
38 | if ( !range.collapsed ) | ||
39 | range.enlarge( CKEDITOR.ENLARGE_ELEMENT ); | ||
40 | |||
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, | ||
46 | currentNode; | ||
47 | |||
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> | ||
57 | |||
58 | var breakParent = function( node ) { | ||
59 | // Let's start checking the start boundary. | ||
60 | var path = editor.elementPath( node ), | ||
61 | pathElements = path.elements; | ||
62 | |||
63 | for ( var i = 1, pathElement; pathElement = pathElements[ i ]; i++ ) { | ||
64 | if ( pathElement.equals( path.block ) || pathElement.equals( path.blockLimit ) ) | ||
65 | break; | ||
66 | |||
67 | // If this element can be removed (even partially). | ||
68 | if ( tagsRegex.test( pathElement.getName() ) && filter( editor, pathElement ) ) | ||
69 | node.breakParent( pathElement ); | ||
70 | } | ||
71 | }; | ||
72 | |||
73 | breakParent( startNode ); | ||
74 | if ( endNode ) { | ||
75 | breakParent( endNode ); | ||
76 | |||
77 | // Navigate through all nodes between the bookmarks. | ||
78 | currentNode = startNode.getNextSourceNode( true, CKEDITOR.NODE_ELEMENT ); | ||
79 | |||
80 | while ( currentNode ) { | ||
81 | // If we have reached the end of the selection, stop looping. | ||
82 | if ( currentNode.equals( endNode ) ) | ||
83 | break; | ||
84 | |||
85 | if ( currentNode.isReadOnly() ) { | ||
86 | // In case of non-editable we're skipping to the next sibling *elmenet*. | ||
87 | |||
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 ) { | ||
92 | break; | ||
93 | } | ||
94 | |||
95 | currentNode = currentNode.getNext( isElement ); | ||
96 | continue; | ||
97 | } | ||
98 | |||
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' ); | ||
103 | |||
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 ); | ||
109 | else { | ||
110 | currentNode.removeAttributes( removeAttributes ); | ||
111 | editor.fire( 'removeFormatCleanup', currentNode ); | ||
112 | } | ||
113 | } | ||
114 | |||
115 | currentNode = nextNode; | ||
116 | } | ||
117 | } | ||
118 | |||
119 | range.moveToBookmark( bookmark ); | ||
120 | } | ||
121 | |||
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 ); | ||
126 | } | ||
127 | } | ||
128 | }, | ||
129 | |||
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 ) | ||
138 | return false; | ||
139 | } | ||
140 | return true; | ||
141 | } | ||
142 | }; | ||
143 | |||
144 | /** | ||
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. | ||
148 | * | ||
149 | * **Note:** Only available with the existence of `removeformat` plugin. | ||
150 | * | ||
151 | * // Don't remove empty span. | ||
152 | * editor.addRemoveFormatFilter( function( element ) { | ||
153 | * return !( element.is( 'span' ) && CKEDITOR.tools.isEmpty( element.getAttributes() ) ); | ||
154 | * } ); | ||
155 | * | ||
156 | * @since 3.3 | ||
157 | * @member CKEDITOR.editor | ||
158 | * @param {Function} func The function to be called, which will be passed a {CKEDITOR.dom.element} element to test. | ||
159 | */ | ||
160 | CKEDITOR.editor.prototype.addRemoveFormatFilter = function( func ) { | ||
161 | if ( !this._.removeFormatFilters ) | ||
162 | this._.removeFormatFilters = []; | ||
163 | |||
164 | this._.removeFormatFilters.push( func ); | ||
165 | }; | ||
166 | |||
167 | /** | ||
168 | * A comma separated list of elements to be removed when executing the `remove | ||
169 | * format` command. Note that only inline elements are allowed. | ||
170 | * | ||
171 | * @cfg | ||
172 | * @member CKEDITOR.config | ||
173 | */ | ||
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'; | ||
175 | |||
176 | /** | ||
177 | * A comma separated list of elements attributes to be removed when executing | ||
178 | * the `remove format` command. | ||
179 | * | ||
180 | * @cfg | ||
181 | * @member CKEDITOR.config | ||
182 | */ | ||
183 | CKEDITOR.config.removeFormatAttributes = 'class,style,lang,width,height,align,hspace,valign'; | ||
184 | |||
185 | /** | ||
186 | * Fired after an element was cleaned by the removeFormat plugin. | ||
187 | * | ||
188 | * @event removeFormatCleanup | ||
189 | * @member CKEDITOR.editor | ||
190 | * @param {CKEDITOR.editor} editor This editor instance. | ||
191 | * @param data | ||
192 | * @param {CKEDITOR.dom.element} data.element The element that was cleaned up. | ||
193 | */ | ||