8
0

domconverter.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 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. document.body.removeChild( domEditableParent );
  44. viewDocument.destroy();
  45. } );
  46. it( 'should call focus on corresponding DOM editable', () => {
  47. const focusSpy = testUtils.sinon.spy( domEditable, 'focus' );
  48. converter.focus( viewEditable );
  49. expect( focusSpy.calledOnce ).to.be.true;
  50. } );
  51. it( 'should not focus already focused editable', () => {
  52. const focusSpy = testUtils.sinon.spy( domEditable, 'focus' );
  53. converter.focus( viewEditable );
  54. converter.focus( viewEditable );
  55. expect( focusSpy.calledOnce ).to.be.true;
  56. } );
  57. // https://github.com/ckeditor/ckeditor5-engine/issues/951
  58. // https://github.com/ckeditor/ckeditor5-engine/issues/957
  59. it( 'should actively prevent scrolling', () => {
  60. const scrollToSpy = testUtils.sinon.stub( global.window, 'scrollTo' );
  61. const editableScrollLeftSpy = sinon.spy();
  62. const editableScrollTopSpy = sinon.spy();
  63. const parentScrollLeftSpy = sinon.spy();
  64. const parentScrollTopSpy = sinon.spy();
  65. const documentElementScrollLeftSpy = sinon.spy();
  66. const documentElementScrollTopSpy = sinon.spy();
  67. Object.defineProperties( domEditable, {
  68. scrollLeft: {
  69. get: () => 20,
  70. set: editableScrollLeftSpy
  71. },
  72. scrollTop: {
  73. get: () => 200,
  74. set: editableScrollTopSpy
  75. }
  76. } );
  77. Object.defineProperties( domEditableParent, {
  78. scrollLeft: {
  79. get: () => 40,
  80. set: parentScrollLeftSpy
  81. },
  82. scrollTop: {
  83. get: () => 400,
  84. set: parentScrollTopSpy
  85. }
  86. } );
  87. Object.defineProperties( global.document.documentElement, {
  88. scrollLeft: {
  89. get: () => 60,
  90. set: documentElementScrollLeftSpy
  91. },
  92. scrollTop: {
  93. get: () => 600,
  94. set: documentElementScrollTopSpy
  95. }
  96. } );
  97. global.window.scrollX = 10;
  98. global.window.scrollY = 100;
  99. converter.focus( viewEditable );
  100. sinon.assert.calledWithExactly( scrollToSpy, 10, 100 );
  101. sinon.assert.calledWithExactly( editableScrollLeftSpy, 20 );
  102. sinon.assert.calledWithExactly( editableScrollTopSpy, 200 );
  103. sinon.assert.calledWithExactly( parentScrollLeftSpy, 40 );
  104. sinon.assert.calledWithExactly( parentScrollTopSpy, 400 );
  105. sinon.assert.calledWithExactly( documentElementScrollLeftSpy, 60 );
  106. sinon.assert.calledWithExactly( documentElementScrollTopSpy, 600 );
  107. } );
  108. } );
  109. describe( 'DOM nodes type checking', () => {
  110. let text, element, documentFragment, comment;
  111. before( () => {
  112. text = document.createTextNode( 'test' );
  113. element = document.createElement( 'div' );
  114. documentFragment = document.createDocumentFragment();
  115. comment = document.createComment( 'a' );
  116. } );
  117. describe( 'isText()', () => {
  118. it( 'should return true for Text nodes', () => {
  119. expect( converter.isText( text ) ).to.be.true;
  120. } );
  121. it( 'should return false for other arguments', () => {
  122. expect( converter.isText( element ) ).to.be.false;
  123. expect( converter.isText( documentFragment ) ).to.be.false;
  124. expect( converter.isText( comment ) ).to.be.false;
  125. expect( converter.isText( {} ) ).to.be.false;
  126. } );
  127. } );
  128. describe( 'isElement()', () => {
  129. it( 'should return true for HTMLElement nodes', () => {
  130. expect( converter.isElement( element ) ).to.be.true;
  131. } );
  132. it( 'should return false for other arguments', () => {
  133. expect( converter.isElement( text ) ).to.be.false;
  134. expect( converter.isElement( documentFragment ) ).to.be.false;
  135. expect( converter.isElement( comment ) ).to.be.false;
  136. expect( converter.isElement( {} ) ).to.be.false;
  137. } );
  138. } );
  139. describe( 'isDocumentFragment()', () => {
  140. it( 'should return true for HTMLElement nodes', () => {
  141. expect( converter.isDocumentFragment( documentFragment ) ).to.be.true;
  142. } );
  143. it( 'should return false for other arguments', () => {
  144. expect( converter.isDocumentFragment( text ) ).to.be.false;
  145. expect( converter.isDocumentFragment( element ) ).to.be.false;
  146. expect( converter.isDocumentFragment( comment ) ).to.be.false;
  147. expect( converter.isDocumentFragment( {} ) ).to.be.false;
  148. } );
  149. } );
  150. describe( 'isComment()', () => {
  151. it( 'should return true for HTML comments', () => {
  152. expect( converter.isComment( comment ) ).to.be.true;
  153. } );
  154. it( 'should return false for other arguments', () => {
  155. expect( converter.isComment( text ) ).to.be.false;
  156. expect( converter.isComment( element ) ).to.be.false;
  157. expect( converter.isComment( documentFragment ) ).to.be.false;
  158. expect( converter.isComment( {} ) ).to.be.false;
  159. } );
  160. } );
  161. } );
  162. describe( 'isDomSelectionCorrect()', () => {
  163. function domSelection( anchorParent, anchorOffset, focusParent, focusOffset ) {
  164. const sel = document.getSelection();
  165. sel.collapse( anchorParent, anchorOffset );
  166. sel.extend( focusParent, focusOffset );
  167. return sel;
  168. }
  169. let domP, domFillerTextNode, domUiSpan, domUiDeepSpan;
  170. beforeEach( () => {
  171. // <p>INLINE_FILLERfoo<span></span></p>.
  172. domP = document.createElement( 'p' );
  173. domFillerTextNode = document.createTextNode( INLINE_FILLER + 'foo' );
  174. domUiSpan = document.createElement( 'span' );
  175. domUiDeepSpan = document.createElement( 'span' );
  176. domUiSpan.appendChild( domUiDeepSpan );
  177. const viewUiSpan = new ViewUIElement( 'span' );
  178. const viewElementSpan = new ViewContainerElement( 'span' );
  179. domP.appendChild( domFillerTextNode );
  180. domP.appendChild( domUiSpan );
  181. converter.bindElements( domUiSpan, viewUiSpan );
  182. converter.bindElements( domUiDeepSpan, viewElementSpan );
  183. document.body.appendChild( domP );
  184. } );
  185. it( 'should return true for correct dom selection', () => {
  186. // <p>INLINE_FILLER{foo}<span></span></p>.
  187. const sel1 = domSelection( domFillerTextNode, INLINE_FILLER_LENGTH, domFillerTextNode, INLINE_FILLER_LENGTH + 3 );
  188. expect( converter.isDomSelectionCorrect( sel1 ) ).to.be.true;
  189. // <p>INLINE_FILLERfoo[]<span></span></p>.
  190. const sel2 = domSelection( domP, 1, domP, 1 );
  191. expect( converter.isDomSelectionCorrect( sel2 ) ).to.be.true;
  192. // <p>INLINE_FILLERfoo<span></span>[]</p>.
  193. const sel3 = domSelection( domP, 2, domP, 2 );
  194. expect( converter.isDomSelectionCorrect( sel3 ) ).to.be.true;
  195. } );
  196. describe( 'should return false', () => {
  197. it( 'if anchor or focus is before filler node', () => {
  198. // Tests forward and backward selection.
  199. // <p>[INLINE_FILLERfoo]<span-ui><span-container></span></span></p>.
  200. const sel1 = domSelection( domP, 0, domP, 1 );
  201. expect( converter.isDomSelectionCorrect( sel1 ) ).to.be.false;
  202. const sel2 = domSelection( domP, 1, domP, 0 );
  203. expect( converter.isDomSelectionCorrect( sel2 ) ).to.be.false;
  204. } );
  205. it( 'if anchor or focus is before filler sequence', () => {
  206. // Tests forward and backward selection.
  207. // <p>{INLINE_FILLERfoo}<span-ui><span-container></span></span></p>.
  208. const sel1 = domSelection( domFillerTextNode, 0, domFillerTextNode, INLINE_FILLER_LENGTH + 3 );
  209. expect( converter.isDomSelectionCorrect( sel1 ) ).to.be.false;
  210. const sel2 = domSelection( domFillerTextNode, INLINE_FILLER_LENGTH + 3, domFillerTextNode, 0 );
  211. expect( converter.isDomSelectionCorrect( sel2 ) ).to.be.false;
  212. } );
  213. it( 'if anchor or focus is in the middle of filler sequence', () => {
  214. // Tests forward and backward selection.
  215. // <p>I{NLINE_FILLERfoo}<span-ui><span-container></span></span></p>.
  216. const sel1 = domSelection( domFillerTextNode, 1, domFillerTextNode, INLINE_FILLER_LENGTH + 3 );
  217. expect( converter.isDomSelectionCorrect( sel1 ) ).to.be.false;
  218. const sel2 = domSelection( domFillerTextNode, INLINE_FILLER_LENGTH + 3, domFillerTextNode, 1 );
  219. expect( converter.isDomSelectionCorrect( sel2 ) ).to.be.false;
  220. } );
  221. it( 'if anchor or focus is directly inside dom element that represents view ui element', () => {
  222. // Tests forward and backward selection.
  223. // <p>INLINE_FILLER{foo<span-ui>]<span-container></span></span></p>.
  224. const sel1 = domSelection( domFillerTextNode, INLINE_FILLER_LENGTH + 3, domUiSpan, 0 );
  225. expect( converter.isDomSelectionCorrect( sel1 ) ).to.be.false;
  226. const sel2 = domSelection( domUiSpan, 0, domFillerTextNode, INLINE_FILLER_LENGTH + 3 );
  227. expect( converter.isDomSelectionCorrect( sel2 ) ).to.be.false;
  228. } );
  229. it( 'if anchor or focus is inside deep ui element structure (not directly in ui element)', () => {
  230. // Tests forward and backward selection.
  231. // <p>INLINE_FILLER{foo<span-ui><span-container>]</span></span></p>.
  232. const sel1 = domSelection( domFillerTextNode, INLINE_FILLER_LENGTH + 3, domUiDeepSpan, 0 );
  233. expect( converter.isDomSelectionCorrect( sel1 ) ).to.be.false;
  234. const sel2 = domSelection( domUiDeepSpan, 0, domFillerTextNode, INLINE_FILLER_LENGTH + 3 );
  235. expect( converter.isDomSelectionCorrect( sel2 ) ).to.be.false;
  236. } );
  237. } );
  238. } );
  239. } );