/** * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license */ /* globals document */ import DomConverter from '../../../src/view/domconverter'; import ViewEditable from '../../../src/view/editableelement'; import ViewDocument from '../../../src/view/document'; import ViewUIElement from '../../../src/view/uielement'; import ViewContainerElement from '../../../src/view/containerelement'; import { BR_FILLER, INLINE_FILLER, INLINE_FILLER_LENGTH, NBSP_FILLER } from '../../../src/view/filler'; import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils'; import global from '@ckeditor/ckeditor5-utils/src/dom/global'; describe( 'DomConverter', () => { let converter; testUtils.createSinonSandbox(); beforeEach( () => { converter = new DomConverter(); } ); describe( 'constructor()', () => { it( 'should create converter with BR block filler mode by default', () => { expect( converter.blockFillerMode ).to.equal( 'br' ); } ); it( 'should create converter with defined block mode filler', () => { converter = new DomConverter( { blockFillerMode: 'nbsp' } ); expect( converter.blockFillerMode ).to.equal( 'nbsp' ); } ); } ); describe( 'focus()', () => { let viewEditable, domEditable, domEditableParent, viewDocument; beforeEach( () => { viewDocument = new ViewDocument(); viewEditable = new ViewEditable( 'div' ); viewEditable._document = viewDocument; domEditable = document.createElement( 'div' ); domEditableParent = document.createElement( 'div' ); converter.bindElements( domEditable, viewEditable ); domEditable.setAttribute( 'contenteditable', 'true' ); domEditableParent.appendChild( domEditable ); document.body.appendChild( domEditableParent ); } ); afterEach( () => { converter.unbindDomElement( domEditable ); document.body.removeChild( domEditableParent ); document.body.focus(); } ); it( 'should call focus on corresponding DOM editable', () => { const focusSpy = testUtils.sinon.spy( domEditable, 'focus' ); converter.focus( viewEditable ); expect( focusSpy.calledOnce ).to.be.true; } ); it( 'should not focus already focused editable', () => { const focusSpy = testUtils.sinon.spy( domEditable, 'focus' ); converter.focus( viewEditable ); converter.focus( viewEditable ); expect( focusSpy.calledOnce ).to.be.true; } ); // https://github.com/ckeditor/ckeditor5-engine/issues/951 // https://github.com/ckeditor/ckeditor5-engine/issues/957 it( 'should actively prevent scrolling', () => { const scrollToSpy = testUtils.sinon.stub( global.window, 'scrollTo' ); const editableScrollLeftSpy = sinon.spy(); const editableScrollTopSpy = sinon.spy(); const parentScrollLeftSpy = sinon.spy(); const parentScrollTopSpy = sinon.spy(); const documentElementScrollLeftSpy = sinon.spy(); const documentElementScrollTopSpy = sinon.spy(); Object.defineProperties( domEditable, { scrollLeft: { get: () => 20, set: editableScrollLeftSpy }, scrollTop: { get: () => 200, set: editableScrollTopSpy } } ); Object.defineProperties( domEditableParent, { scrollLeft: { get: () => 40, set: parentScrollLeftSpy }, scrollTop: { get: () => 400, set: parentScrollTopSpy } } ); Object.defineProperties( global.document.documentElement, { scrollLeft: { get: () => 60, set: documentElementScrollLeftSpy }, scrollTop: { get: () => 600, set: documentElementScrollTopSpy } } ); testUtils.sinon.stub( global.window, 'scrollX' ).get( () => 10 ); testUtils.sinon.stub( global.window, 'scrollY' ).get( () => 100 ); converter.focus( viewEditable ); sinon.assert.calledWithExactly( scrollToSpy, 10, 100 ); sinon.assert.calledWithExactly( editableScrollLeftSpy, 20 ); sinon.assert.calledWithExactly( editableScrollTopSpy, 200 ); sinon.assert.calledWithExactly( parentScrollLeftSpy, 40 ); sinon.assert.calledWithExactly( parentScrollTopSpy, 400 ); sinon.assert.calledWithExactly( documentElementScrollLeftSpy, 60 ); sinon.assert.calledWithExactly( documentElementScrollTopSpy, 600 ); } ); } ); describe( 'DOM nodes type checking', () => { let text, element, documentFragment, comment; before( () => { text = document.createTextNode( 'test' ); element = document.createElement( 'div' ); documentFragment = document.createDocumentFragment(); comment = document.createComment( 'a' ); } ); describe( 'isElement()', () => { it( 'should return true for HTMLElement nodes', () => { expect( converter.isElement( element ) ).to.be.true; } ); it( 'should return false for other arguments', () => { expect( converter.isElement( text ) ).to.be.false; expect( converter.isElement( documentFragment ) ).to.be.false; expect( converter.isElement( comment ) ).to.be.false; expect( converter.isElement( {} ) ).to.be.false; } ); } ); describe( 'isDocumentFragment()', () => { it( 'should return true for HTMLElement nodes', () => { expect( converter.isDocumentFragment( documentFragment ) ).to.be.true; } ); it( 'should return false for other arguments', () => { expect( converter.isDocumentFragment( text ) ).to.be.false; expect( converter.isDocumentFragment( element ) ).to.be.false; expect( converter.isDocumentFragment( comment ) ).to.be.false; expect( converter.isDocumentFragment( {} ) ).to.be.false; } ); } ); describe( 'isComment()', () => { it( 'should return true for HTML comments', () => { expect( converter.isComment( comment ) ).to.be.true; } ); it( 'should return false for other arguments', () => { expect( converter.isComment( text ) ).to.be.false; expect( converter.isComment( element ) ).to.be.false; expect( converter.isComment( documentFragment ) ).to.be.false; expect( converter.isComment( {} ) ).to.be.false; } ); } ); } ); describe( 'isDomSelectionCorrect()', () => { function domSelection( anchorParent, anchorOffset, focusParent, focusOffset ) { const sel = document.getSelection(); sel.collapse( anchorParent, anchorOffset ); sel.extend( focusParent, focusOffset ); return sel; } let domP, domFillerTextNode, domUiSpan, domUiDeepSpan; beforeEach( () => { //
INLINE_FILLERfoo
. domP = document.createElement( 'p' ); domFillerTextNode = document.createTextNode( INLINE_FILLER + 'foo' ); domUiSpan = document.createElement( 'span' ); domUiDeepSpan = document.createElement( 'span' ); domUiSpan.appendChild( domUiDeepSpan ); const viewUiSpan = new ViewUIElement( 'span' ); const viewElementSpan = new ViewContainerElement( 'span' ); domP.appendChild( domFillerTextNode ); domP.appendChild( domUiSpan ); converter.bindElements( domUiSpan, viewUiSpan ); converter.bindElements( domUiDeepSpan, viewElementSpan ); document.body.appendChild( domP ); } ); afterEach( () => { domP.remove(); } ); it( 'should return true for correct dom selection', () => { //INLINE_FILLER{foo}
. const sel1 = domSelection( domFillerTextNode, INLINE_FILLER_LENGTH, domFillerTextNode, INLINE_FILLER_LENGTH + 3 ); expect( converter.isDomSelectionCorrect( sel1 ) ).to.be.true; //INLINE_FILLERfoo[]
. const sel2 = domSelection( domP, 1, domP, 1 ); expect( converter.isDomSelectionCorrect( sel2 ) ).to.be.true; //INLINE_FILLERfoo[]
. const sel3 = domSelection( domP, 2, domP, 2 ); expect( converter.isDomSelectionCorrect( sel3 ) ).to.be.true; } ); describe( 'should return false', () => { it( 'if anchor or focus is before filler node', () => { // Tests forward and backward selection. //[INLINE_FILLERfoo]
{INLINE_FILLERfoo}
I{NLINE_FILLERfoo}
INLINE_FILLER{foo
INLINE_FILLER{foo