?? fckdomtools.js
字號:
// attribute, which will be marked as "specified", even if the
// outerHTML of the element is not displaying the class attribute.
// Note : I was not able to reproduce it outside the editor,
// but I've faced it while working on the TC of #1391.
if ( element.className.length > 0 )
return true ;
}
else if ( attributes[i].specified )
return true ;
}
return false ;
},
/**
* Remove an attribute from an element.
*/
RemoveAttribute : function( element, attributeName )
{
if ( FCKBrowserInfo.IsIE && attributeName.toLowerCase() == 'class' )
attributeName = 'className' ;
return element.removeAttribute( attributeName, 0 ) ;
},
GetAttributeValue : function( element, att )
{
var attName = att ;
if ( typeof att == 'string' )
att = element.attributes[ att ] ;
else
attName = att.nodeName ;
if ( att && att.specified )
{
// IE returns "null" for the nodeValue of a "style" attribute.
if ( attName == 'style' )
return element.style.cssText ;
// There are two cases when the nodeValue must be used:
// - for the "class" attribute (all browsers).
// - for events attributes (IE only).
else if ( attName == 'class' || attName.indexOf('on') == 0 )
return att.nodeValue ;
else
{
// Use getAttribute to get its value exactly as it is
// defined.
return element.getAttribute( attName, 2 ) ;
}
}
return null ;
},
/**
* Checks whether one element contains the other.
*/
Contains : function( mainElement, otherElement )
{
// IE supports contains, but only for element nodes.
if ( mainElement.contains && otherElement.nodeType == 1 )
return mainElement.contains( otherElement ) ;
while ( ( otherElement = otherElement.parentNode ) ) // Only one "="
{
if ( otherElement == mainElement )
return true ;
}
return false ;
},
/**
* Breaks a parent element in the position of one of its contained elements.
* For example, in the following case:
* <b>This <i>is some<span /> sample</i> test text</b>
* If element = <span />, we have these results:
* <b>This <i>is some</i><span /><i> sample</i> test text</b> (If parent = <i>)
* <b>This <i>is some</i></b><span /><b<i> sample</i> test text</b> (If parent = <b>)
*/
BreakParent : function( element, parent, reusableRange )
{
var range = reusableRange || new FCKDomRange( FCKTools.GetElementWindow( element ) ) ;
// We'll be extracting part of this element, so let's use our
// range to get the correct piece.
range.SetStart( element, 4 ) ;
range.SetEnd( parent, 4 ) ;
// Extract it.
var docFrag = range.ExtractContents() ;
// Move the element outside the broken element.
range.InsertNode( element.parentNode.removeChild( element ) ) ;
// Re-insert the extracted piece after the element.
docFrag.InsertAfterNode( element ) ;
range.Release( !!reusableRange ) ;
},
/**
* Retrieves a uniquely identifiable tree address of a DOM tree node.
* The tree address returns is an array of integers, with each integer
* indicating a child index from a DOM tree node, starting from
* document.documentElement.
*
* For example, assuming <body> is the second child from <html> (<head>
* being the first), and we'd like to address the third child under the
* fourth child of body, the tree address returned would be:
* [1, 3, 2]
*
* The tree address cannot be used for finding back the DOM tree node once
* the DOM tree structure has been modified.
*/
GetNodeAddress : function( node, normalized )
{
var retval = [] ;
while ( node && node != node.ownerDocument.documentElement )
{
var parentNode = node.parentNode ;
var currentIndex = -1 ;
for( var i = 0 ; i < parentNode.childNodes.length ; i++ )
{
var candidate = parentNode.childNodes[i] ;
if ( normalized === true &&
candidate.nodeType == 3 &&
candidate.previousSibling &&
candidate.previousSibling.nodeType == 3 )
continue;
currentIndex++ ;
if ( parentNode.childNodes[i] == node )
break ;
}
retval.unshift( currentIndex ) ;
node = node.parentNode ;
}
return retval ;
},
/**
* The reverse transformation of FCKDomTools.GetNodeAddress(). This
* function returns the DOM node pointed to by its index address.
*/
GetNodeFromAddress : function( doc, addr, normalized )
{
var cursor = doc.documentElement ;
for ( var i = 0 ; i < addr.length ; i++ )
{
var target = addr[i] ;
if ( ! normalized )
{
cursor = cursor.childNodes[target] ;
continue ;
}
var currentIndex = -1 ;
for (var j = 0 ; j < cursor.childNodes.length ; j++ )
{
var candidate = cursor.childNodes[j] ;
if ( normalized === true &&
candidate.nodeType == 3 &&
candidate.previousSibling &&
candidate.previousSibling.nodeType == 3 )
continue ;
currentIndex++ ;
if ( currentIndex == target )
{
cursor = candidate ;
break ;
}
}
}
return cursor ;
},
CloneElement : function( element )
{
element = element.cloneNode( false ) ;
// The "id" attribute should never be cloned to avoid duplication.
element.removeAttribute( 'id', false ) ;
return element ;
},
ClearElementJSProperty : function( element, attrName )
{
if ( FCKBrowserInfo.IsIE )
element.removeAttribute( attrName ) ;
else
delete element[attrName] ;
},
SetElementMarker : function ( markerObj, element, attrName, value)
{
var id = String( parseInt( Math.random() * 0xfffffff, 10 ) ) ;
element._FCKMarkerId = id ;
element[attrName] = value ;
if ( ! markerObj[id] )
markerObj[id] = { 'element' : element, 'markers' : {} } ;
markerObj[id]['markers'][attrName] = value ;
},
ClearElementMarkers : function( markerObj, element, clearMarkerObj )
{
var id = element._FCKMarkerId ;
if ( ! id )
return ;
this.ClearElementJSProperty( element, '_FCKMarkerId' ) ;
for ( var j in markerObj[id]['markers'] )
this.ClearElementJSProperty( element, j ) ;
if ( clearMarkerObj )
delete markerObj[id] ;
},
ClearAllMarkers : function( markerObj )
{
for ( var i in markerObj )
this.ClearElementMarkers( markerObj, markerObj[i]['element'], true ) ;
},
/**
* Convert a DOM list tree into a data structure that is easier to
* manipulate. This operation should be non-intrusive in the sense that it
* does not change the DOM tree, with the exception that it may add some
* markers to the list item nodes when markerObj is specified.
*/
ListToArray : function( listNode, markerObj, baseArray, baseIndentLevel, grandparentNode )
{
if ( ! listNode.nodeName.IEquals( ['ul', 'ol'] ) )
return [] ;
if ( ! baseIndentLevel )
baseIndentLevel = 0 ;
if ( ! baseArray )
baseArray = [] ;
// Iterate over all list items to get their contents and look for inner lists.
for ( var i = 0 ; i < listNode.childNodes.length ; i++ )
{
var listItem = listNode.childNodes[i] ;
if ( ! listItem.nodeName.IEquals( 'li' ) )
continue ;
var itemObj = { 'parent' : listNode, 'indent' : baseIndentLevel, 'contents' : [] } ;
if ( ! grandparentNode )
{
itemObj.grandparent = listNode.parentNode ;
if ( itemObj.grandparent && itemObj.grandparent.nodeName.IEquals( 'li' ) )
itemObj.grandparent = itemObj.grandparent.parentNode ;
}
else
itemObj.grandparent = grandparentNode ;
if ( markerObj )
this.SetElementMarker( markerObj, listItem, '_FCK_ListArray_Index', baseArray.length ) ;
baseArray.push( itemObj ) ;
for ( var j = 0 ; j < listItem.childNodes.length ; j++ )
{
var child = listItem.childNodes[j] ;
if ( child.nodeName.IEquals( ['ul', 'ol'] ) )
// Note the recursion here, it pushes inner list items with
// +1 indentation in the correct order.
this.ListToArray( child, markerObj, baseArray, baseIndentLevel + 1, itemObj.grandparent ) ;
else
itemObj.contents.push( child ) ;
}
}
return baseArray ;
},
// Convert our internal representation of a list back to a DOM forest.
ArrayToList : function( listArray, markerObj, baseIndex )
{
if ( baseIndex == undefined )
baseIndex = 0 ;
if ( ! listArray || listArray.length < baseIndex + 1 )
return null ;
var doc = listArray[baseIndex].parent.ownerDocument ;
var retval = doc.createDocumentFragment() ;
var rootNode = null ;
var currentIndex = baseIndex ;
var indentLevel = Math.max( listArray[baseIndex].indent, 0 ) ;
var currentListItem = null ;
while ( true )
{
var item = listArray[currentIndex] ;
if ( item.indent == indentLevel )
{
if ( ! rootNode || listArray[currentIndex].parent.nodeName != rootNode.nodeName )
{
rootNode = listArray[currentIndex].parent.cloneNode( false ) ;
retval.appendChild( rootNode ) ;
}
currentListItem = doc.createElement( 'li' ) ;
rootNode.appendChild( currentListItem ) ;
for ( var i = 0 ; i < item.contents.length ; i++ )
currentListItem.appendChild( item.contents[i].cloneNode( true ) ) ;
currentIndex++ ;
}
else if ( item.indent == Math.max( indentLevel, 0 ) + 1 )
{
var listData = this.ArrayToList( listArray, null, currentIndex ) ;
currentListItem.appendChild( listData.listNode ) ;
currentIndex = listData.nextIndex ;
}
else if ( item.indent == -1 && baseIndex == 0 && item.grandparent )
{
var currentListItem ;
if ( item.grandparent.nodeName.IEquals( ['ul', 'ol'] ) )
currentListItem = doc.createElement( 'li' ) ;
else
{
if ( FCKConfig.EnterMode.IEquals( ['div', 'p'] ) && ! item.grandparent.nodeName.IEquals( 'td' ) )
currentListItem = doc.createElement( FCKConfig.EnterMode ) ;
else
currentListItem = doc.createDocumentFragment() ;
}
for ( var i = 0 ; i < item.contents.length ; i++ )
currentListItem.appendChild( item.contents[i].cloneNode( true ) ) ;
if ( currentListItem.nodeType == 11 )
{
if ( currentListItem.lastChild &&
currentListItem.lastChild.getAttribute &&
currentListItem.lastChild.getAttribute( 'type' ) == '_moz' )
currentListItem.removeChild( currentListItem.lastChild );
currentListItem.appendChild( doc.createElement( 'br' ) ) ;
}
if ( currentListItem.nodeName.IEquals( FCKConfig.EnterMode ) && currentListItem.firstChild )
{
this.TrimNode( currentListItem ) ;
if ( FCKListsLib.BlockBoundaries[currentListItem.firstChild.nodeName.toLowerCase()] )
{
var tmp = doc.createDocumentFragment() ;
while ( currentListItem.firstChild )
tmp.appendChild( currentListItem.removeChild( currentListItem.firstChild ) ) ;
currentListItem = tmp ;
}
}
if ( FCKBrowserInfo.IsGeckoLike && currentListItem.nodeName.IEquals( ['div', 'p'] ) )
FCKTools.AppendBogusBr( currentListItem ) ;
retval.appendChild( currentListItem ) ;
rootNode = null ;
currentIndex++ ;
}
else
return null ;
if ( listArray.length <= currentIndex || Math.max( listArray[currentIndex].indent, 0 ) < indentLevel )
{
break ;
}
}
// Clear marker attributes for the new list tree made of cloned nodes, if any.
if ( markerObj )
{
var currentNode = retval.firstChild ;
while ( currentNode )
{
if ( currentNode.nodeType == 1 )
this.ClearElementMarkers( markerObj, currentNode ) ;
currentNode = this.GetNextSourceNode( currentNode ) ;
}
}
return { 'listNode' : retval, 'nextIndex' : currentIndex } ;
},
/**
* Get the next sibling node for a node. If "includeEmpties" is false,
* only element or non empty text nodes are returned.
*/
GetNextSibling : function( node, includeEmpties )
{
node = node.nextSibling ;
while ( node && !includeEmpties && node.nodeType != 1 && ( node.nodeType != 3 || node.nodeValue.length == 0 ) )
node = node.nextSibling ;
return node ;
},
/**
* Get the previous sibling node for a node. If "includeEmpties" is false,
* only element or non empty text nodes are returned.
*/
GetPreviousSibling : function( node, includeEmpties )
{
node = node.previousSibling ;
while ( node && !includeEmpties && node.nodeType != 1 && ( node.nodeType != 3 || node.nodeValue.length == 0 ) )
node = node.previousSibling ;
return node ;
},
/**
* Checks if an element has no "useful" content inside of it
* node tree. No "useful" content means empty text node or a signle empty
* inline node.
* elementCheckCallback may point to a function that returns a boolean
* indicating that a child element must be considered in the element check.
*/
CheckIsEmptyElement : function( element, elementCheckCallback )
{
var child = element.firstChild ;
var elementChild ;
while ( child )
{
if ( child.nodeType == 1 )
{
if ( elementChild || !FCKListsLib.InlineNonEmptyElements[ child.nodeName.toLowerCase() ] )
return false ;
if ( !elementCheckCallback || elementCheckCallback( child ) === true )
elementChild = child ;
}
else if ( child.nodeType == 3 && child.nodeValue.length > 0 )
return false ;
child = child.nextSibling ;
}
return elementChild ? this.CheckIsEmptyElement( elementChild, elementCheckCallback ) : true ;
},
SetElementStyles : function( element, styleDict )
{
var style = element.style ;
for ( var styleName in styleDict )
style[ styleName ] = styleDict[ styleName ] ;
},
GetCurrentElementStyle : function( w, element, attrName )
{
if ( FCKBrowserInfo.IsIE )
return element.currentStyle[attrName] ;
else
return w.getComputedStyle( element, '' )[attrName] ;
},
GetPositionedAncestor : function( w, element )
{
var currentElement = element ;
while ( currentElement != currentElement.ownerDocument.documentElement )
{
if ( this.GetCurrentElementStyle( w, currentElement, 'position' ) != 'static' )
return currentElement ;
currentElement = currentElement.parentNode ;
}
return null ;
},
/**
* Current implementation for ScrollIntoView (due to #1462). We don't have
* a complete implementation here, just the things that fit our needs.
*/
ScrollIntoView : function( element, alignTop )
{
// Get the element window.
var window = FCKTools.GetElementWindow( element ) ;
var windowHeight = FCKTools.GetViewPaneSize( window ).Height ;
// Starts the offset that will be scrolled with the negative value of
// the visible window height.
var offset = windowHeight * -1 ;
// Appends the height it we are about to align the bottoms.
if ( alignTop === false )
{
offset += element.offsetHeight ;
// Consider the margin in the scroll, which is ok for our current
// needs, but needs investigation if we will be using this function
// in other places.
offset += parseInt( this.GetCurrentElementStyle( window, element, 'marginBottom' ) || 0, 10 ) ;
}
// Appends the offsets for the entire element hierarchy.
offset += element.offsetTop ;
while ( ( element = element.offsetParent ) )
offset += element.offsetTop || 0 ;
// Scroll the window to the desired position, if not already visible.
var currentScroll = FCKTools.GetScrollPosition( window ).Y ;
if ( offset > 0 && offset > currentScroll )
window.scrollTo( 0, offset ) ;
}
} ;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -