|
|
@@ -105,6 +105,35 @@ describe( 'Selection', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
+ describe( 'focus', () => {
|
|
|
+ let r3;
|
|
|
+
|
|
|
+ beforeEach( () => {
|
|
|
+ const r1 = Range.createFromParentsAndOffsets( root, 2, root, 4 );
|
|
|
+ const r2 = Range.createFromParentsAndOffsets( root, 4, root, 6 );
|
|
|
+ r3 = Range.createFromParentsAndOffsets( root, 1, root, 2 );
|
|
|
+ selection.addRange( r1 );
|
|
|
+ selection.addRange( r2 );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should return correct focus when last added range is not backward one', () => {
|
|
|
+ selection.addRange( r3 );
|
|
|
+
|
|
|
+ expect( selection.focus.isEqual( r3.end ) ).to.be.true;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should return correct focus when last added range is backward one', () => {
|
|
|
+ selection.addRange( r3, true );
|
|
|
+
|
|
|
+ expect( selection.focus.isEqual( r3.start ) ).to.be.true;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should return null if no ranges in selection', () => {
|
|
|
+ selection.removeAllRanges();
|
|
|
+ expect( selection.focus ).to.be.null;
|
|
|
+ } );
|
|
|
+ } );
|
|
|
+
|
|
|
describe( 'addRange', () => {
|
|
|
it( 'should copy added ranges and store multiple ranges', () => {
|
|
|
selection.addRange( liveRange );
|
|
|
@@ -183,7 +212,7 @@ describe( 'Selection', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
- describe( 'collapse', () => {
|
|
|
+ describe( 'collapse()', () => {
|
|
|
it( 'fires change:range', () => {
|
|
|
const spy = sinon.spy();
|
|
|
|
|
|
@@ -257,35 +286,7 @@ describe( 'Selection', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
- describe( 'focus', () => {
|
|
|
- let r3;
|
|
|
- beforeEach( () => {
|
|
|
- const r1 = Range.createFromParentsAndOffsets( root, 2, root, 4 );
|
|
|
- const r2 = Range.createFromParentsAndOffsets( root, 4, root, 6 );
|
|
|
- r3 = Range.createFromParentsAndOffsets( root, 1, root, 2 );
|
|
|
- selection.addRange( r1 );
|
|
|
- selection.addRange( r2 );
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should return correct focus when last added range is not backward one', () => {
|
|
|
- selection.addRange( r3 );
|
|
|
-
|
|
|
- expect( selection.focus.isEqual( r3.end ) ).to.be.true;
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should return correct focus when last added range is backward one', () => {
|
|
|
- selection.addRange( r3, true );
|
|
|
-
|
|
|
- expect( selection.focus.isEqual( r3.start ) ).to.be.true;
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should return null if no ranges in selection', () => {
|
|
|
- selection.removeAllRanges();
|
|
|
- expect( selection.focus ).to.be.null;
|
|
|
- } );
|
|
|
- } );
|
|
|
-
|
|
|
- describe( 'setFocus', () => {
|
|
|
+ describe( 'setFocus()', () => {
|
|
|
it( 'keeps all existing ranges and fires no change:range when no modifications needed', () => {
|
|
|
selection.addRange( range );
|
|
|
selection.addRange( liveRange );
|
|
|
@@ -456,7 +457,7 @@ describe( 'Selection', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
- describe( 'removeAllRanges', () => {
|
|
|
+ describe( 'removeAllRanges()', () => {
|
|
|
let spy;
|
|
|
|
|
|
it( 'should remove all stored ranges', () => {
|
|
|
@@ -490,7 +491,7 @@ describe( 'Selection', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
- describe( 'setRanges', () => {
|
|
|
+ describe( 'setRanges()', () => {
|
|
|
let newRanges, spy, oldRanges;
|
|
|
|
|
|
beforeEach( () => {
|
|
|
@@ -552,7 +553,7 @@ describe( 'Selection', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
- describe( 'setTo', () => {
|
|
|
+ describe( 'setTo()', () => {
|
|
|
it( 'should set selection to be same as given selection, using setRanges method', () => {
|
|
|
const spy = sinon.spy( selection, 'setRanges' );
|
|
|
|
|
|
@@ -569,7 +570,7 @@ describe( 'Selection', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
- describe( 'getFirstRange', () => {
|
|
|
+ describe( 'getFirstRange()', () => {
|
|
|
it( 'should return null if no ranges were added', () => {
|
|
|
expect( selection.getFirstRange() ).to.be.null;
|
|
|
} );
|
|
|
@@ -591,7 +592,7 @@ describe( 'Selection', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
- describe( 'getFirstPosition', () => {
|
|
|
+ describe( 'getFirstPosition()', () => {
|
|
|
it( 'should return null if no ranges were added', () => {
|
|
|
expect( selection.getFirstPosition() ).to.be.null;
|
|
|
} );
|
|
|
@@ -612,7 +613,7 @@ describe( 'Selection', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
- describe( 'getLastRange', () => {
|
|
|
+ describe( 'getLastRange()', () => {
|
|
|
it( 'should return null if no ranges were added', () => {
|
|
|
expect( selection.getLastRange() ).to.be.null;
|
|
|
} );
|
|
|
@@ -629,7 +630,7 @@ describe( 'Selection', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
- describe( 'getLastPosition', () => {
|
|
|
+ describe( 'getLastPosition()', () => {
|
|
|
it( 'should return null if no ranges were added', () => {
|
|
|
expect( selection.getLastPosition() ).to.be.null;
|
|
|
} );
|
|
|
@@ -645,7 +646,7 @@ describe( 'Selection', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
- describe( 'isEqual', () => {
|
|
|
+ describe( 'isEqual()', () => {
|
|
|
it( 'should return true if selections equal', () => {
|
|
|
selection.addRange( range1 );
|
|
|
selection.addRange( range2 );
|
|
|
@@ -703,7 +704,7 @@ describe( 'Selection', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
- describe( 'collapseToStart', () => {
|
|
|
+ describe( 'collapseToStart()', () => {
|
|
|
it( 'should collapse to start position and fire change event', () => {
|
|
|
selection.setRanges( [ range2, range1, range3 ] );
|
|
|
|
|
|
@@ -739,7 +740,7 @@ describe( 'Selection', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
- describe( 'collapseToEnd', () => {
|
|
|
+ describe( 'collapseToEnd()', () => {
|
|
|
it( 'should collapse to start position and fire change:range event', () => {
|
|
|
selection.setRanges( [ range2, range3, range1 ] );
|
|
|
|
|
|
@@ -775,7 +776,7 @@ describe( 'Selection', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
- describe( 'createFromSelection', () => {
|
|
|
+ describe( 'createFromSelection()', () => {
|
|
|
it( 'should return a Selection instance with same ranges and direction as given selection', () => {
|
|
|
selection.addRange( liveRange );
|
|
|
selection.addRange( range, true );
|
|
|
@@ -795,6 +796,44 @@ describe( 'Selection', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
+ describe( 'getSelectedElement()', () => {
|
|
|
+ let schema;
|
|
|
+
|
|
|
+ beforeEach( () => {
|
|
|
+ schema = new Schema();
|
|
|
+ schema.registerItem( 'p', '$block' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should return selected element', () => {
|
|
|
+ const { selection, model } = parse( '<p>foo</p>[<p>bar</p>]<p>baz</p>', schema );
|
|
|
+ const p = model.getChild( 1 );
|
|
|
+
|
|
|
+ expect( selection.getSelectedElement() ).to.equal( p );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should return null if there is more than one range', () => {
|
|
|
+ const { selection } = parse( '[<p>foo</p>][<p>bar</p>]<p>baz</p>', schema );
|
|
|
+
|
|
|
+ expect( selection.getSelectedElement() ).to.be.null;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should return null if there is no selection', () => {
|
|
|
+ expect( selection.getSelectedElement() ).to.be.null;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should return null if selection is not over single element #1', () => {
|
|
|
+ const { selection } = parse( '<p>foo</p>[<p>bar</p><p>baz}</p>', schema );
|
|
|
+
|
|
|
+ expect( selection.getSelectedElement() ).to.be.null;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should return null if selection is not over single element #2', () => {
|
|
|
+ const { selection } = parse( '<p>{bar}</p>', schema );
|
|
|
+
|
|
|
+ expect( selection.getSelectedElement() ).to.be.null;
|
|
|
+ } );
|
|
|
+ } );
|
|
|
+
|
|
|
describe( 'attributes interface', () => {
|
|
|
let rangeInFullP;
|
|
|
|
|
|
@@ -807,7 +846,7 @@ describe( 'Selection', () => {
|
|
|
rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
|
|
|
} );
|
|
|
|
|
|
- describe( 'setAttribute', () => {
|
|
|
+ describe( 'setAttribute()', () => {
|
|
|
it( 'should set given attribute on the selection', () => {
|
|
|
selection.setRanges( [ rangeInFullP ] );
|
|
|
selection.setAttribute( 'foo', 'bar' );
|
|
|
@@ -836,13 +875,13 @@ describe( 'Selection', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
- describe( 'getAttribute', () => {
|
|
|
+ describe( 'getAttribute()', () => {
|
|
|
it( 'should return undefined if element does not contain given attribute', () => {
|
|
|
expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
- describe( 'getAttributes', () => {
|
|
|
+ describe( 'getAttributes()', () => {
|
|
|
it( 'should return an iterator that iterates over all attributes set on selection', () => {
|
|
|
selection.setRanges( [ rangeInFullP ] );
|
|
|
selection.setAttribute( 'foo', 'bar' );
|
|
|
@@ -854,7 +893,7 @@ describe( 'Selection', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
- describe( 'getAttributeKeys', () => {
|
|
|
+ describe( 'getAttributeKeys()', () => {
|
|
|
it( 'should return iterator that iterates over all attribute keys set on selection', () => {
|
|
|
selection.setRanges( [ rangeInFullP ] );
|
|
|
selection.setAttribute( 'foo', 'bar' );
|
|
|
@@ -866,7 +905,7 @@ describe( 'Selection', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
- describe( 'hasAttribute', () => {
|
|
|
+ describe( 'hasAttribute()', () => {
|
|
|
it( 'should return true if element contains attribute with given key', () => {
|
|
|
selection.setRanges( [ rangeInFullP ] );
|
|
|
selection.setAttribute( 'foo', 'bar' );
|
|
|
@@ -879,7 +918,7 @@ describe( 'Selection', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
- describe( 'clearAttributes', () => {
|
|
|
+ describe( 'clearAttributes()', () => {
|
|
|
it( 'should remove all attributes from the element', () => {
|
|
|
selection.setRanges( [ rangeInFullP ] );
|
|
|
selection.setAttribute( 'foo', 'bar' );
|
|
|
@@ -912,7 +951,7 @@ describe( 'Selection', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
- describe( 'removeAttribute', () => {
|
|
|
+ describe( 'removeAttribute()', () => {
|
|
|
it( 'should remove attribute', () => {
|
|
|
selection.setRanges( [ rangeInFullP ] );
|
|
|
selection.setAttribute( 'foo', 'bar' );
|
|
|
@@ -942,7 +981,7 @@ describe( 'Selection', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
- describe( 'setAttributesTo', () => {
|
|
|
+ describe( 'setAttributesTo()', () => {
|
|
|
it( 'should remove all attributes set on element and set the given ones', () => {
|
|
|
selection.setAttribute( 'abc', 'xyz' );
|
|
|
selection.setAttributesTo( { foo: 'bar' } );
|
|
|
@@ -986,42 +1025,4 @@ describe( 'Selection', () => {
|
|
|
} );
|
|
|
} );
|
|
|
} );
|
|
|
-
|
|
|
- describe( 'getSelectedElement', () => {
|
|
|
- let schema;
|
|
|
-
|
|
|
- beforeEach( () => {
|
|
|
- schema = new Schema();
|
|
|
- schema.registerItem( 'p', '$block' );
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should return selected element', () => {
|
|
|
- const { selection, model } = parse( '<p>foo</p>[<p>bar</p>]<p>baz</p>', schema );
|
|
|
- const p = model.getChild( 1 );
|
|
|
-
|
|
|
- expect( selection.getSelectedElement() ).to.equal( p );
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should return null if there is more than one range', () => {
|
|
|
- const { selection } = parse( '[<p>foo</p>][<p>bar</p>]<p>baz</p>', schema );
|
|
|
-
|
|
|
- expect( selection.getSelectedElement() ).to.be.null;
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should return null if there is no selection', () => {
|
|
|
- expect( selection.getSelectedElement() ).to.be.null;
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should return null if selection is not over single element #1', () => {
|
|
|
- const { selection } = parse( '<p>foo</p>[<p>bar</p><p>baz}</p>', schema );
|
|
|
-
|
|
|
- expect( selection.getSelectedElement() ).to.be.null;
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should return null if selection is not over single element #2', () => {
|
|
|
- const { selection } = parse( '<p>{bar}</p>', schema );
|
|
|
-
|
|
|
- expect( selection.getSelectedElement() ).to.be.null;
|
|
|
- } );
|
|
|
- } );
|
|
|
} );
|