JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.1
[ckeditor.git] / _source / plugins / htmldataprocessor / plugin.js
index 01be358..47bdb7c 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -11,7 +11,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
        var protectedSourceMarker = '{cke_protected}';\r
 \r
-\r
        // Return the last non-space child node of the block (#4344).\r
        function lastNoneSpaceChild( block )\r
        {\r
@@ -110,18 +109,24 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                [ ( /^_cke_(saved|pa)_/ ), '' ],\r
 \r
                                // All "_cke" attributes are to be ignored.\r
-                               [ ( /^_cke.*/ ), '' ]\r
+                               [ ( /^_cke.*/ ), '' ],\r
+\r
+                               [ 'hidefocus', '' ]\r
                        ],\r
 \r
                        elements :\r
                        {\r
                                $ : function( element )\r
                                {\r
-                                       // Remove duplicated attributes - #3789.\r
                                        var attribs = element.attributes;\r
 \r
                                        if ( attribs )\r
                                        {\r
+                                               // Elements marked as temporary are to be ignored.\r
+                                               if ( attribs.cke_temp )\r
+                                                       return false;\r
+\r
+                                               // Remove duplicated attributes - #3789.\r
                                                var attributeNames = [ 'name', 'href', 'src' ],\r
                                                        savedAttributeName;\r
                                                for ( var i = 0 ; i < attributeNames.length ; i++ )\r
@@ -130,6 +135,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        savedAttributeName in attribs && ( delete attribs[ attributeNames[ i ] ] );\r
                                                }\r
                                        }\r
+\r
+                                       return element;\r
                                },\r
 \r
                                embed : function( element )\r
@@ -163,6 +170,21 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        {\r
                                                return false;\r
                                        }\r
+                               },\r
+\r
+                               body : function( element )\r
+                               {\r
+                                       delete element.attributes.spellcheck;\r
+                                       delete element.attributes.contenteditable;\r
+                               },\r
+\r
+                               style : function( element )\r
+                               {\r
+                                       var child = element.children[ 0 ];\r
+                                       child && child.value && ( child.value = CKEDITOR.tools.trim( child.value ));\r
+\r
+                                       if ( !element.attributes.type )\r
+                                               element.attributes.type = 'text/css';\r
                                }\r
                        },\r
 \r
@@ -177,8 +199,17 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                        comment : function( contents )\r
                        {\r
+                               // If this is a comment for protected source.\r
                                if ( contents.substr( 0, protectedSourceMarker.length ) == protectedSourceMarker )\r
-                                       return new CKEDITOR.htmlParser.cdata( decodeURIComponent( contents.substr( protectedSourceMarker.length ) ) );\r
+                               {\r
+                                       // Remove the extra marker for real comments from it.\r
+                                       if ( contents.substr( protectedSourceMarker.length, 3 ) == '{C}' )\r
+                                               contents = contents.substr( protectedSourceMarker.length + 3 );\r
+                                       else\r
+                                               contents = contents.substr( protectedSourceMarker.length );\r
+\r
+                                       return new CKEDITOR.htmlParser.cdata( decodeURIComponent( contents ) );\r
+                               }\r
 \r
                                return contents;\r
                        }\r
@@ -201,69 +232,100 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
        var protectAttributeRegex = /<(?:a|area|img|input)[\s\S]*?\s((?:href|src|name)\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|(?:[^ "'>]+)))/gi;\r
 \r
+       var protectElementsRegex = /(?:<style(?=[ >])[^>]*>[\s\S]*<\/style>)|(?:<(:?link|meta|base)[^>]*>)/gi,\r
+               encodedElementsRegex = /<cke:encoded>([^<]*)<\/cke:encoded>/gi;\r
+\r
+       var protectElementNamesRegex = /(<\/?)((?:object|embed|param|html|body|head|title)[^>]*>)/gi,\r
+               unprotectElementNamesRegex = /(<\/?)cke:((?:html|body|head|title)[^>]*>)/gi;\r
+\r
+       var protectSelfClosingRegex = /<cke:(param|embed)([^>]*?)\/?>(?!\s*<\/cke:\1)/gi;\r
+\r
        function protectAttributes( html )\r
        {\r
                return html.replace( protectAttributeRegex, '$& _cke_saved_$1' );\r
        }\r
 \r
-       var protectStyleTagsRegex = /<(style)(?=[ >])[^>]*>[^<]*<\/\1>/gi;\r
-       var encodedTagsRegex = /<cke:encoded>([^<]*)<\/cke:encoded>/gi;\r
-       var protectElementNamesRegex = /(<\/?)((?:object|embed|param)[\s\S]*?>)/gi;\r
-       var protectSelfClosingRegex = /<cke:(param|embed)([\s\S]*?)\/?>/gi;\r
-\r
-       function protectStyleTagsMatch( match )\r
+       function protectElements( html )\r
        {\r
-               return '<cke:encoded>' + encodeURIComponent( match ) + '</cke:encoded>';\r
+               return html.replace( protectElementsRegex, function( match )\r
+                       {\r
+                               return '<cke:encoded>' + encodeURIComponent( match ) + '</cke:encoded>';\r
+                       });\r
        }\r
 \r
-       function protectStyleTags( html )\r
+       function unprotectElements( html )\r
        {\r
-               return html.replace( protectStyleTagsRegex, protectStyleTagsMatch );\r
+               return html.replace( encodedElementsRegex, function( match, encoded )\r
+                       {\r
+                               return decodeURIComponent( encoded );\r
+                       });\r
        }\r
+\r
        function protectElementsNames( html )\r
        {\r
                return html.replace( protectElementNamesRegex, '$1cke:$2');\r
        }\r
+\r
+       function unprotectElementNames( html )\r
+       {\r
+               return html.replace( unprotectElementNamesRegex, '$1$2' );\r
+       }\r
+\r
        function protectSelfClosingElements( html )\r
        {\r
                return html.replace( protectSelfClosingRegex, '<cke:$1$2></cke:$1>' );\r
        }\r
 \r
-       function unprotectEncodedTagsMatch( match, encoded )\r
+       function protectRealComments( html )\r
        {\r
-               return decodeURIComponent( encoded );\r
+               return html.replace( /<!--(?!{cke_protected})[\s\S]+?-->/g, function( match )\r
+                       {\r
+                               return '<!--' + protectedSourceMarker +\r
+                                               '{C}' +\r
+                                               encodeURIComponent( match ).replace( /--/g, '%2D%2D' ) +\r
+                                               '-->';\r
+                       });\r
        }\r
 \r
-       function unprotectEncodedTags( html )\r
+       function unprotectRealComments( html )\r
        {\r
-               return html.replace( encodedTagsRegex, unprotectEncodedTagsMatch );\r
+               return html.replace( /<!--\{cke_protected\}\{C\}([\s\S]+?)-->/g, function( match, data )\r
+                       {\r
+                               return decodeURIComponent( data );\r
+                       });\r
        }\r
 \r
        function protectSource( data, protectRegexes )\r
        {\r
                var protectedHtml = [],\r
-                       tempRegex = /<\!--\{cke_temp\}(\d*?)-->/g;\r
+                       tempRegex = /<\!--\{cke_temp(comment)?\}(\d*?)-->/g;\r
+\r
                var regexes =\r
                        [\r
-                               // First of any other protection, we must protect all comments\r
-                               // to avoid loosing them (of course, IE related).\r
-                               (/<!--[\s\S]*?-->/g),\r
-\r
                                // Script tags will also be forced to be protected, otherwise\r
                                // IE will execute them.\r
-                               /<script[\s\S]*?<\/script>/gi,\r
+                               ( /<script[\s\S]*?<\/script>/gi ),\r
 \r
                                // <noscript> tags (get lost in IE and messed up in FF).\r
                                /<noscript[\s\S]*?<\/noscript>/gi\r
                        ]\r
                        .concat( protectRegexes );\r
 \r
+               // First of any other protection, we must protect all comments\r
+               // to avoid loosing them (of course, IE related).\r
+               // Note that we use a different tag for comments, as we need to\r
+               // transform them when applying filters.\r
+               data = data.replace( (/<!--[\s\S]*?-->/g), function( match )\r
+                       {\r
+                               return  '<!--{cke_tempcomment}' + ( protectedHtml.push( match ) - 1 ) + '-->';\r
+                       });\r
+\r
                for ( var i = 0 ; i < regexes.length ; i++ )\r
                {\r
                        data = data.replace( regexes[i], function( match )\r
                                {\r
                                        match = match.replace( tempRegex,               // There could be protected source inside another one. (#3869).\r
-                                               function( $, id )\r
+                                               function( $, isComment, id )\r
                                                {\r
                                                        return protectedHtml[ id ];\r
                                                }\r
@@ -271,9 +333,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        return  '<!--{cke_temp}' + ( protectedHtml.push( match ) - 1 ) + '-->';\r
                                });\r
                }\r
-               data = data.replace( tempRegex, function( $, id )\r
+               data = data.replace( tempRegex, function( $, isComment, id )\r
                        {\r
                                return '<!--' + protectedSourceMarker +\r
+                                               ( isComment ? '{C}' : '' ) +\r
                                                encodeURIComponent( protectedHtml[ id ] ).replace( /--/g, '%2D%2D' ) +\r
                                                '-->';\r
                        }\r
@@ -321,27 +384,33 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        // the code.\r
                        data = protectAttributes( data );\r
 \r
-                       // IE remvoes style tags from innerHTML. (#3710).\r
-                       if ( CKEDITOR.env.ie )\r
-                               data = protectStyleTags( data );\r
+                       // Protect elements than can't be set inside a DIV. E.g. IE removes\r
+                       // style tags from innerHTML. (#3710)\r
+                       data = protectElements( data );\r
 \r
                        // Certain elements has problem to go through DOM operation, protect\r
-                       // them by prefixing 'cke' namespace.(#3591)\r
+                       // them by prefixing 'cke' namespace. (#3591)\r
                        data = protectElementsNames( data );\r
 \r
                        // All none-IE browsers ignore self-closed custom elements,\r
-                       // protecting them into open-close.(#3591)\r
+                       // protecting them into open-close. (#3591)\r
                        data = protectSelfClosingElements( data );\r
 \r
                        // Call the browser to help us fixing a possibly invalid HTML\r
                        // structure.\r
-                       var div = document.createElement( 'div' );\r
+                       var div = new CKEDITOR.dom.element( 'div' );\r
                        // Add fake character to workaround IE comments bug. (#3801)\r
-                       div.innerHTML = 'a' + data;\r
-                       data = div.innerHTML.substr( 1 );\r
+                       div.setHtml( 'a' + data );\r
+                       data = div.getHtml().substr( 1 );\r
 \r
-                       if ( CKEDITOR.env.ie )\r
-                               data = unprotectEncodedTags( data );\r
+                       // Unprotect "some" of the protected elements at this point.\r
+                       data = unprotectElementNames( data );\r
+\r
+                       data = unprotectElements( data );\r
+\r
+                       // Restore the comments that have been protected, in this way they\r
+                       // can be properly filtered.\r
+                       data = unprotectRealComments( data );\r
 \r
                        // Now use our parser to make further fixes to the structure, as\r
                        // well as apply the filter.\r
@@ -349,8 +418,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                writer = new CKEDITOR.htmlParser.basicWriter();\r
 \r
                        fragment.writeHtml( writer, this.dataFilter );\r
+                       data = writer.getHtml( true );\r
 \r
-                       return writer.getHtml( true );\r
+                       // Protect the real comments again.\r
+                       data = protectRealComments( data );\r
+\r
+                       return data;\r
                },\r
 \r
                toDataFormat : function( html, fixForBody )\r