0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2025-01-03 05:00:13 -05:00

Fix inserting line break at start of link

Before, hitting enter at the start of the link would result in the link text
being removed from an <a> on the new line, and an empty <a> tag on the previous
line. Now the link remains on the new line as expected, and no empty <a> tag is
produced.

in the preceding block.
This commit is contained in:
Neil Jenkins 2014-12-25 11:39:07 +07:00
parent f781e07b23
commit 5b52467815
3 changed files with 48 additions and 22 deletions

View file

@ -1366,14 +1366,8 @@ 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
proto._didAddZWS = function () { var removeZWS = function ( root ) {
this._hasZWS = true; var walker = new TreeWalker( root, SHOW_TEXT, function () {
};
proto._removeZWS = function () {
if ( !this._hasZWS ) {
return;
}
var walker = new TreeWalker( this._body, SHOW_TEXT, function () {
return true; return true;
}, false ), }, false ),
node, index; node, index;
@ -1382,6 +1376,16 @@ proto._removeZWS = function () {
node.deleteData( index, 1 ); node.deleteData( index, 1 );
} }
} }
}
proto._didAddZWS = function () {
this._hasZWS = true;
};
proto._removeZWS = function () {
if ( !this._hasZWS ) {
return;
}
removeZWS( this._body );
this._hasZWS = false; this._hasZWS = false;
}; };
@ -2333,11 +2337,13 @@ var removeEmptyInlines = function ( root ) {
child; child;
while ( l-- ) { while ( l-- ) {
child = children[l]; child = children[l];
if ( child.nodeType === ELEMENT_NODE && child.nodeName !== 'IMG' ) { if ( child.nodeType === ELEMENT_NODE && !isLeaf( child ) ) {
removeEmptyInlines( child ); removeEmptyInlines( child );
if ( isInline( child ) && !child.firstChild ) { if ( isInline( child ) && !child.firstChild ) {
root.removeChild( child ); root.removeChild( child );
} }
} else if ( child.nodeType === TEXT_NODE && !child.data ) {
root.removeChild( child );
} }
} }
}; };
@ -2826,6 +2832,12 @@ var keyHandlers = {
// Otherwise, split at cursor point. // Otherwise, split at cursor point.
nodeAfterSplit = splitBlock( block, splitNode, splitOffset ); nodeAfterSplit = splitBlock( block, splitNode, splitOffset );
// Clean up any empty inlines if we hit enter at the beginning of the
// block
removeZWS( block );
removeEmptyInlines( block );
fixCursor( block );
// Focus cursor // Focus cursor
// If there's a <b>/<i> etc. at the beginning of the split // If there's a <b>/<i> etc. at the beginning of the split
// make sure we focus inside it. // make sure we focus inside it.
@ -2835,7 +2847,8 @@ var keyHandlers = {
// Don't continue links over a block break; unlikely to be the // Don't continue links over a block break; unlikely to be the
// desired outcome. // desired outcome.
if ( nodeAfterSplit.nodeName === 'A' ) { if ( nodeAfterSplit.nodeName === 'A' &&
!nodeAfterSplit.textContent ) {
replaceWith( nodeAfterSplit, empty( nodeAfterSplit ) ); replaceWith( nodeAfterSplit, empty( nodeAfterSplit ) );
nodeAfterSplit = child; nodeAfterSplit = child;
continue; continue;

File diff suppressed because one or more lines are too long

View file

@ -281,14 +281,8 @@ 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
proto._didAddZWS = function () { var removeZWS = function ( root ) {
this._hasZWS = true; var walker = new TreeWalker( root, SHOW_TEXT, function () {
};
proto._removeZWS = function () {
if ( !this._hasZWS ) {
return;
}
var walker = new TreeWalker( this._body, SHOW_TEXT, function () {
return true; return true;
}, false ), }, false ),
node, index; node, index;
@ -297,6 +291,16 @@ proto._removeZWS = function () {
node.deleteData( index, 1 ); node.deleteData( index, 1 );
} }
} }
}
proto._didAddZWS = function () {
this._hasZWS = true;
};
proto._removeZWS = function () {
if ( !this._hasZWS ) {
return;
}
removeZWS( this._body );
this._hasZWS = false; this._hasZWS = false;
}; };
@ -1248,11 +1252,13 @@ var removeEmptyInlines = function ( root ) {
child; child;
while ( l-- ) { while ( l-- ) {
child = children[l]; child = children[l];
if ( child.nodeType === ELEMENT_NODE && child.nodeName !== 'IMG' ) { if ( child.nodeType === ELEMENT_NODE && !isLeaf( child ) ) {
removeEmptyInlines( child ); removeEmptyInlines( child );
if ( isInline( child ) && !child.firstChild ) { if ( isInline( child ) && !child.firstChild ) {
root.removeChild( child ); root.removeChild( child );
} }
} else if ( child.nodeType === TEXT_NODE && !child.data ) {
root.removeChild( child );
} }
} }
}; };
@ -1741,6 +1747,12 @@ var keyHandlers = {
// Otherwise, split at cursor point. // Otherwise, split at cursor point.
nodeAfterSplit = splitBlock( block, splitNode, splitOffset ); nodeAfterSplit = splitBlock( block, splitNode, splitOffset );
// Clean up any empty inlines if we hit enter at the beginning of the
// block
removeZWS( block );
removeEmptyInlines( block );
fixCursor( block );
// Focus cursor // Focus cursor
// If there's a <b>/<i> etc. at the beginning of the split // If there's a <b>/<i> etc. at the beginning of the split
// make sure we focus inside it. // make sure we focus inside it.
@ -1750,7 +1762,8 @@ var keyHandlers = {
// Don't continue links over a block break; unlikely to be the // Don't continue links over a block break; unlikely to be the
// desired outcome. // desired outcome.
if ( nodeAfterSplit.nodeName === 'A' ) { if ( nodeAfterSplit.nodeName === 'A' &&
!nodeAfterSplit.textContent ) {
replaceWith( nodeAfterSplit, empty( nodeAfterSplit ) ); replaceWith( nodeAfterSplit, empty( nodeAfterSplit ) );
nodeAfterSplit = child; nodeAfterSplit = child;
continue; continue;