8
0

document.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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, stylesProcessor;
  14. testUtils.createSinonSandbox();
  15. beforeEach( () => {
  16. stylesProcessor = new StylesProcessor();
  17. domRoot = createElement( document, 'div', {
  18. id: 'editor',
  19. contenteditable: 'true'
  20. } );
  21. document.body.appendChild( domRoot );
  22. viewDocument = new Document( stylesProcessor );
  23. } );
  24. afterEach( () => {
  25. domRoot.remove();
  26. } );
  27. describe( 'constructor()', () => {
  28. it( 'should create Document with all properties', () => {
  29. expect( count( viewDocument.roots ) ).to.equal( 0 );
  30. expect( viewDocument ).to.have.property( 'isReadOnly' ).to.false;
  31. } );
  32. } );
  33. describe( 'getRoot()', () => {
  34. it( 'should return "main" root', () => {
  35. createViewRoot( viewDocument, 'div', 'main' );
  36. expect( count( viewDocument.roots ) ).to.equal( 1 );
  37. expect( viewDocument.getRoot() ).to.equal( viewDocument.roots.get( 'main' ) );
  38. } );
  39. it( 'should return named root', () => {
  40. createViewRoot( viewDocument, 'h1', 'header' );
  41. expect( count( viewDocument.roots ) ).to.equal( 1 );
  42. expect( viewDocument.getRoot( 'header' ) ).to.equal( viewDocument.roots.get( 'header' ) );
  43. } );
  44. it( 'should return null when trying to get non-existent root', () => {
  45. expect( viewDocument.getRoot( 'not-existing' ) ).to.null;
  46. } );
  47. } );
  48. describe( 'post-fixers', () => {
  49. it( 'should add a callback that is called on _callPostFixers', () => {
  50. const spy1 = sinon.spy();
  51. const spy2 = sinon.spy();
  52. const writerMock = {};
  53. viewDocument.registerPostFixer( spy1 );
  54. viewDocument.registerPostFixer( spy2 );
  55. sinon.assert.notCalled( spy1 );
  56. sinon.assert.notCalled( spy2 );
  57. viewDocument._callPostFixers( writerMock );
  58. sinon.assert.calledOnce( spy1 );
  59. sinon.assert.calledOnce( spy2 );
  60. sinon.assert.calledWithExactly( spy1, writerMock );
  61. sinon.assert.calledWithExactly( spy2, writerMock );
  62. } );
  63. it( 'should call post-fixer until all returns false', () => {
  64. let calls = 0;
  65. const spy1 = sinon.spy( () => calls++ < 2 );
  66. const spy2 = sinon.spy( () => calls++ < 2 );
  67. viewDocument.registerPostFixer( spy1 );
  68. viewDocument.registerPostFixer( spy2 );
  69. viewDocument._callPostFixers();
  70. expect( calls ).to.equal( 4 );
  71. } );
  72. } );
  73. } );