|
|
@@ -13,7 +13,7 @@ import DomConverter from '/ckeditor5/engine/treeview/domconverter.js';
|
|
|
import ViewDocumentFragment from '/ckeditor5/engine/treeview/documentfragment.js';
|
|
|
import { INLINE_FILLER, INLINE_FILLER_LENGTH, BR_FILLER, NBSP_FILLER, isBlockFiller } from '/ckeditor5/engine/treeview/filler.js';
|
|
|
|
|
|
-import { parse } from '/tests/engine/_utils/view.js';
|
|
|
+import { parse, stringify } from '/tests/engine/_utils/view.js';
|
|
|
|
|
|
import count from '/ckeditor5/utils/count.js';
|
|
|
import createElement from '/ckeditor5/utils/dom/createelement.js';
|
|
|
@@ -207,7 +207,7 @@ describe( 'DomConverter', () => {
|
|
|
expect( domChildren[ 1 ].childNodes.length ).to.equal( 1 );
|
|
|
} );
|
|
|
|
|
|
- it( 'should add bogus', () => {
|
|
|
+ it( 'should add filler', () => {
|
|
|
const viewP = parse( '<container:p></container:p>' );
|
|
|
|
|
|
const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
|
|
|
@@ -216,7 +216,7 @@ describe( 'DomConverter', () => {
|
|
|
expect( isBlockFiller( domChildren[ 0 ], converter.blockFiller ) ).to.be.true;
|
|
|
} );
|
|
|
|
|
|
- it( 'should add bogus according to fillerPositionOffset', () => {
|
|
|
+ it( 'should add filler according to fillerPositionOffset', () => {
|
|
|
const viewP = parse( '<container:p>foo</container:p>' );
|
|
|
viewP.getBlockFillerOffset = () => 0;
|
|
|
|
|
|
@@ -482,6 +482,286 @@ describe( 'DomConverter', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
+ describe( 'domChildrenToView', () => {
|
|
|
+ it( 'should convert children', () => {
|
|
|
+ const domImg = createElement( document, 'img' );
|
|
|
+ const domText = document.createTextNode( 'foo' );
|
|
|
+ const domP = createElement( document, 'p', null, [ domImg, domText ] );
|
|
|
+
|
|
|
+ const viewChildren = Array.from( converter.domChildrenToView( domP ) );
|
|
|
+
|
|
|
+ expect( viewChildren.length ).to.equal( 2 );
|
|
|
+ expect( stringify( viewChildren[ 0 ] ) ).to.equal( '<img></img>' );
|
|
|
+ expect( stringify( viewChildren[ 1 ] ) ).to.equal( 'foo' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should skip filler', () => {
|
|
|
+ const domFiller = converter.blockFiller( document );
|
|
|
+ const domP = createElement( document, 'p', null, domFiller );
|
|
|
+
|
|
|
+ const viewChildren = Array.from( converter.domChildrenToView( domP ) );
|
|
|
+
|
|
|
+ expect( viewChildren.length ).to.equal( 0 );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should pass options', () => {
|
|
|
+ const domText = document.createTextNode( 'foo' );
|
|
|
+ const domB = createElement( document, 'b', null, 'bar' );
|
|
|
+ const domP = createElement( document, 'p', null, [ domB, domText ] );
|
|
|
+
|
|
|
+ const viewChildren = Array.from( converter.domChildrenToView( domP, { withChildren: false } ) );
|
|
|
+
|
|
|
+ expect( viewChildren.length ).to.equal( 2 );
|
|
|
+ expect( stringify( viewChildren[ 0 ] ) ).to.equal( '<b></b>' );
|
|
|
+ expect( stringify( viewChildren[ 1 ] ) ).to.equal( 'foo' );
|
|
|
+ } );
|
|
|
+ } );
|
|
|
+
|
|
|
+ describe( 'domPositionToView', () => {
|
|
|
+ it( 'should converter position in text', () => {
|
|
|
+ const domText = document.createTextNode( 'foo' );
|
|
|
+ const domB = createElement( document, 'b', null, 'bar' );
|
|
|
+ const domP = createElement( document, 'p', null, [ domText, domB ] );
|
|
|
+
|
|
|
+ const viewP = parse( '<p>foo<b>bar</b></p>' );
|
|
|
+
|
|
|
+ converter.bindElements( domP, viewP );
|
|
|
+ converter.bindElements( domB, viewP.getChild( 0 ) );
|
|
|
+
|
|
|
+ const viewPosition = converter.domPositionToView( domText, 2 );
|
|
|
+
|
|
|
+ expect( stringify( viewP, viewPosition ) ).to.equal( '<p>fo{}o<b>bar</b></p>' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should converter position in element', () => {
|
|
|
+ const domText = document.createTextNode( 'foo' );
|
|
|
+ const domB = createElement( document, 'b', null, 'bar' );
|
|
|
+ const domP = createElement( document, 'p', null, [ domText, domB ] );
|
|
|
+
|
|
|
+ const viewP = parse( '<p>foo<b>bar</b></p>' );
|
|
|
+
|
|
|
+ converter.bindElements( domP, viewP );
|
|
|
+ converter.bindElements( domB, viewP.getChild( 0 ) );
|
|
|
+
|
|
|
+ const viewPosition = converter.domPositionToView( domP, 1 );
|
|
|
+
|
|
|
+ expect( stringify( viewP, viewPosition ) ).to.equal( '<p>foo[]<b>bar</b></p>' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should converter position at the beginning', () => {
|
|
|
+ const domText = document.createTextNode( 'foo' );
|
|
|
+ const domP = createElement( document, 'p', null, domText );
|
|
|
+
|
|
|
+ const viewP = parse( '<p>foo</p>' );
|
|
|
+
|
|
|
+ converter.bindElements( domP, viewP );
|
|
|
+
|
|
|
+ const viewPosition = converter.domPositionToView( domP, 0 );
|
|
|
+
|
|
|
+ expect( stringify( viewP, viewPosition ) ).to.equal( '<p>[]foo</p>' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should converter position inside block filler', () => {
|
|
|
+ const converter = new DomConverter( { blockFiller: NBSP_FILLER } );
|
|
|
+ const domFiller = NBSP_FILLER( document );
|
|
|
+ const domP = createElement( document, 'p', null, domFiller );
|
|
|
+
|
|
|
+ const viewP = parse( '<p></p>' );
|
|
|
+
|
|
|
+ converter.bindElements( domP, viewP );
|
|
|
+
|
|
|
+ const viewPosition = converter.domPositionToView( domFiller, 0 );
|
|
|
+
|
|
|
+ expect( stringify( viewP, viewPosition ) ).to.equal( '<p>[]</p>' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should converter position inside inline filler', () => {
|
|
|
+ const domFiller = document.createTextNode( INLINE_FILLER );
|
|
|
+ const domText = document.createTextNode( 'foo' );
|
|
|
+ const domB = createElement( document, 'b', null, domFiller );
|
|
|
+ const domP = createElement( document, 'p', null, [ domText, domB ] );
|
|
|
+
|
|
|
+ const viewP = parse( '<p>foo<b></b></p>' );
|
|
|
+
|
|
|
+ converter.bindElements( domP, viewP );
|
|
|
+ converter.bindElements( domB, viewP.getChild( 1 ) );
|
|
|
+
|
|
|
+ const viewPosition = converter.domPositionToView( domFiller, INLINE_FILLER_LENGTH );
|
|
|
+
|
|
|
+ expect( stringify( viewP, viewPosition ) ).to.equal( '<p>foo<b>[]</b></p>' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should converter position inside inline filler with text', () => {
|
|
|
+ const domFiller = document.createTextNode( INLINE_FILLER + 'bar' );
|
|
|
+ const domText = document.createTextNode( 'foo' );
|
|
|
+ const domB = createElement( document, 'b', null, domFiller );
|
|
|
+ const domP = createElement( document, 'p', null, [ domText, domB ] );
|
|
|
+
|
|
|
+ const viewP = parse( '<p>foo<b>bar</b></p>' );
|
|
|
+
|
|
|
+ converter.bindElements( domP, viewP );
|
|
|
+ converter.bindElements( domB, viewP.getChild( 1 ) );
|
|
|
+
|
|
|
+ const viewPosition = converter.domPositionToView( domFiller, INLINE_FILLER_LENGTH + 2 );
|
|
|
+
|
|
|
+ expect( viewPosition.offset ).to.equal( 2 );
|
|
|
+ expect( stringify( viewP, viewPosition ) ).to.equal( '<p>foo<b>ba{}r</b></p>' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should converter position inside inline filler with text at the beginning', () => {
|
|
|
+ const domFiller = document.createTextNode( INLINE_FILLER + 'bar' );
|
|
|
+ const domText = document.createTextNode( 'foo' );
|
|
|
+ const domB = createElement( document, 'b', null, domFiller );
|
|
|
+ const domP = createElement( document, 'p', null, [ domText, domB ] );
|
|
|
+
|
|
|
+ const viewP = parse( '<p>foo<b>bar</b></p>' );
|
|
|
+
|
|
|
+ converter.bindElements( domP, viewP );
|
|
|
+ converter.bindElements( domB, viewP.getChild( 1 ) );
|
|
|
+
|
|
|
+ const viewPosition = converter.domPositionToView( domFiller, INLINE_FILLER_LENGTH - 1 );
|
|
|
+
|
|
|
+ expect( viewPosition.offset ).to.equal( 0 );
|
|
|
+ expect( stringify( viewP, viewPosition ) ).to.equal( '<p>foo<b>{}bar</b></p>' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should converter position at the end', () => {
|
|
|
+ const domText = document.createTextNode( 'foo' );
|
|
|
+ const domP = createElement( document, 'p', null, domText );
|
|
|
+
|
|
|
+ const viewP = parse( '<p>foo</p>' );
|
|
|
+
|
|
|
+ converter.bindElements( domP, viewP );
|
|
|
+
|
|
|
+ const viewPosition = converter.domPositionToView( domP, 1 );
|
|
|
+
|
|
|
+ expect( stringify( viewP, viewPosition ) ).to.equal( '<p>foo[]</p>' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should return null if there is no corresponding parent node', () => {
|
|
|
+ const domText = document.createTextNode( 'foo' );
|
|
|
+ const domP = createElement( document, 'p', null, domText );
|
|
|
+
|
|
|
+ const viewPosition = converter.domPositionToView( domP, 0 );
|
|
|
+
|
|
|
+ expect( viewPosition ).to.be.null;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should return null if there is no corresponding sibling node', () => {
|
|
|
+ const domB = createElement( document, 'b', null, 'bar' );
|
|
|
+ const domText = document.createTextNode( 'foo' );
|
|
|
+ const domP = createElement( document, 'p', null, [ domB, domText ] );
|
|
|
+
|
|
|
+ const viewPosition = converter.domPositionToView( domP, 1 );
|
|
|
+
|
|
|
+ expect( viewPosition ).to.be.null;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should return null if there is no corresponding text node', () => {
|
|
|
+ const domText = document.createTextNode( 'foo' );
|
|
|
+
|
|
|
+ const viewPosition = converter.domPositionToView( domText, 1 );
|
|
|
+
|
|
|
+ expect( viewPosition ).to.be.null;
|
|
|
+ } );
|
|
|
+ } );
|
|
|
+
|
|
|
+ describe( 'domRangeToView', () => {
|
|
|
+ it( 'should converter DOM range', () => {
|
|
|
+ const domFoo = document.createTextNode( 'foo' );
|
|
|
+ const domBar = document.createTextNode( 'bar' );
|
|
|
+ const domB = createElement( document, 'b', null, domBar );
|
|
|
+ const domP = createElement( document, 'p', null, [ domFoo, domB ] );
|
|
|
+
|
|
|
+ const viewP = parse( '<p>foo<b>bar</b></p>' );
|
|
|
+
|
|
|
+ converter.bindElements( domP, viewP );
|
|
|
+ converter.bindElements( domB, viewP.getChild( 1 ) );
|
|
|
+
|
|
|
+ const domRange = new Range();
|
|
|
+ domRange.setStart( domFoo, 1 );
|
|
|
+ domRange.setEnd( domBar, 2 );
|
|
|
+
|
|
|
+ const viewRange = converter.domRangeToView( domRange );
|
|
|
+
|
|
|
+ expect( stringify( viewP, viewRange ) ).to.equal( '<p>f{oo<b>ba}r</b></p>' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should return null if start or end is null', () => {
|
|
|
+ const domFoo = document.createTextNode( 'foo' );
|
|
|
+ const domBar = document.createTextNode( 'bar' );
|
|
|
+ const domB = createElement( document, 'b', null, domBar );
|
|
|
+ createElement( document, 'p', null, [ domFoo, domB ] );
|
|
|
+
|
|
|
+ const domRange = new Range();
|
|
|
+ domRange.setStart( domFoo, 1 );
|
|
|
+ domRange.setEnd( domBar, 2 );
|
|
|
+
|
|
|
+ const viewRange = converter.domRangeToView( domRange );
|
|
|
+
|
|
|
+ expect( viewRange ).to.be.null;
|
|
|
+ } );
|
|
|
+ } );
|
|
|
+
|
|
|
+ describe( 'domSelectionToView', () => {
|
|
|
+ it( 'should converter selection', () => {
|
|
|
+ const domFoo = document.createTextNode( 'foo' );
|
|
|
+ const domBar = document.createTextNode( 'bar' );
|
|
|
+ const domB = createElement( document, 'b', null, domBar );
|
|
|
+ const domP = createElement( document, 'p', null, [ domFoo, domB ] );
|
|
|
+
|
|
|
+ const viewP = parse( '<p>foo<b>bar</b></p>' );
|
|
|
+
|
|
|
+ converter.bindElements( domP, viewP );
|
|
|
+ converter.bindElements( domB, viewP.getChild( 1 ) );
|
|
|
+
|
|
|
+ document.body.appendChild( domP );
|
|
|
+
|
|
|
+ const domRange = new Range();
|
|
|
+ domRange.setStart( domFoo, 1 );
|
|
|
+ domRange.setEnd( domBar, 2 );
|
|
|
+
|
|
|
+ const domSelection = document.getSelection();
|
|
|
+ domSelection.removeAllRanges();
|
|
|
+ domSelection.addRange( domRange );
|
|
|
+
|
|
|
+ const viewSelection = converter.domSelectionToView( domSelection );
|
|
|
+
|
|
|
+ expect( viewSelection.rangeCount ).to.equal( 1 );
|
|
|
+ expect( stringify( viewP, viewSelection.getFirstRange() ) ).to.equal( '<p>f{oo<b>ba}r</b></p>' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should converter empty selection to empty selection', () => {
|
|
|
+ const domSelection = document.getSelection();
|
|
|
+ domSelection.removeAllRanges();
|
|
|
+
|
|
|
+ const viewSelection = converter.domSelectionToView( domSelection );
|
|
|
+
|
|
|
+ expect( viewSelection.rangeCount ).to.equal( 0 );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should not add null ranges', () => {
|
|
|
+ const domFoo = document.createTextNode( 'foo' );
|
|
|
+ const domBar = document.createTextNode( 'bar' );
|
|
|
+ const domB = createElement( document, 'b', null, domBar );
|
|
|
+ const domP = createElement( document, 'p', null, [ domFoo, domB ] );
|
|
|
+
|
|
|
+ document.body.appendChild( domP );
|
|
|
+
|
|
|
+ const domRange = new Range();
|
|
|
+ domRange.setStart( domFoo, 1 );
|
|
|
+ domRange.setEnd( domBar, 2 );
|
|
|
+
|
|
|
+ const domSelection = document.getSelection();
|
|
|
+ domSelection.removeAllRanges();
|
|
|
+ domSelection.addRange( domRange );
|
|
|
+
|
|
|
+ const viewSelection = converter.domSelectionToView( domSelection );
|
|
|
+
|
|
|
+ expect( viewSelection.rangeCount ).to.equal( 0 );
|
|
|
+ } );
|
|
|
+ } );
|
|
|
+
|
|
|
describe( 'getCorrespondingView', () => {
|
|
|
it( 'should return corresponding view element if element is passed', () => {
|
|
|
const domElement = document.createElement( 'p' );
|