8
0

filler.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * @license Copyright (c) 2003-2019, 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 {
  7. BR_FILLER,
  8. NBSP_FILLER,
  9. INLINE_FILLER_LENGTH,
  10. INLINE_FILLER,
  11. startsWithFiller,
  12. isInlineFiller,
  13. getDataWithoutFiller,
  14. isBlockFiller
  15. } from '../../src/view/filler';
  16. describe( 'filler', () => {
  17. describe( 'INLINE_FILLER', () => {
  18. it( 'should have length equal INLINE_FILLER_LENGTH', () => {
  19. expect( INLINE_FILLER.length ).to.equal( INLINE_FILLER_LENGTH );
  20. } );
  21. } );
  22. describe( 'startsWithFiller', () => {
  23. it( 'should be true for node which contains only filler', () => {
  24. const node = document.createTextNode( INLINE_FILLER );
  25. expect( startsWithFiller( node ) ).to.be.true;
  26. } );
  27. it( 'should be true for node which starts with filler', () => {
  28. const node = document.createTextNode( INLINE_FILLER + 'foo' );
  29. expect( startsWithFiller( node ) ).to.be.true;
  30. } );
  31. it( 'should be false for element', () => {
  32. const node = document.createElement( 'p' );
  33. expect( startsWithFiller( node ) ).to.be.false;
  34. } );
  35. it( 'should be false which contains filler in the middle', () => {
  36. const node = document.createTextNode( 'x' + INLINE_FILLER + 'x' );
  37. expect( startsWithFiller( node ) ).to.be.false;
  38. } );
  39. it( 'should be false for the node which does not contains filler', () => {
  40. const node = document.createTextNode( 'foo' );
  41. expect( startsWithFiller( node ) ).to.be.false;
  42. } );
  43. it( 'should be false for the node which does not contains filler, even if it has the same length', () => {
  44. let text = '';
  45. for ( let i = 0; i < INLINE_FILLER_LENGTH; i++ ) {
  46. text += 'x';
  47. }
  48. const node = document.createTextNode( text );
  49. expect( startsWithFiller( node ) ).to.be.false;
  50. } );
  51. } );
  52. describe( 'getDataWithoutFiller', () => {
  53. it( 'should return data without filler', () => {
  54. const node = document.createTextNode( INLINE_FILLER + 'foo' );
  55. const dataWithoutFiller = getDataWithoutFiller( node );
  56. expect( dataWithoutFiller.length ).to.equals( 3 );
  57. expect( dataWithoutFiller ).to.equals( 'foo' );
  58. } );
  59. it( 'should return the same data for data without filler', () => {
  60. const node = document.createTextNode( 'foo' );
  61. const dataWithoutFiller = getDataWithoutFiller( node );
  62. expect( dataWithoutFiller.length ).to.equals( 3 );
  63. expect( dataWithoutFiller ).to.equals( 'foo' );
  64. } );
  65. } );
  66. describe( 'isInlineFiller', () => {
  67. it( 'should be true for inline filler', () => {
  68. const node = document.createTextNode( INLINE_FILLER );
  69. expect( isInlineFiller( node ) ).to.be.true;
  70. } );
  71. it( 'should be false for element which starts with filler', () => {
  72. const node = document.createTextNode( INLINE_FILLER + 'foo' );
  73. expect( isInlineFiller( node ) ).to.be.false;
  74. } );
  75. it( 'should be false for the node which does not contains filler, even if it has the same length', () => {
  76. let text = '';
  77. for ( let i = 0; i < INLINE_FILLER_LENGTH; i++ ) {
  78. text += 'x';
  79. }
  80. const node = document.createTextNode( text );
  81. expect( isInlineFiller( node ) ).to.be.false;
  82. } );
  83. it( 'should be true for inline filler from inside iframe', () => {
  84. const iframe = document.createElement( 'iframe' );
  85. document.body.appendChild( iframe );
  86. const node = iframe.contentDocument.createTextNode( INLINE_FILLER );
  87. expect( isInlineFiller( node ) ).to.be.true;
  88. document.body.removeChild( iframe );
  89. } );
  90. } );
  91. describe( 'isBlockFiller', () => {
  92. it( 'should return true if the node is an instance of the BR block filler', () => {
  93. const brFillerInstance = BR_FILLER( document ); // eslint-disable-line new-cap
  94. expect( isBlockFiller( brFillerInstance, 'br' ) ).to.be.true;
  95. // Check it twice to ensure that caching breaks nothing.
  96. expect( isBlockFiller( brFillerInstance, 'br' ) ).to.be.true;
  97. } );
  98. it( 'should return true if the node is an instance of the NBSP block filler', () => {
  99. const nbspFillerInstance = NBSP_FILLER( document ); // eslint-disable-line new-cap
  100. expect( isBlockFiller( nbspFillerInstance, 'nbsp' ) ).to.be.true;
  101. // Check it twice to ensure that caching breaks nothing.
  102. expect( isBlockFiller( nbspFillerInstance, 'nbsp' ) ).to.be.true;
  103. } );
  104. it( 'should return false for inline filler', () => {
  105. expect( isBlockFiller( document.createTextNode( INLINE_FILLER ), 'br' ) ).to.be.false;
  106. expect( isBlockFiller( document.createTextNode( INLINE_FILLER ), 'nbsp' ) ).to.be.false;
  107. } );
  108. } );
  109. } );