domconverter.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import DomConverter from '../../../src/view/domconverter';
  7. import ViewEditable from '../../../src/view/editableelement';
  8. import ViewDocument from '../../../src/view/document';
  9. import ViewUIElement from '../../../src/view/uielement';
  10. import ViewContainerElement from '../../../src/view/containerelement';
  11. import { BR_FILLER, NBSP_FILLER, INLINE_FILLER, INLINE_FILLER_LENGTH } from '../../../src/view/filler';
  12. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  13. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  14. testUtils.createSinonSandbox();
  15. describe( 'DomConverter', () => {
  16. let converter;
  17. beforeEach( () => {
  18. converter = new DomConverter();
  19. } );
  20. describe( 'constructor()', () => {
  21. it( 'should create converter with BR block filler by default', () => {
  22. expect( converter.blockFiller ).to.equal( BR_FILLER );
  23. } );
  24. it( 'should create converter with defined block filler', () => {
  25. converter = new DomConverter( { blockFiller: NBSP_FILLER } );
  26. expect( converter.blockFiller ).to.equal( NBSP_FILLER );
  27. } );
  28. } );
  29. describe( 'focus()', () => {
  30. let viewEditable, domEditable, domEditableParent, viewDocument;
  31. beforeEach( () => {
  32. viewDocument = new ViewDocument();
  33. viewEditable = new ViewEditable( 'div' );
  34. viewEditable.document = viewDocument;
  35. domEditable = document.createElement( 'div' );
  36. domEditableParent = document.createElement( 'div' );
  37. converter.bindElements( domEditable, viewEditable );
  38. domEditable.setAttribute( 'contenteditable', 'true' );
  39. domEditableParent.appendChild( domEditable );
  40. document.body.appendChild( domEditableParent );
  41. } );
  42. afterEach( () => {
  43. converter.unbindDomElement( domEditable );
  44. document.body.removeChild( domEditableParent );
  45. viewDocument.destroy();
  46. document.body.focus();
  47. } );
  48. it( 'should call focus on corresponding DOM editable', () => {
  49. const focusSpy = testUtils.sinon.spy( domEditable, 'focus' );
  50. converter.focus( viewEditable );
  51. expect( focusSpy.calledOnce ).to.be.true;
  52. } );
  53. it( 'should not focus already focused editable', () => {
  54. const focusSpy = testUtils.sinon.spy( domEditable, 'focus' );
  55. converter.focus( viewEditable );
  56. converter.focus( viewEditable );
  57. expect( focusSpy.calledOnce ).to.be.true;
  58. } );
  59. // https://github.com/ckeditor/ckeditor5-engine/issues/951
  60. // https://github.com/ckeditor/ckeditor5-engine/issues/957
  61. it( 'should actively prevent scrolling', () => {
  62. const scrollToSpy = testUtils.sinon.stub( global.window, 'scrollTo' );
  63. const editableScrollLeftSpy = sinon.spy();
  64. const editableScrollTopSpy = sinon.spy();
  65. const parentScrollLeftSpy = sinon.spy();
  66. const parentScrollTopSpy = sinon.spy();
  67. const documentElementScrollLeftSpy = sinon.spy();
  68. const documentElementScrollTopSpy = sinon.spy();
  69. Object.defineProperties( domEditable, {
  70. scrollLeft: {
  71. get: () => 20,
  72. set: editableScrollLeftSpy
  73. },
  74. scrollTop: {
  75. get: () => 200,
  76. set: editableScrollTopSpy
  77. }
  78. } );
  79. Object.defineProperties( domEditableParent, {
  80. scrollLeft: {
  81. get: () => 40,
  82. set: parentScrollLeftSpy
  83. },
  84. scrollTop: {
  85. get: () => 400,
  86. set: parentScrollTopSpy
  87. }
  88. } );
  89. Object.defineProperties( global.document.documentElement, {
  90. scrollLeft: {
  91. get: () => 60,
  92. set: documentElementScrollLeftSpy
  93. },
  94. scrollTop: {
  95. get: () => 600,
  96. set: documentElementScrollTopSpy
  97. }
  98. } );
  99. global.window.scrollX = 10;
  100. global.window.scrollY = 100;
  101. converter.focus( viewEditable );
  102. sinon.assert.calledWithExactly( scrollToSpy, 10, 100 );
  103. sinon.assert.calledWithExactly( editableScrollLeftSpy, 20 );
  104. sinon.assert.calledWithExactly( editableScrollTopSpy, 200 );
  105. sinon.assert.calledWithExactly( parentScrollLeftSpy, 40 );
  106. sinon.assert.calledWithExactly( parentScrollTopSpy, 400 );
  107. sinon.assert.calledWithExactly( documentElementScrollLeftSpy, 60 );
  108. sinon.assert.calledWithExactly( documentElementScrollTopSpy, 600 );
  109. } );
  110. } );
  111. describe( 'DOM nodes type checking', () => {
  112. let text, element, documentFragment, comment;
  113. before( () => {
  114. text = document.createTextNode( 'test' );
  115. element = document.createElement( 'div' );
  116. documentFragment = document.createDocumentFragment();
  117. comment = document.createComment( 'a' );
  118. } );
  119. describe( 'isElement()', () => {
  120. it( 'should return true for HTMLElement nodes', () => {
  121. expect( converter.isElement( element ) ).to.be.true;
  122. } );
  123. it( 'should return false for other arguments', () => {
  124. expect( converter.isElement( text ) ).to.be.false;
  125. expect( converter.isElement( documentFragment ) ).to.be.false;
  126. expect( converter.isElement( comment ) ).to.be.false;
  127. expect( converter.isElement( {} ) ).to.be.false;
  128. } );
  129. } );
  130. describe( 'isDocumentFragment()', () => {
  131. it( 'should return true for HTMLElement nodes', () => {
  132. expect( converter.isDocumentFragment( documentFragment ) ).to.be.true;
  133. } );
  134. it( 'should return false for other arguments', () => {
  135. expect( converter.isDocumentFragment( text ) ).to.be.false;
  136. expect( converter.isDocumentFragment( element ) ).to.be.false;
  137. expect( converter.isDocumentFragment( comment ) ).to.be.false;
  138. expect( converter.isDocumentFragment( {} ) ).to.be.false;
  139. } );
  140. } );
  141. describe( 'isComment()', () => {
  142. it( 'should return true for HTML comments', () => {
  143. expect( converter.isComment( comment ) ).to.be.true;
  144. } );
  145. it( 'should return false for other arguments', () => {
  146. expect( converter.isComment( text ) ).to.be.false;
  147. expect( converter.isComment( element ) ).to.be.false;
  148. expect( converter.isComment( documentFragment ) ).to.be.false;
  149. expect( converter.isComment( {} ) ).to.be.false;
  150. } );
  151. } );
  152. } );
  153. describe( 'isDomSelectionCorrect()', () => {
  154. function domSelection( anchorParent, anchorOffset, focusParent, focusOffset ) {
  155. const sel = document.getSelection();
  156. sel.collapse( anchorParent, anchorOffset );
  157. sel.extend( focusParent, focusOffset );
  158. return sel;
  159. }
  160. let domP, domFillerTextNode, domUiSpan, domUiDeepSpan;
  161. beforeEach( () => {
  162. // <p>INLINE_FILLERfoo<span></span></p>.
  163. domP = document.createElement( 'p' );
  164. domFillerTextNode = document.createTextNode( INLINE_FILLER + 'foo' );
  165. domUiSpan = document.createElement( 'span' );
  166. domUiDeepSpan = document.createElement( 'span' );
  167. domUiSpan.appendChild( domUiDeepSpan );
  168. const viewUiSpan = new ViewUIElement( 'span' );
  169. const viewElementSpan = new ViewContainerElement( 'span' );
  170. domP.appendChild( domFillerTextNode );
  171. domP.appendChild( domUiSpan );
  172. converter.bindElements( domUiSpan, viewUiSpan );
  173. converter.bindElements( domUiDeepSpan, viewElementSpan );
  174. document.body.appendChild( domP );
  175. } );
  176. afterEach( () => {
  177. domP.remove();
  178. } );
  179. it( 'should return true for correct dom selection', () => {
  180. // <p>INLINE_FILLER{foo}<span></span></p>.
  181. const sel1 = domSelection( domFillerTextNode, INLINE_FILLER_LENGTH, domFillerTextNode, INLINE_FILLER_LENGTH + 3 );
  182. expect( converter.isDomSelectionCorrect( sel1 ) ).to.be.true;
  183. // <p>INLINE_FILLERfoo[]<span></span></p>.
  184. const sel2 = domSelection( domP, 1, domP, 1 );
  185. expect( converter.isDomSelectionCorrect( sel2 ) ).to.be.true;
  186. // <p>INLINE_FILLERfoo<span></span>[]</p>.
  187. const sel3 = domSelection( domP, 2, domP, 2 );
  188. expect( converter.isDomSelectionCorrect( sel3 ) ).to.be.true;
  189. } );
  190. describe( 'should return false', () => {
  191. it( 'if anchor or focus is before filler node', () => {
  192. // Tests forward and backward selection.
  193. // <p>[INLINE_FILLERfoo]<span-ui><span-container></span></span></p>.
  194. const sel1 = domSelection( domP, 0, domP, 1 );
  195. expect( converter.isDomSelectionCorrect( sel1 ) ).to.be.false;
  196. const sel2 = domSelection( domP, 1, domP, 0 );
  197. expect( converter.isDomSelectionCorrect( sel2 ) ).to.be.false;
  198. } );
  199. it( 'if anchor or focus is before filler sequence', () => {
  200. // Tests forward and backward selection.
  201. // <p>{INLINE_FILLERfoo}<span-ui><span-container></span></span></p>.
  202. const sel1 = domSelection( domFillerTextNode, 0, domFillerTextNode, INLINE_FILLER_LENGTH + 3 );
  203. expect( converter.isDomSelectionCorrect( sel1 ) ).to.be.false;
  204. const sel2 = domSelection( domFillerTextNode, INLINE_FILLER_LENGTH + 3, domFillerTextNode, 0 );
  205. expect( converter.isDomSelectionCorrect( sel2 ) ).to.be.false;
  206. } );
  207. it( 'if anchor or focus is in the middle of filler sequence', () => {
  208. // Tests forward and backward selection.
  209. // <p>I{NLINE_FILLERfoo}<span-ui><span-container></span></span></p>.
  210. const sel1 = domSelection( domFillerTextNode, 1, domFillerTextNode, INLINE_FILLER_LENGTH + 3 );
  211. expect( converter.isDomSelectionCorrect( sel1 ) ).to.be.false;
  212. const sel2 = domSelection( domFillerTextNode, INLINE_FILLER_LENGTH + 3, domFillerTextNode, 1 );
  213. expect( converter.isDomSelectionCorrect( sel2 ) ).to.be.false;
  214. } );
  215. it( 'if anchor or focus is directly inside dom element that represents view ui element', () => {
  216. // Set text indside ui element to put selection there.
  217. domUiSpan.innerText = 'xxx';
  218. // Tests forward and backward selection.
  219. // <p>INLINE_FILLER{foo<span-ui>xxx]<span-container></span></span></p>.
  220. const sel1 = domSelection( domFillerTextNode, INLINE_FILLER_LENGTH, domUiSpan, 1 );
  221. expect( converter.isDomSelectionCorrect( sel1 ) ).to.be.false;
  222. const sel2 = domSelection( domUiSpan, 1, domFillerTextNode, INLINE_FILLER_LENGTH );
  223. expect( converter.isDomSelectionCorrect( sel2 ) ).to.be.false;
  224. } );
  225. it( 'if anchor or focus is inside deep ui element structure (not directly in ui element)', () => {
  226. // Set text indside ui element to put selection there.
  227. domUiDeepSpan.innerText = 'xxx';
  228. // Tests forward and backward selection.
  229. // <p>INLINE_FILLER{foo<span-ui><span-container>xxx]</span></span></p>.
  230. const sel1 = domSelection( domFillerTextNode, INLINE_FILLER_LENGTH, domUiDeepSpan, 1 );
  231. expect( converter.isDomSelectionCorrect( sel1 ) ).to.be.false;
  232. const sel2 = domSelection( domUiDeepSpan, 1, domFillerTextNode, INLINE_FILLER_LENGTH );
  233. expect( converter.isDomSelectionCorrect( sel2 ) ).to.be.false;
  234. } );
  235. } );
  236. } );
  237. } );