textproxy.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. it( 'should get text from specified offset to the end of textNode if length is not defined', () => {
  29. textProxy = new TextProxy( text, 2 );
  30. expect( textProxy ).to.have.property( '_data' ).to.equal( 'cdefgh' );
  31. expect( textProxy ).to.have.property( '_index' ).to.equal( 2 );
  32. } );
  33. } );
  34. describe( 'getDocument', () => {
  35. it( 'should return null if any parent has not set Document', () => {
  36. expect( textProxy.getDocument() ).to.be.null;
  37. } );
  38. it( 'should return null if has no parent', () => {
  39. text = new Text( 'abcdefgh' );
  40. textProxy = new TextProxy( text, 1 );
  41. expect( textProxy.getDocument() ).to.be.null;
  42. } );
  43. it( 'should return Document attached to the parent element', () => {
  44. const docMock = createDocumentMock();
  45. const root = new RootEditableElement( docMock, 'div' );
  46. wrapper.parent = root;
  47. expect( textProxy.getDocument() ).to.equal( docMock );
  48. } );
  49. it( 'should return null if element is inside DocumentFragment', () => {
  50. new DocumentFragment( [ wrapper ] );
  51. expect( textProxy.getDocument() ).to.be.null;
  52. } );
  53. } );
  54. describe( 'getRoot', () => {
  55. it( 'should return root element', () => {
  56. const root = new RootEditableElement( createDocumentMock(), 'div' );
  57. wrapper.parent = root;
  58. expect( textProxy.getRoot() ).to.equal( root );
  59. } );
  60. } );
  61. describe( 'getAncestors', () => {
  62. it( 'should return array of ancestors', () => {
  63. const result = textProxy.getAncestors();
  64. expect( result ).to.be.an( 'array' );
  65. expect( result ).to.length( 2 );
  66. expect( result[ 0 ] ).to.equal( wrapper );
  67. expect( result[ 1 ] ).to.equal( parent );
  68. } );
  69. it( 'should return array of ancestors starting from parent `parentFirst`', () => {
  70. const result = textProxy.getAncestors( { parentFirst: true } );
  71. expect( result.length ).to.equal( 2 );
  72. expect( result[ 0 ] ).to.equal( parent );
  73. expect( result[ 1 ] ).to.equal( wrapper );
  74. } );
  75. it( 'should return array including node itself `includeNode`', () => {
  76. const result = textProxy.getAncestors( { includeNode: true } );
  77. expect( result ).to.be.an( 'array' );
  78. expect( result ).to.length( 3 );
  79. expect( result[ 0 ] ).to.equal( wrapper );
  80. expect( result[ 1 ] ).to.equal( parent );
  81. expect( result[ 2 ] ).to.equal( textProxy );
  82. } );
  83. it( 'should return array of ancestors including node itself `includeNode` starting from parent `parentFirst`', () => {
  84. const result = textProxy.getAncestors( { includeNode: true, parentFirst: true } );
  85. expect( result.length ).to.equal( 3 );
  86. expect( result[ 0 ] ).to.equal( textProxy );
  87. expect( result[ 1 ] ).to.equal( parent );
  88. expect( result[ 2 ] ).to.equal( wrapper );
  89. } );
  90. } );
  91. } );