JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5.3
[ckeditor.git] / _source / plugins / styles / plugin.js
index 40dc6d1..9a4f13d 100644 (file)
@@ -19,11 +19,11 @@ CKEDITOR.plugins.add( 'styles',
 });\r
 \r
 /**\r
- * Registers a function to be called whenever a style changes its state in the\r
+ * Registers a function to be called whenever the selection position changes 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} style The style to be watched.\r
- * @param {Function} callback The function to be called when the style state changes.\r
+ * @param {Function} callback The function to be called.\r
  * @example\r
  * // Create a style object for the <b> element.\r
  * var style = new CKEDITOR.style( { element : 'b' } );\r
@@ -61,17 +61,9 @@ CKEDITOR.editor.prototype.attachStyleStateChange = function( style, callback )
                                        // callback.\r
                                        var currentState = callback.style.checkActive( ev.data.path ) ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF;\r
 \r
-                                       // If the state changed since the last check.\r
-                                       if ( callback.state !== currentState )\r
-                                       {\r
-                                               // Call the callback function, passing the current\r
-                                               // state to it.\r
-                                               callback.fn.call( this, currentState );\r
-\r
-                                               // Save the current state, so it can be compared next\r
-                                               // time.\r
-                                               callback.state = currentState;\r
-                                       }\r
+                                       // Call the callback function, passing the current\r
+                                       // state to it.\r
+                                       callback.fn.call( this, currentState );\r
                                }\r
                        });\r
        }\r
@@ -87,12 +79,14 @@ CKEDITOR.STYLE_OBJECT = 3;
 \r
 (function()\r
 {\r
-       var blockElements       = { address:1,div:1,h1:1,h2:1,h3:1,h4:1,h5:1,h6:1,p:1,pre:1 };\r
-       var objectElements      = { a:1,embed:1,hr:1,img:1,li:1,object:1,ol:1,table:1,td:1,tr:1,th:1,ul:1,dl:1,dt:1,dd:1,form:1};\r
+       var blockElements       = { address:1,div:1,h1:1,h2:1,h3:1,h4:1,h5:1,h6:1,p:1,pre:1 },\r
+               objectElements  = { a:1,embed:1,hr:1,img:1,li:1,object:1,ol:1,table:1,td:1,tr:1,th:1,ul:1,dl:1,dt:1,dd:1,form:1};\r
 \r
-       var semicolonFixRegex = /\s*(?:;\s*|$)/;\r
+       var semicolonFixRegex = /\s*(?:;\s*|$)/,\r
+               varRegex = /#\((.+?)\)/g;\r
 \r
-       var notBookmark = CKEDITOR.dom.walker.bookmark( 0, 1 );\r
+       var notBookmark = CKEDITOR.dom.walker.bookmark( 0, 1 ),\r
+               nonWhitespaces = CKEDITOR.dom.walker.whitespaces( 1 );\r
 \r
        CKEDITOR.style = function( styleDefinition, variablesValues )\r
        {\r
@@ -107,7 +101,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                var element = this.element = ( styleDefinition.element || '*' ).toLowerCase();\r
 \r
                this.type =\r
-                       ( element == '#' || blockElements[ element ] ) ?\r
+                       blockElements[ element ] ?\r
                                CKEDITOR.STYLE_BLOCK\r
                        : objectElements[ element ] ?\r
                                CKEDITOR.STYLE_OBJECT\r
@@ -703,8 +697,10 @@ CKEDITOR.STYLE_OBJECT = 3;
                                                 * them before removal.\r
                                                 */\r
                                                element.mergeSiblings();\r
-                                               removeFromElement( this, element );\r
-\r
+                                               if ( element.getName() == this.element )\r
+                                                       removeFromElement( this, element );\r
+                                               else\r
+                                                       removeOverrides( element, getOverrides( this )[ element.getName() ] );\r
                                        }\r
                                }\r
                        }\r
@@ -813,26 +809,26 @@ CKEDITOR.STYLE_OBJECT = 3;
                }\r
 \r
                range.moveToBookmark( bookmark );\r
-}\r
+       }\r
 \r
        function applyObjectStyle( range )\r
        {\r
                var root = range.getCommonAncestor( true, true ),\r
-                               element = root.getAscendant( this.element, true );\r
+                       element = root.getAscendant( this.element, true );\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
+                       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 style = this,\r
+                       def = style._.definition,\r
+                       attributes = def.attributes;\r
                var styles = CKEDITOR.style.getStyleText( def );\r
 \r
                // Remove all defined attributes.\r
@@ -956,7 +952,6 @@ CKEDITOR.STYLE_OBJECT = 3;
                        removeNoAttribsElement( newBlock );\r
        }\r
 \r
-       var nonWhitespaces = CKEDITOR.dom.walker.whitespaces( 1 );\r
        /**\r
         * Merge a <pre> block with a previous sibling if available.\r
         */\r
@@ -1025,6 +1020,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                        } );\r
                return headBookmark + str.replace( regexp, replacement ) + tailBookmark;\r
        }\r
+\r
        /**\r
         * Converting a list of <pre> into blocks with format well preserved.\r
         */\r
@@ -1160,9 +1156,8 @@ CKEDITOR.STYLE_OBJECT = 3;
                var def = style._.definition,\r
                        attribs = def.attributes,\r
                        styles = def.styles,\r
-                       overrides = getOverrides( style );\r
-\r
-               var innerElements = element.getElementsByTag( style.element );\r
+                       overrides = getOverrides( style ),\r
+                       innerElements = element.getElementsByTag( style.element );\r
 \r
                for ( var i = innerElements.count(); --i >= 0 ; )\r
                        removeFromElement( style,  innerElements.getItem( i ) );\r
@@ -1181,7 +1176,6 @@ CKEDITOR.STYLE_OBJECT = 3;
                                }\r
                        }\r
                }\r
-\r
        }\r
 \r
        /**\r
@@ -1265,11 +1259,9 @@ CKEDITOR.STYLE_OBJECT = 3;
 \r
        function getElement( style, targetDocument, element )\r
        {\r
-               var el;\r
-\r
-               var def = style._.definition;\r
-\r
-               var elementName = style.element;\r
+               var el,\r
+                       def = style._.definition,\r
+                       elementName = style.element;\r
 \r
                // The "*" element name will always be a span for this function.\r
                if ( elementName == '*' )\r
@@ -1295,9 +1287,9 @@ CKEDITOR.STYLE_OBJECT = 3;
 \r
        function setupElement( el, style )\r
        {\r
-               var def = style._.definition;\r
-               var attributes = def.attributes;\r
-               var styles = CKEDITOR.style.getStyleText( def );\r
+               var def = style._.definition,\r
+                       attributes = def.attributes,\r
+                       styles = CKEDITOR.style.getStyleText( def );\r
 \r
                // Assign all defined attributes.\r
                if ( attributes )\r
@@ -1315,7 +1307,6 @@ CKEDITOR.STYLE_OBJECT = 3;
                return el;\r
        }\r
 \r
-       var varRegex = /#\((.+?)\)/g;\r
        function replaceVariables( list, variablesValues )\r
        {\r
                for ( var item in list )\r
@@ -1327,7 +1318,6 @@ CKEDITOR.STYLE_OBJECT = 3;
                }\r
        }\r
 \r
-\r
        // Returns an object that can be used for style matching comparison.\r
        // Attributes names and values are all lowercased, and the styles get\r
        // merged with the style attribute.\r
@@ -1543,6 +1533,37 @@ CKEDITOR.styleCommand.prototype.exec = function( editor )
        return !!doc;\r
 };\r
 \r
+/**\r
+ * Manages styles registration and loading. See also {@link CKEDITOR.config.stylesSet}.\r
+ * @namespace\r
+ * @augments CKEDITOR.resourceManager\r
+ * @constructor\r
+ * @since 3.2\r
+ * @example\r
+ * // The set of styles for the <b>Styles</b> combo\r
+ * CKEDITOR.stylesSet.add( 'default',\r
+ * [\r
+ *     // Block Styles\r
+ *     { name : 'Blue Title'           , element : 'h3', styles : { 'color' : 'Blue' } },\r
+ *     { name : 'Red Title'            , element : 'h3', styles : { 'color' : 'Red' } },\r
+ *\r
+ *     // Inline Styles\r
+ *     { name : 'Marker: Yellow'       , element : 'span', styles : { 'background-color' : 'Yellow' } },\r
+ *     { name : 'Marker: Green'        , element : 'span', styles : { 'background-color' : 'Lime' } },\r
+ *\r
+ *     // Object Styles\r
+ *     {\r
+ *             name : 'Image on Left',\r
+ *             element : 'img',\r
+ *             attributes :\r
+ *             {\r
+ *                     'style' : 'padding: 5px; margin-right: 5px',\r
+ *                     'border' : '2',\r
+ *                     'align' : 'left'\r
+ *             }\r
+ *     }\r
+ * ]);\r
+ */\r
 CKEDITOR.stylesSet = new CKEDITOR.resourceManager( '', 'stylesSet' );\r
 \r
 // Backward compatibility (#5025).\r