bogusbr-integration.js 5.8 KB

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