filler.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 {
  7. INLINE_FILLER_LENGTH,
  8. INLINE_FILLER,
  9. startsWithFiller,
  10. isInlineFiller,
  11. getDataWithoutFiller
  12. } from '../../src/view/filler';
  13. describe( 'filler', () => {
  14. describe( 'INLINE_FILLER', () => {
  15. it( 'should have length equal INLINE_FILLER_LENGTH', () => {
  16. expect( INLINE_FILLER.length ).to.equal( INLINE_FILLER_LENGTH );
  17. } );
  18. } );
  19. describe( 'startsWithFiller()', () => {
  20. it( 'should be true for node which contains only filler', () => {
  21. const node = document.createTextNode( INLINE_FILLER );
  22. expect( startsWithFiller( node ) ).to.be.true;
  23. } );
  24. it( 'should be true for node which starts with filler', () => {
  25. const node = document.createTextNode( INLINE_FILLER + 'foo' );
  26. expect( startsWithFiller( node ) ).to.be.true;
  27. } );
  28. it( 'should be false for element', () => {
  29. const node = document.createElement( 'p' );
  30. expect( startsWithFiller( node ) ).to.be.false;
  31. } );
  32. it( 'should be false which contains filler in the middle', () => {
  33. const node = document.createTextNode( 'x' + INLINE_FILLER + 'x' );
  34. expect( startsWithFiller( node ) ).to.be.false;
  35. } );
  36. it( 'should be false for the node which does not contains filler', () => {
  37. const node = document.createTextNode( 'foo' );
  38. expect( startsWithFiller( node ) ).to.be.false;
  39. } );
  40. it( 'should be false for the node which does not contains filler, even if it has the same length', () => {
  41. let text = '';
  42. for ( let i = 0; i < INLINE_FILLER_LENGTH; i++ ) {
  43. text += 'x';
  44. }
  45. const node = document.createTextNode( text );
  46. expect( startsWithFiller( node ) ).to.be.false;
  47. } );
  48. } );
  49. describe( 'getDataWithoutFiller()', () => {
  50. it( 'should return data without filler', () => {
  51. const node = document.createTextNode( INLINE_FILLER + 'foo' );
  52. const dataWithoutFiller = getDataWithoutFiller( node );
  53. expect( dataWithoutFiller.length ).to.equals( 3 );
  54. expect( dataWithoutFiller ).to.equals( 'foo' );
  55. } );
  56. it( 'should return the same data for data without filler', () => {
  57. const node = document.createTextNode( 'foo' );
  58. const dataWithoutFiller = getDataWithoutFiller( node );
  59. expect( dataWithoutFiller.length ).to.equals( 3 );
  60. expect( dataWithoutFiller ).to.equals( 'foo' );
  61. } );
  62. } );
  63. describe( 'isInlineFiller()', () => {
  64. it( 'should be true for inline filler', () => {
  65. const node = document.createTextNode( INLINE_FILLER );
  66. expect( isInlineFiller( node ) ).to.be.true;
  67. } );
  68. it( 'should be false for element which starts with filler', () => {
  69. const node = document.createTextNode( INLINE_FILLER + 'foo' );
  70. expect( isInlineFiller( node ) ).to.be.false;
  71. } );
  72. it( 'should be false for the node which does not contains filler, even if it has the same length', () => {
  73. let text = '';
  74. for ( let i = 0; i < INLINE_FILLER_LENGTH; i++ ) {
  75. text += 'x';
  76. }
  77. const node = document.createTextNode( text );
  78. expect( isInlineFiller( node ) ).to.be.false;
  79. } );
  80. it( 'should be true for inline filler from inside iframe', () => {
  81. const iframe = document.createElement( 'iframe' );
  82. document.body.appendChild( iframe );
  83. const node = iframe.contentDocument.createTextNode( INLINE_FILLER );
  84. expect( isInlineFiller( node ) ).to.be.true;
  85. document.body.removeChild( iframe );
  86. } );
  87. } );
  88. } );