8
0

domconverter.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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, INLINE_FILLER, INLINE_FILLER_LENGTH, NBSP_FILLER } 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 mode by default', () => {
  22. expect( converter.blockFillerMode ).to.equal( 'br' );
  23. } );
  24. it( 'should create converter with defined block mode filler', () => {
  25. converter = new DomConverter( { blockFillerMode: 'nbsp' } );
  26. expect( converter.blockFillerMode ).to.equal( 'nbsp' );
  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. document.body.focus();
  46. } );
  47. it( 'should call focus on corresponding DOM editable', () => {
  48. const focusSpy = testUtils.sinon.spy( domEditable, 'focus' );
  49. converter.focus( viewEditable );
  50. expect( focusSpy.calledOnce ).to.be.true;
  51. } );
  52. it( 'should not focus already focused editable', () => {
  53. const focusSpy = testUtils.sinon.spy( domEditable, 'focus' );
  54. converter.focus( viewEditable );
  55. converter.focus( viewEditable );
  56. expect( focusSpy.calledOnce ).to.be.true;
  57. } );
  58. // https://github.com/ckeditor/ckeditor5-engine/issues/951
  59. // https://github.com/ckeditor/ckeditor5-engine/issues/957
  60. it( 'should actively prevent scrolling', () => {
  61. const scrollToSpy = testUtils.sinon.stub( global.window, 'scrollTo' );
  62. const editableScrollLeftSpy = sinon.spy();
  63. const editableScrollTopSpy = sinon.spy();
  64. const parentScrollLeftSpy = sinon.spy();
  65. const parentScrollTopSpy = sinon.spy();
  66. const documentElementScrollLeftSpy = sinon.spy();
  67. const documentElementScrollTopSpy = sinon.spy();
  68. Object.defineProperties( domEditable, {
  69. scrollLeft: {
  70. get: () => 20,
  71. set: editableScrollLeftSpy
  72. },
  73. scrollTop: {
  74. get: () => 200,
  75. set: editableScrollTopSpy
  76. }
  77. } );
  78. Object.defineProperties( domEditableParent, {
  79. scrollLeft: {
  80. get: () => 40,
  81. set: parentScrollLeftSpy
  82. },
  83. scrollTop: {
  84. get: () => 400,
  85. set: parentScrollTopSpy
  86. }
  87. } );
  88. Object.defineProperties( global.document.documentElement, {
  89. scrollLeft: {
  90. get: () => 60,
  91. set: documentElementScrollLeftSpy
  92. },
  93. scrollTop: {
  94. get: () => 600,
  95. set: documentElementScrollTopSpy
  96. }
  97. } );
  98. testUtils.sinon.stub( global.window, 'scrollX' ).get( () => 10 );
  99. testUtils.sinon.stub( global.window, 'scrollY' ).get( () => 100 );
  100. converter.focus( viewEditable );
  101. sinon.assert.calledWithExactly( scrollToSpy, 10, 100 );
  102. sinon.assert.calledWithExactly( editableScrollLeftSpy, 20 );
  103. sinon.assert.calledWithExactly( editableScrollTopSpy, 200 );
  104. sinon.assert.calledWithExactly( parentScrollLeftSpy, 40 );
  105. sinon.assert.calledWithExactly( parentScrollTopSpy, 400 );
  106. sinon.assert.calledWithExactly( documentElementScrollLeftSpy, 60 );
  107. sinon.assert.calledWithExactly( documentElementScrollTopSpy, 600 );
  108. } );
  109. } );
  110. describe( 'DOM nodes type checking', () => {
  111. let text, element, documentFragment, comment;
  112. before( () => {
  113. text = document.createTextNode( 'test' );
  114. element = document.createElement( 'div' );
  115. documentFragment = document.createDocumentFragment();
  116. comment = document.createComment( 'a' );
  117. } );
  118. describe( 'isElement()', () => {
  119. it( 'should return true for HTMLElement nodes', () => {
  120. expect( converter.isElement( element ) ).to.be.true;
  121. } );
  122. it( 'should return false for other arguments', () => {
  123. expect( converter.isElement( text ) ).to.be.false;
  124. expect( converter.isElement( documentFragment ) ).to.be.false;
  125. expect( converter.isElement( comment ) ).to.be.false;
  126. expect( converter.isElement( {} ) ).to.be.false;
  127. } );
  128. } );
  129. describe( 'isDocumentFragment()', () => {
  130. it( 'should return true for HTMLElement nodes', () => {
  131. expect( converter.isDocumentFragment( documentFragment ) ).to.be.true;
  132. } );
  133. it( 'should return false for other arguments', () => {
  134. expect( converter.isDocumentFragment( text ) ).to.be.false;
  135. expect( converter.isDocumentFragment( element ) ).to.be.false;
  136. expect( converter.isDocumentFragment( comment ) ).to.be.false;
  137. expect( converter.isDocumentFragment( {} ) ).to.be.false;
  138. } );
  139. } );
  140. describe( 'isComment()', () => {
  141. it( 'should return true for HTML comments', () => {
  142. expect( converter.isComment( comment ) ).to.be.true;
  143. } );
  144. it( 'should return false for other arguments', () => {
  145. expect( converter.isComment( text ) ).to.be.false;
  146. expect( converter.isComment( element ) ).to.be.false;
  147. expect( converter.isComment( documentFragment ) ).to.be.false;
  148. expect( converter.isComment( {} ) ).to.be.false;
  149. } );
  150. } );
  151. } );
  152. describe( 'isDomSelectionCorrect()', () => {
  153. function domSelection( anchorParent, anchorOffset, focusParent, focusOffset ) {
  154. const sel = document.getSelection();
  155. sel.collapse( anchorParent, anchorOffset );
  156. sel.extend( focusParent, focusOffset );
  157. return sel;
  158. }
  159. let domP, domFillerTextNode, domUiSpan, domUiDeepSpan;
  160. beforeEach( () => {
  161. // <p>INLINE_FILLERfoo<span></span></p>.
  162. domP = document.createElement( 'p' );
  163. domFillerTextNode = document.createTextNode( INLINE_FILLER + 'foo' );
  164. domUiSpan = document.createElement( 'span' );
  165. domUiDeepSpan = document.createElement( 'span' );
  166. domUiSpan.appendChild( domUiDeepSpan );
  167. const viewUiSpan = new ViewUIElement( 'span' );
  168. const viewElementSpan = new ViewContainerElement( 'span' );
  169. domP.appendChild( domFillerTextNode );
  170. domP.appendChild( domUiSpan );
  171. converter.bindElements( domUiSpan, viewUiSpan );
  172. converter.bindElements( domUiDeepSpan, viewElementSpan );
  173. document.body.appendChild( domP );
  174. } );
  175. afterEach( () => {
  176. domP.remove();
  177. } );
  178. it( 'should return true for correct dom selection', () => {
  179. // <p>INLINE_FILLER{foo}<span></span></p>.
  180. const sel1 = domSelection( domFillerTextNode, INLINE_FILLER_LENGTH, domFillerTextNode, INLINE_FILLER_LENGTH + 3 );
  181. expect( converter.isDomSelectionCorrect( sel1 ) ).to.be.true;
  182. // <p>INLINE_FILLERfoo[]<span></span></p>.
  183. const sel2 = domSelection( domP, 1, domP, 1 );
  184. expect( converter.isDomSelectionCorrect( sel2 ) ).to.be.true;
  185. // <p>INLINE_FILLERfoo<span></span>[]</p>.
  186. const sel3 = domSelection( domP, 2, domP, 2 );
  187. expect( converter.isDomSelectionCorrect( sel3 ) ).to.be.true;
  188. } );
  189. describe( 'should return false', () => {
  190. it( 'if anchor or focus is before filler node', () => {
  191. // Tests forward and backward selection.
  192. // <p>[INLINE_FILLERfoo]<span-ui><span-container></span></span></p>.
  193. const sel1 = domSelection( domP, 0, domP, 1 );
  194. expect( converter.isDomSelectionCorrect( sel1 ) ).to.be.false;
  195. const sel2 = domSelection( domP, 1, domP, 0 );
  196. expect( converter.isDomSelectionCorrect( sel2 ) ).to.be.false;
  197. } );
  198. it( 'if anchor or focus is before filler sequence', () => {
  199. // Tests forward and backward selection.
  200. // <p>{INLINE_FILLERfoo}<span-ui><span-container></span></span></p>.
  201. const sel1 = domSelection( domFillerTextNode, 0, domFillerTextNode, INLINE_FILLER_LENGTH + 3 );
  202. expect( converter.isDomSelectionCorrect( sel1 ) ).to.be.false;
  203. const sel2 = domSelection( domFillerTextNode, INLINE_FILLER_LENGTH + 3, domFillerTextNode, 0 );
  204. expect( converter.isDomSelectionCorrect( sel2 ) ).to.be.false;
  205. } );
  206. it( 'if anchor or focus is in the middle of filler sequence', () => {
  207. // Tests forward and backward selection.
  208. // <p>I{NLINE_FILLERfoo}<span-ui><span-container></span></span></p>.
  209. const sel1 = domSelection( domFillerTextNode, 1, domFillerTextNode, INLINE_FILLER_LENGTH + 3 );
  210. expect( converter.isDomSelectionCorrect( sel1 ) ).to.be.false;
  211. const sel2 = domSelection( domFillerTextNode, INLINE_FILLER_LENGTH + 3, domFillerTextNode, 1 );
  212. expect( converter.isDomSelectionCorrect( sel2 ) ).to.be.false;
  213. } );
  214. it( 'if anchor or focus is directly inside dom element that represents view ui element', () => {
  215. // Set text indside ui element to put selection there.
  216. domUiSpan.innerText = 'xxx';
  217. // Tests forward and backward selection.
  218. // <p>INLINE_FILLER{foo<span-ui>xxx]<span-container></span></span></p>.
  219. const sel1 = domSelection( domFillerTextNode, INLINE_FILLER_LENGTH, domUiSpan, 1 );
  220. expect( converter.isDomSelectionCorrect( sel1 ) ).to.be.false;
  221. const sel2 = domSelection( domUiSpan, 1, domFillerTextNode, INLINE_FILLER_LENGTH );
  222. expect( converter.isDomSelectionCorrect( sel2 ) ).to.be.false;
  223. } );
  224. it( 'if anchor or focus is inside deep ui element structure (not directly in ui element)', () => {
  225. // Set text indside ui element to put selection there.
  226. domUiDeepSpan.innerText = 'xxx';
  227. // Tests forward and backward selection.
  228. // <p>INLINE_FILLER{foo<span-ui><span-container>xxx]</span></span></p>.
  229. const sel1 = domSelection( domFillerTextNode, INLINE_FILLER_LENGTH, domUiDeepSpan, 1 );
  230. expect( converter.isDomSelectionCorrect( sel1 ) ).to.be.false;
  231. const sel2 = domSelection( domUiDeepSpan, 1, domFillerTextNode, INLINE_FILLER_LENGTH );
  232. expect( converter.isDomSelectionCorrect( sel2 ) ).to.be.false;
  233. } );
  234. } );
  235. } );
  236. describe( 'isBlockFiller()', () => {
  237. describe( 'mode "nbsp"', () => {
  238. beforeEach( () => {
  239. converter = new DomConverter( { blockFillerMode: 'nbsp' } );
  240. } );
  241. it( 'should return true if the node is an instance of the NBSP block filler', () => {
  242. converter = new DomConverter( { blockFillerMode: 'nbsp' } );
  243. const nbspFillerInstance = NBSP_FILLER( document ); // eslint-disable-line new-cap
  244. // NBSP must be check inside a context.
  245. const context = document.createElement( 'div' );
  246. context.appendChild( nbspFillerInstance );
  247. expect( converter.isBlockFiller( nbspFillerInstance ) ).to.be.true;
  248. } );
  249. it( 'should return false if the node is an instance of the BR block filler', () => {
  250. const brFillerInstance = BR_FILLER( document ); // eslint-disable-line new-cap
  251. expect( converter.isBlockFiller( brFillerInstance ) ).to.be.false;
  252. } );
  253. it( 'should return false if the nbsp filler is inside context', () => {
  254. converter = new DomConverter( { blockFillerMode: 'nbsp' } );
  255. const nbspFillerInstance = NBSP_FILLER( document ); // eslint-disable-line new-cap
  256. // NBSP must be check inside a context.
  257. const context = document.createElement( 'div' );
  258. context.appendChild( nbspFillerInstance );
  259. // eslint-disable-next-line new-cap
  260. context.appendChild( NBSP_FILLER( document ) );
  261. expect( converter.isBlockFiller( nbspFillerInstance ) ).to.be.false;
  262. } );
  263. it( 'should return false for inline filler', () => {
  264. expect( converter.isBlockFiller( document.createTextNode( INLINE_FILLER ) ) ).to.be.false;
  265. } );
  266. } );
  267. describe( 'mode "br"', () => {
  268. beforeEach( () => {
  269. converter = new DomConverter( { blockFillerMode: 'br' } );
  270. } );
  271. it( 'should return true if the node is an instance of the BR block filler', () => {
  272. const brFillerInstance = BR_FILLER( document ); // eslint-disable-line new-cap
  273. expect( converter.isBlockFiller( brFillerInstance ) ).to.be.true;
  274. // Check it twice to ensure that caching breaks nothing.
  275. expect( converter.isBlockFiller( brFillerInstance ) ).to.be.true;
  276. } );
  277. it( 'should return false if the node is an instance of the NBSP block filler', () => {
  278. converter = new DomConverter( { blockFillerMode: 'br' } );
  279. const nbspFillerInstance = NBSP_FILLER( document ); // eslint-disable-line new-cap
  280. // NBSP must be check inside a context.
  281. const context = document.createElement( 'div' );
  282. context.appendChild( nbspFillerInstance );
  283. expect( converter.isBlockFiller( nbspFillerInstance ) ).to.be.false;
  284. } );
  285. it( 'should return false for inline filler', () => {
  286. expect( converter.isBlockFiller( document.createTextNode( INLINE_FILLER ) ) ).to.be.false;
  287. } );
  288. } );
  289. } );
  290. } );