plugin.js 7.3 KB

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