placeholder.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { attachPlaceholder, detachPlaceholder } from '../../src/view/placeholder';
  6. import createViewRoot from './_utils/createroot';
  7. import View from '../../src/view/view';
  8. import ViewRange from '../../src/view/range';
  9. import { setData } from '../../src/dev-utils/view';
  10. describe( 'placeholder', () => {
  11. let view, viewDocument, viewRoot;
  12. beforeEach( () => {
  13. view = new View();
  14. viewDocument = view.document;
  15. viewRoot = createViewRoot( viewDocument );
  16. viewDocument.isFocused = true;
  17. } );
  18. describe( 'createPlaceholder', () => {
  19. it( 'should attach proper CSS class and data attribute', () => {
  20. setData( view, '<div></div><div>{another div}</div>' );
  21. const element = viewRoot.getChild( 0 );
  22. attachPlaceholder( view, element, 'foo bar baz' );
  23. expect( element.getAttribute( 'data-placeholder' ) ).to.equal( 'foo bar baz' );
  24. expect( element.hasClass( 'ck-placeholder' ) ).to.be.true;
  25. } );
  26. it( 'if element has children set only data attribute', () => {
  27. setData( view, '<div>first div</div><div>{another div}</div>' );
  28. const element = viewRoot.getChild( 0 );
  29. attachPlaceholder( view, element, 'foo bar baz' );
  30. expect( element.getAttribute( 'data-placeholder' ) ).to.equal( 'foo bar baz' );
  31. expect( element.hasClass( 'ck-placeholder' ) ).to.be.false;
  32. } );
  33. it( 'if element has only ui elements, set CSS class and data attribute', () => {
  34. setData( view, '<div><ui:span></ui:span><ui:span></ui:span></div><div>{another div}</div>' );
  35. const element = viewRoot.getChild( 0 );
  36. attachPlaceholder( view, element, 'foo bar baz' );
  37. expect( element.getAttribute( 'data-placeholder' ) ).to.equal( 'foo bar baz' );
  38. expect( element.hasClass( 'ck-placeholder' ) ).to.be.true;
  39. } );
  40. it( 'if element has selection inside set only data attribute', () => {
  41. setData( view, '<div>[]</div><div>another div</div>' );
  42. const element = viewRoot.getChild( 0 );
  43. attachPlaceholder( view, element, 'foo bar baz' );
  44. expect( element.getAttribute( 'data-placeholder' ) ).to.equal( 'foo bar baz' );
  45. expect( element.hasClass( 'ck-placeholder' ) ).to.be.false;
  46. } );
  47. it( 'if element has selection inside but document is blurred should contain placeholder CSS class', () => {
  48. setData( view, '<div>[]</div><div>another div</div>' );
  49. const element = viewRoot.getChild( 0 );
  50. viewDocument.isFocused = false;
  51. attachPlaceholder( view, element, 'foo bar baz' );
  52. expect( element.getAttribute( 'data-placeholder' ) ).to.equal( 'foo bar baz' );
  53. expect( element.hasClass( 'ck-placeholder' ) ).to.be.true;
  54. } );
  55. it( 'use check function if one is provided', () => {
  56. setData( view, '<div></div><div>{another div}</div>' );
  57. const element = viewRoot.getChild( 0 );
  58. let result = true;
  59. const spy = sinon.spy( () => result );
  60. attachPlaceholder( view, element, 'foo bar baz', spy );
  61. sinon.assert.called( spy );
  62. expect( element.getAttribute( 'data-placeholder' ) ).to.equal( 'foo bar baz' );
  63. expect( element.hasClass( 'ck-placeholder' ) ).to.be.true;
  64. result = false;
  65. view.render();
  66. expect( element.hasClass( 'ck-placeholder' ) ).to.be.false;
  67. } );
  68. it( 'should remove CSS class if selection is moved inside', () => {
  69. setData( view, '<div></div><div>{another div}</div>' );
  70. const element = viewRoot.getChild( 0 );
  71. attachPlaceholder( view, element, 'foo bar baz' );
  72. expect( element.getAttribute( 'data-placeholder' ) ).to.equal( 'foo bar baz' );
  73. expect( element.hasClass( 'ck-placeholder' ) ).to.be.true;
  74. view.change( writer => {
  75. writer.setSelection( ViewRange.createIn( element ) );
  76. } );
  77. expect( element.hasClass( 'ck-placeholder' ) ).to.be.false;
  78. } );
  79. it( 'should change placeholder settings when called twice', () => {
  80. setData( view, '<div></div><div>{another div}</div>' );
  81. const element = viewRoot.getChild( 0 );
  82. attachPlaceholder( view, element, 'foo bar baz' );
  83. attachPlaceholder( view, element, 'new text' );
  84. expect( element.getAttribute( 'data-placeholder' ) ).to.equal( 'new text' );
  85. expect( element.hasClass( 'ck-placeholder' ) ).to.be.true;
  86. } );
  87. it( 'should not throw when element is no longer in document', () => {
  88. setData( view, '<div></div><div>{another div}</div>' );
  89. const element = viewRoot.getChild( 0 );
  90. attachPlaceholder( view, element, 'foo bar baz' );
  91. setData( view, '<p>paragraph</p>' );
  92. view.render();
  93. } );
  94. it( 'should allow to add placeholder to elements from different documents', () => {
  95. setData( view, '<div></div><div>{another div}</div>' );
  96. const element = viewRoot.getChild( 0 );
  97. const secondView = new View();
  98. const secondDocument = secondView.document;
  99. secondDocument.isFocused = true;
  100. const secondRoot = createViewRoot( secondDocument );
  101. setData( secondView, '<div></div><div>{another div}</div>' );
  102. const secondElement = secondRoot.getChild( 0 );
  103. attachPlaceholder( view, element, 'first placeholder' );
  104. attachPlaceholder( secondView, secondElement, 'second placeholder' );
  105. expect( element.getAttribute( 'data-placeholder' ) ).to.equal( 'first placeholder' );
  106. expect( element.hasClass( 'ck-placeholder' ) ).to.be.true;
  107. expect( secondElement.getAttribute( 'data-placeholder' ) ).to.equal( 'second placeholder' );
  108. expect( secondElement.hasClass( 'ck-placeholder' ) ).to.be.true;
  109. // Move selection to the elements with placeholders.
  110. view.change( writer => {
  111. writer.setSelection( ViewRange.createIn( element ) );
  112. } );
  113. secondView.change( writer => {
  114. writer.setSelection( ViewRange.createIn( secondElement ) );
  115. } );
  116. expect( element.getAttribute( 'data-placeholder' ) ).to.equal( 'first placeholder' );
  117. expect( element.hasClass( 'ck-placeholder' ) ).to.be.false;
  118. expect( secondElement.getAttribute( 'data-placeholder' ) ).to.equal( 'second placeholder' );
  119. expect( secondElement.hasClass( 'ck-placeholder' ) ).to.be.false;
  120. } );
  121. it( 'should update placeholder before rendering', () => {
  122. setData( view, '<div></div><div>{another div}</div>' );
  123. const element = viewRoot.getChild( 0 );
  124. attachPlaceholder( view, element, 'foo bar baz' );
  125. view.change( writer => {
  126. writer.setSelection( ViewRange.createIn( element ) );
  127. // Here we are before rendering - placeholder is visible in first element;
  128. expect( element.getAttribute( 'data-placeholder' ) ).to.equal( 'foo bar baz' );
  129. expect( element.hasClass( 'ck-placeholder' ) ).to.be.true;
  130. } );
  131. // After rendering - placeholder should be invisible since selection is moved there.
  132. expect( element.hasClass( 'ck-placeholder' ) ).to.be.false;
  133. } );
  134. } );
  135. describe( 'detachPlaceholder', () => {
  136. it( 'should remove placeholder from element', () => {
  137. setData( view, '<div></div><div>{another div}</div>' );
  138. const element = viewRoot.getChild( 0 );
  139. attachPlaceholder( view, element, 'foo bar baz' );
  140. expect( element.getAttribute( 'data-placeholder' ) ).to.equal( 'foo bar baz' );
  141. expect( element.hasClass( 'ck-placeholder' ) ).to.be.true;
  142. detachPlaceholder( view, element );
  143. expect( element.hasAttribute( 'data-placeholder' ) ).to.be.false;
  144. expect( element.hasClass( 'ck-placeholder' ) ).to.be.false;
  145. } );
  146. it( 'should not blow up when called on element without placeholder', () => {
  147. setData( view, '<div></div><div>{another div}</div>' );
  148. const element = viewRoot.getChild( 0 );
  149. detachPlaceholder( view, element );
  150. expect( element.hasAttribute( 'data-placeholder' ) ).to.be.false;
  151. expect( element.hasClass( 'ck-placeholder' ) ).to.be.false;
  152. } );
  153. } );
  154. } );