[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/libraries/ckeditor/_source/plugins/removeformat/ -> plugin.js (source)

   1  /*

   2  Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.

   3  For licensing, see LICENSE.html or http://ckeditor.com/license

   4  */
   5  
   6  CKEDITOR.plugins.add( 'removeformat',
   7  {
   8      requires : [ 'selection' ],
   9  
  10      init : function( editor )
  11      {
  12          editor.addCommand( 'removeFormat', CKEDITOR.plugins.removeformat.commands.removeformat );
  13          editor.ui.addButton( 'RemoveFormat',
  14              {
  15                  label : editor.lang.removeFormat,
  16                  command : 'removeFormat'
  17              });
  18  
  19          editor._.removeFormat = { filters: [] };
  20      }
  21  });
  22  
  23  CKEDITOR.plugins.removeformat =
  24  {
  25      commands :
  26      {
  27          removeformat :
  28          {
  29              exec : function( editor )
  30              {
  31                  var tagsRegex = editor._.removeFormatRegex ||
  32                      ( editor._.removeFormatRegex = new RegExp( '^(?:' + editor.config.removeFormatTags.replace( /,/g,'|' ) + ')$', 'i' ) );
  33  
  34                  var removeAttributes = editor._.removeAttributes ||
  35                      ( editor._.removeAttributes = editor.config.removeFormatAttributes.split( ',' ) );
  36  
  37                  var filter = CKEDITOR.plugins.removeformat.filter;
  38                  var ranges = editor.getSelection().getRanges( true ),
  39                      iterator = ranges.createIterator(),
  40                      range;
  41  
  42                  while ( ( range = iterator.getNextRange() ) )
  43                  {
  44                      if ( range.collapsed )
  45                          continue;
  46  
  47                      range.enlarge( CKEDITOR.ENLARGE_ELEMENT );
  48  
  49                      // Bookmark the range so we can re-select it after processing.

  50                      var bookmark = range.createBookmark();
  51  
  52                      // The style will be applied within the bookmark boundaries.

  53                      var startNode    = bookmark.startNode;
  54                      var endNode        = bookmark.endNode;
  55  
  56                      // We need to check the selection boundaries (bookmark spans) to break

  57                      // the code in a way that we can properly remove partially selected nodes.

  58                      // For example, removing a <b> style from

  59                      //        <b>This is [some text</b> to show <b>the] problem</b>

  60                      // ... where [ and ] represent the selection, must result:

  61                      //        <b>This is </b>[some text to show the]<b> problem</b>

  62                      // The strategy is simple, we just break the partial nodes before the

  63                      // removal logic, having something that could be represented this way:

  64                      //        <b>This is </b>[<b>some text</b> to show <b>the</b>]<b> problem</b>

  65  
  66                      var breakParent = function( node )
  67                      {
  68                          // Let's start checking the start boundary.

  69                          var path = new CKEDITOR.dom.elementPath( node );
  70                          var pathElements = path.elements;
  71  
  72                          for ( var i = 1, pathElement ; pathElement = pathElements[ i ] ; i++ )
  73                          {
  74                              if ( pathElement.equals( path.block ) || pathElement.equals( path.blockLimit ) )
  75                                  break;
  76  
  77                              // If this element can be removed (even partially).

  78                              if ( tagsRegex.test( pathElement.getName() ) && filter( editor, pathElement ) )
  79                                  node.breakParent( pathElement );
  80                          }
  81                      };
  82  
  83                      breakParent( startNode );
  84                      breakParent( endNode );
  85  
  86                      // Navigate through all nodes between the bookmarks.

  87                      var currentNode = startNode.getNextSourceNode( true, CKEDITOR.NODE_ELEMENT );
  88  
  89                      while ( currentNode )
  90                      {
  91                          // If we have reached the end of the selection, stop looping.

  92                          if ( currentNode.equals( endNode ) )
  93                              break;
  94  
  95                          // Cache the next node to be processed. Do it now, because

  96                          // currentNode may be removed.

  97                          var nextNode = currentNode.getNextSourceNode( false, CKEDITOR.NODE_ELEMENT );
  98  
  99                          // This node must not be a fake element.

 100                          if ( !( currentNode.getName() == 'img'
 101                              && currentNode.getAttribute( '_cke_realelement' ) )
 102                              && filter( editor, currentNode ) )
 103                          {
 104                              // Remove elements nodes that match with this style rules.

 105                              if ( tagsRegex.test( currentNode.getName() ) )
 106                                  currentNode.remove( true );
 107                              else
 108                              {
 109                                  currentNode.removeAttributes( removeAttributes );
 110                                  editor.fire( 'removeFormatCleanup', currentNode );
 111                              }
 112                          }
 113  
 114                          currentNode = nextNode;
 115                      }
 116  
 117                      range.moveToBookmark( bookmark );
 118                  }
 119  
 120                  editor.getSelection().selectRanges( ranges );
 121              }
 122          }
 123      },
 124  
 125      /**

 126       * Perform the remove format filters on the passed element.

 127       * @param {CKEDITOR.editor} editor

 128       * @param {CKEDITOR.dom.element} element

 129       */
 130      filter : function ( editor, element )
 131      {
 132          var filters = editor._.removeFormat.filters;
 133          for ( var i = 0; i < filters.length; i++ )
 134          {
 135              if ( filters[ i ]( element ) === false )
 136                  return false;
 137          }
 138          return true;
 139      }
 140  };
 141  
 142  /**

 143   * Add to a collection of functions to decide whether a specific

 144   * element should be considered as formatting element and thus

 145   * could be removed during <b>removeFormat</b> command,

 146   * Note: Only available with the existence of 'removeformat' plugin.

 147   * @since 3.3

 148   * @param {Function} func The function to be called, which will be passed a {CKEDITOR.dom.element} element to test.

 149   * @example

 150   *  // Don't remove empty span

 151   *  editor.addRemoveFormatFilter.push( function( element )

 152   *        {

 153   *            return !( element.is( 'span' ) && CKEDITOR.tools.isEmpty( element.getAttributes() ) );

 154   *        });

 155   */
 156  CKEDITOR.editor.prototype.addRemoveFormatFilter = function( func )
 157  {
 158      this._.removeFormat.filters.push( func );
 159  };
 160  
 161  /**

 162   * A comma separated list of elements to be removed when executing the "remove

 163   " format" command. Note that only inline elements are allowed.

 164   * @type String

 165   * @default 'b,big,code,del,dfn,em,font,i,ins,kbd,q,samp,small,span,strike,strong,sub,sup,tt,u,var'

 166   * @example

 167   */
 168  CKEDITOR.config.removeFormatTags = 'b,big,code,del,dfn,em,font,i,ins,kbd,q,samp,small,span,strike,strong,sub,sup,tt,u,var';
 169  
 170  /**

 171   * A comma separated list of elements attributes to be removed when executing

 172   * the "remove format" command.

 173   * @type String

 174   * @default 'class,style,lang,width,height,align,hspace,valign'

 175   * @example

 176   */
 177  CKEDITOR.config.removeFormatAttributes = 'class,style,lang,width,height,align,hspace,valign';
 178  
 179  /**

 180   * Fired after an element was cleaned by the removeFormat plugin.

 181   * @name CKEDITOR#removeFormatCleanup

 182   * @event

 183   * @param {Object} data.element The element that was cleaned up.

 184   */


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7