Skip to content

Commit

Permalink
Full table/list deletion is now handled specificall in editable keyst…
Browse files Browse the repository at this point in the history
…roke handlers.
  • Loading branch information
Garry Yao committed Oct 22, 2012
1 parent a9da7a5 commit a930e53
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 62 deletions.
50 changes: 48 additions & 2 deletions core/editable.js
Expand Up @@ -532,15 +532,16 @@
// Backspace OR Delete.
if ( keyCode in { 8:1,46:1 } ) {
var sel = editor.getSelection(),
selected = sel.getSelectedElement(),
selected,
range = sel.getRanges()[ 0 ],
path = range.startPath(),
block,
parent,
next,
rtl = keyCode == 8;

if ( selected ) {
// Remove the entire list/table on fully selected content. (#7645)
if ( selected = getSelectedTableList( sel ) ) {
// Make undo snapshot.
editor.fire( 'saveSnapshot' );

Expand Down Expand Up @@ -824,6 +825,51 @@
};
}

// Check if the entire table/list contents is selected.
function getSelectedTableList( sel ) {
var selected,
range = sel.getRanges()[ 0 ],
editable = sel.root,
path = range.startPath(),
structural = { table:1,ul:1,ol:1,dl:1 };

var isBogus = CKEDITOR.dom.walker.bogus();

if ( path.contains( structural ) ) {
// Enlarging the start boundary.
var walkerRng = range.clone();
walkerRng.collapse( 1 );
walkerRng.setStartAt( editable, CKEDITOR.POSITION_AFTER_START );

var walker = new CKEDITOR.dom.walker( walkerRng ),
// Check the range is at the inner boundary of the structural element.
guard = function( walker, isEnd ) {
return function( node, isWalkOut ) {
if ( isWalkOut && node.type == CKEDITOR.NODE_ELEMENT && node.is( structural ) )
selected = node;

if ( isNotEmpty( node ) && !isWalkOut && !( isEnd && isBogus( node ) ) )
return false;
};
};

walker.guard = guard( walker );
walker.checkBackward();
if ( selected ) {
walkerRng = range.clone();
walkerRng.collapse();
walkerRng.setEndAt( editable, CKEDITOR.POSITION_BEFORE_END );
walker = new CKEDITOR.dom.walker( walkerRng );
walker.guard = guard( walker, 1 );
selected = 0;
walker.checkForward();
return selected;
}
}

return null;
}


// Matching an empty paragraph at the end of document.
var emptyParagraphRegexp = /(^|<body\b[^>]*>)\s*<(p|div|address|h\d|center|pre)[^>]*>\s*(?:<br[^>]*>|&nbsp;|\u00A0|&#160;)?\s*(:?<\/\2>)?\s*(?=$|<\/body>)/gi;
Expand Down
60 changes: 0 additions & 60 deletions core/selection.js
Expand Up @@ -1163,66 +1163,6 @@
function() {
return self.getNative().createRange().item( 0 );
},
// If a table or list is fully selected.
function() {
var root, retval,
range = self.getRanges()[ 0 ],
ancestor = range.getCommonAncestor( 1, 1 ),
tags = { table:1,ul:1,ol:1,dl:1 };

for ( var t in tags ) {
if ( ( root = ancestor.getAscendant( t, 1 ) ) )
break;
}

if ( root ) {
// Enlarging the start boundary.
var testRange = new CKEDITOR.dom.range( self.root );
testRange.setStartAt( root, CKEDITOR.POSITION_AFTER_START );
testRange.setEnd( range.startContainer, range.startOffset );

var enlargeables = CKEDITOR.tools.extend( tags, CKEDITOR.dtd.$listItem, CKEDITOR.dtd.$tableContent ),
walker = new CKEDITOR.dom.walker( testRange ),
// Check the range is at the inner boundary of the structural element.
guard = function( walker, isEnd ) {
return function( node, isWalkOut ) {
if ( node.type == CKEDITOR.NODE_TEXT && ( !CKEDITOR.tools.trim( node.getText() ) || node.getParent().data( 'cke-bookmark' ) ) )
return true;

var tag;
if ( node.type == CKEDITOR.NODE_ELEMENT ) {
tag = node.getName();

// Bypass bogus br at the end of block.
if ( tag == 'br' && isEnd && node.equals( node.getParent().getBogus() ) )
return true;

if ( isWalkOut && tag in enlargeables || tag in CKEDITOR.dtd.$removeEmpty )
return true;
}

walker.halted = 1;
return false;
};
};

walker.guard = guard( walker );

if ( walker.checkBackward() && !walker.halted ) {
walker = new CKEDITOR.dom.walker( testRange );
testRange.setStart( range.endContainer, range.endOffset );
testRange.setEndAt( root, CKEDITOR.POSITION_BEFORE_END );
walker.guard = guard( walker, 1 );
if ( walker.checkForward() && !walker.halted )
retval = root.$;
}
}

if ( !retval )
throw 0;

return retval;
},
// Figure it out by checking if there's a single enclosed
// node of the range.
function() {
Expand Down

0 comments on commit a930e53

Please sign in to comment.