mirror of
https://github.com/fastmail/Squire.git
synced 2024-12-22 07:13:08 -05:00
Refactor cleaning code and merge blockquotes
This commit is contained in:
parent
9aaefdf586
commit
0d706180fe
1 changed files with 50 additions and 27 deletions
|
@ -739,7 +739,7 @@ document.addEventListener( 'DOMContentLoaded', function () {
|
||||||
return frag;
|
return frag;
|
||||||
};
|
};
|
||||||
|
|
||||||
// --- Paste and clean ---
|
// --- Clean ---
|
||||||
|
|
||||||
var allowedBlock = /^A(?:DDRESS|RTICLE|SIDE)|BLOCKQUOTE|CAPTION|D(?:[DLT]|IV)|F(?:IGURE|OOTER)|H[1-6]|HEADER|L(?:ABEL|EGEND|I)|O(?:L|UTPUT)|P(?:RE)?|SECTION|T(?:ABLE|BODY|D|FOOT|H|HEAD|R)|UL$/;
|
var allowedBlock = /^A(?:DDRESS|RTICLE|SIDE)|BLOCKQUOTE|CAPTION|D(?:[DLT]|IV)|F(?:IGURE|OOTER)|H[1-6]|HEADER|L(?:ABEL|EGEND|I)|O(?:L|UTPUT)|P(?:RE)?|SECTION|T(?:ABLE|BODY|D|FOOT|H|HEAD|R)|UL$/;
|
||||||
|
|
||||||
|
@ -861,7 +861,46 @@ document.addEventListener( 'DOMContentLoaded', function () {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var onPaste = function () {
|
var mergeAdjacent = function ( root, tag ) {
|
||||||
|
var els = root.querySelectorAll( tag ),
|
||||||
|
i, l, el, prev;
|
||||||
|
for ( i = 1, l = els.length; i < l; i += 1 ) {
|
||||||
|
el = els[i];
|
||||||
|
prev = el.previousSibling;
|
||||||
|
if ( prev && prev.nodeName === tag ) {
|
||||||
|
prev.appendChild( el.detach().empty() );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
};
|
||||||
|
|
||||||
|
var wrapTopLevelInline = function ( root, tag ) {
|
||||||
|
var children = root.childNodes,
|
||||||
|
wrapper = null,
|
||||||
|
i, l, child;
|
||||||
|
for ( i = 0, l = children.length; i < l; i += 1 ) {
|
||||||
|
child = children[i];
|
||||||
|
if ( child.isInline() ) {
|
||||||
|
if ( !wrapper ) { wrapper = createElement( tag ); }
|
||||||
|
wrapper.appendChild( child );
|
||||||
|
i -= 1;
|
||||||
|
l -= 1;
|
||||||
|
} else if ( wrapper ) {
|
||||||
|
root.insertBefore( wrapper, child );
|
||||||
|
wrapper = null;
|
||||||
|
i += 1;
|
||||||
|
l += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ( wrapper ) {
|
||||||
|
root.appendChild( wrapper );
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
};
|
||||||
|
|
||||||
|
// --- Paste ---
|
||||||
|
|
||||||
|
doc.addEventListener( 'paste', function () {
|
||||||
var range = getSelection(),
|
var range = getSelection(),
|
||||||
startContainer = range.startContainer,
|
startContainer = range.startContainer,
|
||||||
startOffset = range.startOffset,
|
startOffset = range.startOffset,
|
||||||
|
@ -895,9 +934,7 @@ document.addEventListener( 'DOMContentLoaded', function () {
|
||||||
range.collapse( false );
|
range.collapse( false );
|
||||||
setSelection( range );
|
setSelection( range );
|
||||||
}, 0 );
|
}, 0 );
|
||||||
};
|
}, false );
|
||||||
|
|
||||||
doc.addEventListener( 'paste', onPaste, false );
|
|
||||||
|
|
||||||
// --- Keyboard interaction ---
|
// --- Keyboard interaction ---
|
||||||
|
|
||||||
|
@ -1159,35 +1196,21 @@ document.addEventListener( 'DOMContentLoaded', function () {
|
||||||
return '';
|
return '';
|
||||||
});
|
});
|
||||||
|
|
||||||
// Parse HTML into DOM tree and clean.
|
// Parse HTML into DOM tree
|
||||||
var frag = doc.createDocumentFragment(),
|
var frag = doc.createDocumentFragment(),
|
||||||
div = createElement( 'DIV' ),
|
div = createElement( 'DIV' ),
|
||||||
children, child, l, i;
|
children, child, l, i;
|
||||||
|
|
||||||
div.innerHTML = html;
|
div.innerHTML = html;
|
||||||
cleanTree( div, frag, true );
|
|
||||||
cleanupBRs( frag );
|
|
||||||
|
|
||||||
|
// Clean
|
||||||
|
cleanTree( div, frag, true );
|
||||||
|
// Remove <br>s
|
||||||
|
cleanupBRs( frag );
|
||||||
|
// Merge adjacent blockquotes
|
||||||
|
mergeAdjacent( frag, 'BLOCKQUOTE' );
|
||||||
// Wrap top-level inline nodes in div.
|
// Wrap top-level inline nodes in div.
|
||||||
children = frag.childNodes;
|
wrapTopLevelInline( frag, 'DIV' );
|
||||||
div = null;
|
|
||||||
for ( i = 0, l = children.length; i < l; i += 1 ) {
|
|
||||||
child = children[i];
|
|
||||||
if ( child.isInline() ) {
|
|
||||||
if ( !div ) { div = createElement( 'DIV' ); }
|
|
||||||
div.appendChild( child );
|
|
||||||
i -= 1;
|
|
||||||
l -= 1;
|
|
||||||
} else if ( div ) {
|
|
||||||
frag.insertBefore( div, child );
|
|
||||||
div = null;
|
|
||||||
i += 1;
|
|
||||||
l += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ( div ) {
|
|
||||||
frag.appendChild( div );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fix cursor
|
// Fix cursor
|
||||||
var node = frag;
|
var node = frag;
|
||||||
|
|
Loading…
Reference in a new issue