document.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals document */
  6. import createElement from '@ckeditor/ckeditor5-utils/src/dom/createelement';
  7. import Document from '../../src/view/document';
  8. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  9. import count from '@ckeditor/ckeditor5-utils/src/count';
  10. import createViewRoot from './_utils/createroot';
  11. import { StylesProcessor } from '../../src/view/stylesmap';
  12. describe( 'Document', () => {
  13. let domRoot, viewDocument;
  14. testUtils.createSinonSandbox();
  15. beforeEach( () => {
  16. domRoot = createElement( document, 'div', {
  17. id: 'editor',
  18. contenteditable: 'true'
  19. } );
  20. document.body.appendChild( domRoot );
  21. viewDocument = new Document( new StylesProcessor() );
  22. } );
  23. afterEach( () => {
  24. domRoot.remove();
  25. } );
  26. describe( 'constructor()', () => {
  27. it( 'should create Document with all properties', () => {
  28. expect( count( viewDocument.roots ) ).to.equal( 0 );
  29. expect( viewDocument ).to.have.property( 'isReadOnly' ).to.false;
  30. } );
  31. } );
  32. describe( 'getRoot()', () => {
  33. it( 'should return "main" root', () => {
  34. createViewRoot( viewDocument, 'div', 'main' );
  35. expect( count( viewDocument.roots ) ).to.equal( 1 );
  36. expect( viewDocument.getRoot() ).to.equal( viewDocument.roots.get( 'main' ) );
  37. } );
  38. it( 'should return named root', () => {
  39. createViewRoot( viewDocument, 'h1', 'header' );
  40. expect( count( viewDocument.roots ) ).to.equal( 1 );
  41. expect( viewDocument.getRoot( 'header' ) ).to.equal( viewDocument.roots.get( 'header' ) );
  42. } );
  43. it( 'should return null when trying to get non-existent root', () => {
  44. expect( viewDocument.getRoot( 'not-existing' ) ).to.null;
  45. } );
  46. } );
  47. describe( 'post-fixers', () => {
  48. it( 'should add a callback that is called on _callPostFixers', () => {
  49. const spy1 = sinon.spy();
  50. const spy2 = sinon.spy();
  51. const writerMock = {};
  52. viewDocument.registerPostFixer( spy1 );
  53. viewDocument.registerPostFixer( spy2 );
  54. sinon.assert.notCalled( spy1 );
  55. sinon.assert.notCalled( spy2 );
  56. viewDocument._callPostFixers( writerMock );
  57. sinon.assert.calledOnce( spy1 );
  58. sinon.assert.calledOnce( spy2 );
  59. sinon.assert.calledWithExactly( spy1, writerMock );
  60. sinon.assert.calledWithExactly( spy2, writerMock );
  61. } );
  62. it( 'should call post-fixer until all returns false', () => {
  63. let calls = 0;
  64. const spy1 = sinon.spy( () => calls++ < 2 );
  65. const spy2 = sinon.spy( () => calls++ < 2 );
  66. viewDocument.registerPostFixer( spy1 );
  67. viewDocument.registerPostFixer( spy2 );
  68. viewDocument._callPostFixers();
  69. expect( calls ).to.equal( 4 );
  70. } );
  71. } );
  72. } );