JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5.3
[ckeditor.git] / _source / plugins / htmldataprocessor / plugin.js
index a460bba..677d8f3 100644 (file)
@@ -80,6 +80,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
        var dtd = CKEDITOR.dtd;\r
 \r
+       // Define orders of table elements.\r
+       var tableOrder = [ 'caption', 'colgroup', 'col', 'thead', 'tfoot', 'tbody' ];\r
+\r
        // Find out the list of block-like tags that can contain <br>.\r
        var blockLikeTags = CKEDITOR.tools.extend( {}, dtd.$block, dtd.$listItem, dtd.$tableContent );\r
        for ( var i in blockLikeTags )\r
@@ -160,6 +163,17 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        return element;\r
                                },\r
 \r
+                               // The contents of table should be in correct order (#4809).\r
+                               table : function( element )\r
+                               {\r
+                                       var children = element.children;\r
+                                       children.sort( function ( node1, node2 )\r
+                                                                  {\r
+                                                                          return node1.type == CKEDITOR.NODE_ELEMENT && node2.type == node1.type ?\r
+                                                                                       CKEDITOR.tools.indexOf( tableOrder, node1.name )  > CKEDITOR.tools.indexOf( tableOrder, node2.name ) ? 1 : -1 : 0;\r
+                                                                  } );\r
+                               },\r
+\r
                                embed : function( element )\r
                                {\r
                                        var parent = element.parent;\r
@@ -238,23 +252,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        // Remove all class names starting with "cke_".\r
                                        return CKEDITOR.tools.ltrim( value.replace( /(?:^|\s+)cke_[^\s]*/g, '' ) ) || false;\r
                                }\r
-                       },\r
-\r
-                       comment : function( contents )\r
-                       {\r
-                               // If this is a comment for protected source.\r
-                               if ( contents.substr( 0, protectedSourceMarker.length ) == protectedSourceMarker )\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
                };\r
 \r
@@ -379,9 +376,24 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        });\r
        }\r
 \r
-       function protectSource( data, protectRegexes )\r
+       function unprotectSource( html, editor )\r
+       {\r
+               var store = editor._.dataStore;\r
+\r
+               return html.replace( /<!--\{cke_protected\}([\s\S]+?)-->/g, function( match, data )\r
+                       {\r
+                               return decodeURIComponent( data );\r
+                       }).replace( /\{cke_protected_(\d+)\}/g, function( match, id )\r
+                       {\r
+                               return store && store[ id ] || '';\r
+                       });\r
+       }\r
+\r
+       function protectSource( data, editor )\r
        {\r
                var protectedHtml = [],\r
+                       protectRegexes = editor.config.protectedSource,\r
+                       store = editor._.dataStore || ( editor._.dataStore = { id : 1 } ),\r
                        tempRegex = /<\!--\{cke_temp(comment)?\}(\d*?)-->/g;\r
 \r
                var regexes =\r
@@ -414,7 +426,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        return protectedHtml[ id ];\r
                                                }\r
                                        );\r
-                                       return  '<!--{cke_temp}' + ( protectedHtml.push( match ) - 1 ) + '-->';\r
+\r
+                                       // Avoid protecting over protected, e.g. /\{.*?\}/\r
+                                       return ( /cke_temp(comment)?/ ).test( match ) ? match\r
+                                               : '<!--{cke_temp}' + ( protectedHtml.push( match ) - 1 ) + '-->';\r
                                });\r
                }\r
                data = data.replace( tempRegex, function( $, isComment, id )\r
@@ -425,7 +440,17 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                '-->';\r
                        }\r
                );\r
-               return data;\r
+\r
+               // Different protection pattern is used for those that\r
+               // live in attributes to avoid from being HTML encoded.\r
+               return data.replace( /(['"]).*?\1/g, function ( match )\r
+               {\r
+                       return match.replace( /<!--\{cke_protected\}([\s\S]+?)-->/g, function( match, data )\r
+                       {\r
+                               store[ store.id ] = decodeURIComponent( data );\r
+                               return '{cke_protected_'+ ( store.id++ )  + '}';\r
+                       });\r
+               });\r
        }\r
 \r
        CKEDITOR.plugins.add( 'htmldataprocessor',\r
@@ -471,7 +496,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        // The source data is already HTML, but we need to clean\r
                        // it up and apply the filter.\r
 \r
-                       data = protectSource( data, this.editor.config.protectedSource );\r
+                       data = protectSource( data, this.editor );\r
 \r
                        // Before anything, we must protect the URL attributes as the\r
                        // browser may changing them when setting the innerHTML later in\r
@@ -533,7 +558,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                        fragment.writeHtml( writer, this.htmlFilter );\r
 \r
-                       return writer.getHtml( true );\r
+                       var data = writer.getHtml( true );\r
+\r
+                       // Restore those non-HTML protected source. (#4475,#4880)\r
+                       data = unprotectRealComments( data );\r
+                       data = unprotectSource( data, this.editor );\r
+\r
+                       return data;\r
                }\r
        };\r
 })();\r