filler.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treeview */
  6. 'use strict';
  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/treeview/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 element 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 element 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 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. } );
  81. describe( 'isBlockFiller', () => {
  82. it( 'should return true if the node is an instance of the BR block filler', () => {
  83. const brFillerInstance = BR_FILLER( document );
  84. expect( isBlockFiller( brFillerInstance, BR_FILLER ) ).to.be.true;
  85. // Check it twice to ensure that caching breaks nothing.
  86. expect( isBlockFiller( brFillerInstance, BR_FILLER ) ).to.be.true;
  87. } );
  88. it( 'should return true if the node is an instance of the NBSP block filler', () => {
  89. const nbspFillerInstance = NBSP_FILLER( document );
  90. expect( isBlockFiller( nbspFillerInstance, NBSP_FILLER ) ).to.be.true;
  91. // Check it twice to ensure that caching breaks nothing.
  92. expect( isBlockFiller( nbspFillerInstance, NBSP_FILLER ) ).to.be.true;
  93. } );
  94. it( 'should return false for inline filler', () => {
  95. expect( isBlockFiller( document.createTextNode( INLINE_FILLER ), BR_FILLER ) ).to.be.false;
  96. expect( isBlockFiller( document.createTextNode( INLINE_FILLER ), NBSP_FILLER ) ).to.be.false;
  97. } );
  98. } );
  99. } );