From 14898683021d1fc44823afe4fd49ce72f2fdbc17 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Mon, 6 Jul 2015 11:28:49 +0200 Subject: [PATCH] Improved pasting of formatted content --- .../js/3rdParty/redactor/plugins/wbbcode.js | 125 ++++++------------ 1 file changed, 37 insertions(+), 88 deletions(-) diff --git a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js index e8a2b8f026..9af01197be 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js +++ b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js @@ -343,74 +343,57 @@ RedactorPlugins.wbbcode = function() { * @return string */ _pasteBeforeCallback: function(html) { - var $levels = { + var container = document.createElement('div'); + container.innerHTML = html; + document.body.appendChild(container); + + // handle

...

tags + var defaultSizes = { 1: 24, 2: 22, 3: 18, 4: 14, 5: 12, 6: 10 - }; - - // replace

...

tags - html = html.replace(/]*)>/g, function(match, level, elementAttributes) { - if (elementAttributes && elementAttributes.match(/style="([^"]+?)"/)) { - if (/font-size: ?(\d+|\d+\.\d+)(px|pt|em|rem|%)/.test(RegExp.$1)) { - var $div = $('
').appendTo(document.body); - var $width = parseInt($div[0].clientWidth); - $div.remove(); - - // look for the closest matching size - var $bestMatch = -1; - var $isExactMatch = false; - $.each($levels, function(k, v) { - if ($bestMatch === -1) { - $bestMatch = k; - } - else { - if (Math.abs($width - v) < Math.abs($width - $levels[$bestMatch])) { - $bestMatch = k; - } - } - - if ($width == v) { - $isExactMatch = true; - } - }); - - if (!$isExactMatch) { - // if dealing with non-exact matches, lower size by one level - $bestMatch = ($bestMatch < 6) ? parseInt($bestMatch) + 1 : $bestMatch; - } + }, element, elements, size, styles; + for (var i = 1; i <= 6; i++) { + elements = container.getElementsByTagName('H' + i); + while (elements.length) { + element = elements[0]; + size = defaultSizes[i]; + + if (element.style.fontSize) { + styles = window.getComputedStyle(element); + size = Math.round(styles.getPropertyValue('font-size').replace(/px$/, '')); - level = $bestMatch; + if (size > 24) size = 24; + else if (size > 22) size = 22; + else if (size > 18) size = 22; + else if (size > 14) size = 14; + else if (size > 12) size = 14; + else if (size > 10) size = 12; + else size = 10; } + + element.outerHTML = '' + element.innerHTML + ''; } - - return '[size=' + $levels[level] + ']'; - }); - html = html.replace(/<\/h[1-6]>/g, '[/size]'); - - // convert block-level elements - html = html.replace(/<(article|header)[^>]+>/g, '
'); - html = html.replace(/<\/(article|header)>/g, '
'); + } - // replace nested elements e.g.

...

- html = html.replace(/<(div|p)([^>]+)?><(div|p)([^>]+)?>/g, '

'); - html = html.replace(/<\/(div|p)><\/(div|p)>/g, '

'); - //html = html.replace(/<(div|p)>
<\/(div|p)>/g, '

'); + document.body.removeChild(container); - // strip classes from certain elements - html = html.replace(/<(?:div|p|span)[^>]+>/gi, function(match) { - return match.replace(/ class="[^"]+"/, ''); - }); + // remove CSS classes from certain elements + elements = container.querySelectorAll('div,p,span'); + for (var i = 0, length = elements.length; i < length; i++) { + if (elements[0].className) elements[0].className = ''; + } // drop - html = html.replace(/<\/?wbr[^>]*>/g, ''); - - WCF.System.Event.fireEvent('com.woltlab.wcf.redactor', 'beforePaste', { html: html }); + elements = container.getElementsByTagName('WBR'); + while (elements.length) { + elements[0].parentNode.removeChild(elements[0]); + } - return html; + return container.innerHTML; }, /** @@ -420,28 +403,6 @@ RedactorPlugins.wbbcode = function() { * @return string */ _pasteCallback: function(html) { - /*var $uuid = WCF.getUUID(); - - // replace

...

with

...


unless there is already a newline - html = html.replace(/

([\s\S]*?)<\/p>/gi, function(match, content) { - if (content.match(/^$/)) { - return match; - } - - return match + '@@@' + $uuid + '@@@'; - }); - html = html.replace(new RegExp('@@@' + $uuid + '@@@(

)?', 'g'), function(match, next) { - if (next) { - return next; - } - - return '


'; - });*/ - - // restore font size - html = html.replace(/\[size=(\d+)\]/g, '

'); - html = html.replace(/\[\/size\]/g, '

'); - // strip background-color html = html.replace(/style="([^"]+)"/, function(match, inlineStyles) { var $parts = inlineStyles.split(';'); @@ -458,18 +419,6 @@ RedactorPlugins.wbbcode = function() { WCF.System.Event.fireEvent('com.woltlab.wcf.redactor', 'afterPaste', { html: html }); - return html; - - // TODO - - // handle pasting of images in Firefox - html = html.replace(/]+)>/g, function(match, content) { - match = match.replace(/data-mozilla-paste-image="0"/, 'data-mozilla-paste-image="0" style="display:none"'); - return match; - }); - - - return html; }, -- 2.20.1