diff --git a/plugins/break/break.js b/plugins/break/break.js index 54aac4c..6d8f025 100644 --- a/plugins/break/break.js +++ b/plugins/break/break.js @@ -38,6 +38,13 @@ Drupal.wysiwyg.plugins['break'] = { * Replace all tags with images. */ attach: function(content, settings, instanceId) { + // Remove unnecessary paragraph. + content = content.replace(/

<\/p>/ig, this._getPlaceholder(settings)); + // Move breaks starting at the beginning of paragraphs to before them. + content = content.replace(/

(<[^p])/ig, this._getPlaceholder(settings) + '

$1'); + // Move breaks starting at the end of to after the paragraphs. + content = content.replace(/([^p]>)<\/p>/ig, '$1

' + this._getPlaceholder(settings)); + // Other breaks. content = content.replace(//g, this._getPlaceholder(settings)); return content; }, @@ -46,22 +53,33 @@ Drupal.wysiwyg.plugins['break'] = { * Replace images with tags in content upon detaching editor. */ detach: function(content, settings, instanceId) { - var $content = $('
' + content + '
'); // 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. - // @todo Add a generic implementation for all Drupal plugins for this. - $.each($('img.wysiwyg-break', $content), function (i, elem) { - elem.parentNode.insertBefore(document.createComment('break'), elem); - 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*]*?>(?:\s*\s*)+<\/p>\s*/ig, ''); + // console.log('1\n'+newContent); + // Replace all other placeholders. + newContent = newContent.replace(//ig, ''); + // console.log('2\n'+newContent); + // Fix paragraphs opening just before breaks. + newContent = newContent.replace(/(?:)*(]*?>\s*)/ig, '$1'); + // console.log('3\n'+newContent); + // Remove duplicate breaks and any preceding whitespaces. + newContent = newContent.replace(/(?:\s*){2,}/g, ''); + // console.log('4\n'+newContent); + // Fix paragraphs ending after breaks. + newContent = newContent.replace(/(\s*<\/p>)(?:)*/ig, '$1'); + // console.log('5\n'+newContent); + // Remove duplicate breaks with trailing whitespaces. + newContent = newContent.replace(/(?:\s*){2,}/g, ''); + // console.log('done\n'+newContent); + return newContent; }, /** * Helper function to return a HTML placeholder. */ _getPlaceholder: function (settings) { - return '<--break->'; + return '<--break-->'; } };