8
0

domconverter.js 11 KB

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