html = mpOnPaste.call(this, div.innerHTML, data, insert);
- html = html.replace(/\n*@@@WOLTLAB-BR-MARKER@@@/g, '<woltlab-br-marker></woltlab-br-marker>');
+ html = html.replace(/\n*@@@WOLTLAB-BR-MARKER@@@\n*/g, '<woltlab-br-marker></woltlab-br-marker>');
html = html.replace(/(<p>)?\s*@@@WOLTLAB-P-ALIGN-(left|right|center|justify)@@@/g, function (match, p, alignment) {
if (p) {
return '<p class="text-' + alignment + '">';
elBySelAll('woltlab-br-marker', div, function (marker) {
var parent = marker.parentNode;
+ if (parent === null) {
+ // marker was already removed, ignore it
+ return;
+ }
if (parent.nodeName === 'P') {
var p = elCreate('p');
p.innerHTML = '<br>';
- var emptySiblings = true;
+ var isDoubleBr = false;
+ var sibling = marker.nextSibling;
+ if (sibling && sibling.nodeName === 'WOLTLAB-BR-MARKER') {
+ // equals <br><br> and should be converted into an empty line, splitting the ancestors
+ isDoubleBr = true;
+ }
+
+ var emptySiblings = !isDoubleBr;
while (marker.nextSibling) {
if (emptySiblings && marker.nextSibling.textContent.replace(/\u200B/g, '').trim().length !== 0) {
emptySiblings = false;
}
if (!emptySiblings) {
- // the <br> is not required when there is text afterwards
+ // the <br> is not required when there is text afterwards, or if this is a <br><br> match
elRemove(p.firstElementChild);
}
}
parent.parentNode.insertBefore(p, parent.nextSibling);
+
+ if (isDoubleBr) {
+ p = elCreate('p');
+ p.innerHTML = '<br>';
+ parent.parentNode.insertBefore(p, parent.nextSibling);
+ }
}
else {
parent.insertBefore(elCreate('br'), marker);
}
});
}
+ else if (p.textContent.trim().length === 0) {
+ elBySelAll('span', p, function (span) {
+ if (!span.hasAttribute('style') || !span.style.length) {
+ while (span.childNodes.length) {
+ span.parentNode.insertBefore(span.childNodes[0], span);
+ }
+
+ elRemove(span);
+ }
+ });
+
+ if (p.children.length === 0) {
+ // fix <p> </p> pasted from Office
+ p.innerHTML = '<br>';
+ }
+ }
if (remove) {
elRemove(p);