[ 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/ -> fcktools_gecko.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: fcktools_gecko.js
  14   *     Utility functions. (Gecko version).
  15   * 
  16   * File Authors:
  17   *         Frederico Caldeira Knabben (fredck@fckeditor.net)
  18   */
  19  
  20  // Constant for the Gecko Bogus Node.
  21  var GECKO_BOGUS = FCKBrowserInfo.IsGecko ? '<br _moz_editor_bogus_node="TRUE">' : '' ;
  22  
  23  FCKTools.CancelEvent = function( e )
  24  {
  25      if ( e )
  26          e.preventDefault() ;
  27  }
  28  
  29  FCKTools.DisableSelection = function( element )
  30  {
  31      if ( FCKBrowserInfo.IsGecko )
  32          element.style.MozUserSelect    = 'none' ;    // Gecko only.    
  33      else
  34          element.style.userSelect    = 'none' ;    // CSS3 (not supported yet).
  35  }
  36  
  37  // Appends a CSS file to a document.
  38  FCKTools.AppendStyleSheet = function( documentElement, cssFileUrl )
  39  {
  40      var e = documentElement.createElement( 'LINK' ) ;
  41      e.rel    = 'stylesheet' ;
  42      e.type    = 'text/css' ;
  43      e.href    = cssFileUrl ;
  44      documentElement.getElementsByTagName("HEAD")[0].appendChild( e ) ;
  45      return e ;
  46  }
  47  
  48  // Removes all attributes and values from the element.
  49  FCKTools.ClearElementAttributes = function( element )
  50  {
  51      // Loop throw all attributes in the element
  52      for ( var i = 0 ; i < element.attributes.length ; i++ )
  53      {
  54          // Remove the element by name.
  55          element.removeAttribute( element.attributes[i].name, 0 ) ;    // 0 : Case Insensitive
  56      }
  57  }
  58  
  59  // Returns an Array of strings with all defined in the elements inside another element.
  60  FCKTools.GetAllChildrenIds = function( parentElement )
  61  {
  62      // Create the array that will hold all Ids.
  63      var aIds = new Array() ;
  64      
  65      // Define a recursive function that search for the Ids.
  66      var fGetIds = function( parent )
  67      {
  68          for ( var i = 0 ; i < parent.childNodes.length ; i++ )
  69          {
  70              var sId = parent.childNodes[i].id ;
  71              
  72              // Check if the Id is defined for the element.
  73              if ( sId && sId.length > 0 ) aIds[ aIds.length ] = sId ;
  74              
  75              // Recursive call.
  76              fGetIds( parent.childNodes[i] ) ;
  77          }
  78      }
  79      
  80      // Start the recursive calls.
  81      fGetIds( parentElement ) ;
  82  
  83      return aIds ;
  84  }
  85  
  86  FCKTools.RemoveOuterTags = function( e )
  87  {
  88      var oFragment = e.ownerDocument.createDocumentFragment() ;
  89              
  90      for ( var i = 0 ; i < e.childNodes.length ; i++ )
  91          oFragment.appendChild( e.childNodes[i] ) ;
  92              
  93      e.parentNode.replaceChild( oFragment, e ) ;
  94  }
  95  
  96  FCKTools.CreateXmlObject = function( object )
  97  {
  98      switch ( object )
  99      {
 100          case 'XmlHttp' :
 101              return new XMLHttpRequest() ;
 102          case 'DOMDocument' :
 103              return document.implementation.createDocument( '', '', null ) ;
 104      }
 105      return null ;
 106  }
 107  
 108  FCKTools.GetScrollPosition = function( relativeWindow )
 109  {
 110      return { X : relativeWindow.pageXOffset, Y : relativeWindow.pageYOffset } ;
 111  }
 112  
 113  FCKTools.AddEventListener = function( sourceObject, eventName, listener )
 114  {
 115      sourceObject.addEventListener( eventName, listener, false ) ;
 116  }
 117  
 118  FCKTools.RemoveEventListener = function( sourceObject, eventName, listener )
 119  {
 120      sourceObject.removeEventListener( eventName, listener, false ) ;
 121  }
 122  
 123  // Listeners attached with this function cannot be detached.
 124  FCKTools.AddEventListenerEx = function( sourceObject, eventName, listener, paramsArray )
 125  {
 126      sourceObject.addEventListener( 
 127          eventName, 
 128          function( e )
 129          {
 130              listener.apply( sourceObject, [ e ].concat( paramsArray || [] ) ) ;
 131          },
 132          false 
 133      ) ;
 134  }
 135  
 136  // Returns and object with the "Width" and "Height" properties.
 137  FCKTools.GetViewPaneSize = function( win )
 138  {
 139      return { Width : win.innerWidth, Height : win.innerHeight } ;
 140  }
 141  
 142  FCKTools.SaveStyles = function( element )
 143  {
 144      var oSavedStyles = new Object() ;
 145      
 146      if ( element.className.length > 0 )
 147      {
 148          oSavedStyles.Class = element.className ;
 149          element.className = '' ;
 150      }
 151  
 152      var sInlineStyle = element.getAttribute( 'style' ) ;
 153  
 154      if ( sInlineStyle && sInlineStyle.length > 0 )
 155      {
 156          oSavedStyles.Inline = sInlineStyle ;
 157          element.setAttribute( 'style', '', 0 ) ;    // 0 : Case Insensitive
 158      }
 159  
 160      return oSavedStyles ;
 161  }
 162  
 163  FCKTools.RestoreStyles = function( element, savedStyles )
 164  {
 165      element.className = savedStyles.Class || '' ;
 166  
 167      if ( savedStyles.Inline )
 168          element.setAttribute( 'style', savedStyles.Inline, 0 ) ;    // 0 : Case Insensitive
 169      else
 170          element.removeAttribute( 'style', 0 ) ;
 171  }
 172  
 173  FCKTools.RegisterDollarFunction = function( targetWindow )
 174  {
 175      targetWindow.$ = function( id ) 
 176      { 
 177          return this.document.getElementById( id ) ;
 178      } ;
 179  }
 180  
 181  FCKTools.AppendElement = function( target, elementName )
 182  {
 183      return target.appendChild( target.ownerDocument.createElement( elementName ) ) ;
 184  }
 185  
 186  // START iCM Modifications
 187  /*
 188  // Starting at the specified node, find the first inline node of the sequence
 189  // For example, assume we have the following elements : <p>Some text <span>some more text</span> and <a href="href">some link</a> yet some more text</p>
 190  // If the "some link" text node is the one specified, then the "Some text" text node will be the first inline node returned.
 191  FCKTools.GetFirstInlineNode = function( oNode )
 192  {
 193      if ( FCKRegexLib.BlockElements.test( oNode.nodeName ) )
 194          return oNode ;
 195      else if ( oNode.previousSibling && !FCKRegexLib.BlockElements.test( oNode.previousSibling.nodeName ) )
 196          return FCKTools.GetFirstInlineNode( oNode.previousSibling ) ;
 197      else if ( oNode.parentNode && !FCKRegexLib.BlockElements.test( oNode.parentNode.nodeName ) && oNode.parentNode.nodeName.toUpperCase() != "BODY" )
 198          return FCKTools.GetFirstInlineNode( oNode.parentNode ) ;
 199      else 
 200          return oNode ;
 201  }
 202  
 203  // Starting at the specified node, find the last inline node of the sequence
 204  // For example, assume we have the following elements : <p>Some text <span>some more text</span> and <a href="href">some link</a> yet some more text</p>
 205  // If the "some link" text node is the one specified, then the " yet some more text" text node will be the last inline node returned.
 206  FCKTools.GetLastInlineNode = function( oNode )
 207  {
 208      if ( FCKRegexLib.BlockElements.test( oNode.nodeName ) )
 209          return oNode ;
 210      else if ( oNode.nextSibling && !FCKRegexLib.BlockElements.test( oNode.nextSibling.nodeName ) )
 211          return FCKTools.GetLastInlineNode( oNode.nextSibling ) ;
 212      else if ( oNode.parentNode && !FCKRegexLib.BlockElements.test( oNode.parentNode.nodeName ) && oNode.parentNode.nodeName.toUpperCase() != "BODY" )
 213          return FCKTools.GetLastInlineNode( oNode.parentNode ) ;
 214      else
 215          return oNode ;
 216  }
 217  
 218  
 219  // Split the supplied parent at the specified child and (optionally) offset.
 220  // Ensure that enclosing block elements are created where missing but that existing 
 221  // block elements (table for example) don't get incorrectly nested. 
 222  FCKTools.SplitNode = function( oParentBlockNode, oChildNode, nOffset )
 223  {
 224      if ( typeof nOffset == "undefined" ) nOffset = 0 ;
 225  
 226      var oFragment = FCK.EditorDocument.createDocumentFragment() ;
 227      var oRange = FCK.EditorDocument.createRange() ;
 228  
 229      if ( FCKRegexLib.ListElements.test( oParentBlockNode.nodeName ) )
 230      {
 231          // Treat OL/UL parents differently as want to split at the specified
 232          // child LI node to create to OL/UL lists.
 233          oStartNode = oParentBlockNode.firstChild ;
 234          oEndNode = oParentBlockNode.lastChild ;
 235      }
 236      else
 237      {
 238          // Locate the inline nodes adjacent to the specified child node so that these can
 239          // be kept together.
 240          oStartNode = FCKTools.GetFirstInlineNode( oChildNode ) ;
 241          oEndNode = FCKTools.GetLastInlineNode( oChildNode ) ;
 242      }
 243  
 244      // Create a new tag which holds the content of the affected node(s) located before (but not including) the child node and offset
 245      if ( FCKRegexLib.BlockElements.test( oStartNode.nodeName ) && !FCKRegexLib.ListElements.test( oParentBlockNode.nodeName ) )
 246      {
 247          // First element of the bunch is already a block element so we don't want to wrap it with a new block element.
 248          // Just use this first node provided it is not the same as the last node (to prevent duplication), otherwise
 249          // create a new empty P element.
 250          if ( oStartNode != oEndNode )
 251          {
 252              oBlockNode1 = oStartNode.cloneNode( true ) ;
 253          }
 254          else
 255          {
 256              oBlockNode1 = FCK.EditorDocument.createElement( "P" ) ;
 257              oBlockNode1.innerHTML = GECKO_BOGUS ;
 258              
 259              if ( !FCKRegexLib.SpecialBlockElements.test( oParentBlockNode.nodeName ) )
 260                  FCKTools.SetElementAttributes( oBlockNode1, oParentBlockNode.attributes ) ;  // Transfer across any class attributes, etc
 261          }
 262      }
 263      else
 264      {
 265          // First element of the bunch is not a block element (or it is a LI element which is a special case).
 266          // So ensure all of the inline nodes before the selection are wrapped with a suitable block element.
 267          var oBlockNode1 = FCK.EditorDocument.createElement( FCKRegexLib.SpecialBlockElements.test( oParentBlockNode.nodeName ) ? "P" : oParentBlockNode.tagName ) ;
 268          oRange.setStartBefore( oStartNode ) ;
 269          if ( nOffset == 0 )
 270              oRange.setEndBefore( oChildNode ) ;
 271          else
 272              oRange.setEnd( oChildNode, nOffset ) ;
 273          oBlockNode1.appendChild( oRange.cloneContents() ) ;
 274          oBlockNode1.innerHTML = oBlockNode1.innerHTML.replace(/[\x00-\x1F]/g, "") ; // Prevent any control characters returned within the innerHTML from causing problems
 275          if ( FCKTools.NodeIsEmpty( oBlockNode1 ) )
 276              oBlockNode1.innerHTML = GECKO_BOGUS ;        // Ensure it has some content, required for Gecko
 277          else
 278              oBlockNode1.innerHTML = oBlockNode1.innerHTML.replace( FCKRegexLib.EmptyElement, "" ) ; // Strip out any empty tags that may have been generated by the split
 279          if ( !FCKRegexLib.SpecialBlockElements.test( oParentBlockNode.nodeName ) )
 280              FCKTools.SetElementAttributes( oBlockNode1, oParentBlockNode.attributes ) ;     // Transfer across any class attributes, etc
 281      }
 282  
 283      // Create a new tag which holds the content of the affected node(s) located after (and including) the child node
 284      if ( FCKRegexLib.BlockElements.test( oEndNode.nodeName ) && !FCKRegexLib.ListElements.test( oParentBlockNode.nodeName ) )
 285      {
 286          // Last element of the bunch is already a block element so we don't want to wrap it with a new block element.
 287          oBlockNode2 = oEndNode.cloneNode( true ) ;
 288      }
 289      else
 290      {
 291          // Last element of the bunch is not a block element (or it is a LI element which is a special case).
 292          // So ensure all of the inline nodes after and including the child/offset are wrapped with a suitable block element.
 293          var oBlockNode2 = FCK.EditorDocument.createElement( FCKRegexLib.SpecialBlockElements.test( oParentBlockNode.nodeName ) ? "P" : oParentBlockNode.tagName );
 294          oRange.setEndAfter( oEndNode );
 295          if ( nOffset == 0 )
 296              oRange.setStartBefore( oChildNode ) ;
 297          else
 298              oRange.setStart( oChildNode, nOffset );
 299          oBlockNode2.appendChild( oRange.cloneContents() ) ;
 300          oBlockNode2.innerHTML = oBlockNode2.innerHTML.replace(/[\x00-\x1F]/g, "") ;  // Prevent any control characters returned within the innerHTML from causing problems
 301          if ( FCKTools.NodeIsEmpty( oBlockNode2 ) ) 
 302              oBlockNode2.innerHTML = GECKO_BOGUS ;             // Ensure it has some content, required for Gecko
 303          else
 304              oBlockNode2.innerHTML = oBlockNode2.innerHTML.replace( FCKRegexLib.EmptyElement, "" ) ; // Strip out any empty tags that may have been generated by the split
 305          if ( !FCKRegexLib.SpecialBlockElements.test( oParentBlockNode.nodeName ) )
 306              FCKTools.SetElementAttributes( oBlockNode2, oParentBlockNode.attributes ) ;     // Transfer across any class attributes, etc
 307      }
 308      
 309      // Insert the resulting nodes into a document fragment
 310      oFragment.appendChild( oBlockNode1 );
 311      oFragment.appendChild( oBlockNode2 );
 312      
 313      // Replace the affected nodes with the new nodes (fragment)
 314      FCKTools.ReplaceNodes( oParentBlockNode, oStartNode, oEndNode, oFragment ) ;    
 315      
 316      // Return the second node so it can be used for setting cursor position, etc
 317      return oBlockNode2 ;
 318  }
 319  
 320  // Function that replaces the specified range of nodes (inclusive) within the supplied parent
 321  // with the nodes stored in the supplied document fragment.
 322  FCKTools.ReplaceNodes = function( oParentBlockNode, oStartNode, oEndNode, oFragment )
 323  {
 324      var oRange = FCK.EditorDocument.createRange() ;
 325      
 326      // Delete the affected node(s)
 327      if ( !FCKRegexLib.SpecialBlockElements.test( oParentBlockNode.nodeName ) && (oParentBlockNode.firstChild == oStartNode) && (oParentBlockNode.lastChild == oEndNode) )
 328      {
 329          // Entire parent block node is to be replaced so insert the two new block elements before it 
 330          // and then remove the old node
 331          oRange.selectNode ( oParentBlockNode );
 332      }
 333      else
 334      {
 335          // Only part of the parent block node is to be replaced so insert the two new block elements
 336          // before the first inline node of the affected content and then remove the old nodes
 337          oRange.setEndAfter( oEndNode ) ;
 338          oRange.setStartBefore( oStartNode ) ;
 339      }
 340      
 341      // Insert the replacement nodes
 342      oRange.deleteContents() ;
 343      oRange.insertNode( oFragment ) ;
 344  }
 345  */
 346  // END iCM Modifications
 347  


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