8
0

bogusbr-integration.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import MutationObserver from '@ckeditor/ckeditor5-engine/src/view/observer/mutationobserver';
  8. import Typing from '../src/typing';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  11. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. describe( 'Typing – bogus BR integration', () => {
  13. let editor, domRoot, mutationObserver, editorElement;
  14. beforeEach( () => {
  15. editorElement = document.createElement( 'div' );
  16. document.body.appendChild( editorElement );
  17. return ClassicTestEditor
  18. .create( editorElement, {
  19. plugins: [ Typing, Paragraph, Bold ]
  20. } )
  21. .then( newEditor => {
  22. editor = newEditor;
  23. domRoot = editor.editing.view.getDomRoot();
  24. mutationObserver = editor.editing.view.getObserver( MutationObserver );
  25. } );
  26. } );
  27. afterEach( () => {
  28. editorElement.remove();
  29. return editor.destroy();
  30. } );
  31. describe( 'insertText', () => {
  32. // Tests using 'insertText' generates same mutations as typing. This means they will behave
  33. // a little different in every browser (as native mutations may be different in different browsers).
  34. it( 'space is inserted on the end of the line (paragraph)', done => {
  35. editor.model.change( () => {
  36. editor.editing.view.getDomRoot().focus();
  37. setData( editor.model, '<paragraph>Foo[]</paragraph>' );
  38. } );
  39. editor.model.document.once( 'change', () => {
  40. expect( editor.getData() ).to.equal( '<p>Foo&nbsp;</p>' );
  41. done();
  42. }, { priority: 'low' } );
  43. document.execCommand( 'insertText', false, ' ' );
  44. } );
  45. it( 'space is inserted on the end of the line (empty paragraph)', done => {
  46. editor.model.change( () => {
  47. editor.editing.view.getDomRoot().focus();
  48. setData( editor.model, '<paragraph>[]</paragraph>' );
  49. } );
  50. editor.model.document.once( 'change', () => {
  51. expect( editor.getData() ).to.equal( '<p>&nbsp;</p>' );
  52. done();
  53. }, { priority: 'low' } );
  54. document.execCommand( 'insertText', false, ' ' );
  55. } );
  56. it( 'space is inserted on the end of the line (bold)', done => {
  57. editor.model.change( () => {
  58. editor.editing.view.getDomRoot().focus();
  59. setData( editor.model, '<paragraph><$text bold="true">Foo[]</$text></paragraph>' );
  60. } );
  61. editor.model.document.once( 'change', () => {
  62. expect( editor.getData() ).to.equal( '<p><strong>Foo&nbsp;</strong></p>' );
  63. done();
  64. }, { priority: 'low' } );
  65. document.execCommand( 'insertText', false, ' ' );
  66. } );
  67. } );
  68. describe( 'mutations', () => {
  69. // This tests use fixed list of mutation mocks to simulate specific browser behaviours.
  70. it( 'space is inserted on the end of the paragraph', done => {
  71. editor.model.change( () => {
  72. editor.editing.view.getDomRoot().focus();
  73. setData( editor.model, '<paragraph>Foo[]</paragraph>' );
  74. } );
  75. editor.model.document.once( 'change', () => {
  76. expect( editor.getData() ).to.equal( '<p>Foo&nbsp;</p>' );
  77. done();
  78. }, { priority: 'low' } );
  79. const mutationTarget = domRoot.childNodes[ 0 ].childNodes[ 0 ];
  80. mutationObserver.disable();
  81. mutationTarget.data = 'Foo ';
  82. mutationObserver.enable();
  83. mutationObserver._onMutations( [
  84. generateMutationMock( 'characterData', mutationTarget ),
  85. generateMutationMock( 'characterData', mutationTarget ),
  86. generateMutationMock( 'characterData', mutationTarget )
  87. ] );
  88. } );
  89. it( 'space is inserted on the end of the line paragraph (with bogus br)', done => {
  90. editor.model.change( () => {
  91. editor.editing.view.getDomRoot().focus();
  92. setData( editor.model, '<paragraph>Foo[]</paragraph>' );
  93. } );
  94. editor.model.document.once( 'change', () => {
  95. expect( editor.getData() ).to.equal( '<p>Foo&nbsp;</p>' );
  96. done();
  97. }, { priority: 'low' } );
  98. const paragraph = domRoot.childNodes[ 0 ];
  99. const text = paragraph.childNodes[ 0 ];
  100. const br = document.createElement( 'br' );
  101. mutationObserver.disable();
  102. text.data = 'Foo ';
  103. mutationObserver.enable();
  104. mutationObserver._onMutations( [
  105. generateMutationMock( 'characterData', text ),
  106. generateMutationMock( 'childList', paragraph, text, [ br ] ),
  107. generateMutationMock( 'characterData', text ),
  108. generateMutationMock( 'characterData', text )
  109. ] );
  110. } );
  111. it( 'word is properly corrected on the end of the block element (with bogus br)', done => {
  112. editor.model.change( () => {
  113. editor.editing.view.getDomRoot().focus();
  114. setData( editor.model, '<paragraph>Foo hous[]</paragraph>' );
  115. } );
  116. editor.model.document.once( 'change', () => {
  117. expect( editor.getData() ).to.equal( '<p>Foo house</p>' );
  118. done();
  119. }, { priority: 'low' } );
  120. const paragraph = domRoot.childNodes[ 0 ];
  121. const text = paragraph.childNodes[ 0 ];
  122. const br = document.createElement( 'br' );
  123. mutationObserver.disable();
  124. text.data = 'Foo house';
  125. mutationObserver.enable();
  126. mutationObserver._onMutations( [
  127. generateMutationMock( 'characterData', text ),
  128. generateMutationMock( 'characterData', text ),
  129. generateMutationMock( 'characterData', text ),
  130. generateMutationMock( 'childList', paragraph, text, [ br ] ),
  131. generateMutationMock( 'characterData', text ),
  132. generateMutationMock( 'characterData', text ),
  133. generateMutationMock( 'characterData', text ),
  134. ] );
  135. } );
  136. } );
  137. function generateMutationMock( type, target, previousSibling, addedNodes ) {
  138. return {
  139. addedNodes: addedNodes || [],
  140. attributeName: null,
  141. attributeNamespace: null,
  142. nextSibling: null,
  143. oldValue: null,
  144. previousSibling: previousSibling || null,
  145. removedNodes: [],
  146. target,
  147. type
  148. };
  149. }
  150. } );