[ Index ] |
|
Code source de FCKeditor 2.4 |
1 /* 2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net 3 * Copyright (C) 2003-2007 Frederico Caldeira Knabben 4 * 5 * == BEGIN LICENSE == 6 * 7 * Licensed under the terms of any of the following licenses at your 8 * choice: 9 * 10 * - GNU General Public License Version 2 or later (the "GPL") 11 * http://www.gnu.org/licenses/gpl.html 12 * 13 * - GNU Lesser General Public License Version 2.1 or later (the "LGPL") 14 * http://www.gnu.org/licenses/lgpl.html 15 * 16 * - Mozilla Public License Version 1.1 or later (the "MPL") 17 * http://www.mozilla.org/MPL/MPL-1.1.html 18 * 19 * == END LICENSE == 20 * 21 * File Name: fckdomtools.js 22 * Utility functions to work with the DOM. 23 * 24 * File Authors: 25 * Frederico Caldeira Knabben (www.fckeditor.net) 26 */ 27 28 var FCKDomTools = 29 { 30 MoveChildren : function( source, target ) 31 { 32 if ( source == target ) 33 return ; 34 35 var eChild ; 36 while ( (eChild = source.firstChild) ) 37 target.appendChild( source.removeChild( eChild ) ) ; 38 }, 39 40 // Remove blank spaces from the beginning and the end of the contents of a node. 41 TrimNode : function( node, ignoreEndBRs ) 42 { 43 this.LTrimNode( node ) ; 44 this.RTrimNode( node, ignoreEndBRs ) ; 45 }, 46 47 LTrimNode : function( node ) 48 { 49 var eChildNode ; 50 51 while ( (eChildNode = node.firstChild) ) 52 { 53 if ( eChildNode.nodeType == 3 ) 54 { 55 var sTrimmed = eChildNode.nodeValue.LTrim() ; 56 var iOriginalLength = eChildNode.nodeValue.length ; 57 58 if ( sTrimmed.length == 0 ) 59 { 60 node.removeChild( eChildNode ) ; 61 continue ; 62 } 63 else if ( sTrimmed.length < iOriginalLength ) 64 { 65 eChildNode.splitText( iOriginalLength - sTrimmed.length ) ; 66 node.removeChild( node.firstChild ) ; 67 } 68 } 69 break ; 70 } 71 }, 72 73 RTrimNode : function( node, ignoreEndBRs ) 74 { 75 var eChildNode ; 76 77 while ( (eChildNode = node.lastChild) ) 78 { 79 switch ( eChildNode.nodeType ) 80 { 81 case 1 : 82 if ( eChildNode.nodeName.toUpperCase() == 'BR' && ( ignoreEndBRs || eChildNode.getAttribute( 'type', 2 ) == '_moz' ) ) 83 { 84 node.removeChild( eChildNode ) ; 85 continue ; 86 } 87 break ; 88 89 case 3 : 90 var sTrimmed = eChildNode.nodeValue.RTrim() ; 91 var iOriginalLength = eChildNode.nodeValue.length ; 92 93 if ( sTrimmed.length == 0 ) 94 { 95 node.removeChild( eChildNode ) ; 96 continue ; 97 } 98 else if ( sTrimmed.length < iOriginalLength ) 99 { 100 eChildNode.splitText( sTrimmed.length ) ; 101 node.removeChild( node.lastChild ) ; 102 } 103 } 104 break ; 105 } 106 }, 107 108 RemoveNode : function( node, excludeChildren ) 109 { 110 if ( excludeChildren ) 111 { 112 // Move all children before the node. 113 var eChild ; 114 while ( (eChild = node.firstChild) ) 115 node.parentNode.insertBefore( node.removeChild( eChild ), node ) ; 116 } 117 118 return node.parentNode.removeChild( node ) ; 119 }, 120 121 GetFirstChild : function( node, childNames ) 122 { 123 // If childNames is a string, transform it in a Array. 124 if ( typeof ( childNames ) == 'string' ) 125 childNames = [ childNames ] ; 126 127 var eChild = node.firstChild ; 128 while( eChild ) 129 { 130 if ( eChild.nodeType == 1 && eChild.tagName.Equals.apply( eChild.tagName, childNames ) ) 131 return eChild ; 132 133 eChild = eChild.nextSibling ; 134 } 135 136 return null ; 137 }, 138 139 GetLastChild : function( node, childNames ) 140 { 141 // If childNames is a string, transform it in a Array. 142 if ( typeof ( childNames ) == 'string' ) 143 childNames = [ childNames ] ; 144 145 var eChild = node.lastChild ; 146 while( eChild ) 147 { 148 if ( eChild.nodeType == 1 && ( !childNames || eChild.tagName.Equals( childNames ) ) ) 149 return eChild ; 150 151 eChild = eChild.previousSibling ; 152 } 153 154 return null ; 155 }, 156 157 // Get the previous element in the source order. 158 GetPreviousSourceElement : function( currentNode, ignoreSpaceTextOnly, stopSearchElements, ignoreElements ) 159 { 160 if ( !currentNode ) 161 return null ; 162 163 if ( stopSearchElements && currentNode.nodeType == 1 && currentNode.nodeName.IEquals( stopSearchElements ) ) 164 return null ; 165 166 if ( currentNode.previousSibling ) 167 currentNode = currentNode.previousSibling ; 168 else 169 return this.GetPreviousSourceElement( currentNode.parentNode, ignoreSpaceTextOnly, stopSearchElements, ignoreElements ) ; 170 171 while ( currentNode ) 172 { 173 if ( currentNode.nodeType == 1 ) 174 { 175 if ( stopSearchElements && currentNode.nodeName.IEquals( stopSearchElements ) ) 176 break ; 177 178 if ( !ignoreElements || !currentNode.nodeName.IEquals( ignoreElements ) ) 179 return currentNode ; 180 } 181 else if ( ignoreSpaceTextOnly && currentNode.nodeType == 3 && currentNode.nodeValue.RTrim().length > 0 ) 182 break ; 183 184 if ( currentNode.lastChild ) 185 currentNode = currentNode.lastChild ; 186 else 187 return this.GetPreviousSourceElement( currentNode, ignoreSpaceTextOnly, stopSearchElements, ignoreElements ) ; 188 } 189 190 return null ; 191 }, 192 193 // Get the previous element in the source order. 194 GetNextSourceElement : function( currentNode, ignoreSpaceTextOnly, stopSearchElements, ignoreElements ) 195 { 196 if ( !currentNode ) 197 return null ; 198 199 if ( currentNode.nextSibling ) 200 currentNode = currentNode.nextSibling ; 201 else 202 return this.GetNextSourceElement( currentNode.parentNode, ignoreSpaceTextOnly, stopSearchElements, ignoreElements ) ; 203 204 while ( currentNode ) 205 { 206 if ( currentNode.nodeType == 1 ) 207 { 208 if ( stopSearchElements && currentNode.nodeName.IEquals( stopSearchElements ) ) 209 break ; 210 211 if ( !ignoreElements || !currentNode.nodeName.IEquals( ignoreElements ) ) 212 return currentNode ; 213 } 214 else if ( ignoreSpaceTextOnly && currentNode.nodeType == 3 && currentNode.nodeValue.RTrim().length > 0 ) 215 break ; 216 217 if ( currentNode.firstChild ) 218 currentNode = currentNode.firstChild ; 219 else 220 return this.GetNextSourceElement( currentNode, ignoreSpaceTextOnly, stopSearchElements, ignoreElements ) ; 221 } 222 223 return null ; 224 }, 225 226 // Inserts a element after a existing one. 227 InsertAfterNode : function( existingNode, newNode ) 228 { 229 return existingNode.parentNode.insertBefore( newNode, existingNode.nextSibling ) ; 230 }, 231 232 GetParents : function( node ) 233 { 234 var parents = new Array() ; 235 236 while ( node ) 237 { 238 parents.splice( 0, 0, node ) ; 239 node = node.parentNode ; 240 } 241 242 return parents ; 243 }, 244 245 GetIndexOf : function( node ) 246 { 247 var currentNode = node.parentNode ? node.parentNode.firstChild : null ; 248 var currentIndex = -1 ; 249 250 while ( currentNode ) 251 { 252 currentIndex++ ; 253 254 if ( currentNode == node ) 255 return currentIndex ; 256 257 currentNode = currentNode.nextSibling ; 258 } 259 260 return -1 ; 261 } 262 } ;
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 15:28:05 2007 | par Balluche grâce à PHPXref 0.7 |