| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- /**
- * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /* globals Range, DocumentFragment, HTMLElement, document */
- /* bender-tags: view, domconverter, browser-only */
- import ViewText from '/ckeditor5/engine/view/text.js';
- import ViewElement from '/ckeditor5/engine/view/element.js';
- import DomConverter from '/ckeditor5/engine/view/domconverter.js';
- import ViewDocumentFragment from '/ckeditor5/engine/view/documentfragment.js';
- import { INLINE_FILLER, INLINE_FILLER_LENGTH, isBlockFiller } from '/ckeditor5/engine/view/filler.js';
- import { parse } from '/tests/engine/_utils/view.js';
- import createElement from '/ckeditor5/utils/dom/createelement.js';
- describe( 'DomConverter', () => {
- let converter;
- before( () => {
- converter = new DomConverter();
- } );
- describe( 'viewToDom', () => {
- it( 'should create tree of DOM elements from view elements', () => {
- const viewImg = new ViewElement( 'img' );
- const viewText = new ViewText( 'foo' );
- const viewP = new ViewElement( 'p' );
- viewP.setAttribute( 'class', 'foo' );
- viewP.appendChildren( viewImg );
- viewP.appendChildren( viewText );
- const domImg = document.createElement( 'img' );
- converter.bindElements( domImg, viewImg );
- const domP = converter.viewToDom( viewP, document );
- expect( domP ).to.be.an.instanceof( HTMLElement );
- expect( domP.tagName ).to.equal( 'P' );
- expect( domP.getAttribute( 'class' ) ).to.equal( 'foo' );
- expect( domP.attributes.length ).to.equal( 1 );
- expect( domP.childNodes.length ).to.equal( 2 );
- expect( domP.childNodes[ 0 ].tagName ).to.equal( 'IMG' );
- expect( domP.childNodes[ 1 ].data ).to.equal( 'foo' );
- expect( converter.getCorrespondingView( domP ) ).not.to.equal( viewP );
- expect( converter.getCorrespondingView( domP.childNodes[ 0 ] ) ).to.equal( viewImg );
- } );
- it( 'should create tree of DOM elements from view elements and bind elements', () => {
- const viewImg = new ViewElement( 'img' );
- const viewText = new ViewText( 'foo' );
- const viewP = new ViewElement( 'p' );
- viewP.setAttribute( 'class', 'foo' );
- viewP.appendChildren( viewImg );
- viewP.appendChildren( viewText );
- const domP = converter.viewToDom( viewP, document, { bind: true } );
- expect( domP ).to.be.an.instanceof( HTMLElement );
- expect( domP.tagName ).to.equal( 'P' );
- expect( domP.getAttribute( 'class' ) ).to.equal( 'foo' );
- expect( domP.attributes.length ).to.equal( 1 );
- expect( domP.childNodes.length ).to.equal( 2 );
- expect( domP.childNodes[ 0 ].tagName ).to.equal( 'IMG' );
- expect( domP.childNodes[ 1 ].data ).to.equal( 'foo' );
- expect( converter.getCorrespondingView( domP ) ).to.equal( viewP );
- expect( converter.getCorrespondingView( domP.childNodes[ 0 ] ) ).to.equal( viewP.getChild( 0 ) );
- } );
- it( 'should support unicode', () => {
- const viewText = new ViewText( 'நிலைக்கு' );
- const viewP = new ViewElement( 'p', null, viewText );
- const domP = converter.viewToDom( viewP, document, { bind: true } );
- expect( domP.childNodes.length ).to.equal( 1 );
- expect( domP.childNodes[ 0 ].data ).to.equal( 'நிலைக்கு' );
- expect( converter.getCorrespondingView( domP ) ).to.equal( viewP );
- expect( converter.getCorrespondingView( domP.childNodes[ 0 ] ) ).to.equal( viewP.getChild( 0 ) );
- } );
- it( 'should create tree of DOM elements from view element without children', () => {
- const viewImg = new ViewElement( 'img' );
- const viewText = new ViewText( 'foo' );
- const viewP = new ViewElement( 'p' );
- viewP.setAttribute( 'class', 'foo' );
- viewP.appendChildren( viewImg );
- viewP.appendChildren( viewText );
- const domImg = document.createElement( 'img' );
- converter.bindElements( domImg, viewImg );
- const domP = converter.viewToDom( viewP, document, { withChildren: false } );
- expect( domP ).to.be.an.instanceof( HTMLElement );
- expect( domP.tagName ).to.equal( 'P' );
- expect( domP.getAttribute( 'class' ) ).to.equal( 'foo' );
- expect( domP.attributes.length ).to.equal( 1 );
- expect( domP.childNodes.length ).to.equal( 0 );
- expect( converter.getCorrespondingView( domP ) ).not.to.equal( viewP );
- } );
- it( 'should create DOM document fragment from view document fragment and bind elements', () => {
- const viewImg = new ViewElement( 'img' );
- const viewText = new ViewText( 'foo' );
- const viewFragment = new ViewDocumentFragment();
- viewFragment.appendChildren( viewImg );
- viewFragment.appendChildren( viewText );
- const domFragment = converter.viewToDom( viewFragment, document, { bind: true } );
- expect( domFragment ).to.be.an.instanceof( DocumentFragment );
- expect( domFragment.childNodes.length ).to.equal( 2 );
- expect( domFragment.childNodes[ 0 ].tagName ).to.equal( 'IMG' );
- expect( domFragment.childNodes[ 1 ].data ).to.equal( 'foo' );
- expect( converter.getCorrespondingView( domFragment ) ).to.equal( viewFragment );
- expect( converter.getCorrespondingView( domFragment.childNodes[ 0 ] ) ).to.equal( viewFragment.getChild( 0 ) );
- } );
- it( 'should create DOM document fragment from view document without children', () => {
- const viewImg = new ViewElement( 'img' );
- const viewText = new ViewText( 'foo' );
- const viewFragment = new ViewDocumentFragment();
- viewFragment.appendChildren( viewImg );
- viewFragment.appendChildren( viewText );
- const domImg = document.createElement( 'img' );
- converter.bindElements( domImg, viewImg );
- const domFragment = converter.viewToDom( viewFragment, document, { withChildren: false } );
- expect( domFragment ).to.be.an.instanceof( DocumentFragment );
- expect( domFragment.childNodes.length ).to.equal( 0 );
- expect( converter.getCorrespondingView( domFragment ) ).not.to.equal( viewFragment );
- } );
- it( 'should return already bind document fragment', () => {
- const domFragment = document.createDocumentFragment();
- const viewFragment = new ViewDocumentFragment();
- converter.bindDocumentFragments( domFragment, viewFragment );
- const domFragment2 = converter.viewToDom( viewFragment );
- expect( domFragment2 ).to.equal( domFragment );
- } );
- } );
- describe( 'viewChildrenToDom', () => {
- it( 'should convert children', () => {
- const viewP = parse( '<container:p>foo<attribute:b>bar</attribute:b></container:p>' );
- const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
- expect( domChildren.length ).to.equal( 2 );
- expect( domChildren[ 0 ].data ).to.equal( 'foo' );
- expect( domChildren[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
- expect( domChildren[ 1 ].childNodes.length ).to.equal( 1 );
- } );
- it( 'should add filler', () => {
- const viewP = parse( '<container:p></container:p>' );
- const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
- expect( domChildren.length ).to.equal( 1 );
- expect( isBlockFiller( domChildren[ 0 ], converter.blockFiller ) ).to.be.true;
- } );
- it( 'should add filler according to fillerPositionOffset', () => {
- const viewP = parse( '<container:p>foo</container:p>' );
- viewP.getFillerOffset = () => 0;
- const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
- expect( domChildren.length ).to.equal( 2 );
- expect( isBlockFiller( domChildren[ 0 ], converter.blockFiller ) ).to.be.true;
- expect( domChildren[ 1 ].data ).to.equal( 'foo' );
- } );
- it( 'should pass options', () => {
- const viewP = parse( '<container:p>foo<attribute:b>bar</attribute:b></container:p>' );
- const domChildren = Array.from( converter.viewChildrenToDom( viewP, document, { withChildren: false } ) );
- expect( domChildren.length ).to.equal( 2 );
- expect( domChildren[ 0 ].data ).to.equal( 'foo' );
- expect( domChildren[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
- expect( domChildren[ 1 ].childNodes.length ).to.equal( 0 );
- } );
- } );
- describe( 'viewPositionToDom', () => {
- it( 'should convert the position in the text', () => {
- const domFoo = document.createTextNode( 'foo' );
- const domP = createElement( document, 'p', null, domFoo );
- const { view: viewP, selection } = parse( '<container:p>fo{}o</container:p>' );
- converter.bindElements( domP, viewP );
- const viewPosition = selection.getFirstPosition();
- const domPosition = converter.viewPositionToDom( viewPosition );
- expect( domPosition.offset ).to.equal( 2 );
- expect( domPosition.parent ).to.equal( domFoo );
- } );
- it( 'should support unicode', () => {
- const domText = document.createTextNode( 'நிலைக்கு' );
- const domP = createElement( document, 'p', null, domText );
- const { view: viewP, selection } = parse( '<container:p>நிலை{}க்கு</container:p>' );
- converter.bindElements( domP, viewP );
- const viewPosition = selection.getFirstPosition();
- const domPosition = converter.viewPositionToDom( viewPosition );
- expect( domPosition.offset ).to.equal( 4 );
- expect( domPosition.parent ).to.equal( domText );
- } );
- it( 'should convert the position in the empty element', () => {
- const domP = createElement( document, 'p' );
- const { view: viewP, selection } = parse( '<container:p>[]</container:p>' );
- converter.bindElements( domP, viewP );
- const viewPosition = selection.getFirstPosition();
- const domPosition = converter.viewPositionToDom( viewPosition );
- expect( domPosition.offset ).to.equal( 0 );
- expect( domPosition.parent ).to.equal( domP );
- } );
- it( 'should convert the position in the non-empty element', () => {
- const domB = createElement( document, 'b', null, 'foo' );
- const domP = createElement( document, 'p', null, domB );
- const { view: viewP, selection } = parse( '<container:p><attribute:b>foo</attribute:b>[]</container:p>' );
- converter.bindElements( domP, viewP );
- converter.bindElements( domB, viewP.getChild( 0 ) );
- const viewPosition = selection.getFirstPosition();
- const domPosition = converter.viewPositionToDom( viewPosition );
- expect( domPosition.offset ).to.equal( 1 );
- expect( domPosition.parent ).to.equal( domP );
- } );
- it( 'should convert the position after text', () => {
- const domP = createElement( document, 'p', null, 'foo' );
- const { view: viewP, selection } = parse( '<container:p>foo[]</container:p>' );
- converter.bindElements( domP, viewP );
- const viewPosition = selection.getFirstPosition();
- const domPosition = converter.viewPositionToDom( viewPosition );
- expect( domPosition.offset ).to.equal( 1 );
- expect( domPosition.parent ).to.equal( domP );
- } );
- it( 'should convert the position before text', () => {
- const domP = createElement( document, 'p', null, 'foo' );
- const { view: viewP, selection } = parse( '<container:p>[]foo</container:p>' );
- converter.bindElements( domP, viewP );
- const viewPosition = selection.getFirstPosition();
- const domPosition = converter.viewPositionToDom( viewPosition );
- expect( domPosition.offset ).to.equal( 0 );
- expect( domPosition.parent ).to.equal( domP );
- } );
- it( 'should update offset if DOM text node starts with inline filler', () => {
- const domFoo = document.createTextNode( INLINE_FILLER + 'foo' );
- const domP = createElement( document, 'p', null, domFoo );
- const { view: viewP, selection } = parse( '<container:p>fo{}o</container:p>' );
- converter.bindElements( domP, viewP );
- const viewPosition = selection.getFirstPosition();
- const domPosition = converter.viewPositionToDom( viewPosition );
- expect( domPosition.offset ).to.equal( INLINE_FILLER_LENGTH + 2 );
- expect( domPosition.parent ).to.equal( domFoo );
- } );
- it( 'should move the position to the text node if the position is where inline filler is', () => {
- const domFiller = document.createTextNode( INLINE_FILLER );
- const domP = createElement( document, 'p', null, domFiller );
- const { view: viewP, selection } = parse( '<container:p>[]</container:p>' );
- converter.bindElements( domP, viewP );
- const viewPosition = selection.getFirstPosition();
- const domPosition = converter.viewPositionToDom( viewPosition );
- expect( domPosition.offset ).to.equal( INLINE_FILLER_LENGTH );
- expect( domPosition.parent ).to.equal( domFiller );
- } );
- } );
- describe( 'viewRangeToDom', () => {
- it( 'should convert view range to DOM range', () => {
- const domFoo = document.createTextNode( 'foo' );
- const domP = createElement( document, 'p', null, domFoo );
- const { view: viewP, selection } = parse( '<container:p>fo{o]</container:p>' );
- converter.bindElements( domP, viewP );
- const viewRange = selection.getFirstRange();
- const domRange = converter.viewRangeToDom( viewRange );
- expect( domRange ).to.be.instanceof( Range );
- expect( domRange.startContainer ).to.equal( domFoo );
- expect( domRange.startOffset ).to.equal( 2 );
- expect( domRange.endContainer ).to.equal( domP );
- expect( domRange.endOffset ).to.equal( 1 );
- } );
- } );
- } );
|