\r
// Save the current state, so it can be compared next\r
// time.\r
- callback.state !== currentState;\r
+ callback.state = currentState;\r
}\r
}\r
});\r
}\r
\r
- // Save the callback info, so it can be checked on the next occurence of\r
+ // Save the callback info, so it can be checked on the next occurrence of\r
// selectionChange.\r
styleStateChangeCallbacks.push( { style : style, fn : callback } );\r
};\r
return ( this.removeFromRange =\r
this.type == CKEDITOR.STYLE_INLINE ?\r
removeInlineStyle\r
+ : this.type == CKEDITOR.STYLE_OBJECT ?\r
+ removeObjectStyle\r
: null ).call( this, range );\r
},\r
\r
return false;\r
},\r
\r
+ /**\r
+ * Whether this style can be applied at the element path.\r
+ * @param elementPath\r
+ */\r
checkApplicable : function( elementPath )\r
{\r
switch ( this.type )\r
continue;\r
\r
var elementAttr = element.getAttribute( attName ) || '';\r
+\r
+ // Special treatment for 'style' attribute is required.\r
if ( attName == 'style' ?\r
compareCssText( attribs[ attName ], normalizeCssText( elementAttr, false ) )\r
: attribs[ attName ] == elementAttr )\r
// Get the DTD definition for the element. Defaults to "span".\r
var dtd = CKEDITOR.dtd[ elementName ] || ( isUnknownElement = true, CKEDITOR.dtd.span );\r
\r
- // Bookmark the range so we can re-select it after processing.\r
- var bookmark = range.createBookmark();\r
-\r
// Expand the range.\r
range.enlarge( CKEDITOR.ENLARGE_ELEMENT );\r
range.trim();\r
var nodeType = currentNode.type;\r
var nodeName = nodeType == CKEDITOR.NODE_ELEMENT ? currentNode.getName() : null;\r
\r
- if ( nodeName && currentNode.getAttribute( '_fck_bookmark' ) )\r
+ if ( nodeName && currentNode.getAttribute( '_cke_bookmark' ) )\r
{\r
currentNode = currentNode.getNextSourceNode( true );\r
continue;\r
if ( applyStyle && styleRange && !styleRange.collapsed )\r
{\r
// Build the style element, based on the style object definition.\r
- var styleNode = getElement( this, document );\r
+ var styleNode = getElement( this, document ),\r
+ styleHasAttrs = styleNode.hasAttributes();\r
\r
// Get the element that holds the entire range.\r
var parent = styleRange.getCommonAncestor();\r
\r
+ var removeList = {\r
+ styles : {},\r
+ attrs : {},\r
+ // Styles cannot be removed.\r
+ blockedStyles : {},\r
+ // Attrs cannot be removed.\r
+ blockedAttrs : {}\r
+ };\r
+\r
+ var attName, styleName, value;\r
+\r
// Loop through the parents, removing the redundant attributes\r
// from the element to be applied.\r
while ( styleNode && parent )\r
{\r
if ( parent.getName() == elementName )\r
{\r
- for ( var attName in def.attributes )\r
+ for ( attName in def.attributes )\r
{\r
- if ( styleNode.getAttribute( attName ) == parent.getAttribute( attName ) )\r
- styleNode.removeAttribute( attName );\r
- }\r
+ if ( removeList.blockedAttrs[ attName ] || !( value = parent.getAttribute( styleName ) ) )\r
+ continue;\r
\r
- for ( var styleName in def.styles )\r
- {\r
- if ( styleNode.getStyle( styleName ) == parent.getStyle( styleName ) )\r
- styleNode.removeStyle( styleName );\r
+ if ( styleNode.getAttribute( attName ) == value )\r
+ removeList.attrs[ attName ] = 1;\r
+ else\r
+ removeList.blockedAttrs[ attName ] = 1;\r
}\r
\r
- if ( !styleNode.hasAttributes() )\r
+ for ( styleName in def.styles )\r
{\r
- styleNode = null;\r
- break;\r
+ if ( removeList.blockedStyles[ styleName ] || !( value = parent.getStyle( styleName ) ) )\r
+ continue;\r
+\r
+ if ( styleNode.getStyle( styleName ) == value )\r
+ removeList.styles[ styleName ] = 1;\r
+ else\r
+ removeList.blockedStyles[ styleName ] = 1;\r
}\r
}\r
\r
parent = parent.getParent();\r
}\r
\r
+ for ( attName in removeList.attrs )\r
+ styleNode.removeAttribute( attName );\r
+\r
+ for ( styleName in removeList.styles )\r
+ styleNode.removeStyle( styleName );\r
+\r
+ if ( styleHasAttrs && !styleNode.hasAttributes() )\r
+ styleNode = null;\r
+\r
if ( styleNode )\r
{\r
// Move the contents of the range to the style element.\r
if ( !CKEDITOR.env.ie )\r
styleNode.$.normalize();\r
}\r
+ // Style already inherit from parents, left just to clear up any internal overrides. (#5931)\r
+ else\r
+ {\r
+ styleNode = new CKEDITOR.dom.element( 'span' );\r
+ styleRange.extractContents().appendTo( styleNode );\r
+ styleRange.insertNode( styleNode );\r
+ removeFromInsideElement( this, styleNode );\r
+ styleNode.remove( true );\r
+ }\r
\r
// Style applied, let's release the range, so it gets\r
// re-initialization in the next loop.\r
}\r
}\r
\r
- firstNode.remove();\r
- lastNode.remove();\r
- range.moveToBookmark( bookmark );\r
+ // Remove the bookmark nodes.\r
+ range.moveToBookmark( boundaryNodes );\r
+\r
// Minimize the result range to exclude empty text nodes. (#5374)\r
range.shrink( CKEDITOR.SHRINK_TEXT );\r
}\r
\r
if ( this.checkElementRemovable( element ) )\r
{\r
- var endOfElement = range.checkBoundaryOfElement( element, CKEDITOR.END ),\r
- startOfElement = !endOfElement && range.checkBoundaryOfElement( element, CKEDITOR.START );\r
- if ( startOfElement || endOfElement )\r
+ var isStart;\r
+\r
+ if ( range.collapsed && (\r
+ range.checkBoundaryOfElement( element, CKEDITOR.END ) ||\r
+ ( isStart = range.checkBoundaryOfElement( element, CKEDITOR.START ) ) ) )\r
{\r
boundaryElement = element;\r
- boundaryElement.match = startOfElement ? 'start' : 'end';\r
+ boundaryElement.match = isStart ? 'start' : 'end';\r
}\r
else\r
{\r
element && setupElement( element, this );\r
}\r
\r
+ function removeObjectStyle( range )\r
+ {\r
+ var root = range.getCommonAncestor( true, true ),\r
+ element = root.getAscendant( this.element, true );\r
+\r
+ if ( !element )\r
+ return;\r
+\r
+ var style = this;\r
+ var def = style._.definition;\r
+ var attributes = def.attributes;\r
+ var styles = CKEDITOR.style.getStyleText( def );\r
+\r
+ // Remove all defined attributes.\r
+ if ( attributes )\r
+ {\r
+ for ( var att in attributes )\r
+ {\r
+ element.removeAttribute( att, attributes[ att ] );\r
+ }\r
+ }\r
+\r
+ // Assign all defined styles.\r
+ if ( def.styles )\r
+ {\r
+ for ( var i in def.styles )\r
+ {\r
+ if ( !def.styles.hasOwnProperty( i ) )\r
+ continue;\r
+\r
+ element.removeStyle( i );\r
+ }\r
+ }\r
+ }\r
+\r
function applyBlockStyle( range )\r
{\r
// Serializible bookmarks is needed here since\r
\r
while ( ( block = iterator.getNextParagraph() ) ) // Only one =\r
{\r
- var newBlock = getElement( this, doc );\r
+ var newBlock = getElement( this, doc, block );\r
replaceBlock( block, newBlock );\r
}\r
\r
}\r
}\r
\r
+ var nonWhitespaces = CKEDITOR.dom.walker.whitespaces( true );\r
/**\r
* Merge a <pre> block with a previous sibling if available.\r
*/\r
function mergePre( preBlock )\r
{\r
var previousBlock;\r
- if ( !( ( previousBlock = preBlock.getPreviousSourceNode( true, CKEDITOR.NODE_ELEMENT ) )\r
+ if ( !( ( previousBlock = preBlock.getPrevious( nonWhitespaces ) )\r
&& previousBlock.is\r
&& previousBlock.is( 'pre') ) )\r
return;\r
{\r
// Exclude the ones at header OR at tail,\r
// and ignore bookmark content between them.\r
- var duoBrRegex = /(\S\s*)\n(?:\s|(<span[^>]+_fck_bookmark.*?\/span>))*\n(?!$)/gi,\r
+ var duoBrRegex = /(\S\s*)\n(?:\s|(<span[^>]+_cke_bookmark.*?\/span>))*\n(?!$)/gi,\r
blockName = preBlock.getName(),\r
splitedHtml = replace( preBlock.getOuterHtml(),\r
duoBrRegex,\r
var headBookmark = '',\r
tailBookmark = '';\r
\r
- str = str.replace( /(^<span[^>]+_fck_bookmark.*?\/span>)|(<span[^>]+_fck_bookmark.*?\/span>$)/gi,\r
+ str = str.replace( /(^<span[^>]+_cke_bookmark.*?\/span>)|(<span[^>]+_cke_bookmark.*?\/span>$)/gi,\r
function( str, m1, m2 ){\r
m1 && ( headBookmark = m1 );\r
m2 && ( tailBookmark = m2 );\r
}\r
}\r
\r
- function getElement( style, targetDocument )\r
+ function getElement( style, targetDocument, element )\r
{\r
var el;\r
\r
// Create the element.\r
el = new CKEDITOR.dom.element( elementName, targetDocument );\r
\r
+ // #6226: attributes should be copied before the new ones are applied\r
+ if ( element )\r
+ element.copyAttributes( el );\r
+\r
return setupElement( el, style );\r
}\r
\r
}\r
\r
// Assign all defined styles.\r
- if ( styles )\r
+ if( styles )\r
el.setAttribute( 'style', styles );\r
\r
return el;\r
return overrides;\r
}\r
\r
+ // Make the comparison of attribute value easier by standardizing it.\r
function normalizeProperty( name, value, isStyle )\r
{\r
var temp = new CKEDITOR.dom.element( 'span' );\r
return temp[ isStyle ? 'getStyle' : 'getAttribute' ]( name );\r
}\r
\r
+ // Make the comparison of style text easier by standardizing it.\r
function normalizeCssText( unparsedCssText, nativeNormalize )\r
{\r
var styleText;\r
// Compensate tail semi-colon.\r
return styleText.replace( /\s*([;:])\s*/, '$1' )\r
.replace( /([^\s;])$/, '$1;')\r
- .replace( /,\s+/g, ',' ) // Trimming spaces after comma (e.g. font-family name)(#4107).\r
+ // Trimming spaces after comma(#4107),\r
+ // remove quotations(#6403),\r
+ // mostly for differences on "font-family".\r
+ .replace( /,\s+/g, ',' )\r
+ .replace( /\"/g,'' )\r
.toLowerCase();\r
}\r
\r
return retval;\r
}\r
\r
+ /**\r
+ * Compare two bunch of styles, with the speciality that value 'inherit'\r
+ * is treated as a wildcard which will match any value.\r
+ * @param {Object|String} source\r
+ * @param {Object|String} target\r
+ */\r
function compareCssText( source, target )\r
{\r
typeof source == 'string' && ( source = parseStyleText( source ) );\r
typeof target == 'string' && ( target = parseStyleText( target ) );\r
for( var name in source )\r
{\r
- // Value 'inherit' is treated as a wildcard,\r
- // which will match any value.\r
if ( !( name in target &&\r
( target[ name ] == source[ name ]\r
|| source[ name ] == 'inherit'\r
\r
function applyStyle( document, remove )\r
{\r
- // Get all ranges from the selection.\r
- var selection = document.getSelection();\r
- var ranges = selection.getRanges();\r
- var func = remove ? this.removeFromRange : this.applyToRange;\r
-\r
- // Apply the style to the ranges.\r
- for ( var i = 0 ; i < ranges.length ; i++ )\r
- func.call( this, ranges[ i ] );\r
-\r
- // Select the ranges again.\r
- selection.selectRanges( ranges );\r
+ var selection = document.getSelection(),\r
+ // Bookmark the range so we can re-select it after processing.\r
+ bookmarks = selection.createBookmarks( 1 ),\r
+ ranges = selection.getRanges( 1 ),\r
+ func = remove ? this.removeFromRange : this.applyToRange,\r
+ range;\r
+\r
+ var iterator = ranges.createIterator();\r
+ while ( ( range = iterator.getNextRange() ) )\r
+ func.call( this, range );\r
+\r
+ if ( bookmarks.length == 1 && bookmarks[0].collapsed )\r
+ {\r
+ selection.selectRanges( ranges );\r
+ document.getById( bookmarks[ 0 ].startNode ).remove();\r
+ }\r
+ else\r
+ selection.selectBookmarks( bookmarks );\r
}\r
})();\r
\r