0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 07:13:08 -05:00

Remove Unneeded ZWS

ZWS are added when formats are added, however these are not cleaned up
if the format is changed. Now they will be cleaned up if they only
contain ZWS and no text.
This commit is contained in:
Andy Kauffman 2016-07-25 12:24:51 -04:00
parent df9b5add68
commit a5d4bfb7d6
3 changed files with 34 additions and 6 deletions

View file

@ -2856,13 +2856,18 @@ proto.getPath = function () {
// WebKit bug: https://bugs.webkit.org/show_bug.cgi?id=15256
var removeZWS = function ( root ) {
// Walk down the tree starting at the root and remove any ZWS. If the node only contained
// ZWS space then remove it too.
//
// We may want to keep one ZWS node at the bottom of the tree so the block can be selected. Define that
// node as the keepNode.
var removeZWS = function ( root, keepNode ) {
var walker = new TreeWalker( root, SHOW_TEXT, function () {
return true;
}, false ),
parent, node, index;
while ( node = walker.nextNode() ) {
while ( ( index = node.data.indexOf( ZWS ) ) > -1 ) {
while ( ( index = node.data.indexOf( ZWS ) ) > -1 && node.parentNode !== keepNode ) {
if ( node.length === 1 ) {
do {
parent = node.parentNode;
@ -3267,6 +3272,15 @@ proto._addFormat = function ( tag, attributes, range ) {
insertNodeInRange( range, el );
range.setStart( el.firstChild, el.firstChild.length );
range.collapse( true );
// Clean up any previous formats that may have been set on this block
// that are unused.
var block = el;
while ( isInline( block ) ) {
block = block.parentNode;
}
removeZWS( block, el );
}
// Otherwise we find all the textnodes in the range (splitting
// partially selected nodes) and if they're not already formatted

File diff suppressed because one or more lines are too long

View file

@ -546,13 +546,18 @@ proto.getPath = function () {
// WebKit bug: https://bugs.webkit.org/show_bug.cgi?id=15256
var removeZWS = function ( root ) {
// Walk down the tree starting at the root and remove any ZWS. If the node only contained
// ZWS space then remove it too.
//
// We may want to keep one ZWS node at the bottom of the tree so the block can be selected. Define that
// node as the keepNode.
var removeZWS = function ( root, keepNode ) {
var walker = new TreeWalker( root, SHOW_TEXT, function () {
return true;
}, false ),
parent, node, index;
while ( node = walker.nextNode() ) {
while ( ( index = node.data.indexOf( ZWS ) ) > -1 ) {
while ( ( index = node.data.indexOf( ZWS ) ) > -1 && node.parentNode !== keepNode ) {
if ( node.length === 1 ) {
do {
parent = node.parentNode;
@ -957,6 +962,15 @@ proto._addFormat = function ( tag, attributes, range ) {
insertNodeInRange( range, el );
range.setStart( el.firstChild, el.firstChild.length );
range.collapse( true );
// Clean up any previous formats that may have been set on this block
// that are unused.
var block = el;
while ( isInline( block ) ) {
block = block.parentNode;
}
removeZWS( block, el );
}
// Otherwise we find all the textnodes in the range (splitting
// partially selected nodes) and if they're not already formatted