filler.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. /* bender-tags: view, browser-only */
  7. import {
  8. BR_FILLER,
  9. NBSP_FILLER,
  10. INLINE_FILLER_LENGTH,
  11. INLINE_FILLER,
  12. startsWithFiller,
  13. isInlineFiller,
  14. getDataWithoutFiller,
  15. isBlockFiller
  16. } from '/ckeditor5/engine/view/filler.js';
  17. describe( 'filler', () => {
  18. describe( 'INLINE_FILLER', () => {
  19. it( 'should have length equal INLINE_FILLER_LENGTH', () => {
  20. expect( INLINE_FILLER.length ).to.equal( INLINE_FILLER_LENGTH );
  21. } );
  22. } );
  23. describe( 'startsWithFiller', () => {
  24. it( 'should be true for node which contains only filler', () => {
  25. const node = document.createTextNode( INLINE_FILLER );
  26. expect( startsWithFiller( node ) ).to.be.true;
  27. } );
  28. it( 'should be true for node which starts with filler', () => {
  29. const node = document.createTextNode( INLINE_FILLER + 'foo' );
  30. expect( startsWithFiller( node ) ).to.be.true;
  31. } );
  32. it( 'should be false for element', () => {
  33. const node = document.createElement( 'p' );
  34. expect( startsWithFiller( node ) ).to.be.false;
  35. } );
  36. it( 'should be false which contains filler in the middle', () => {
  37. const node = document.createTextNode( 'x' + INLINE_FILLER + 'x' );
  38. expect( startsWithFiller( node ) ).to.be.false;
  39. } );
  40. it( 'should be false for the node which does not contains filler', () => {
  41. const node = document.createTextNode( 'foo' );
  42. expect( startsWithFiller( node ) ).to.be.false;
  43. } );
  44. it( 'should be false for the node which does not contains filler, even if it has the same length', () => {
  45. let text = '';
  46. for ( let i = 0; i < INLINE_FILLER_LENGTH; i++ ) {
  47. text += 'x';
  48. }
  49. const node = document.createTextNode( text );
  50. expect( startsWithFiller( node ) ).to.be.false;
  51. } );
  52. } );
  53. describe( 'getDataWithoutFiller', () => {
  54. it( 'should return data without filler', () => {
  55. const node = document.createTextNode( INLINE_FILLER + 'foo' );
  56. const dataWithoutFiller = getDataWithoutFiller( node );
  57. expect( dataWithoutFiller.length ).to.equals( 3 );
  58. expect( dataWithoutFiller ).to.equals( 'foo' );
  59. } );
  60. it( 'should return the same data for data without filler', () => {
  61. const node = document.createTextNode( 'foo' );
  62. const dataWithoutFiller = getDataWithoutFiller( node );
  63. expect( dataWithoutFiller.length ).to.equals( 3 );
  64. expect( dataWithoutFiller ).to.equals( 'foo' );
  65. } );
  66. } );
  67. describe( 'isInlineFiller', () => {
  68. it( 'should be true for inline filler', () => {
  69. const node = document.createTextNode( INLINE_FILLER );
  70. expect( isInlineFiller( node ) ).to.be.true;
  71. } );
  72. it( 'should be false for element which starts with filler', () => {
  73. const node = document.createTextNode( INLINE_FILLER + 'foo' );
  74. expect( isInlineFiller( node ) ).to.be.false;
  75. } );
  76. it( 'should be false for the node which does not contains filler, even if it has the same length', () => {
  77. let text = '';
  78. for ( let i = 0; i < INLINE_FILLER_LENGTH; i++ ) {
  79. text += 'x';
  80. }
  81. const node = document.createTextNode( text );
  82. expect( isInlineFiller( node ) ).to.be.false;
  83. } );
  84. } );
  85. describe( 'isBlockFiller', () => {
  86. it( 'should return true if the node is an instance of the BR block filler', () => {
  87. const brFillerInstance = BR_FILLER( document );
  88. expect( isBlockFiller( brFillerInstance, BR_FILLER ) ).to.be.true;
  89. // Check it twice to ensure that caching breaks nothing.
  90. expect( isBlockFiller( brFillerInstance, BR_FILLER ) ).to.be.true;
  91. } );
  92. it( 'should return true if the node is an instance of the NBSP block filler', () => {
  93. const nbspFillerInstance = NBSP_FILLER( document );
  94. expect( isBlockFiller( nbspFillerInstance, NBSP_FILLER ) ).to.be.true;
  95. // Check it twice to ensure that caching breaks nothing.
  96. expect( isBlockFiller( nbspFillerInstance, NBSP_FILLER ) ).to.be.true;
  97. } );
  98. it( 'should return false for inline filler', () => {
  99. expect( isBlockFiller( document.createTextNode( INLINE_FILLER ), BR_FILLER ) ).to.be.false;
  100. expect( isBlockFiller( document.createTextNode( INLINE_FILLER ), NBSP_FILLER ) ).to.be.false;
  101. } );
  102. } );
  103. } );