[ Index ]
 

Code source de GeekLog 1.4.1

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/public_html/fckeditor/editor/_source/internals/ -> fckxhtml.js (source)

   1  /*
   2   * FCKeditor - The text editor for internet
   3   * Copyright (C) 2003-2006 Frederico Caldeira Knabben
   4   * 
   5   * Licensed under the terms of the GNU Lesser General Public License:
   6   *         http://www.opensource.org/licenses/lgpl-license.php
   7   * 
   8   * For further information visit:
   9   *         http://www.fckeditor.net/
  10   * 
  11   * "Support Open Source software. What about a donation today?"
  12   * 
  13   * File Name: fckxhtml.js
  14   *     Defines the FCKXHtml object, responsible for the XHTML operations.
  15   * 
  16   * File Authors:
  17   *         Frederico Caldeira Knabben (fredck@fckeditor.net)
  18   */
  19  
  20  var FCKXHtml = new Object() ;
  21  
  22  FCKXHtml.CurrentJobNum = 0 ;
  23  
  24  FCKXHtml.GetXHTML = function( node, includeNode, format )
  25  {
  26      FCKXHtmlEntities.Initialize() ;
  27      
  28      this._CreateNode = FCKConfig.ForceStrongEm ? FCKXHtml_CreateNode_StrongEm : FCKXHtml_CreateNode_Normal ;
  29  
  30      // Special blocks are blocks of content that remain untouched during the
  31      // process. It is used for SCRIPTs and STYLEs.
  32      FCKXHtml.SpecialBlocks = new Array() ;
  33  
  34      // Create the XML DOMDocument object.
  35      this.XML = FCKTools.CreateXmlObject( 'DOMDocument' ) ;
  36  
  37      // Add a root element that holds all child nodes.
  38      this.MainNode = this.XML.appendChild( this.XML.createElement( 'xhtml' ) ) ;
  39  
  40      FCKXHtml.CurrentJobNum++ ;
  41  
  42      if ( includeNode )
  43          this._AppendNode( this.MainNode, node ) ;
  44      else
  45          this._AppendChildNodes( this.MainNode, node, false ) ;
  46  
  47      // Get the resulting XHTML as a string.
  48      var sXHTML = this._GetMainXmlString() ;
  49  
  50      // Strip the "XHTML" root node.
  51      sXHTML = sXHTML.substr( 7, sXHTML.length - 15 ).trim() ;
  52      
  53      // Remove the trailing <br> added by Gecko.
  54      if ( FCKBrowserInfo.IsGecko )
  55          sXHTML = sXHTML.replace( /<br\/>$/, '' ) ;
  56  
  57      // Add a space in the tags with no closing tags, like <br/> -> <br />
  58      sXHTML = sXHTML.replace( FCKRegexLib.SpaceNoClose, ' />');
  59  
  60      if ( FCKConfig.ForceSimpleAmpersand )
  61          sXHTML = sXHTML.replace( FCKRegexLib.ForceSimpleAmpersand, '&' ) ;
  62  
  63      if ( format )
  64          sXHTML = FCKCodeFormatter.Format( sXHTML ) ;
  65  
  66      // Now we put back the SpecialBlocks contents.
  67      for ( var i = 0 ; i < FCKXHtml.SpecialBlocks.length ; i++ )
  68      {
  69          var oRegex = new RegExp( '___FCKsi___' + i ) ;
  70          sXHTML = sXHTML.replace( oRegex, FCKXHtml.SpecialBlocks[i] ) ;
  71      }
  72  
  73      this.XML = null ;
  74  
  75      return sXHTML
  76  }
  77  
  78  FCKXHtml._AppendAttribute = function( xmlNode, attributeName, attributeValue )
  79  {
  80      try
  81      {
  82          // Create the attribute.
  83          var oXmlAtt = this.XML.createAttribute( attributeName ) ;
  84  
  85          oXmlAtt.value = attributeValue ? attributeValue : '' ;
  86  
  87          // Set the attribute in the node.
  88          xmlNode.attributes.setNamedItem( oXmlAtt ) ;
  89      }
  90      catch (e)
  91      {}
  92  }
  93  
  94  FCKXHtml._AppendChildNodes = function( xmlNode, htmlNode, isBlockElement )
  95  {
  96      var iCount = 0 ;
  97      
  98      var oNode = htmlNode.firstChild ;
  99  
 100      while ( oNode )
 101      {
 102          if ( this._AppendNode( xmlNode, oNode ) )
 103              iCount++ ;
 104  
 105          oNode = oNode.nextSibling ;
 106      }
 107      
 108      if ( iCount == 0 )
 109      {
 110          if ( isBlockElement && FCKConfig.FillEmptyBlocks )
 111          {
 112              this._AppendEntity( xmlNode, 'nbsp' ) ;
 113              return ;
 114          }
 115  
 116          // We can't use short representation of empty elements that are not marked
 117          // as empty in th XHTML DTD.
 118          if ( !FCKRegexLib.EmptyElements.test( htmlNode.nodeName ) )
 119              xmlNode.appendChild( this.XML.createTextNode('') ) ;
 120      }
 121  }
 122  
 123  FCKXHtml._AppendNode = function( xmlNode, htmlNode )
 124  {
 125      if ( !htmlNode )
 126          return ;
 127  
 128      switch ( htmlNode.nodeType )
 129      {
 130          // Element Node.
 131          case 1 :
 132  
 133              // Here we found an element that is not the real element, but a 
 134              // fake one (like the Flash placeholder image), so we must get the real one.
 135              if ( htmlNode.getAttribute('_fckfakelement') )
 136                  return FCKXHtml._AppendNode( xmlNode, FCK.GetRealElement( htmlNode ) ) ;
 137          
 138              // Mozilla insert custom nodes in the DOM.
 139              if ( FCKBrowserInfo.IsGecko && htmlNode.hasAttribute('_moz_editor_bogus_node') )
 140                  return false ;
 141              
 142              // This is for elements that are instrumental to FCKeditor and 
 143              // must be removed from the final HTML.
 144              if ( htmlNode.getAttribute('_fcktemp') )
 145                  return false ;
 146  
 147              // Get the element name.
 148              var sNodeName = htmlNode.nodeName ;
 149              
 150              //Add namespace:
 151              if ( FCKBrowserInfo.IsIE && htmlNode.scopeName && htmlNode.scopeName != 'HTML' )
 152                  sNodeName = htmlNode.scopeName + ':' + sNodeName ;
 153  
 154              // Check if the node name is valid, otherwise ignore this tag.
 155              // If the nodeName starts with a slash, it is a orphan closing tag.
 156              // On some strange cases, the nodeName is empty, even if the node exists.
 157              if ( !FCKRegexLib.ElementName.test( sNodeName ) )
 158                  return false ;
 159  
 160              sNodeName = sNodeName.toLowerCase() ;
 161  
 162              if ( FCKBrowserInfo.IsGecko && sNodeName == 'br' && htmlNode.hasAttribute('type') && htmlNode.getAttribute( 'type', 2 ) == '_moz' )
 163                  return false ;
 164  
 165              // The already processed nodes must be marked to avoid then to be duplicated (bad formatted HTML).
 166              // So here, the "mark" is checked... if the element is Ok, then mark it.
 167              if ( htmlNode._fckxhtmljob && htmlNode._fckxhtmljob == FCKXHtml.CurrentJobNum )
 168                  return false ;
 169  
 170              var oNode = this._CreateNode( sNodeName ) ;
 171              
 172              // Add all attributes.
 173              FCKXHtml._AppendAttributes( xmlNode, htmlNode, oNode, sNodeName ) ;
 174              
 175              htmlNode._fckxhtmljob = FCKXHtml.CurrentJobNum ;
 176  
 177              // Tag specific processing.
 178              var oTagProcessor = FCKXHtml.TagProcessors[ sNodeName ] ;
 179  
 180              if ( oTagProcessor )
 181              {
 182                  oNode = oTagProcessor( oNode, htmlNode, xmlNode ) ;
 183                  if ( !oNode ) break ;
 184              }
 185              else
 186                  this._AppendChildNodes( oNode, htmlNode, FCKRegexLib.BlockElements.test( sNodeName ) ) ;
 187  
 188              xmlNode.appendChild( oNode ) ;
 189  
 190              break ;
 191  
 192          // Text Node.
 193          case 3 :
 194              this._AppendTextNode( xmlNode, htmlNode.nodeValue.replaceNewLineChars(' ') ) ;
 195              break ;
 196  
 197          // Comment
 198          case 8 :
 199              try { xmlNode.appendChild( this.XML.createComment( htmlNode.nodeValue ) ) ; }
 200              catch (e) { /* Do nothing... probably this is a wrong format comment. */ }
 201              break ;
 202  
 203          // Unknown Node type.
 204          default :
 205              xmlNode.appendChild( this.XML.createComment( "Element not supported - Type: " + htmlNode.nodeType + " Name: " + htmlNode.nodeName ) ) ;
 206              break ;
 207      }
 208      return true ;
 209  }
 210  
 211  function FCKXHtml_CreateNode_StrongEm( nodeName )
 212  {
 213      switch ( nodeName )
 214      {
 215          case 'b' :
 216              nodeName = 'strong' ;
 217              break ;
 218          case 'i' :
 219              nodeName = 'em' ;
 220              break ;
 221      }
 222      return this.XML.createElement( nodeName ) ;
 223  }
 224  
 225  function FCKXHtml_CreateNode_Normal( nodeName )
 226  {
 227      return this.XML.createElement( nodeName ) ;
 228  }
 229  
 230  // Append an item to the SpecialBlocks array and returns the tag to be used.
 231  FCKXHtml._AppendSpecialItem = function( item )
 232  {
 233      return '___FCKsi___' + FCKXHtml.SpecialBlocks.AddItem( item ) ;
 234  }
 235  
 236  //if ( FCKConfig.ProcessHTMLEntities )
 237  //{
 238      FCKXHtml._AppendTextNode = function( targetNode, textValue )
 239      {
 240          // We can't just replace the special chars with entities and create a
 241          // text node with it. We must split the text isolating the special chars
 242          // and add each piece a time.
 243          var asPieces = textValue.match( FCKXHtmlEntities.EntitiesRegex ) ;
 244  
 245          if ( asPieces )
 246          {
 247              for ( var i = 0 ; i < asPieces.length ; i++ )
 248              {
 249                  if ( asPieces[i].length == 1 )
 250                  {
 251                      var sEntity = FCKXHtmlEntities.Entities[ asPieces[i] ] ;
 252                      if ( sEntity != null )
 253                      {
 254                          this._AppendEntity( targetNode, sEntity ) ;
 255                          continue ;
 256                      }
 257                  }
 258                  targetNode.appendChild( this.XML.createTextNode( asPieces[i] ) ) ;
 259              }
 260          }
 261      }
 262  //}
 263  //else
 264  //{
 265  //    FCKXHtml._AppendTextNode = function( targetNode, textValue )
 266  //    {
 267  //        targetNode.appendChild( this.XML.createTextNode( textValue ) ) ;
 268  //    }
 269  //}
 270  
 271  // An object that hold tag specific operations.
 272  FCKXHtml.TagProcessors = new Object() ;
 273  
 274  FCKXHtml.TagProcessors['img'] = function( node, htmlNode )
 275  {
 276      // The "ALT" attribute is required in XHTML.
 277      if ( ! node.attributes.getNamedItem( 'alt' ) )
 278          FCKXHtml._AppendAttribute( node, 'alt', '' ) ;
 279  
 280      var sSavedUrl = htmlNode.getAttribute( '_fcksavedurl' ) ;
 281      if ( sSavedUrl && sSavedUrl.length > 0 )
 282          FCKXHtml._AppendAttribute( node, 'src', sSavedUrl ) ;
 283  
 284      return node ;
 285  }
 286  
 287  FCKXHtml.TagProcessors['a'] = function( node, htmlNode )
 288  {
 289      var sSavedUrl = htmlNode.getAttribute( '_fcksavedurl' ) ;
 290      if ( sSavedUrl && sSavedUrl.length > 0 )
 291          FCKXHtml._AppendAttribute( node, 'href', sSavedUrl ) ;
 292  
 293      FCKXHtml._AppendChildNodes( node, htmlNode, false ) ;
 294  
 295      return node ;
 296  }
 297  
 298  FCKXHtml.TagProcessors['script'] = function( node, htmlNode )
 299  {
 300      // The "TYPE" attribute is required in XHTML.
 301      if ( ! node.attributes.getNamedItem( 'type' ) )
 302          FCKXHtml._AppendAttribute( node, 'type', 'text/javascript' ) ;
 303  
 304      node.appendChild( FCKXHtml.XML.createTextNode( FCKXHtml._AppendSpecialItem( htmlNode.text ) ) ) ;
 305  
 306      return node ;
 307  }
 308  
 309  FCKXHtml.TagProcessors['style'] = function( node, htmlNode )
 310  {
 311      // The "TYPE" attribute is required in XHTML.
 312      if ( ! node.attributes.getNamedItem( 'type' ) )
 313          FCKXHtml._AppendAttribute( node, 'type', 'text/css' ) ;
 314  
 315      node.appendChild( FCKXHtml.XML.createTextNode( FCKXHtml._AppendSpecialItem( htmlNode.innerHTML ) ) ) ;
 316  
 317      return node ;
 318  }
 319  
 320  FCKXHtml.TagProcessors['title'] = function( node, htmlNode )
 321  {
 322      node.appendChild( FCKXHtml.XML.createTextNode( FCK.EditorDocument.title ) ) ;
 323  
 324      return node ;
 325  }
 326  
 327  FCKXHtml.TagProcessors['table'] = function( node, htmlNode )
 328  {
 329      // There is a trick to show table borders when border=0. We add to the
 330      // table class the FCK__ShowTableBorders rule. So now we must remove it.
 331  
 332      var oClassAtt = node.attributes.getNamedItem( 'class' ) ;
 333  
 334      if ( oClassAtt && FCKRegexLib.TableBorderClass.test( oClassAtt.nodeValue ) )
 335      {
 336          var sClass = oClassAtt.nodeValue.replace( FCKRegexLib.TableBorderClass, '' ) ;
 337  
 338          if ( sClass.length == 0 )
 339              node.attributes.removeNamedItem( 'class' ) ;
 340          else
 341              FCKXHtml._AppendAttribute( node, 'class', sClass ) ;
 342      }
 343  
 344      FCKXHtml._AppendChildNodes( node, htmlNode, false ) ;
 345  
 346      return node ;
 347  }
 348  
 349  // Fix nested <ul> and <ol>.
 350  FCKXHtml.TagProcessors['ol'] = FCKXHtml.TagProcessors['ul'] = function( node, htmlNode, targetNode )
 351  {
 352      if ( htmlNode.innerHTML.trim().length == 0 )
 353          return ;
 354  
 355      var ePSibling = targetNode.lastChild ;
 356      
 357      if ( ePSibling && ePSibling.nodeType == 3 )
 358          ePSibling = ePSibling.previousSibling ;
 359      
 360      if ( ePSibling && ePSibling.nodeName.toUpperCase() == 'LI' )
 361      {
 362          htmlNode._fckxhtmljob = null ;
 363          FCKXHtml._AppendNode( ePSibling, htmlNode ) ;
 364          return ;
 365      }
 366  
 367      FCKXHtml._AppendChildNodes( node, htmlNode ) ;
 368  
 369      return node ;
 370  }


Généré le : Wed Nov 21 12:27:40 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics