JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4b
[ckeditor.git] / _source / plugins / styles / plugin.js
index 2d9878b..3c6fedb 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
@@ -180,6 +180,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 +226,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,9 +376,6 @@ 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
@@ -538,9 +541,9 @@ CKEDITOR.STYLE_OBJECT = 3;
                        }\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
@@ -579,12 +582,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
@@ -768,13 +773,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
@@ -1198,6 +1204,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
@@ -1205,6 +1212,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
@@ -1240,14 +1248,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
@@ -1261,17 +1273,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