JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5
[ckeditor.git] / _source / plugins / styles / plugin.js
index 3c6fedb..eade43b 100644 (file)
@@ -5,15 +5,25 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
 CKEDITOR.plugins.add( 'styles',\r
 {\r
-       requires : [ 'selection' ]\r
+       requires : [ 'selection' ],\r
+       init : function( editor )\r
+       {\r
+               // This doesn't look like correct, but it's the safest way to proper\r
+               // pass the disableReadonlyStyling configuration to the style system\r
+               // without having to change any method signature in the API. (#6103)\r
+               editor.on( 'contentDom', function()\r
+                       {\r
+                               editor.document.setCustomData( 'cke_includeReadonly', !editor.config.disableReadonlyStyling );\r
+                       });\r
+       }\r
 });\r
 \r
 /**\r
  * Registers a function to be called whenever a style changes its state in the\r
  * editing area. The current state is passed to the function. The possible\r
  * states are {@link CKEDITOR.TRISTATE_ON} and {@link CKEDITOR.TRISTATE_OFF}.\r
- * @param {CKEDITOR.style} The style to be watched.\r
- * @param {Function} The function to be called when the style state changes.\r
+ * @param {CKEDITOR.style} style The style to be watched.\r
+ * @param {Function} callback The function to be called when the style state changes.\r
  * @example\r
  * // Create a style object for the <b> element.\r
  * var style = new CKEDITOR.style( { element : 'b' } );\r
@@ -137,6 +147,8 @@ CKEDITOR.STYLE_OBJECT = 3;
                        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
@@ -351,6 +363,35 @@ CKEDITOR.STYLE_OBJECT = 3;
                return ( styleDefinition._ST = stylesText );\r
        };\r
 \r
+       // Gets the parent element which blocks the styling for an element. This\r
+       // can be done through read-only elements (contenteditable=false) or\r
+       // elements with the "data-cke-nostyle" attribute.\r
+       function getUnstylableParent( element )\r
+       {\r
+               var unstylable,\r
+                       editable;\r
+\r
+               while ( ( element = element.getParent() ) )\r
+               {\r
+                       if ( element.getName() == 'body' )\r
+                               break;\r
+\r
+                       if ( element.getAttribute( 'data-cke-nostyle' ) )\r
+                               unstylable = element;\r
+                       else if ( !editable )\r
+                       {\r
+                               var contentEditable = element.getAttribute( 'contentEditable' );\r
+\r
+                               if ( contentEditable == 'false' )\r
+                                       unstylable = element;\r
+                               else if ( contentEditable == 'true' )\r
+                                       editable = 1;\r
+                       }\r
+               }\r
+\r
+               return unstylable;\r
+       }\r
+\r
        function applyInlineStyle( range )\r
        {\r
                var document = range.document;\r
@@ -373,6 +414,14 @@ CKEDITOR.STYLE_OBJECT = 3;
                var def = this._.definition;\r
                var isUnknownElement;\r
 \r
+               // Indicates that fully selected read-only elements are to be included in the styling range.\r
+               var includeReadonly = def.includeReadonly;\r
+\r
+               // If the read-only inclusion is not available in the definition, try\r
+               // to get it from the document data.\r
+               if ( includeReadonly == undefined )\r
+                       includeReadonly = document.getCustomData( 'cke_includeReadonly' );\r
+\r
                // Get the DTD definition for the element. Defaults to "span".\r
                var dtd = CKEDITOR.dtd[ elementName ] || ( isUnknownElement = true, CKEDITOR.dtd.span );\r
 \r
@@ -390,6 +439,24 @@ CKEDITOR.STYLE_OBJECT = 3;
 \r
                var styleRange;\r
 \r
+               // Check if the boundaries are inside non stylable elements.\r
+               var firstUnstylable = getUnstylableParent( firstNode ),\r
+                       lastUnstylable = getUnstylableParent( lastNode );\r
+\r
+               // If the first element can't be styled, we'll start processing right\r
+               // after its unstylable root.\r
+               if ( firstUnstylable )\r
+                       currentNode = firstUnstylable.getNextSourceNode( true );\r
+\r
+               // If the last element can't be styled, we'll stop processing on its\r
+               // unstylable root.\r
+               if ( lastUnstylable )\r
+                       lastNode = lastUnstylable;\r
+\r
+               // Do nothing if the current node now follows the last node to be processed.\r
+               if ( currentNode.getPosition( lastNode ) == CKEDITOR.POSITION_FOLLOWING )\r
+                       currentNode = 0;\r
+\r
                while ( currentNode )\r
                {\r
                        var applyStyle = false;\r
@@ -403,8 +470,10 @@ CKEDITOR.STYLE_OBJECT = 3;
                        {\r
                                var nodeType = currentNode.type;\r
                                var nodeName = nodeType == CKEDITOR.NODE_ELEMENT ? currentNode.getName() : null;\r
+                               var nodeIsReadonly = nodeName && ( currentNode.getAttribute( 'contentEditable' ) == 'false' );\r
+                               var nodeIsNoStyle = nodeName && currentNode.getAttribute( 'data-cke-nostyle' );\r
 \r
-                               if ( nodeName && currentNode.getAttribute( '_fck_bookmark' ) )\r
+                               if ( nodeName && currentNode.data( 'cke-bookmark' ) )\r
                                {\r
                                        currentNode = currentNode.getNextSourceNode( true );\r
                                        continue;\r
@@ -412,6 +481,8 @@ CKEDITOR.STYLE_OBJECT = 3;
 \r
                                // Check if the current node can be a child of the style element.\r
                                if ( !nodeName || ( dtd[ nodeName ]\r
+                                       && !nodeIsNoStyle\r
+                                       && ( !nodeIsReadonly || includeReadonly )\r
                                        && ( currentNode.getPosition( lastNode ) | CKEDITOR.POSITION_PRECEDING | CKEDITOR.POSITION_IDENTICAL | CKEDITOR.POSITION_IS_CONTAINED ) == ( CKEDITOR.POSITION_PRECEDING + CKEDITOR.POSITION_IDENTICAL + CKEDITOR.POSITION_IS_CONTAINED )\r
                                        && ( !def.childRule || def.childRule( currentNode ) ) ) )\r
                                {\r
@@ -433,9 +504,9 @@ CKEDITOR.STYLE_OBJECT = 3;
                                                        styleRange.setStartBefore( currentNode );\r
                                                }\r
 \r
-                                               // Non element nodes, or empty elements can be added\r
-                                               // completely to the range.\r
-                                               if ( nodeType == CKEDITOR.NODE_TEXT || ( nodeType == CKEDITOR.NODE_ELEMENT && !currentNode.getChildCount() ) )\r
+                                               // Non element nodes, readonly elements, or empty\r
+                                               // elements can be added completely to the range.\r
+                                               if ( nodeType == CKEDITOR.NODE_TEXT || nodeIsReadonly || ( nodeType == CKEDITOR.NODE_ELEMENT && !currentNode.getChildCount() ) )\r
                                                {\r
                                                        var includedNode = currentNode;\r
                                                        var parentNode;\r
@@ -459,7 +530,6 @@ CKEDITOR.STYLE_OBJECT = 3;
                                                        // in this style DTD, so apply the style immediately.\r
                                                        if ( !includedNode.$.nextSibling )\r
                                                                applyStyle = true;\r
-\r
                                                }\r
                                        }\r
                                        else\r
@@ -469,46 +539,71 @@ CKEDITOR.STYLE_OBJECT = 3;
                                        applyStyle = true;\r
 \r
                                // Get the next node to be processed.\r
-                               currentNode = currentNode.getNextSourceNode();\r
+                               currentNode = currentNode.getNextSourceNode( nodeIsNoStyle || nodeIsReadonly );\r
                        }\r
 \r
                        // Apply the style if we have something to which apply it.\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
@@ -534,6 +629,15 @@ CKEDITOR.STYLE_OBJECT = 3;
                                        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
@@ -719,6 +823,41 @@ CKEDITOR.STYLE_OBJECT = 3;
                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
@@ -738,7 +877,7 @@ CKEDITOR.STYLE_OBJECT = 3;
 \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
@@ -812,7 +951,7 @@ CKEDITOR.STYLE_OBJECT = 3;
        {\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[^>]+data-cke-bookmark.*?\/span>))*\n(?!$)/gi,\r
                        blockName = preBlock.getName(),\r
                        splitedHtml = replace( preBlock.getOuterHtml(),\r
                                duoBrRegex,\r
@@ -834,7 +973,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                var headBookmark = '',\r
                        tailBookmark = '';\r
 \r
-               str = str.replace( /(^<span[^>]+_fck_bookmark.*?\/span>)|(<span[^>]+_fck_bookmark.*?\/span>$)/gi,\r
+               str = str.replace( /(^<span[^>]+data-cke-bookmark.*?\/span>)|(<span[^>]+data-cke-bookmark.*?\/span>$)/gi,\r
                        function( str, m1, m2 ){\r
                                        m1 && ( headBookmark = m1 );\r
                                        m2 && ( tailBookmark = m2 );\r
@@ -1046,7 +1185,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                }\r
        }\r
 \r
-       function getElement( style, targetDocument )\r
+       function getElement( style, targetDocument, element )\r
        {\r
                var el;\r
 \r
@@ -1061,6 +1200,10 @@ CKEDITOR.STYLE_OBJECT = 3;
                // 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
@@ -1080,7 +1223,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                }\r
 \r
                // Assign all defined styles.\r
-               if ( styles )\r
+               if( styles )\r
                        el.setAttribute( 'style', styles );\r
 \r
                return el;\r
@@ -1231,7 +1374,11 @@ CKEDITOR.STYLE_OBJECT = 3;
                // 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
@@ -1275,8 +1422,8 @@ CKEDITOR.STYLE_OBJECT = 3;
        {\r
                var selection = document.getSelection(),\r
                        // Bookmark the range so we can re-select it after processing.\r
-                       bookmarks = selection.createBookmarks(),\r
-                       ranges = selection.getRanges( true ),\r
+                       bookmarks = selection.createBookmarks( 1 ),\r
+                       ranges = selection.getRanges(),\r
                        func = remove ? this.removeFromRange : this.applyToRange,\r
                        range;\r
 \r
@@ -1287,7 +1434,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                if ( bookmarks.length == 1 && bookmarks[0].collapsed )\r
                {\r
                        selection.selectRanges( ranges );\r
-                       bookmarks[0].startNode.remove();\r
+                       document.getById( bookmarks[ 0 ].startNode ).remove();\r
                }\r
                else\r
                        selection.selectBookmarks( bookmarks );\r
@@ -1329,7 +1476,7 @@ CKEDITOR.loadStylesSet = function( name, url, callback )
 \r
 /**\r
  * Gets the current styleSet for this instance\r
- * @param {Function} The function to be called with the styles data.\r
+ * @param {Function} callback The function to be called with the styles data.\r
  * @example\r
  * editor.getStylesSet( function( stylesDefinitions ) {} );\r
  */\r
@@ -1370,6 +1517,23 @@ CKEDITOR.editor.prototype.getStylesSet = function( callback )
 };\r
 \r
 /**\r
+ * Indicates that fully selected read-only elements will be included when\r
+ * applying the style (for inline styles only).\r
+ * @name CKEDITOR.style.includeReadonly\r
+ * @type Boolean\r
+ * @default false\r
+ * @since 3.5\r
+ */\r
+\r
+ /**\r
+  * Disables inline styling on read-only elements.\r
+  * @name CKEDITOR.config.disableReadonlyStyling\r
+  * @type Boolean\r
+  * @default false\r
+  * @since 3.5\r
+  */\r
+\r
+/**\r
  * The "styles definition set" to use in the editor. They will be used in the\r
  * styles combo and the Style selector of the div container. <br>\r
  * The styles may be defined in the page containing the editor, or can be\r