Skip to content
Permalink
Browse files

#9129: Fixed backspace/del behaviors around list item.

git-svn-id: https://svn.ckeditor.com/CKEditor/trunk@7601 da63caf2-3823-0410-a1e8-d69ccee00dfb
  • Loading branch information...
garry.yao Garry Yao
garry.yao authored and Garry Yao committed Sep 7, 2012
1 parent 2ff0035 commit b285549975ada6a8721c01f102025ab7e38a54de
Showing with 89 additions and 18 deletions.
  1. +8 −9 core/selection.js
  2. +81 −9 plugins/list/plugin.js
@@ -64,21 +64,19 @@
return node && node.type == CKEDITOR.NODE_ELEMENT && node.getName() in CKEDITOR.dtd.$removeEmpty;
}

function singletonBlock( node ) {
var body = range.document.getBody();
return !node.is( 'body' ) && body.getChildCount() == 1;
}

var start = range.startContainer,
offset = range.startOffset;

if ( start.type == CKEDITOR.NODE_TEXT )
return false;

// 1. Empty inline element. <span>^</span>
// 2. Adjoin to inline element. <p><strong>text</strong>^</p>
// 3. The only empty block in document. <body><p>^</p></body> (#7222)
return !CKEDITOR.tools.trim( start.getHtml() ) ? isInlineCt( start ) || singletonBlock( start ) : isInlineCt( start.getChild( offset - 1 ) ) || isInlineCt( start.getChild( offset ) );
// 2. Empty block. <p>^</p> (#7222)
// 3. Adjoin to inline element. <p><strong>text</strong>^</p>
return !CKEDITOR.tools.trim( start.getHtml() ) ?
isInlineCt( start ) || start.isBlockBoundary() :
isInlineCt( start.getChild( offset - 1 ) ) ||
isInlineCt( start.getChild( offset ) );
}

function createFillingChar( element ) {
@@ -306,6 +304,7 @@
});

if ( CKEDITOR.env.webkit ) {
// Before keystroke is handled by editor, check to remove the filling char.
doc.on( 'keydown', function( evt ) {
var key = evt.data.getKey();
// Remove the filling char before some keys get
@@ -324,7 +323,7 @@
removeFillingChar( editor.editable() );
}

}, null, null, 10 );
}, null, null, -1 );
}
});

@@ -683,7 +683,7 @@
return node.type == CKEDITOR.NODE_ELEMENT && ( node.getName() in CKEDITOR.dtd.$block || node.getName() in CKEDITOR.dtd.$listItem ) && CKEDITOR.dtd[ node.getName() ][ '#' ];
}

// Merge the visual line content at the cursor range into the block.
// Join visually two block lines.
function joinNextLineToCursor( editor, cursor, nextCursor ) {
editor.fire( 'saveSnapshot' );

@@ -696,7 +696,7 @@

// Kill original bogus;
var currentPath = editor.elementPath( cursor.startContainer );
var currentLi = currentPath.lastElement.getAscendant( 'li', 1 );
var currentBlock = currentPath.lastElement.getAscendant( 'li', 1 ) || currentPath.block;

var bogus = currentPath.block.getBogus();
bogus && bogus.remove();
@@ -721,13 +721,13 @@
var sublist = getSubList( nextLi );
if ( sublist ) {
// If next line is in the sub list of the current list item.
if ( currentLi.contains( nextLi ) ) {
if ( currentBlock.contains( nextLi ) ) {
mergeChildren( sublist, nextLi.getParent(), nextLi );
sublist.remove();
}
// Migrate the sub list to current list item.
else
currentLi.append( sublist );
currentBlock.append( sublist );
}
}

@@ -808,12 +808,17 @@
if ( !range.collapsed )
return;

var path = new CKEDITOR.dom.elementPath( range.startContainer );
var isBackspace = key == 8;
var editable = editor.editable();
var walker = new CKEDITOR.dom.walker( range.clone() );
walker.evaluator = function( node ) {
return nonEmpty( node ) && !blockBogus( node );
};
// Backspace/Del behavior at the start/end of table is handled in core.
walker.guard = function( node, isOut ) {
return !( isOut && node.type == CKEDITOR.NODE_ELEMENT && node.is( 'table' ) );
};

var cursor = range.clone();

@@ -841,8 +846,7 @@

if ( previous && previous.type == CKEDITOR.NODE_ELEMENT &&
( previous.getName() in listNodeNames ||
previous.is( 'li' ) ) )
{
previous.is( 'li' ) ) ) {
if ( !previous.is( 'li' ) ) {
walker.range.selectNodeContents( previous );
walker.reset();
@@ -860,8 +864,37 @@
joinNextLineToCursor( editor, cursor, range );
evt.cancel();
}
else {
var list = path.contains( listNodeNames );
// Backspace pressed at the start of list outdents the first list item. (#9129)
if ( list && range.checkBoundaryOfElement( list, CKEDITOR.START ) ) {
li = list.getFirst( nonEmpty );

if ( range.checkBoundaryOfElement( li, CKEDITOR.START ) ) {
previous = list.getPrevious( nonEmpty );

// Only if the list item contains a sub list, do nothing but
// simply move cursor backward one character.
if ( getSubList( li ) ) {
if ( previous ) {
range.moveToElementEditEnd( previous );
range.select();
}

evt.cancel();
}
else {
editor.execCommand( 'outdent' );
evt.cancel();
}
}
}
}

} else {
var li = path.contains( 'li' );

var next, nextLine, li = path.contains( 'li' );

if ( li ) {
walker.range.setEndAt( editable, CKEDITOR.POSITION_BEFORE_END );

@@ -871,7 +904,7 @@
// Indicate cursor at the visual end of an list item.
var isAtEnd = 0;

var next = walker.next();
next = walker.next();

// When list item contains a sub list.
if ( next && next.type == CKEDITOR.NODE_ELEMENT &&
@@ -890,13 +923,52 @@

if ( isAtEnd && next ) {
// Put cursor range there.
var nextLine = range.clone();
nextLine = range.clone();
nextLine.moveToElementEditStart( next );

joinNextLineToCursor( editor, cursor, nextLine );
evt.cancel();
}
}
else
{
// Handle Del key pressed before the list.
walker.range.setEndAt( editable, CKEDITOR.POSITION_BEFORE_END );
next = walker.next();

if ( next && next.type == CKEDITOR.NODE_ELEMENT &&
next.is( listNodeNames ) ) {
// The start <li>
next = next.getFirst( nonEmpty );

// Simply remove the current empty block, move cursor to the
// subsequent list.
if ( path.block &&
range.checkStartOfBlock() &&
range.checkEndOfBlock() ) {
path.block.remove();
range.moveToElementEditStart( next );
range.select();
evt.cancel();
}
// Preventing the default (merge behavior), but simply move
// the cursor one character forward if subsequent list item
// contains sub list.
else if ( getSubList( next ) ) {
range.moveToElementEditStart( next );
range.select();
evt.cancel();
}
// Merge the first list item with the current line.
else {
nextLine = range.clone();
nextLine.moveToElementEditStart( next );
joinNextLineToCursor( editor, cursor, nextLine );
evt.cancel();
}
}
}

}

// The backspace/del could potentially put cursor at a bad position,

0 comments on commit b285549

Please sign in to comment.
You can’t perform that action at this time.