[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/libraries/ckeditor/_source/core/htmlparser/ -> element.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  /**

   7   * A lightweight representation of an HTML element.

   8   * @param {String} name The element name.

   9   * @param {Object} attributes And object holding all attributes defined for

  10   *        this element.

  11   * @constructor

  12   * @example

  13   */
  14  CKEDITOR.htmlParser.element = function( name, attributes )
  15  {
  16      /**

  17       * The element name.

  18       * @type String

  19       * @example

  20       */
  21      this.name = name;
  22  
  23      /**

  24       * Holds the attributes defined for this element.

  25       * @type Object

  26       * @example

  27       */
  28      this.attributes = attributes || ( attributes = {} );
  29  
  30      /**

  31       * The nodes that are direct children of this element.

  32       * @type Array

  33       * @example

  34       */
  35      this.children = [];
  36  
  37      var tagName = attributes._cke_real_element_type || name;
  38  
  39      var dtd            = CKEDITOR.dtd,
  40          isBlockLike    = !!( dtd.$nonBodyContent[ tagName ] || dtd.$block[ tagName ] || dtd.$listItem[ tagName ] || dtd.$tableContent[ tagName ] || dtd.$nonEditable[ tagName ] || tagName == 'br' ),
  41          isEmpty        = !!dtd.$empty[ name ];
  42  
  43      this.isEmpty    = isEmpty;
  44      this.isUnknown    = !dtd[ name ];
  45  
  46      /** @private */

  47      this._ =
  48      {
  49          isBlockLike : isBlockLike,
  50          hasInlineStarted : isEmpty || !isBlockLike
  51      };
  52  };
  53  
  54  (function()
  55  {
  56      // Used to sort attribute entries in an array, where the first element of

  57      // each object is the attribute name.

  58      var sortAttribs = function( a, b )
  59      {
  60          a = a[0];
  61          b = b[0];
  62          return a < b ? -1 : a > b ? 1 : 0;
  63      };
  64  
  65      CKEDITOR.htmlParser.element.prototype =
  66      {
  67          /**

  68           * The node type. This is a constant value set to {@link CKEDITOR.NODE_ELEMENT}.

  69           * @type Number

  70           * @example

  71           */
  72          type : CKEDITOR.NODE_ELEMENT,
  73  
  74          /**

  75           * Adds a node to the element children list.

  76           * @param {Object} node The node to be added. It can be any of of the

  77           *        following types: {@link CKEDITOR.htmlParser.element},

  78           *        {@link CKEDITOR.htmlParser.text} and

  79           *        {@link CKEDITOR.htmlParser.comment}.

  80           * @function

  81           * @example

  82           */
  83          add : CKEDITOR.htmlParser.fragment.prototype.add,
  84  
  85          /**

  86           * Clone this element.

  87           * @returns {CKEDITOR.htmlParser.element} The element clone.

  88           * @example

  89           */
  90          clone : function()
  91          {
  92              return new CKEDITOR.htmlParser.element( this.name, this.attributes );
  93          },
  94  
  95          /**

  96           * Writes the element HTML to a CKEDITOR.htmlWriter.

  97           * @param {CKEDITOR.htmlWriter} writer The writer to which write the HTML.

  98           * @example

  99           */
 100          writeHtml : function( writer, filter )
 101          {
 102              var attributes = this.attributes;
 103  
 104              // Ignore cke: prefixes when writing HTML.

 105              var element = this,
 106                  writeName = element.name,
 107                  a, newAttrName, value;
 108  
 109              var isChildrenFiltered;
 110  
 111              /**

 112               * Providing an option for bottom-up filtering order ( element

 113               * children to be pre-filtered before the element itself ).

 114               */
 115              element.filterChildren = function()
 116              {
 117                  if ( !isChildrenFiltered )
 118                  {
 119                      var writer = new CKEDITOR.htmlParser.basicWriter();
 120                      CKEDITOR.htmlParser.fragment.prototype.writeChildrenHtml.call( element, writer, filter );
 121                      element.children = new CKEDITOR.htmlParser.fragment.fromHtml( writer.getHtml() ).children;
 122                      isChildrenFiltered = 1;
 123                  }
 124              };
 125  
 126              if ( filter )
 127              {
 128                  while ( true )
 129                  {
 130                      if ( !( writeName = filter.onElementName( writeName ) ) )
 131                          return;
 132  
 133                      element.name = writeName;
 134  
 135                      if ( !( element = filter.onElement( element ) ) )
 136                          return;
 137  
 138                      element.parent = this.parent;
 139  
 140                      if ( element.name == writeName )
 141                          break;
 142  
 143                      // If the element has been replaced with something of a

 144                      // different type, then make the replacement write itself.

 145                      if ( element.type != CKEDITOR.NODE_ELEMENT )
 146                      {
 147                          element.writeHtml( writer, filter );
 148                          return;
 149                      }
 150  
 151                      writeName = element.name;
 152  
 153                      // This indicate that the element has been dropped by

 154                      // filter but not the children.

 155                      if ( !writeName )
 156                      {
 157                          this.writeChildrenHtml.call( element, writer, isChildrenFiltered ? null : filter );
 158                          return;
 159                      }
 160                  }
 161  
 162                  // The element may have been changed, so update the local

 163                  // references.

 164                  attributes = element.attributes;
 165              }
 166  
 167              // Open element tag.

 168              writer.openTag( writeName, attributes );
 169  
 170              // Copy all attributes to an array.

 171              var attribsArray = [];
 172              // Iterate over the attributes twice since filters may alter

 173              // other attributes.

 174              for ( var i = 0 ; i < 2; i++ )
 175              {
 176                  for ( a in attributes )
 177                  {
 178                      newAttrName = a;
 179                      value = attributes[ a ];
 180                      if ( i == 1 )
 181                          attribsArray.push( [ a, value ] );
 182                      else if ( filter )
 183                      {
 184                          while ( true )
 185                          {
 186                              if ( !( newAttrName = filter.onAttributeName( a ) ) )
 187                              {
 188                                  delete attributes[ a ];
 189                                  break;
 190                              }
 191                              else if ( newAttrName != a )
 192                              {
 193                                  delete attributes[ a ];
 194                                  a = newAttrName;
 195                                  continue;
 196                              }
 197                              else
 198                                  break;
 199                          }
 200                          if ( newAttrName )
 201                          {
 202                              if ( ( value = filter.onAttribute( element, newAttrName, value ) ) === false )
 203                                  delete attributes[ newAttrName ];
 204                              else
 205                                  attributes [ newAttrName ] = value;
 206                          }
 207                      }
 208                  }
 209              }
 210              // Sort the attributes by name.

 211              if ( writer.sortAttributes )
 212                  attribsArray.sort( sortAttribs );
 213  
 214              // Send the attributes.

 215              var len = attribsArray.length;
 216              for ( i = 0 ; i < len ; i++ )
 217              {
 218                  var attrib = attribsArray[ i ];
 219                  writer.attribute( attrib[0], attrib[1] );
 220              }
 221  
 222              // Close the tag.

 223              writer.openTagClose( writeName, element.isEmpty );
 224  
 225              if ( !element.isEmpty )
 226              {
 227                  this.writeChildrenHtml.call( element, writer, isChildrenFiltered ? null : filter );
 228                  // Close the element.

 229                  writer.closeTag( writeName );
 230              }
 231          },
 232  
 233          writeChildrenHtml : function( writer, filter )
 234          {
 235              // Send children.

 236              CKEDITOR.htmlParser.fragment.prototype.writeChildrenHtml.apply( this, arguments );
 237  
 238          }
 239      };
 240  })();


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