| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import createDocumentMock from '../../tests/view/_utils/createdocumentmock';
- import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
- import RootEditableElement from '../../src/view/rooteditableelement';
- import Range from '../../src/view/range';
- describe( 'EditableElement', () => {
- describe( 'document', () => {
- let element, docMock;
- beforeEach( () => {
- element = new RootEditableElement( 'div' );
- docMock = createDocumentMock();
- } );
- it( 'should allow to set document', () => {
- element._document = docMock;
- expect( element.document ).to.equal( docMock );
- } );
- it( 'should return undefined if document is not set', () => {
- expect( element.document ).to.be.undefined;
- } );
- it( 'should throw if trying to set document again', () => {
- element._document = docMock;
- const newDoc = createDocumentMock();
- expect( () => {
- element._document = newDoc;
- } ).to.throw( CKEditorError, 'view-editableelement-document-already-set: View document is already set.' );
- } );
- it( 'should be cloned properly', () => {
- element._document = docMock;
- const newElement = element.clone();
- expect( newElement.document ).to.equal( docMock );
- } );
- } );
- describe( 'isFocused', () => {
- let docMock, viewMain, viewHeader;
- beforeEach( () => {
- docMock = createDocumentMock();
- viewMain = new RootEditableElement( 'div' );
- viewMain._document = docMock;
- viewHeader = new RootEditableElement( 'h1' );
- viewHeader._document = docMock;
- viewHeader.rootName = 'header';
- } );
- it( 'should be observable', () => {
- const root = new RootEditableElement( 'div' );
- root._document = createDocumentMock();
- expect( root.isFocused ).to.be.false;
- const isFocusedSpy = sinon.spy();
- root.on( 'change:isFocused', isFocusedSpy );
- root.isFocused = true;
- expect( root.isFocused ).to.be.true;
- expect( isFocusedSpy.calledOnce ).to.be.true;
- } );
- it( 'should change isFocused when selection changes', () => {
- const rangeMain = Range.createFromParentsAndOffsets( viewMain, 0, viewMain, 0 );
- const rangeHeader = Range.createFromParentsAndOffsets( viewHeader, 0, viewHeader, 0 );
- docMock.selection._setTo( rangeMain );
- docMock.isFocused = true;
- expect( viewMain.isFocused ).to.be.true;
- expect( viewHeader.isFocused ).to.be.false;
- docMock.selection._setTo( [ rangeHeader ] );
- expect( viewMain.isFocused ).to.be.false;
- expect( viewHeader.isFocused ).to.be.true;
- } );
- it( 'should change isFocused when document.isFocus changes', () => {
- const rangeMain = Range.createFromParentsAndOffsets( viewMain, 0, viewMain, 0 );
- const rangeHeader = Range.createFromParentsAndOffsets( viewHeader, 0, viewHeader, 0 );
- docMock.selection._setTo( rangeMain );
- docMock.isFocused = true;
- expect( viewMain.isFocused ).to.be.true;
- expect( viewHeader.isFocused ).to.be.false;
- docMock.isFocused = false;
- expect( viewMain.isFocused ).to.be.false;
- expect( viewHeader.isFocused ).to.be.false;
- docMock.selection._setTo( [ rangeHeader ] );
- expect( viewMain.isFocused ).to.be.false;
- expect( viewHeader.isFocused ).to.be.false;
- } );
- } );
- describe( 'isReadOnly', () => {
- it( 'should be observable', () => {
- const root = new RootEditableElement( 'div' );
- root._document = createDocumentMock();
- expect( root.isReadOnly ).to.be.false;
- const isReadOnlySpy = sinon.spy();
- root.on( 'change:isReadOnly', isReadOnlySpy );
- root.isReadOnly = true;
- expect( root.isReadOnly ).to.be.true;
- expect( isReadOnlySpy.calledOnce ).to.be.true;
- } );
- it( 'should be bound to the document#isReadOnly', () => {
- const root = new RootEditableElement( 'div' );
- root._document = createDocumentMock();
- root.document.isReadOnly = false;
- expect( root.isReadOnly ).to.false;
- root.document.isReadOnly = true;
- expect( root.isReadOnly ).to.true;
- } );
- } );
- describe( 'getDocument', () => {
- it( 'should return document', () => {
- const docMock = createDocumentMock();
- const root = new RootEditableElement( 'div' );
- root._document = docMock;
- expect( root.document ).to.equal( docMock );
- } );
- } );
- } );
|