document.js 3.1 KB

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