mirror of
https://github.com/fastmail/Squire.git
synced 2025-01-03 05:00:13 -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:
parent
1a797aca48
commit
6e1b864663
3 changed files with 34 additions and 6 deletions
|
@ -2856,13 +2856,18 @@ proto.getPath = function () {
|
||||||
|
|
||||||
// WebKit bug: https://bugs.webkit.org/show_bug.cgi?id=15256
|
// 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 () {
|
var walker = new TreeWalker( root, SHOW_TEXT, function () {
|
||||||
return true;
|
return true;
|
||||||
}, false ),
|
}, false ),
|
||||||
parent, node, index;
|
parent, node, index;
|
||||||
while ( node = walker.nextNode() ) {
|
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 ) {
|
if ( node.length === 1 ) {
|
||||||
do {
|
do {
|
||||||
parent = node.parentNode;
|
parent = node.parentNode;
|
||||||
|
@ -3267,6 +3272,15 @@ proto._addFormat = function ( tag, attributes, range ) {
|
||||||
insertNodeInRange( range, el );
|
insertNodeInRange( range, el );
|
||||||
range.setStart( el.firstChild, el.firstChild.length );
|
range.setStart( el.firstChild, el.firstChild.length );
|
||||||
range.collapse( true );
|
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
|
// Otherwise we find all the textnodes in the range (splitting
|
||||||
// partially selected nodes) and if they're not already formatted
|
// partially selected nodes) and if they're not already formatted
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -547,13 +547,18 @@ proto.getPath = function () {
|
||||||
|
|
||||||
// WebKit bug: https://bugs.webkit.org/show_bug.cgi?id=15256
|
// 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 () {
|
var walker = new TreeWalker( root, SHOW_TEXT, function () {
|
||||||
return true;
|
return true;
|
||||||
}, false ),
|
}, false ),
|
||||||
parent, node, index;
|
parent, node, index;
|
||||||
while ( node = walker.nextNode() ) {
|
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 ) {
|
if ( node.length === 1 ) {
|
||||||
do {
|
do {
|
||||||
parent = node.parentNode;
|
parent = node.parentNode;
|
||||||
|
@ -968,6 +973,15 @@ proto._addFormat = function ( tag, attributes, range ) {
|
||||||
insertNodeInRange( range, el );
|
insertNodeInRange( range, el );
|
||||||
range.setStart( el.firstChild, el.firstChild.length );
|
range.setStart( el.firstChild, el.firstChild.length );
|
||||||
range.collapse( true );
|
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
|
// Otherwise we find all the textnodes in the range (splitting
|
||||||
// partially selected nodes) and if they're not already formatted
|
// partially selected nodes) and if they're not already formatted
|
||||||
|
|
Loading…
Reference in a new issue