8
0

domconverter.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /**
  2. * @license Copyright (c) 2003-2020, 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( viewDocument, '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, viewDocument;
  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. viewDocument = new ViewDocument();
  168. const viewUiSpan = new ViewUIElement( viewDocument, 'span' );
  169. const viewElementSpan = new ViewContainerElement( viewDocument, '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. describe( 'isBlockFiller()', () => {
  238. describe( 'mode "nbsp"', () => {
  239. beforeEach( () => {
  240. converter = new DomConverter( { blockFillerMode: 'nbsp' } );
  241. } );
  242. it( 'should return true if the node is an nbsp filler and is a single child of a block level element', () => {
  243. const nbspFillerInstance = NBSP_FILLER( document ); // eslint-disable-line new-cap
  244. const context = document.createElement( 'div' );
  245. context.appendChild( nbspFillerInstance );
  246. expect( converter.isBlockFiller( nbspFillerInstance ) ).to.be.true;
  247. } );
  248. it( 'should return false if the node is an nbsp filler and is not a single child of a block level element', () => {
  249. const nbspFillerInstance = NBSP_FILLER( document ); // eslint-disable-line new-cap
  250. const context = document.createElement( 'div' );
  251. context.appendChild( nbspFillerInstance );
  252. context.appendChild( document.createTextNode( 'a' ) );
  253. expect( converter.isBlockFiller( nbspFillerInstance ) ).to.be.false;
  254. } );
  255. it( 'should return false if there are two nbsp fillers in a block element', () => {
  256. const nbspFillerInstance = NBSP_FILLER( document ); // eslint-disable-line new-cap
  257. const context = document.createElement( 'div' );
  258. context.appendChild( nbspFillerInstance );
  259. context.appendChild( NBSP_FILLER( document ) ); // eslint-disable-line new-cap
  260. expect( converter.isBlockFiller( nbspFillerInstance ) ).to.be.false;
  261. } );
  262. it( 'should return false filler is placed in a non-block element', () => {
  263. const nbspFillerInstance = NBSP_FILLER( document ); // eslint-disable-line new-cap
  264. const context = document.createElement( 'span' );
  265. context.appendChild( nbspFillerInstance );
  266. expect( converter.isBlockFiller( nbspFillerInstance ) ).to.be.false;
  267. } );
  268. it( 'should return false if the node is an instance of the BR block filler', () => {
  269. const brFillerInstance = BR_FILLER( document ); // eslint-disable-line new-cap
  270. expect( converter.isBlockFiller( brFillerInstance ) ).to.be.false;
  271. } );
  272. it( 'should return false for inline filler', () => {
  273. expect( converter.isBlockFiller( document.createTextNode( INLINE_FILLER ) ) ).to.be.false;
  274. } );
  275. it( 'should return false for a normal <br> element', () => {
  276. const context = document.createElement( 'div' );
  277. context.innerHTML = 'x<br>x';
  278. expect( converter.isBlockFiller( context.childNodes[ 1 ] ) ).to.be.false;
  279. } );
  280. // SPECIAL CASE (see ckeditor5#5564).
  281. it( 'should return true for a <br> element which is the only child of its block parent', () => {
  282. const context = document.createElement( 'div' );
  283. context.innerHTML = '<br>';
  284. expect( converter.isBlockFiller( context.firstChild ) ).to.be.true;
  285. } );
  286. it( 'should return false for a <br> element which is the only child of its non-block parent', () => {
  287. const context = document.createElement( 'span' );
  288. context.innerHTML = '<br>';
  289. expect( converter.isBlockFiller( context.firstChild ) ).to.be.false;
  290. } );
  291. it( 'should return false for a <br> element which is followed by an nbsp', () => {
  292. const context = document.createElement( 'span' );
  293. context.innerHTML = '<br>&nbsp;';
  294. expect( converter.isBlockFiller( context.firstChild ) ).to.be.false;
  295. } );
  296. } );
  297. describe( 'mode "br"', () => {
  298. beforeEach( () => {
  299. converter = new DomConverter( { blockFillerMode: 'br' } );
  300. } );
  301. it( 'should return true if the node is an instance of the BR block filler', () => {
  302. const brFillerInstance = BR_FILLER( document ); // eslint-disable-line new-cap
  303. expect( converter.isBlockFiller( brFillerInstance ) ).to.be.true;
  304. // Check it twice to ensure that caching breaks nothing.
  305. expect( converter.isBlockFiller( brFillerInstance ) ).to.be.true;
  306. } );
  307. it( 'should return false if the node is an instance of the NBSP block filler', () => {
  308. converter = new DomConverter( { blockFillerMode: 'br' } );
  309. const nbspFillerInstance = NBSP_FILLER( document ); // eslint-disable-line new-cap
  310. // NBSP must be check inside a context.
  311. const context = document.createElement( 'div' );
  312. context.appendChild( nbspFillerInstance );
  313. expect( converter.isBlockFiller( nbspFillerInstance ) ).to.be.false;
  314. } );
  315. it( 'should return false for inline filler', () => {
  316. expect( converter.isBlockFiller( document.createTextNode( INLINE_FILLER ) ) ).to.be.false;
  317. } );
  318. } );
  319. } );
  320. } );