Index: break.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/wysiwyg/plugins/break/break.js,v
retrieving revision 1.6
diff -u -p -r1.6 break.js
--- break.js	13 Jun 2009 01:14:43 -0000	1.6
+++ break.js	21 Dec 2009 21:58:22 -0000
@@ -38,7 +38,9 @@ Drupal.wysiwyg.plugins['break'] = {
    * Replace all <!--break--> tags with images.
    */
   attach: function(content, settings, instanceId) {
+//    console.log("Plugin in\n"+content);
     content = content.replace(/<!--break-->/g, this._getPlaceholder(settings));
+//    console.log("Plugin out\n"+content);
     return content;
   },
 
@@ -46,6 +48,7 @@ Drupal.wysiwyg.plugins['break'] = {
    * Replace images with <!--break--> tags in content upon detaching editor.
    */
   detach: function(content, settings, instanceId) {
+    /*
     var $content = $('<div>' + content + '</div>'); // No .outerHTML() in jQuery :(
     // #404532: document.createComment() required or IE will strip the comment.
     // #474908: IE 8 breaks when using jQuery methods to replace the elements.
@@ -55,12 +58,33 @@ Drupal.wysiwyg.plugins['break'] = {
       elem.parentNode.removeChild(elem);
     });
     return $content.html();
+    */
+//   console.log('original:\n'+content);
+    // Replace (duplicate) placeholders within p taqs with a single break.
+    var newContent = content.replace(/\s*<p[^>]*?>(?:\s*<img(?:\s*\w+=['"][^'"]*?['"]\s*)*?\s*class=['"][^'"]*?wysiwyg-break[^'"]*?['"]\s*(?:\s*\w+=['"][^'"]*?['"]\s*)*?(?:\/)?>\s*)+<\/p>\s*/ig, '<!--break-->');
+//    console.log('1\n'+newContent);
+    // Replace all other placeholders.
+    newContent = newContent.replace(/<img(?:\s*\w+=['"][^'"]*?['"]\s*)*?\s*class=['"][^'"]*?wysiwyg-break[^'"]*?['"]\s*(?:\s*\w+=['"][^'"]*?['"]\s*)*?(?:\/)?>/ig, '<!--break-->');
+//    console.log('2\n'+newContent);
+    // Fix paragraphs opening just before breaks.
+    newContent = newContent.replace(/(?:<!--break-->)*(<p[^>]*?>\s*)<!--break-->/ig, '<!--break-->$1');
+//    console.log('3\n'+newContent);
+    // Remove duplicate breaks and any preceding whitespaces.
+    newContent = newContent.replace(/(?:\s*<!--break-->){2,}/g, '<!--break-->');
+//    console.log('4\n'+newContent);
+    // Fix paragraphs ending after breaks.
+    newContent = newContent.replace(/<!--break-->(\s*<\/p>)(?:<!--break-->)*/ig, '$1<!--break-->');
+//    console.log('5\n'+newContent);
+    // Remove duplicate breaks with trailing whitespaces.
+    newContent = newContent.replace(/(?:<!--break-->\s*){2,}/g, '<!--break-->');
+//    console.log('done\n'+newContent);
+    return newContent;
   },
 
   /**
    * Helper function to return a HTML placeholder.
    */
   _getPlaceholder: function (settings) {
-    return '<img src="' + settings.path + '/images/spacer.gif" alt="&lt;--break-&gt;" title="&lt;--break--&gt;" class="wysiwyg-break drupal-content" />';
+    return '<img src="' + settings.path + '/images/spacer.gif" alt="&lt;--break--&gt;" title="&lt;--break--&gt;" class="wysiwyg-break drupal-content" />';
   }
 };
