Skip to content
Permalink
Browse files

Merge pull request #355 from cksource/t/17010

Range#shrink method should allow for skipping bogus BR elements
  • Loading branch information...
mlewand committed May 5, 2017
2 parents 77664e8 + 939e358 commit 2be3a01663e65edd9722cd854dbb79c016f7957a
Showing with 49 additions and 4 deletions.
  1. +1 −0 CHANGES.md
  2. +19 −3 core/dom/range.js
  3. +8 −1 tests/core/dom/range/shrink.html
  4. +21 −0 tests/core/dom/range/shrink.js
@@ -15,6 +15,7 @@ New Features:
* [#16818](http://dev.ckeditor.com/ticket/16818): Added table cell height parsing in the [Paste from Word](http://ckeditor.com/addon/pastefromword) plugin.
* [#16850](http://dev.ckeditor.com/ticket/16850): Added a new [`config.enableContextMenu`](http://docs.ckeditor.com/#!/api/CKEDITOR.config-cfg-enableContextMenu) configuration option for enabling and disabling the [context menu](http://ckeditor.com/addon/contextmenu).
* [#16937](http://dev.ckeditor.com/ticket/16937): The `command` parameter in [CKEDITOR.editor.getCommandKeystroke](http://docs.ckeditor.dev/#!/api/CKEDITOR.editor-method-getCommandKeystroke) now also accepts a command name as an argument.
* [#17010](http://dev.ckeditor.com/ticket/17010): The [CKEDITOR.dom.range.shrink](http://docs.ckeditor.com/#!/api/CKEDITOR.dom.range-method-shrink) now allows for skipping bogus `br` elements.

Fixed Issues:

@@ -1724,9 +1724,19 @@ CKEDITOR.dom.range = function( root ) {
* * {@link CKEDITOR#SHRINK_TEXT} - Shrink the range boudaries to anchor by the side of enclosed text
* node, range remains if there's no text nodes on boundaries at all.
*
* @param {Boolean} selectContents Whether result range anchors at the inner OR outer boundary of the node.
* @param {Boolean} [selectContents=false] Whether result range anchors at the inner OR outer boundary of the node.
* @param {Boolean/Object} [options=true] If this parameter is of a boolean type, it's treated as
* `options.shrinkOnBlockBoundary`. This parameter was added in 4.7.0.
* @param {Boolean} [options.shrinkOnBlockBoundary=true] Whether block boundary should be included in
* shrinked range.
* @param {Boolean} [options.skipBogus=false] Whether bogus `<br>` elements should be ignored while
* `mode` is set to {@link CKEDITOR#SHRINK_TEXT}. This option was added in 4.7.0.
*/
shrink: function( mode, selectContents, shrinkOnBlockBoundary ) {
shrink: function( mode, selectContents, options ) {
var shrinkOnBlockBoundary = typeof options === 'boolean' ? options :
( options && typeof options.shrinkOnBlockBoundary === 'boolean' ? options.shrinkOnBlockBoundary : true ),
skipBogus = options && options.skipBogus;

// Unable to shrink a collapsed range.
if ( !this.collapsed ) {
mode = mode || CKEDITOR.SHRINK_TEXT;
@@ -1767,14 +1777,20 @@ CKEDITOR.dom.range = function( root ) {
}

var walker = new CKEDITOR.dom.walker( walkerRange ),
isBookmark = CKEDITOR.dom.walker.bookmark();
isBookmark = CKEDITOR.dom.walker.bookmark(),
isBogus = CKEDITOR.dom.walker.bogus();

walker.evaluator = function( node ) {
return node.type == ( mode == CKEDITOR.SHRINK_ELEMENT ? CKEDITOR.NODE_ELEMENT : CKEDITOR.NODE_TEXT );
};

var currentElement;
walker.guard = function( node, movingOut ) {
// Skipping bogus before other cases (#17010).
if ( skipBogus && isBogus( node ) ) {
return true;
}

if ( isBookmark( node ) )
return true;

@@ -5,4 +5,11 @@
<p id="_ShrinkP4"> Test <b id="_ShrinkB4">shrink <i id="_ShrinkI4"><img /></i>element</b>.</p>
<p id="_P1">some text <i id="_I2">and</i><b id="_B2"><a id="_L1" href="javascript:void(0)">a link</a></b></p>
</div>
<div id="editable_playground" contenteditable="true"></div>
<textarea id="bogus_table">
<table>
<tr>
[<td>Cell 1.1<br></td>]
</tr>
</table>
</textarea>
<div id="editable_playground" contenteditable="true"></div>
@@ -325,6 +325,27 @@
var range = bender.tools.setHtmlWithRange( ct, source )[ 0 ];
range.shrink( CKEDITOR.SHRINK_TEXT );
assert.areSame( source, bender.tools.getHtmlWithRanges( ct, new CKEDITOR.dom.rangeList( [ range ] ) ) );
},

// (#17010)
'test shrink with skipBogus param - SHRINK_TEXT': function() {
var ct = doc.getById( 'editable_playground' ),
source = CKEDITOR.document.getById( 'bogus_table' ).getValue(),
range = bender.tools.setHtmlWithRange( ct, source )[ 0 ];

range.shrink( CKEDITOR.SHRINK_TEXT, false, { skipBogus: true } );
assert.areSame( 'Cell 1.1', range.cloneContents().getHtml() );
},

// (#17010)
'test shrink with skipBogus param - SHRINK_ELEMENT': function() {
var ct = doc.getById( 'editable_playground' ),
source = CKEDITOR.document.getById( 'bogus_table' ).getValue(),
range = bender.tools.setHtmlWithRange( ct, source )[ 0 ],
cell = ct.findOne( 'td' );

range.shrink( CKEDITOR.SHRINK_ELEMENT, false, { skipBogus: true } );
assert.areSame( cell.getOuterHtml(), range.cloneContents().getHtml() );
}
};

0 comments on commit 2be3a01

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