JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4.1
[ckeditor.git] / _source / plugins / styles / plugin.js
index 65a36c2..8bab79d 100644 (file)
@@ -60,13 +60,13 @@ CKEDITOR.editor.prototype.attachStyleStateChange = function( style, callback )
 \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
@@ -137,6 +137,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
@@ -180,6 +182,10 @@ CKEDITOR.STYLE_OBJECT = 3;
                        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
@@ -222,6 +228,8 @@ CKEDITOR.STYLE_OBJECT = 3;
                                                        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
@@ -370,48 +378,15 @@ CKEDITOR.STYLE_OBJECT = 3;
                // 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
 \r
                // Get the first node to be processed and the last, which concludes the\r
                // processing.\r
-               var boundaryNodes = range.getBoundaryNodes();\r
-               var firstNode = boundaryNodes.startNode;\r
-               var lastNode = boundaryNodes.endNode.getNextSourceNode( true );\r
-\r
-               // Probably the document end is reached, we need a marker node.\r
-               if ( !lastNode )\r
-               {\r
-                               var marker;\r
-                               lastNode = marker = document.createText( '' );\r
-                               lastNode.insertAfter( range.endContainer );\r
-               }\r
-               // The detection algorithm below skips the contents inside bookmark nodes, so\r
-               // we'll need to make sure lastNode isn't the   inside a bookmark node.\r
-               var lastParent = lastNode.getParent();\r
-               if ( lastParent && lastParent.getAttribute( '_fck_bookmark' ) )\r
-                       lastNode = lastParent;\r
-\r
-               if ( lastNode.equals( firstNode ) )\r
-               {\r
-                       // If the last node is the same as the the first one, we must move\r
-                       // it to the next one, otherwise the first one will not be\r
-                       // processed.\r
-                       lastNode = lastNode.getNextSourceNode( true );\r
-\r
-                       // It may happen that there are no more nodes after it (the end of\r
-                       // the document), so we must add something there to make our code\r
-                       // simpler.\r
-                       if ( !lastNode )\r
-                       {\r
-                               lastNode = marker = document.createText( '' );\r
-                               lastNode.insertAfter( firstNode );\r
-                       }\r
-               }\r
+               var boundaryNodes = range.createBookmark(),\r
+                       firstNode = boundaryNodes.startNode,\r
+                       lastNode = boundaryNodes.endNode;\r
 \r
                var currentNode = firstNode;\r
 \r
@@ -431,7 +406,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                                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
@@ -550,7 +525,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                                        styleRange.insertNode( styleNode );\r
 \r
                                        // Let's merge our new style with its neighbors, if possible.\r
-                                       mergeSiblings( styleNode );\r
+                                       styleNode.mergeSiblings();\r
 \r
                                        // As the style system breaks text nodes constantly, let's normalize\r
                                        // things for performance.\r
@@ -568,9 +543,9 @@ CKEDITOR.STYLE_OBJECT = 3;
                        }\r
                }\r
 \r
-               // Remove the temporary marking node.(#4111)\r
-               marker && marker.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
@@ -609,12 +584,14 @@ CKEDITOR.STYLE_OBJECT = 3;
 \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
@@ -624,7 +601,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                                                 * no difference that they're separate entities in the DOM tree. So, merge\r
                                                 * them before removal.\r
                                                 */\r
-                                               mergeSiblings( element );\r
+                                               element.mergeSiblings();\r
                                                removeFromElement( this, element );\r
 \r
                                        }\r
@@ -744,6 +721,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
@@ -753,6 +765,10 @@ CKEDITOR.STYLE_OBJECT = 3;
                var iterator = range.createIterator();\r
                iterator.enforceRealBlocks = true;\r
 \r
+               // make recognize <br /> tag as a separator in ENTER_BR mode (#5121)\r
+               if ( this._.enterMode )\r
+                       iterator.enlargeBr = ( this._.enterMode != CKEDITOR.ENTER_BR );\r
+\r
                var block;\r
                var doc = range.document;\r
                var previousPreBlock;\r
@@ -794,13 +810,14 @@ CKEDITOR.STYLE_OBJECT = 3;
                }\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
@@ -832,7 +849,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[^>]+_cke_bookmark.*?\/span>))*\n(?!$)/gi,\r
                        blockName = preBlock.getName(),\r
                        splitedHtml = replace( preBlock.getOuterHtml(),\r
                                duoBrRegex,\r
@@ -854,7 +871,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[^>]+_cke_bookmark.*?\/span>)|(<span[^>]+_cke_bookmark.*?\/span>$)/gi,\r
                        function( str, m1, m2 ){\r
                                        m1 && ( headBookmark = m1 );\r
                                        m2 && ( tailBookmark = m2 );\r
@@ -1057,47 +1074,11 @@ CKEDITOR.STYLE_OBJECT = 3;
                        if ( firstChild )\r
                        {\r
                                // Check the cached nodes for merging.\r
-                               mergeSiblings( firstChild );\r
-\r
-                               if ( lastChild && !firstChild.equals( lastChild ) )\r
-                                       mergeSiblings( lastChild );\r
-                       }\r
-               }\r
-       }\r
-\r
-       function mergeSiblings( element )\r
-       {\r
-               if ( !element || element.type != CKEDITOR.NODE_ELEMENT || !CKEDITOR.dtd.$removeEmpty[ element.getName() ] )\r
-                       return;\r
-\r
-               mergeElements( element, element.getNext(), true );\r
-               mergeElements( element, element.getPrevious() );\r
-       }\r
-\r
-       function mergeElements( element, sibling, isNext )\r
-       {\r
-               if ( sibling && sibling.type == CKEDITOR.NODE_ELEMENT )\r
-               {\r
-                       var hasBookmark = sibling.getAttribute( '_fck_bookmark' );\r
+                               firstChild.type == CKEDITOR.NODE_ELEMENT && firstChild.mergeSiblings();\r
 \r
-                       if ( hasBookmark )\r
-                               sibling = isNext ? sibling.getNext() : sibling.getPrevious();\r
-\r
-                       if ( sibling && sibling.type == CKEDITOR.NODE_ELEMENT && element.isIdentical( sibling ) )\r
-                       {\r
-                               // Save the last child to be checked too, to merge things like\r
-                               // <b><i></i></b><b><i></i></b> => <b><i></i></b>\r
-                               var innerSibling = isNext ? element.getLast() : element.getFirst();\r
-\r
-                               if ( hasBookmark )\r
-                                       ( isNext ? sibling.getPrevious() : sibling.getNext() ).move( element, !isNext );\r
-\r
-                               sibling.moveChildren( element, !isNext );\r
-                               sibling.remove();\r
-\r
-                               // Now check the last inner child (see two comments above).\r
-                               if ( innerSibling )\r
-                                       mergeSiblings( innerSibling );\r
+                               if ( lastChild && !firstChild.equals( lastChild )\r
+                                       && lastChild.type == CKEDITOR.NODE_ELEMENT  )\r
+                                       lastChild.mergeSiblings();\r
                        }\r
                }\r
        }\r
@@ -1136,8 +1117,16 @@ CKEDITOR.STYLE_OBJECT = 3;
                }\r
 \r
                // Assign all defined styles.\r
-               if ( styles )\r
-                       el.setAttribute( 'style', styles );\r
+               if ( def.styles )\r
+               {\r
+                       for ( var i in def.styles )\r
+                       {\r
+                               if ( !def.styles.hasOwnProperty( i ) )\r
+                                       continue;\r
+\r
+                               el.setStyle( i, def.styles[ i ] );\r
+                       }\r
+               }\r
 \r
                return el;\r
        }\r
@@ -1260,6 +1249,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                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
@@ -1267,6 +1257,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                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
@@ -1302,14 +1293,18 @@ CKEDITOR.STYLE_OBJECT = 3;
                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
@@ -1323,17 +1318,24 @@ CKEDITOR.STYLE_OBJECT = 3;
 \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(),\r
+                       ranges = selection.getRanges( true ),\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
+                       bookmarks[0].startNode.remove();\r
+               }\r
+               else\r
+                       selection.selectBookmarks( bookmarks );\r
        }\r
 })();\r
 \r
@@ -1422,6 +1424,7 @@ CKEDITOR.editor.prototype.getStylesSet = function( callback )
  * Otherwise, this setting has the "name:url" syntax, making it\r
  * possible to set the URL from which loading the styles file.<br>\r
  * Previously this setting was available as config.stylesCombo_stylesSet<br>\r
+ * @name CKEDITOR.config.stylesSet\r
  * @type String|Array\r
  * @default 'default'\r
  * @since 3.3\r