📄 fckstyle.js
字号:
return false ;
},
/**
* Get the style state for an element path. Returns "true" if the element
* is active in the path.
*/
CheckActive : function( elementPath )
{
switch ( this.GetType() )
{
case FCK_STYLE_BLOCK :
return this.CheckElementRemovable( elementPath.Block || elementPath.BlockLimit, true ) ;
case FCK_STYLE_INLINE :
var elements = elementPath.Elements ;
for ( var i = 0 ; i < elements.length ; i++ )
{
var element = elements[i] ;
if ( element == elementPath.Block || element == elementPath.BlockLimit )
continue ;
if ( this.CheckElementRemovable( element, true ) )
return true ;
}
}
return false ;
},
/**
* Removes an inline style from inside an element tree. The element node
* itself is not checked or removed, only the child tree inside of it.
*/
RemoveFromElement : function( element )
{
var attribs = this._GetAttribsForComparison() ;
var overrides = this._GetOverridesForComparison() ;
// Get all elements with the same name.
var innerElements = element.getElementsByTagName( this.Element ) ;
for ( var i = innerElements.length - 1 ; i >= 0 ; i-- )
{
var innerElement = innerElements[i] ;
// Remove any attribute that conflict with this style, no matter
// their values.
for ( var att in attribs )
{
if ( FCKDomTools.HasAttribute( innerElement, att ) )
{
switch ( att )
{
case 'style' :
this._RemoveStylesFromElement( innerElement ) ;
break ;
case 'class' :
// The 'class' element value must match (#1318).
if ( FCKDomTools.GetAttributeValue( innerElement, att ) != this.GetFinalAttributeValue( att ) )
continue ;
default :
FCKDomTools.RemoveAttribute( innerElement, att ) ;
}
}
}
// Remove overrides defined to the same element name.
this._RemoveOverrides( innerElement, overrides[ this.Element ] ) ;
// Remove the element if no more attributes are available.
this._RemoveNoAttribElement( innerElement ) ;
}
// Now remove any other element with different name that is
// defined to be overriden.
for ( var overrideElement in overrides )
{
if ( overrideElement != this.Element )
{
// Get all elements.
innerElements = element.getElementsByTagName( overrideElement ) ;
for ( var i = innerElements.length - 1 ; i >= 0 ; i-- )
{
var innerElement = innerElements[i] ;
this._RemoveOverrides( innerElement, overrides[ overrideElement ] ) ;
this._RemoveNoAttribElement( innerElement ) ;
}
}
}
},
_RemoveStylesFromElement : function( element )
{
var elementStyle = element.style.cssText ;
var pattern = this.GetFinalStyleValue() ;
if ( elementStyle.length > 0 && pattern.length == 0 )
return ;
pattern = '(^|;)\\s*(' +
pattern.replace( /\s*([^ ]+):.*?(;|$)/g, '$1|' ).replace( /\|$/, '' ) +
'):[^;]+' ;
var regex = new RegExp( pattern, 'gi' ) ;
elementStyle = elementStyle.replace( regex, '' ).Trim() ;
if ( elementStyle.length == 0 || elementStyle == ';' )
FCKDomTools.RemoveAttribute( element, 'style' ) ;
else
element.style.cssText = elementStyle.replace( regex, '' ) ;
},
/**
* Remove all attributes that are defined to be overriden,
*/
_RemoveOverrides : function( element, override )
{
var attributes = override && override.Attributes ;
if ( attributes )
{
for ( var i = 0 ; i < attributes.length ; i++ )
{
var attName = attributes[i][0] ;
if ( FCKDomTools.HasAttribute( element, attName ) )
{
var attValue = attributes[i][1] ;
// Remove the attribute if:
// - The override definition value is null ;
// - The override definition valie is a string that
// matches the attribute value exactly.
// - The override definition value is a regex that
// has matches in the attribute value.
if ( attValue == null ||
( attValue.test && attValue.test( FCKDomTools.GetAttributeValue( element, attName ) ) ) ||
( typeof attValue == 'string' && FCKDomTools.GetAttributeValue( element, attName ) == attValue ) )
FCKDomTools.RemoveAttribute( element, attName ) ;
}
}
}
},
/**
* If the element has no more attributes, remove it.
*/
_RemoveNoAttribElement : function( element )
{
// If no more attributes remained in the element, remove it,
// leaving its children.
if ( !FCKDomTools.HasAttributes( element ) )
{
// Removing elements may open points where merging is possible,
// so let's cache the first and last nodes for later checking.
var firstChild = element.firstChild ;
var lastChild = element.lastChild ;
FCKDomTools.RemoveNode( element, true ) ;
// Check the cached nodes for merging.
this._MergeSiblings( firstChild ) ;
if ( firstChild != lastChild )
this._MergeSiblings( lastChild ) ;
}
},
/**
* Creates a DOM element for this style object.
*/
BuildElement : function( targetDoc, element )
{
// Create the element.
var el = element || targetDoc.createElement( this.Element ) ;
// Assign all defined attributes.
var attribs = this._StyleDesc.Attributes ;
var attValue ;
if ( attribs )
{
for ( var att in attribs )
{
attValue = this.GetFinalAttributeValue( att ) ;
if ( att.toLowerCase() == 'class' )
el.className = attValue ;
else
el.setAttribute( att, attValue ) ;
}
}
// Assign the style attribute.
if ( this._GetStyleText().length > 0 )
el.style.cssText = this.GetFinalStyleValue() ;
return el ;
},
_CompareAttributeValues : function( attName, valueA, valueB )
{
if ( attName == 'style' && valueA && valueB )
{
valueA = valueA.replace( /;$/, '' ).toLowerCase() ;
valueB = valueB.replace( /;$/, '' ).toLowerCase() ;
}
// Return true if they match or if valueA is null and valueB is an empty string
return ( valueA == valueB || ( ( valueA === null || valueA === '' ) && ( valueB === null || valueB === '' ) ) )
},
GetFinalAttributeValue : function( attName )
{
var attValue = this._StyleDesc.Attributes ;
var attValue = attValue ? attValue[ attName ] : null ;
if ( !attValue && attName == 'style' )
return this.GetFinalStyleValue() ;
if ( attValue && this._Variables )
// Using custom Replace() to guarantee the correct scope.
attValue = attValue.Replace( FCKRegexLib.StyleVariableAttName, this._GetVariableReplace, this ) ;
return attValue ;
},
GetFinalStyleValue : function()
{
var attValue = this._GetStyleText() ;
if ( attValue.length > 0 && this._Variables )
{
// Using custom Replace() to guarantee the correct scope.
attValue = attValue.Replace( FCKRegexLib.StyleVariableAttName, this._GetVariableReplace, this ) ;
attValue = FCKTools.NormalizeCssText( attValue ) ;
}
return attValue ;
},
_GetVariableReplace : function()
{
// The second group in the regex is the variable name.
return this._Variables[ arguments[2] ] || arguments[0] ;
},
/**
* Set the value of a variable attribute or style, to be used when
* appliying the style.
*/
SetVariable : function( name, value )
{
var variables = this._Variables ;
if ( !variables )
variables = this._Variables = {} ;
this._Variables[ name ] = value ;
},
/**
* Converting from a PRE block to a non-PRE block in formatting operations.
*/
_FromPre : function( doc, block, newBlock )
{
var innerHTML = block.innerHTML ;
// Trim the first and last linebreaks immediately after and before <pre>, </pre>,
// if they exist.
// This is done because the linebreaks are not rendered.
innerHTML = innerHTML.replace( /(\r\n|\r)/g, '\n' ) ;
innerHTML = innerHTML.replace( /^[ \t]*\n/, '' ) ;
innerHTML = innerHTML.replace( /\n$/, '' ) ;
// 1. Convert spaces or tabs at the beginning or at the end to
innerHTML = innerHTML.replace( /^[ \t]+|[ \t]+$/g, function( match, offset, s )
{
if ( match.length == 1 ) // one space, preserve it
return ' ' ;
else if ( offset == 0 ) // beginning of block
return new Array( match.length ).join( ' ' ) + ' ' ;
else // end of block
return ' ' + new Array( match.length ).join( ' ' ) ;
} ) ;
// 2. Convert \n to <BR>.
// 3. Convert contiguous (i.e. non-singular) spaces or tabs to
var htmlIterator = new FCKHtmlIterator( innerHTML ) ;
var results = [] ;
htmlIterator.Each( function( isTag, value )
{
if ( !isTag )
{
value = value.replace( /\n/g, '<BR>' ) ;
value = value.replace( /[ \t]{2,}/g,
function ( match )
{
return new Array( match.length ).join( ' ' ) + ' ' ;
} ) ;
}
results.push( value ) ;
} ) ;
newBlock.innerHTML = results.join( '' ) ;
return newBlock ;
},
/**
* Converting from a non-PRE block to a PRE block in formatting operations.
*/
_ToPre : function( doc, block, newBlock )
{
// Handle converting from a regular block to a <pre> block.
var innerHTML = block.innerHTML.Trim() ;
// 1. Delete ANSI whitespaces immediately before and after <BR> because they are not visible.
// 2. Mark down any <BR /> nodes here so they can be turned into \n in the next step and avoid being compressed.
innerHTML = innerHTML.replace( /[ \t\r\n]*(<br[^>]*>)[ \t\r\n]*/gi, '<BR />' ) ;
// 3. Compress other ANSI whitespaces since they're only visible as one single space previously.
// 4. Convert to spaces since is no longer needed in <PRE>.
// 5. Convert any <BR /> to \n. This must not be done earlier because the \n would then get compressed.
var htmlIterator = new FCKHtmlIterator( innerHTML ) ;
var results = [] ;
htmlIterator.Each( function( isTag, value )
{
if ( !isTag )
value = value.replace( /([ \t\n\r]+| )/g, ' ' ) ;
else if ( isTag && value == '<BR />' )
value = '\n' ;
results.push( value ) ;
} ) ;
// Assigning innerHTML to <PRE> in IE causes all linebreaks to be reduced to spaces.
// Assigning outerHTML to <PRE> in IE doesn't work if the <PRE> isn't contained in another node
// since the node reference is changed after outerHTML assignment.
// So, we need some hacks to workaround IE bugs here.
if ( FCKBrowserInfo.IsIE )
{
var temp = doc.createElement( 'div' ) ;
temp.appendChild( newBlock ) ;
newBlock.outerHTML = '<PRE>\n' + results.join( '' ) + '</PRE>' ;
newBlock = temp.removeChild( temp.firstChild ) ;
}
else
newBlock.innerHTML = results.join( '' ) ;
return newBlock ;
},
/**
* Apply an inline style to a FCKDomRange.
*
* TODO
* - Implement the "#" style handling.
* - Properly handle block containers like <div> and <blockquote>.
*/
_ApplyBlockStyle : function( range, selectIt, updateRange )
{
// Bookmark the range so we can re-select it after processing.
var bookmark ;
if ( selectIt )
bookmark = range.CreateBookmark() ;
var iterator = new FCKDomRangeIterator( range ) ;
iterator.EnforceRealBlocks = true ;
var block ;
var doc = range.Window.document ;
var preBlocks = [] ;
var convertedPreBlocks = [] ;
while( ( block = iterator.GetNextParagraph() ) ) // Only one =
{
// Create the new node right before the current one.
var newBlock = this.BuildElement( doc ) ;
// Move everything from the current node to the new one.
var newBlockIsPre = newBlock.nodeName.IEquals( 'pre' ) ;
var blockIsPre = block.nodeName.IEquals( 'pre' ) ;
if ( newBlockIsPre && !blockIsPre )
{
newBlock = this._ToPre( doc, block, newBlock ) ;
preBlocks.push( newBlock ) ;
}
else if ( !newBlockIsPre && blockIsPre )
{
newBlock = this._FromPre( doc, block, newBlock ) ;
convertedPreBlocks.push( newBlock ) ;
}
else // Convering from a regular block to another regular block.
FCKDomTools.MoveChildren( block, newBlock ) ;
// Replace the current block.
block.parentNode.insertBefore( newBlock, block ) ;
FCKDomTools.RemoveNode( block ) ;
}
// Merge adjacent <PRE> blocks for #1229.
for ( var i = 0 ; i < preBlocks.length - 1 ; i++ )
{
// Check if the next block in HTML equals the next <PRE> block generated.
if ( FCKDomTools.GetNextSourceElement( preBlocks[i], true, [], [], true ) != preBlocks[i+1] )
continue ;
// Merge the upper <PRE> block's content into the lower <PRE> block.
// Remove the upper <PRE> block.
preBlocks[i+1].innerHTML = preBlocks[i].innerHTML + '\n\n' + preBlocks[i+1].innerHTML ;
FCKDomTools.RemoveNode( preBlocks[i] ) ;
}
// Split converted <PRE> blocks for #1229.
for ( var i = 0 ; i < convertedPreBlocks.length ; i++ )
{
var currentBlock = convertedPreBlocks[i] ;
var lastNewBlock = null ;
for ( var j = 0 ; j < currentBlock.childNodes.length ; j++ )
{
var cursor = currentBlock.childNodes[j] ;
// If we have two <BR>s, and they're not at the beginning or the end,
// then we'll split up the contents following them into another block.
if ( cursor.nodeName.IEquals( 'br' ) && j != 0 && j != currentBlock.childNodes.length - 2
&& cursor.nextSibling && cursor.nextSibling.nodeName.IEquals( 'br' ) )
{
FCKDomTools.RemoveNode( cursor.nextSibling ) ;
FCKDomTools.RemoveNode( cursor ) ;
j-- ; // restart at current index at next iteration
lastNewBlock = FCKDomTools.InsertAfterNode( lastNewBlock || currentBlock, doc.createElement( currentBlock.nodeName ) ) ;
continue ;
}
if ( lastNewBlock )
{
FCKDomTools.MoveNode( cursor, lastNewBlock ) ;
j-- ; // restart at current index at next iteration
}
}
}
// Re-select the original range.
if ( selectIt )
range.SelectBookmark( bookmark ) ;
if ( updateRange )
range.MoveToBookmark( bookmark ) ;
},
/**
* Apply an inline style to a FCKDomRange.
*
* TODO
* - Merge elements, when applying styles to similar elements that enclose
* the entire selection, outputing:
* <span style="color: #ff0000; background-color: #ffffff">XYZ</span>
* instead of:
* <span style="color: #ff0000;"><span style="background-color: #ffffff">XYZ</span></span>
*/
_ApplyInlineStyle : function( range, selectIt, updateRange )
{
var doc = range.Window.document ;
if ( range.CheckIsCollapsed() )
{
// Create the element to be inserted in the DOM.
var collapsedElement = this.BuildElement( doc ) ;
range.InsertNode( collapsedElement ) ;
range.MoveToPosition( collapsedElement, 2 ) ;
range.Select() ;
return ;
}
// The general idea here is navigating through all nodes inside the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -