|
|
@@ -11,6 +11,7 @@ import Range from '/ckeditor5/core/treemodel/range.js';
|
|
|
import Position from '/ckeditor5/core/treemodel/position.js';
|
|
|
import Element from '/ckeditor5/core/treemodel/element.js';
|
|
|
import Character from '/ckeditor5/core/treemodel/character.js';
|
|
|
+import Text from '/ckeditor5/core/treemodel/text.js';
|
|
|
import Document from '/ckeditor5/core/treemodel/document.js';
|
|
|
|
|
|
describe( 'Range', () => {
|
|
|
@@ -188,8 +189,6 @@ describe( 'Range', () => {
|
|
|
|
|
|
describe( 'getAllNodes', () => {
|
|
|
it( 'should iterate over all nodes which "starts" in the range', () => {
|
|
|
- let nodes = [];
|
|
|
-
|
|
|
const a = new Character( 'a' );
|
|
|
const b = new Character( 'b' );
|
|
|
const x = new Character( 'x' );
|
|
|
@@ -207,12 +206,20 @@ describe( 'Range', () => {
|
|
|
new Position( root, [ 1, 1 ] )
|
|
|
);
|
|
|
|
|
|
- for ( let node of range.getAllNodes() ) {
|
|
|
- nodes.push( node );
|
|
|
- }
|
|
|
+ let nodes = Array.from( range.getAllNodes() );
|
|
|
|
|
|
expect( nodes ).to.deep.equal( [ b, e2, x ] );
|
|
|
} );
|
|
|
+
|
|
|
+ it( 'should merge characters with same attributes', () => {
|
|
|
+ prepareRichRoot( root );
|
|
|
+
|
|
|
+ let range = new Range( new Position( root, [ 0, 0, 3 ] ), new Position( root, [ 3, 0, 2 ] ) );
|
|
|
+ let nodes = Array.from( range.getAllNodes( true ) );
|
|
|
+ let nodeNames = mapNodesToNames( nodes );
|
|
|
+
|
|
|
+ expect( nodeNames ).to.deep.equal( [ 'T:st', 'E:p', 'T:lorem ipsum', 'E:p', 'T:foo', 'E:p', 'T:bar', 'E:div', 'E:h', 'T:se' ] );
|
|
|
+ } );
|
|
|
} );
|
|
|
|
|
|
describe( 'containsPosition', () => {
|
|
|
@@ -429,12 +436,58 @@ describe( 'Range', () => {
|
|
|
it( 'should iterate over all top-level nodes of this range', () => {
|
|
|
let range = new Range( new Position( root, [ 0, 0, 3 ] ), new Position( root, [ 3, 0, 2 ] ) );
|
|
|
let nodes = Array.from( range.getTopLevelNodes() );
|
|
|
- let nodeNames = nodes.map( ( node ) => {
|
|
|
- return ( node instanceof Element ) ? 'E:' + node.name : 'C:' + node.character;
|
|
|
- } );
|
|
|
+ let nodeNames = mapNodesToNames( nodes );
|
|
|
|
|
|
expect( nodeNames ).to.deep.equal( [ 'C:s', 'C:t', 'E:p', 'E:p', 'E:p', 'C:s', 'C:e' ] );
|
|
|
} );
|
|
|
+
|
|
|
+ it( 'should merge characters with same attributes', () => {
|
|
|
+ let range = new Range( new Position( root, [ 0, 0, 3 ] ), new Position( root, [ 3, 0, 2 ] ) );
|
|
|
+ let nodes = Array.from( range.getTopLevelNodes( true ) );
|
|
|
+ let nodeNames = mapNodesToNames( nodes );
|
|
|
+
|
|
|
+ expect( nodeNames ).to.deep.equal( [ 'T:st', 'E:p', 'E:p', 'E:p', 'T:se' ] );
|
|
|
+ } );
|
|
|
+ } );
|
|
|
+
|
|
|
+ describe( 'getPositions', () => {
|
|
|
+ beforeEach( () => {
|
|
|
+ prepareRichRoot( root );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should iterate over all positions in this range', () => {
|
|
|
+ let expectedPaths = [
|
|
|
+ [ 1, 2 ], [ 1, 3 ],
|
|
|
+ [ 2 ], [ 2, 0 ], [ 2, 1 ], [ 2, 2 ], [ 2, 3 ],
|
|
|
+ [ 3 ], [ 3, 0 ], [ 3, 0, 0 ], [ 3, 0, 1 ], [ 3, 0, 2 ]
|
|
|
+ ];
|
|
|
+ let range = new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 3, 0, 2 ] ) );
|
|
|
+ let i = 0;
|
|
|
+
|
|
|
+ for ( let position of range.getPositions() ) {
|
|
|
+ expect( position.path ).to.deep.equal( expectedPaths[ i ] );
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+
|
|
|
+ expect( i ).to.equal( expectedPaths.length );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should merge characters with same attributes', () => {
|
|
|
+ let expectedPaths = [
|
|
|
+ [ 1, 2 ], [ 1, 3 ],
|
|
|
+ [ 2 ], [ 2, 0 ], [ 2, 3 ],
|
|
|
+ [ 3 ], [ 3, 0 ], [ 3, 0, 0 ], [ 3, 0, 2 ]
|
|
|
+ ];
|
|
|
+ let range = new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 3, 0, 2 ] ) );
|
|
|
+ let i = 0;
|
|
|
+
|
|
|
+ for ( let position of range.getPositions( true ) ) {
|
|
|
+ expect( position.path ).to.deep.equal( expectedPaths[ i ] );
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+
|
|
|
+ expect( i ).to.equal( expectedPaths.length );
|
|
|
+ } );
|
|
|
} );
|
|
|
|
|
|
describe( 'isFlat', () => {
|
|
|
@@ -453,6 +506,13 @@ describe( 'Range', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
+ function mapNodesToNames( nodes ) {
|
|
|
+ return nodes.map( ( node ) => {
|
|
|
+ return ( node instanceof Element ) ? 'E:' + node.name :
|
|
|
+ ( node instanceof Text ) ? 'T:' + node.text : 'C:' + node.character;
|
|
|
+ } );
|
|
|
+ }
|
|
|
+
|
|
|
function prepareRichRoot() {
|
|
|
root.insertChildren( 0, [
|
|
|
new Element( 'div', [], [
|