8
0

textproxy.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: view */
  6. 'use strict';
  7. import TextProxy from '/ckeditor5/engine/view/textproxy.js';
  8. import Text from '/ckeditor5/engine/view/text.js';
  9. import ContainerElement from '/ckeditor5/engine/view/containerelement.js';
  10. import DocumentFragment from '/ckeditor5/engine/view/documentfragment.js';
  11. import RootEditableElement from '/ckeditor5/engine/view/rooteditableelement.js';
  12. import createDocumentMock from '/tests/engine/view/_utils/createdocumentmock.js';
  13. describe( 'TextProxy', () => {
  14. let text, parent, wrapper, textProxy;
  15. beforeEach( () => {
  16. text = new Text( 'abcdefgh' );
  17. parent = new ContainerElement( 'p', [], [ text ] );
  18. wrapper = new ContainerElement( 'div', [], parent );
  19. textProxy = new TextProxy( text, 2, 3 );
  20. } );
  21. describe( 'constructor', () => {
  22. it( 'should create TextProxy instance with specified properties', () => {
  23. expect( textProxy ).to.have.property( 'parent' ).to.equal( parent );
  24. expect( textProxy ).to.have.property( '_data' ).to.equal( 'cde' );
  25. expect( textProxy ).to.have.property( '_textNode' ).to.equal( text );
  26. expect( textProxy ).to.have.property( '_index' ).to.equal( 2 );
  27. } );
  28. } );
  29. describe( 'getDocument', () => {
  30. it( 'should return null if any parent has not set Document', () => {
  31. expect( textProxy.getDocument() ).to.be.null;
  32. } );
  33. it( 'should return Document attached to the parent element', () => {
  34. const docMock = createDocumentMock();
  35. const root = new RootEditableElement( docMock, 'div' );
  36. wrapper.parent = root;
  37. expect( textProxy.getDocument() ).to.equal( docMock );
  38. } );
  39. it( 'should return null if element is inside DocumentFragment', () => {
  40. new DocumentFragment( [ wrapper ] );
  41. expect( textProxy.getDocument() ).to.be.null;
  42. } );
  43. } );
  44. describe( 'getRoot', () => {
  45. it( 'should return root element', () => {
  46. const root = new RootEditableElement( createDocumentMock(), 'div' );
  47. wrapper.parent = root;
  48. expect( textProxy.getRoot() ).to.equal( root );
  49. } );
  50. } );
  51. describe( 'getAncestors', () => {
  52. it( 'should return array of ancestors', () => {
  53. const result = textProxy.getAncestors();
  54. expect( result ).to.be.an( 'array' );
  55. expect( result ).to.length( 2 );
  56. expect( result[ 0 ] ).to.equal( wrapper );
  57. expect( result[ 1 ] ).to.equal( parent );
  58. } );
  59. it( 'should return array of ancestors starting from parent `parentFirst`', () => {
  60. const result = textProxy.getAncestors( { parentFirst: true } );
  61. expect( result.length ).to.equal( 2 );
  62. expect( result[ 0 ] ).to.equal( parent );
  63. expect( result[ 1 ] ).to.equal( wrapper );
  64. } );
  65. it( 'should return array including node itself `includeNode`', () => {
  66. const result = textProxy.getAncestors( { includeNode: true } );
  67. expect( result ).to.be.an( 'array' );
  68. expect( result ).to.length( 3 );
  69. expect( result[ 0 ] ).to.equal( wrapper );
  70. expect( result[ 1 ] ).to.equal( parent );
  71. expect( result[ 2 ] ).to.equal( textProxy );
  72. } );
  73. it( 'should return array of ancestors including node itself `includeNode` starting from parent `parentFirst`', () => {
  74. const result = textProxy.getAncestors( { includeNode: true, parentFirst: true } );
  75. expect( result.length ).to.equal( 3 );
  76. expect( result[ 0 ] ).to.equal( textProxy );
  77. expect( result[ 1 ] ).to.equal( parent );
  78. expect( result[ 2 ] ).to.equal( wrapper );
  79. } );
  80. } );
  81. } );