verticalnavigation.js 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  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. import Table from '@ckeditor/ckeditor5-table/src/table';
  6. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  8. import Image from '@ckeditor/ckeditor5-image/src/image';
  9. import ImageCaption from '@ckeditor/ckeditor5-image/src/imagecaption';
  10. import HorizontalLine from '@ckeditor/ckeditor5-horizontal-line/src/horizontalline';
  11. import BlockQuote from '@ckeditor/ckeditor5-block-quote/src/blockquote';
  12. import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  13. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  15. import { modelTable } from '@ckeditor/ckeditor5-table/tests/_utils/utils';
  16. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  17. import env from '@ckeditor/ckeditor5-utils/src/env';
  18. describe( 'Widget - vertical keyboard navigation near widgets', () => {
  19. let editorElement, editor, model;
  20. let leftArrowDomEvtDataStub, rightArrowDomEvtDataStub, upArrowDomEvtDataStub, downArrowDomEvtDataStub;
  21. const imageUrl = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAUCAQAAADRyVAeAAAAKklEQVR42u3PAQ0AAAwCI' +
  22. 'O0f+u/hoAHNZUJFRERERERERERERERERLYiD9N4FAFj2iK6AAAAAElFTkSuQmCC';
  23. beforeEach( async () => {
  24. editorElement = global.document.createElement( 'div' );
  25. global.document.body.appendChild( editorElement );
  26. editor = await ClassicTestEditor.create( editorElement, {
  27. plugins: [ Table, Paragraph, Image, ImageCaption, HorizontalLine, BlockQuote ]
  28. } );
  29. model = editor.model;
  30. // The editing view must be focused because otherwise in Chrome the DOM selection will not contain
  31. // any ranges and jumpOverUiElement will crash (for the right arrow when shift is pressed).
  32. editor.editing.view.focus();
  33. leftArrowDomEvtDataStub = {
  34. keyCode: getCode( 'ArrowLeft' ),
  35. preventDefault: sinon.spy(),
  36. stopPropagation: sinon.spy(),
  37. domTarget: global.document.body
  38. };
  39. rightArrowDomEvtDataStub = {
  40. keyCode: getCode( 'ArrowRight' ),
  41. preventDefault: sinon.spy(),
  42. stopPropagation: sinon.spy(),
  43. domTarget: global.document.body
  44. };
  45. upArrowDomEvtDataStub = {
  46. keyCode: getCode( 'ArrowUp' ),
  47. preventDefault: sinon.spy(),
  48. stopPropagation: sinon.spy(),
  49. domTarget: global.document.body
  50. };
  51. downArrowDomEvtDataStub = {
  52. keyCode: getCode( 'ArrowDown' ),
  53. preventDefault: sinon.spy(),
  54. stopPropagation: sinon.spy(),
  55. domTarget: global.document.body
  56. };
  57. } );
  58. afterEach( async () => {
  59. editorElement.remove();
  60. await editor.destroy();
  61. } );
  62. it( 'should do nothing if pressed left-arrow key', () => {
  63. setModelData( model,
  64. '<paragraph>foo</paragraph>' +
  65. '<paragraph>b[]ar</paragraph>' +
  66. '<paragraph>abc</paragraph>'
  67. );
  68. editor.editing.view.document.fire( 'keydown', leftArrowDomEvtDataStub );
  69. sinon.assert.notCalled( leftArrowDomEvtDataStub.preventDefault );
  70. sinon.assert.notCalled( leftArrowDomEvtDataStub.stopPropagation );
  71. assertEqualMarkup(
  72. getModelData( model ),
  73. '<paragraph>foo</paragraph>' +
  74. '<paragraph>b[]ar</paragraph>' +
  75. '<paragraph>abc</paragraph>'
  76. );
  77. } );
  78. it( 'should do nothing if pressed right-arrow key', () => {
  79. setModelData( model,
  80. '<paragraph>foo</paragraph>' +
  81. '<paragraph>b[]ar</paragraph>' +
  82. '<paragraph>abc</paragraph>'
  83. );
  84. editor.editing.view.document.fire( 'keydown', rightArrowDomEvtDataStub );
  85. sinon.assert.notCalled( rightArrowDomEvtDataStub.preventDefault );
  86. sinon.assert.notCalled( rightArrowDomEvtDataStub.stopPropagation );
  87. assertEqualMarkup(
  88. getModelData( model ),
  89. '<paragraph>foo</paragraph>' +
  90. '<paragraph>b[]ar</paragraph>' +
  91. '<paragraph>abc</paragraph>'
  92. );
  93. } );
  94. it( 'should do nothing if shrinking non-collapsed forward selection', () => {
  95. setModelData( model, '<paragraph>fo[ob]ar</paragraph>' );
  96. upArrowDomEvtDataStub.shiftKey = true;
  97. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  98. sinon.assert.notCalled( upArrowDomEvtDataStub.preventDefault );
  99. sinon.assert.notCalled( upArrowDomEvtDataStub.stopPropagation );
  100. } );
  101. it( 'should do nothing if shrinking non-collapsed backward selection', () => {
  102. setModelData( model, '<paragraph>fo[ob]ar</paragraph>', { lastRangeBackward: true } );
  103. downArrowDomEvtDataStub.shiftKey = true;
  104. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  105. sinon.assert.notCalled( downArrowDomEvtDataStub.preventDefault );
  106. sinon.assert.notCalled( downArrowDomEvtDataStub.stopPropagation );
  107. } );
  108. describe( 'with selection inside root content editable', () => {
  109. let styleElement;
  110. beforeEach( () => {
  111. styleElement = global.document.createElement( 'style' );
  112. styleElement.type = 'text/css';
  113. styleElement.appendChild( global.document.createTextNode(
  114. `
  115. * {
  116. font-size: 12px !important;
  117. font-family: serif !important;
  118. margin: 0 !important;
  119. padding: 0 !important;
  120. border: 0 !important
  121. }
  122. .ck-editor__editable:not(.ck-editor__nested-editable ) {
  123. width: 300px !important;
  124. }
  125. td { width: 30px !important; }
  126. tr:nth-child(2) td:nth-child(2) { width: 300px !important; }
  127. `
  128. ) );
  129. global.document.querySelector( 'head' ).appendChild( styleElement );
  130. } );
  131. afterEach( () => {
  132. styleElement.remove();
  133. } );
  134. describe( 'single paragraph surrounded with objects', () => {
  135. describe( 'collapsed selection', () => {
  136. beforeEach( () => {
  137. setModelData( model,
  138. '<horizontalLine></horizontalLine>' +
  139. '<paragraph>foo[]bar</paragraph>' +
  140. '<horizontalLine></horizontalLine>'
  141. );
  142. } );
  143. it( 'should move caret to the position closest to object (navigating forward)', () => {
  144. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  145. sinon.assert.calledOnce( downArrowDomEvtDataStub.preventDefault );
  146. sinon.assert.calledOnce( downArrowDomEvtDataStub.stopPropagation );
  147. assertEqualMarkup( getModelData( model ),
  148. '<horizontalLine></horizontalLine>' +
  149. '<paragraph>foobar[]</paragraph>' +
  150. '<horizontalLine></horizontalLine>'
  151. );
  152. } );
  153. it( 'should move caret to the position closest to object (navigating backward)', () => {
  154. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  155. sinon.assert.calledOnce( upArrowDomEvtDataStub.preventDefault );
  156. sinon.assert.calledOnce( upArrowDomEvtDataStub.stopPropagation );
  157. assertEqualMarkup( getModelData( model ),
  158. '<horizontalLine></horizontalLine>' +
  159. '<paragraph>[]foobar</paragraph>' +
  160. '<horizontalLine></horizontalLine>'
  161. );
  162. } );
  163. } );
  164. describe( 'non-collapsed forward selection', () => {
  165. beforeEach( () => {
  166. setModelData( model,
  167. '<horizontalLine></horizontalLine>' +
  168. '<paragraph>fo[ob]ar</paragraph>' +
  169. '<horizontalLine></horizontalLine>'
  170. );
  171. } );
  172. it( 'should move caret to the position closest to object (navigating forward)', () => {
  173. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  174. sinon.assert.calledOnce( downArrowDomEvtDataStub.preventDefault );
  175. sinon.assert.calledOnce( downArrowDomEvtDataStub.stopPropagation );
  176. assertEqualMarkup( getModelData( model ),
  177. '<horizontalLine></horizontalLine>' +
  178. '<paragraph>foobar[]</paragraph>' +
  179. '<horizontalLine></horizontalLine>'
  180. );
  181. } );
  182. it( 'should move caret to the position closest to object (navigating backward)', () => {
  183. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  184. sinon.assert.calledOnce( upArrowDomEvtDataStub.preventDefault );
  185. sinon.assert.calledOnce( upArrowDomEvtDataStub.stopPropagation );
  186. assertEqualMarkup( getModelData( model ),
  187. '<horizontalLine></horizontalLine>' +
  188. '<paragraph>[]foobar</paragraph>' +
  189. '<horizontalLine></horizontalLine>'
  190. );
  191. } );
  192. describe( 'with shift pressed', () => {
  193. it( 'should expand the selection to the position closest to object (navigating forward)', () => {
  194. downArrowDomEvtDataStub.shiftKey = true;
  195. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  196. sinon.assert.calledOnce( downArrowDomEvtDataStub.preventDefault );
  197. sinon.assert.calledOnce( downArrowDomEvtDataStub.stopPropagation );
  198. assertEqualMarkup( getModelData( model ),
  199. '<horizontalLine></horizontalLine>' +
  200. '<paragraph>fo[obar]</paragraph>' +
  201. '<horizontalLine></horizontalLine>'
  202. );
  203. } );
  204. it( 'should not prevent default browser behavior while navigating backward', () => {
  205. upArrowDomEvtDataStub.shiftKey = true;
  206. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  207. sinon.assert.notCalled( upArrowDomEvtDataStub.preventDefault );
  208. sinon.assert.notCalled( upArrowDomEvtDataStub.stopPropagation );
  209. } );
  210. } );
  211. } );
  212. describe( 'non-collapsed backward selection', () => {
  213. beforeEach( () => {
  214. setModelData( model,
  215. '<horizontalLine></horizontalLine>' +
  216. '<paragraph>fo[ob]ar</paragraph>' +
  217. '<horizontalLine></horizontalLine>',
  218. { lastRangeBackward: true }
  219. );
  220. } );
  221. it( 'should move caret to the position closest to object (navigating forward)', () => {
  222. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  223. sinon.assert.calledOnce( downArrowDomEvtDataStub.preventDefault );
  224. sinon.assert.calledOnce( downArrowDomEvtDataStub.stopPropagation );
  225. assertEqualMarkup( getModelData( model ),
  226. '<horizontalLine></horizontalLine>' +
  227. '<paragraph>foobar[]</paragraph>' +
  228. '<horizontalLine></horizontalLine>'
  229. );
  230. } );
  231. it( 'should move caret to the position closest to object (navigating backward)', () => {
  232. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  233. sinon.assert.calledOnce( upArrowDomEvtDataStub.preventDefault );
  234. sinon.assert.calledOnce( upArrowDomEvtDataStub.stopPropagation );
  235. assertEqualMarkup( getModelData( model ),
  236. '<horizontalLine></horizontalLine>' +
  237. '<paragraph>[]foobar</paragraph>' +
  238. '<horizontalLine></horizontalLine>'
  239. );
  240. } );
  241. describe( 'with shift pressed', () => {
  242. it( 'should not prevent default browser behavior while navigating forward', () => {
  243. downArrowDomEvtDataStub.shiftKey = true;
  244. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  245. sinon.assert.notCalled( downArrowDomEvtDataStub.preventDefault );
  246. sinon.assert.notCalled( downArrowDomEvtDataStub.stopPropagation );
  247. } );
  248. it( 'should expand the selection to the position closest to object (navigating backward)', () => {
  249. upArrowDomEvtDataStub.shiftKey = true;
  250. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  251. sinon.assert.calledOnce( upArrowDomEvtDataStub.preventDefault );
  252. sinon.assert.calledOnce( upArrowDomEvtDataStub.stopPropagation );
  253. assertEqualMarkup( getModelData( model ),
  254. '<horizontalLine></horizontalLine>' +
  255. '<paragraph>[foob]ar</paragraph>' +
  256. '<horizontalLine></horizontalLine>'
  257. );
  258. } );
  259. } );
  260. } );
  261. } );
  262. describe( 'multiple paragraphs with object inside', () => {
  263. describe( 'caret in the first paragraph', () => {
  264. beforeEach( () => {
  265. setModelData( model,
  266. '<paragraph>fo[]oo</paragraph>' +
  267. '<paragraph>bar</paragraph>' +
  268. '<horizontalLine></horizontalLine>' +
  269. '<paragraph>FOO</paragraph>' +
  270. '<paragraph>BAR</paragraph>'
  271. );
  272. } );
  273. it( 'should not prevent default on forward navigation', () => {
  274. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  275. sinon.assert.notCalled( downArrowDomEvtDataStub.preventDefault );
  276. sinon.assert.notCalled( downArrowDomEvtDataStub.stopPropagation );
  277. } );
  278. it( 'should not prevent default on backward navigation', () => {
  279. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  280. sinon.assert.notCalled( upArrowDomEvtDataStub.preventDefault );
  281. sinon.assert.notCalled( upArrowDomEvtDataStub.stopPropagation );
  282. } );
  283. } );
  284. describe( 'caret in the second paragraph', () => {
  285. beforeEach( () => {
  286. setModelData( model,
  287. '<paragraph>foo</paragraph>' +
  288. '<paragraph>ba[]ar</paragraph>' +
  289. '<horizontalLine></horizontalLine>' +
  290. '<paragraph>FOO</paragraph>' +
  291. '<paragraph>BAR</paragraph>'
  292. );
  293. } );
  294. it( 'should move caret to the position closest to object on forward navigation', () => {
  295. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  296. sinon.assert.calledOnce( downArrowDomEvtDataStub.preventDefault );
  297. sinon.assert.calledOnce( downArrowDomEvtDataStub.stopPropagation );
  298. assertEqualMarkup( getModelData( model ),
  299. '<paragraph>foo</paragraph>' +
  300. '<paragraph>baar[]</paragraph>' +
  301. '<horizontalLine></horizontalLine>' +
  302. '<paragraph>FOO</paragraph>' +
  303. '<paragraph>BAR</paragraph>'
  304. );
  305. } );
  306. it( 'should not prevent default on backward navigation', () => {
  307. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  308. sinon.assert.notCalled( upArrowDomEvtDataStub.preventDefault );
  309. sinon.assert.notCalled( upArrowDomEvtDataStub.stopPropagation );
  310. } );
  311. } );
  312. describe( 'caret in the third paragraph', () => {
  313. beforeEach( () => {
  314. setModelData( model,
  315. '<paragraph>foo</paragraph>' +
  316. '<paragraph>bar</paragraph>' +
  317. '<horizontalLine></horizontalLine>' +
  318. '<paragraph>FO[]OO</paragraph>' +
  319. '<paragraph>BAR</paragraph>'
  320. );
  321. } );
  322. it( 'should not prevent default on forward navigation', () => {
  323. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  324. sinon.assert.notCalled( downArrowDomEvtDataStub.preventDefault );
  325. sinon.assert.notCalled( downArrowDomEvtDataStub.stopPropagation );
  326. } );
  327. it( 'should move caret to the position closest to object on backward navigation', () => {
  328. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  329. sinon.assert.calledOnce( upArrowDomEvtDataStub.preventDefault );
  330. sinon.assert.calledOnce( upArrowDomEvtDataStub.stopPropagation );
  331. assertEqualMarkup( getModelData( model ),
  332. '<paragraph>foo</paragraph>' +
  333. '<paragraph>bar</paragraph>' +
  334. '<horizontalLine></horizontalLine>' +
  335. '<paragraph>[]FOOO</paragraph>' +
  336. '<paragraph>BAR</paragraph>'
  337. );
  338. } );
  339. } );
  340. describe( 'caret in the forth paragraph', () => {
  341. beforeEach( () => {
  342. setModelData( model,
  343. '<paragraph>foo</paragraph>' +
  344. '<paragraph>bar</paragraph>' +
  345. '<horizontalLine></horizontalLine>' +
  346. '<paragraph>FOO</paragraph>' +
  347. '<paragraph>BA[]AR</paragraph>'
  348. );
  349. } );
  350. it( 'should not prevent default on backward navigation', () => {
  351. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  352. sinon.assert.notCalled( upArrowDomEvtDataStub.preventDefault );
  353. sinon.assert.notCalled( upArrowDomEvtDataStub.stopPropagation );
  354. } );
  355. it( 'should not prevent default on forward navigation', () => {
  356. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  357. sinon.assert.notCalled( downArrowDomEvtDataStub.preventDefault );
  358. sinon.assert.notCalled( downArrowDomEvtDataStub.stopPropagation );
  359. } );
  360. } );
  361. } );
  362. it( 'should integrate with the blockquote (forward navigation)', () => {
  363. setModelData( model,
  364. '<blockQuote>' +
  365. '<paragraph>f[]oo</paragraph>' +
  366. '</blockQuote>' +
  367. '<horizontalLine></horizontalLine>' +
  368. '<paragraph>bar</paragraph>'
  369. );
  370. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  371. sinon.assert.calledOnce( downArrowDomEvtDataStub.preventDefault );
  372. sinon.assert.calledOnce( downArrowDomEvtDataStub.stopPropagation );
  373. assertEqualMarkup( getModelData( model ),
  374. '<blockQuote>' +
  375. '<paragraph>foo[]</paragraph>' +
  376. '</blockQuote>' +
  377. '<horizontalLine></horizontalLine>' +
  378. '<paragraph>bar</paragraph>'
  379. );
  380. } );
  381. it( 'should integrate with the blockquote (backward navigation)', () => {
  382. setModelData( model,
  383. '<paragraph>foo</paragraph>' +
  384. '<horizontalLine></horizontalLine>' +
  385. '<blockQuote>' +
  386. '<paragraph>ba[]r</paragraph>' +
  387. '</blockQuote>'
  388. );
  389. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  390. sinon.assert.calledOnce( upArrowDomEvtDataStub.preventDefault );
  391. sinon.assert.calledOnce( upArrowDomEvtDataStub.stopPropagation );
  392. assertEqualMarkup( getModelData( model ),
  393. '<paragraph>foo</paragraph>' +
  394. '<horizontalLine></horizontalLine>' +
  395. '<blockQuote>' +
  396. '<paragraph>[]bar</paragraph>' +
  397. '</blockQuote>'
  398. );
  399. } );
  400. } );
  401. describe( 'with selection inside nested content editable', () => {
  402. let styleElement;
  403. beforeEach( () => {
  404. styleElement = global.document.createElement( 'style' );
  405. styleElement.type = 'text/css';
  406. styleElement.appendChild( global.document.createTextNode(
  407. `
  408. * {
  409. font-size: 12px !important;
  410. font-family: serif !important;
  411. margin: 0 !important;
  412. padding: 0 !important;
  413. border: 0 !important
  414. }
  415. td { width: 30px !important; }
  416. tr:nth-child(2) td:nth-child(2) { width: 300px !important; }
  417. `
  418. ) );
  419. global.document.querySelector( 'head' ).appendChild( styleElement );
  420. } );
  421. afterEach( () => {
  422. styleElement.remove();
  423. } );
  424. describe( 'simple cell text content', () => {
  425. describe( 'with collapsed selection', () => {
  426. beforeEach( () => {
  427. setModelData( model, modelTable( [
  428. [ '00', '01', '02' ],
  429. [ '10', 'foo[]bar', '12' ],
  430. [ '20', '21', '22' ]
  431. ] ) );
  432. } );
  433. it( 'should move caret to the beginning of the cell content', () => {
  434. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  435. sinon.assert.calledOnce( upArrowDomEvtDataStub.preventDefault );
  436. sinon.assert.calledOnce( upArrowDomEvtDataStub.stopPropagation );
  437. assertEqualMarkup( getModelData( model ), modelTable( [
  438. [ '00', '01', '02' ],
  439. [ '10', '[]foobar', '12' ],
  440. [ '20', '21', '22' ]
  441. ] ) );
  442. } );
  443. it( 'should move caret to the end of the cell content', () => {
  444. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  445. sinon.assert.calledOnce( downArrowDomEvtDataStub.preventDefault );
  446. sinon.assert.calledOnce( downArrowDomEvtDataStub.stopPropagation );
  447. assertEqualMarkup( getModelData( model ), modelTable( [
  448. [ '00', '01', '02' ],
  449. [ '10', 'foobar[]', '12' ],
  450. [ '20', '21', '22' ]
  451. ] ) );
  452. } );
  453. describe( 'when shift key is pressed', () => {
  454. beforeEach( () => {
  455. upArrowDomEvtDataStub.shiftKey = true;
  456. downArrowDomEvtDataStub.shiftKey = true;
  457. } );
  458. it( 'should expand selection to the beginning of the cell content', () => {
  459. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  460. sinon.assert.calledOnce( upArrowDomEvtDataStub.preventDefault );
  461. sinon.assert.calledOnce( upArrowDomEvtDataStub.stopPropagation );
  462. assertEqualMarkup( getModelData( model ), modelTable( [
  463. [ '00', '01', '02' ],
  464. [ '10', '[foo]bar', '12' ],
  465. [ '20', '21', '22' ]
  466. ] ) );
  467. } );
  468. it( 'should expand selection to the end of the cell content', () => {
  469. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  470. sinon.assert.calledOnce( downArrowDomEvtDataStub.preventDefault );
  471. sinon.assert.calledOnce( downArrowDomEvtDataStub.stopPropagation );
  472. assertEqualMarkup( getModelData( model ), modelTable( [
  473. [ '00', '01', '02' ],
  474. [ '10', 'foo[bar]', '12' ],
  475. [ '20', '21', '22' ]
  476. ] ) );
  477. } );
  478. } );
  479. } );
  480. describe( 'with non-collapsed forward selection', () => {
  481. beforeEach( () => {
  482. setModelData( model, modelTable( [
  483. [ '00', '01', '02' ],
  484. [ '10', 'fo[ob]ar', '12' ],
  485. [ '20', '21', '22' ]
  486. ] ) );
  487. } );
  488. it( 'should move caret to the beginning of the cell content', () => {
  489. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  490. sinon.assert.calledOnce( upArrowDomEvtDataStub.preventDefault );
  491. sinon.assert.calledOnce( upArrowDomEvtDataStub.stopPropagation );
  492. assertEqualMarkup( getModelData( model ), modelTable( [
  493. [ '00', '01', '02' ],
  494. [ '10', '[]foobar', '12' ],
  495. [ '20', '21', '22' ]
  496. ] ) );
  497. } );
  498. it( 'should move caret to the end of the cell content', () => {
  499. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  500. sinon.assert.calledOnce( downArrowDomEvtDataStub.preventDefault );
  501. sinon.assert.calledOnce( downArrowDomEvtDataStub.stopPropagation );
  502. assertEqualMarkup( getModelData( model ), modelTable( [
  503. [ '00', '01', '02' ],
  504. [ '10', 'foobar[]', '12' ],
  505. [ '20', '21', '22' ]
  506. ] ) );
  507. } );
  508. describe( 'when shift key is pressed', () => {
  509. beforeEach( () => {
  510. upArrowDomEvtDataStub.shiftKey = true;
  511. downArrowDomEvtDataStub.shiftKey = true;
  512. } );
  513. it( 'should not prevent default browser behavior on arrow up press', () => {
  514. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  515. sinon.assert.notCalled( upArrowDomEvtDataStub.preventDefault );
  516. sinon.assert.notCalled( upArrowDomEvtDataStub.stopPropagation );
  517. assertEqualMarkup( getModelData( model ), modelTable( [
  518. [ '00', '01', '02' ],
  519. [ '10', 'fo[ob]ar', '12' ],
  520. [ '20', '21', '22' ]
  521. ] ) );
  522. } );
  523. it( 'should expand selection to the end of the cell content', () => {
  524. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  525. sinon.assert.calledOnce( downArrowDomEvtDataStub.preventDefault );
  526. sinon.assert.calledOnce( downArrowDomEvtDataStub.stopPropagation );
  527. assertEqualMarkup( getModelData( model ), modelTable( [
  528. [ '00', '01', '02' ],
  529. [ '10', 'fo[obar]', '12' ],
  530. [ '20', '21', '22' ]
  531. ] ) );
  532. } );
  533. } );
  534. } );
  535. describe( 'with non-collapsed backward selection', () => {
  536. beforeEach( () => {
  537. setModelData( model, modelTable( [
  538. [ '00', '01', '02' ],
  539. [ '10', 'fo[ob]ar', '12' ],
  540. [ '20', '21', '22' ]
  541. ] ), { lastRangeBackward: true } );
  542. } );
  543. it( 'should move caret to the beginning of the cell content', () => {
  544. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  545. sinon.assert.calledOnce( upArrowDomEvtDataStub.preventDefault );
  546. sinon.assert.calledOnce( upArrowDomEvtDataStub.stopPropagation );
  547. assertEqualMarkup( getModelData( model ), modelTable( [
  548. [ '00', '01', '02' ],
  549. [ '10', '[]foobar', '12' ],
  550. [ '20', '21', '22' ]
  551. ] ) );
  552. } );
  553. it( 'should move caret to the end of the cell content', () => {
  554. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  555. sinon.assert.calledOnce( downArrowDomEvtDataStub.preventDefault );
  556. sinon.assert.calledOnce( downArrowDomEvtDataStub.stopPropagation );
  557. assertEqualMarkup( getModelData( model ), modelTable( [
  558. [ '00', '01', '02' ],
  559. [ '10', 'foobar[]', '12' ],
  560. [ '20', '21', '22' ]
  561. ] ) );
  562. } );
  563. describe( 'when shift key is pressed', () => {
  564. beforeEach( () => {
  565. upArrowDomEvtDataStub.shiftKey = true;
  566. downArrowDomEvtDataStub.shiftKey = true;
  567. } );
  568. it( 'should expand selection to the beginning of the cell content', () => {
  569. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  570. sinon.assert.calledOnce( upArrowDomEvtDataStub.preventDefault );
  571. sinon.assert.calledOnce( upArrowDomEvtDataStub.stopPropagation );
  572. assertEqualMarkup( getModelData( model ), modelTable( [
  573. [ '00', '01', '02' ],
  574. [ '10', '[foob]ar', '12' ],
  575. [ '20', '21', '22' ]
  576. ] ) );
  577. } );
  578. it( 'should not prevent default browser behavior on arrow down press', () => {
  579. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  580. sinon.assert.notCalled( downArrowDomEvtDataStub.preventDefault );
  581. sinon.assert.notCalled( downArrowDomEvtDataStub.stopPropagation );
  582. assertEqualMarkup( getModelData( model ), modelTable( [
  583. [ '00', '01', '02' ],
  584. [ '10', 'fo[ob]ar', '12' ],
  585. [ '20', '21', '22' ]
  586. ] ) );
  587. } );
  588. } );
  589. } );
  590. } );
  591. describe( 'selection inside paragraph', () => {
  592. const text = new Array( 20 ).fill( 0 ).map( () => 'word' ).join( ' ' );
  593. it( 'should not prevent default browser behavior if caret is in the middle line of a text', () => {
  594. setModelData( model, modelTable( [
  595. [ '00', '01', '02' ],
  596. [ '10', text + '[] ' + text, '12' ],
  597. [ '20', '21', '22' ]
  598. ] ) );
  599. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  600. sinon.assert.notCalled( upArrowDomEvtDataStub.preventDefault );
  601. sinon.assert.notCalled( upArrowDomEvtDataStub.stopPropagation );
  602. } );
  603. it( 'should move caret to beginning of cell content if caret is in the first line of a text', () => {
  604. setModelData( model, modelTable( [
  605. [ '00', '01', '02' ],
  606. [ '10', 'word[] word' + text, '12' ],
  607. [ '20', '21', '22' ]
  608. ] ) );
  609. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  610. sinon.assert.calledOnce( upArrowDomEvtDataStub.preventDefault );
  611. sinon.assert.calledOnce( upArrowDomEvtDataStub.stopPropagation );
  612. assertEqualMarkup( getModelData( model ), modelTable( [
  613. [ '00', '01', '02' ],
  614. [ '10', '[]word word' + text, '12' ],
  615. [ '20', '21', '22' ]
  616. ] ) );
  617. } );
  618. it( 'should move caret to end of cell content if caret is in the last line of a text', () => {
  619. setModelData( model, modelTable( [
  620. [ '00', '01', '02' ],
  621. [ '10', text + 'word[] word', '12' ],
  622. [ '20', '21', '22' ]
  623. ] ) );
  624. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  625. sinon.assert.calledOnce( downArrowDomEvtDataStub.preventDefault );
  626. sinon.assert.calledOnce( downArrowDomEvtDataStub.stopPropagation );
  627. assertEqualMarkup( getModelData( model ), modelTable( [
  628. [ '00', '01', '02' ],
  629. [ '10', text + 'word word[]', '12' ],
  630. [ '20', '21', '22' ]
  631. ] ) );
  632. } );
  633. describe( 'when shift key is pressed', () => {
  634. beforeEach( () => {
  635. upArrowDomEvtDataStub.shiftKey = true;
  636. downArrowDomEvtDataStub.shiftKey = true;
  637. } );
  638. it( 'should not prevent default browser behavior for the up arrow in the middle lines of the cell text', () => {
  639. setModelData( model, modelTable( [
  640. [ '00', '01', '02' ],
  641. [ '10', text + '[] ' + text, '12' ],
  642. [ '20', '21', '22' ]
  643. ] ) );
  644. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  645. sinon.assert.notCalled( upArrowDomEvtDataStub.preventDefault );
  646. sinon.assert.notCalled( upArrowDomEvtDataStub.stopPropagation );
  647. } );
  648. it( 'should not prevent default browser behavior for the down arrow in the middle lines of cell text', () => {
  649. setModelData( model, modelTable( [
  650. [ '00', '01', '02' ],
  651. [ '10', text + '[] ' + text, '12' ],
  652. [ '20', '21', '22' ]
  653. ] ) );
  654. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  655. sinon.assert.notCalled( downArrowDomEvtDataStub.preventDefault );
  656. sinon.assert.notCalled( downArrowDomEvtDataStub.stopPropagation );
  657. } );
  658. it( 'should expand collapsed selection to the beginning of the cell content', () => {
  659. setModelData( model, modelTable( [
  660. [ '00', '01', '02' ],
  661. [ '10', 'word[] word' + text, '12' ],
  662. [ '20', '21', '22' ]
  663. ] ) );
  664. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  665. sinon.assert.calledOnce( upArrowDomEvtDataStub.preventDefault );
  666. sinon.assert.calledOnce( upArrowDomEvtDataStub.stopPropagation );
  667. assertEqualMarkup( getModelData( model ), modelTable( [
  668. [ '00', '01', '02' ],
  669. [ '10', '[word] word' + text, '12' ],
  670. [ '20', '21', '22' ]
  671. ] ) );
  672. } );
  673. it( 'should not prevent default browser behavior for shrinking selection (up arrow)', () => {
  674. setModelData( model, modelTable( [
  675. [ '00', '01', '02' ],
  676. [ '10', 'word [word]' + text, '12' ],
  677. [ '20', '21', '22' ]
  678. ] ) );
  679. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  680. sinon.assert.notCalled( upArrowDomEvtDataStub.preventDefault );
  681. sinon.assert.notCalled( upArrowDomEvtDataStub.stopPropagation );
  682. } );
  683. it( 'should expand not collapsed selection to the beginning of the cell content from the selection anchor', () => {
  684. setModelData( model, modelTable( [
  685. [ '00', '01', '02' ],
  686. [ '10', 'word [word]' + text, '12' ],
  687. [ '20', '21', '22' ]
  688. ] ), { lastRangeBackward: true } );
  689. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  690. sinon.assert.calledOnce( upArrowDomEvtDataStub.preventDefault );
  691. sinon.assert.calledOnce( upArrowDomEvtDataStub.stopPropagation );
  692. assertEqualMarkup( getModelData( model ), modelTable( [
  693. [ '00', '01', '02' ],
  694. [ '10', '[word word]' + text, '12' ],
  695. [ '20', '21', '22' ]
  696. ] ) );
  697. } );
  698. it( 'should expand collapsed selection to the end of the cell content', () => {
  699. setModelData( model, modelTable( [
  700. [ '00', '01', '02' ],
  701. [ '10', text + 'word[] word', '12' ],
  702. [ '20', '21', '22' ]
  703. ] ) );
  704. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  705. sinon.assert.calledOnce( downArrowDomEvtDataStub.preventDefault );
  706. sinon.assert.calledOnce( downArrowDomEvtDataStub.stopPropagation );
  707. assertEqualMarkup( getModelData( model ), modelTable( [
  708. [ '00', '01', '02' ],
  709. [ '10', text + 'word[ word]', '12' ],
  710. [ '20', '21', '22' ]
  711. ] ) );
  712. } );
  713. it( 'should not prevent default browser behavior for shrinking selection (down arrow)', () => {
  714. setModelData( model, modelTable( [
  715. [ '00', '01', '02' ],
  716. [ '10', text + '[word] word', '12' ],
  717. [ '20', '21', '22' ]
  718. ] ), { lastRangeBackward: true } );
  719. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  720. sinon.assert.notCalled( downArrowDomEvtDataStub.preventDefault );
  721. sinon.assert.notCalled( downArrowDomEvtDataStub.stopPropagation );
  722. } );
  723. it( 'should expand not collapsed selection to the end of the cell content from the selection anchor', () => {
  724. setModelData( model, modelTable( [
  725. [ '00', '01', '02' ],
  726. [ '10', text + '[word] word', '12' ],
  727. [ '20', '21', '22' ]
  728. ] ) );
  729. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  730. sinon.assert.calledOnce( downArrowDomEvtDataStub.preventDefault );
  731. sinon.assert.calledOnce( downArrowDomEvtDataStub.stopPropagation );
  732. assertEqualMarkup( getModelData( model ), modelTable( [
  733. [ '00', '01', '02' ],
  734. [ '10', text + '[word word]', '12' ],
  735. [ '20', '21', '22' ]
  736. ] ) );
  737. } );
  738. } );
  739. } );
  740. if ( !env.isGecko ) {
  741. // These tests fails on Travis. They work correctly when started on local machine.
  742. // Issue is probably related to text rendering and wrapping.
  743. describe( 'with selection in the wrap area', () => {
  744. const text = new Array( 10 ).fill( 0 ).map( () => 'word' ).join( ' ' );
  745. it( 'should move the caret to end if the caret is after the last space in the line next to the last one', () => {
  746. // This is also first position in the last line.
  747. setModelData( model, modelTable( [
  748. [ '00', '01', '02' ],
  749. [ '10', text + ' []word word word', '12' ],
  750. [ '20', '21', '22' ]
  751. ] ) );
  752. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  753. sinon.assert.calledOnce( downArrowDomEvtDataStub.preventDefault );
  754. sinon.assert.calledOnce( downArrowDomEvtDataStub.stopPropagation );
  755. assertEqualMarkup( getModelData( model ), modelTable( [
  756. [ '00', '01', '02' ],
  757. [ '10', text + ' word word word[]', '12' ],
  758. [ '20', '21', '22' ]
  759. ] ) );
  760. } );
  761. it( 'should move the caret to end if the caret is at the last space in the line next to last one', () => {
  762. setModelData( model, modelTable( [
  763. [ '00', '01', '02' ],
  764. [ '10', text + '[] word word word', '12' ],
  765. [ '20', '21', '22' ]
  766. ] ) );
  767. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  768. sinon.assert.calledOnce( downArrowDomEvtDataStub.preventDefault );
  769. sinon.assert.calledOnce( downArrowDomEvtDataStub.stopPropagation );
  770. assertEqualMarkup( getModelData( model ), modelTable( [
  771. [ '00', '01', '02' ],
  772. [ '10', text + ' word word word[]', '12' ],
  773. [ '20', '21', '22' ]
  774. ] ) );
  775. } );
  776. it( 'should not move the caret if it\'s just before the last space in the line next to last one', () => {
  777. setModelData( model, modelTable( [
  778. [ '00', '01', '02' ],
  779. [ '10', text.substring( 0, text.length - 1 ) + '[]d word word word', '12' ],
  780. [ '20', '21', '22' ]
  781. ] ) );
  782. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  783. sinon.assert.notCalled( downArrowDomEvtDataStub.preventDefault );
  784. sinon.assert.notCalled( downArrowDomEvtDataStub.stopPropagation );
  785. } );
  786. describe( 'when shift key is pressed', () => {
  787. beforeEach( () => {
  788. upArrowDomEvtDataStub.shiftKey = true;
  789. downArrowDomEvtDataStub.shiftKey = true;
  790. } );
  791. it( 'should expand collapsed selection to the end of the cell content', () => {
  792. setModelData( model, modelTable( [
  793. [ '00', '01', '02' ],
  794. [ '10', text + '[] word word word', '12' ],
  795. [ '20', '21', '22' ]
  796. ] ) );
  797. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  798. sinon.assert.calledOnce( downArrowDomEvtDataStub.preventDefault );
  799. sinon.assert.calledOnce( downArrowDomEvtDataStub.stopPropagation );
  800. assertEqualMarkup( getModelData( model ), modelTable( [
  801. [ '00', '01', '02' ],
  802. [ '10', text + '[ word word word]', '12' ],
  803. [ '20', '21', '22' ]
  804. ] ) );
  805. } );
  806. it( 'should expand not collapsed selection to the end of the cell content from the selection anchor', () => {
  807. setModelData( model, modelTable( [
  808. [ '00', '01', '02' ],
  809. [ '10', text + '[ word] word word', '12' ],
  810. [ '20', '21', '22' ]
  811. ] ) );
  812. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  813. sinon.assert.calledOnce( downArrowDomEvtDataStub.preventDefault );
  814. sinon.assert.calledOnce( downArrowDomEvtDataStub.stopPropagation );
  815. assertEqualMarkup( getModelData( model ), modelTable( [
  816. [ '00', '01', '02' ],
  817. [ '10', text + '[ word word word]', '12' ],
  818. [ '20', '21', '22' ]
  819. ] ) );
  820. } );
  821. } );
  822. } );
  823. }
  824. describe( 'with multiple paragraphs of text', () => {
  825. const text = new Array( 100 ).fill( 0 ).map( () => 'word' ).join( ' ' );
  826. it( 'should not prevent default browser behavior if caret is in the middle of a line of text', () => {
  827. setModelData( model, modelTable( [
  828. [ '00', '01', '02' ],
  829. [ '10', `<paragraph>${ text }[]${ text }</paragraph><paragraph>foobar</paragraph>`, '12' ],
  830. [ '20', '21', '22' ]
  831. ] ) );
  832. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  833. sinon.assert.notCalled( upArrowDomEvtDataStub.preventDefault );
  834. sinon.assert.notCalled( upArrowDomEvtDataStub.stopPropagation );
  835. } );
  836. it( 'should move the caret to the beginning of a cell content if the caret is in the first line of text', () => {
  837. setModelData( model, modelTable( [
  838. [ '00', '01', '02' ],
  839. [ '10', `<paragraph>word[]${ text }</paragraph><paragraph>foobar</paragraph>`, '12' ],
  840. [ '20', '21', '22' ]
  841. ] ) );
  842. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  843. sinon.assert.calledOnce( upArrowDomEvtDataStub.preventDefault );
  844. sinon.assert.calledOnce( upArrowDomEvtDataStub.stopPropagation );
  845. assertEqualMarkup( getModelData( model ), modelTable( [
  846. [ '00', '01', '02' ],
  847. [ '10', `<paragraph>[]word${ text }</paragraph><paragraph>foobar</paragraph>`, '12' ],
  848. [ '20', '21', '22' ]
  849. ] ) );
  850. } );
  851. it( 'should not move the caret to the end of a cell content if the caret is not in the last line of text', () => {
  852. setModelData( model, modelTable( [
  853. [ '00', '01', '02' ],
  854. [ '10', `<paragraph>${ text }word []word</paragraph><paragraph>foobar</paragraph>`, '12' ],
  855. [ '20', '21', '22' ]
  856. ] ) );
  857. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  858. sinon.assert.notCalled( downArrowDomEvtDataStub.preventDefault );
  859. sinon.assert.notCalled( downArrowDomEvtDataStub.stopPropagation );
  860. } );
  861. it( 'should move the caret to end of a cell content if the caret is in the last line of text', () => {
  862. setModelData( model, modelTable( [
  863. [ '00', '01', '02' ],
  864. [ '10', `<paragraph>foobar</paragraph><paragraph>${ text }word []word</paragraph>`, '12' ],
  865. [ '20', '21', '22' ]
  866. ] ) );
  867. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  868. sinon.assert.calledOnce( downArrowDomEvtDataStub.preventDefault );
  869. sinon.assert.calledOnce( downArrowDomEvtDataStub.stopPropagation );
  870. assertEqualMarkup( getModelData( model ), modelTable( [
  871. [ '00', '01', '02' ],
  872. [ '10', `<paragraph>foobar</paragraph><paragraph>${ text }word word[]</paragraph>`, '12' ],
  873. [ '20', '21', '22' ]
  874. ] ) );
  875. } );
  876. describe( 'when shift key is pressed', () => {
  877. beforeEach( () => {
  878. upArrowDomEvtDataStub.shiftKey = true;
  879. downArrowDomEvtDataStub.shiftKey = true;
  880. } );
  881. it( 'should expand selection to the beginning of the cell content', () => {
  882. setModelData( model, modelTable( [
  883. [ '00', '01', '02' ],
  884. [ '10', `<paragraph>word[] ${ text }</paragraph><paragraph>${ text }</paragraph>`, '12' ],
  885. [ '20', '21', '22' ]
  886. ] ) );
  887. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  888. sinon.assert.calledOnce( upArrowDomEvtDataStub.preventDefault );
  889. sinon.assert.calledOnce( upArrowDomEvtDataStub.stopPropagation );
  890. assertEqualMarkup( getModelData( model ), modelTable( [
  891. [ '00', '01', '02' ],
  892. [ '10', `<paragraph>[word] ${ text }</paragraph><paragraph>${ text }</paragraph>`, '12' ],
  893. [ '20', '21', '22' ]
  894. ] ) );
  895. } );
  896. it( 'should expand selection to the end of the cell content', () => {
  897. setModelData( model, modelTable( [
  898. [ '00', '01', '02' ],
  899. [ '10', `<paragraph>${ text }</paragraph><paragraph>${ text } []word</paragraph>`, '12' ],
  900. [ '20', '21', '22' ]
  901. ] ) );
  902. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  903. sinon.assert.calledOnce( downArrowDomEvtDataStub.preventDefault );
  904. sinon.assert.calledOnce( downArrowDomEvtDataStub.stopPropagation );
  905. assertEqualMarkup( getModelData( model ), modelTable( [
  906. [ '00', '01', '02' ],
  907. [ '10', `<paragraph>${ text }</paragraph><paragraph>${ text } [word]</paragraph>`, '12' ],
  908. [ '20', '21', '22' ]
  909. ] ) );
  910. } );
  911. } );
  912. } );
  913. describe( 'with horizontal line widget', () => {
  914. const text = new Array( 100 ).fill( 0 ).map( () => 'word' ).join( ' ' );
  915. it( 'should not navigate if the caret is in the middle line of text', () => {
  916. setModelData( model, modelTable( [
  917. [ '00', '01', '02' ],
  918. [ '10', `<horizontalLine></horizontalLine><paragraph>${ text }[]${ text }</paragraph>`, '12' ],
  919. [ '20', '21', '22' ]
  920. ] ) );
  921. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  922. sinon.assert.notCalled( upArrowDomEvtDataStub.preventDefault );
  923. sinon.assert.notCalled( upArrowDomEvtDataStub.stopPropagation );
  924. } );
  925. it( 'should move the caret to the beginning of cell non-object content if the caret is in the first line of text', () => {
  926. setModelData( model, modelTable( [
  927. [ '00', '01', '02' ],
  928. [ '10', `<horizontalLine></horizontalLine><paragraph>word[] ${ text }</paragraph>`, '12' ],
  929. [ '20', '21', '22' ]
  930. ] ) );
  931. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  932. sinon.assert.calledOnce( upArrowDomEvtDataStub.preventDefault );
  933. sinon.assert.calledOnce( upArrowDomEvtDataStub.stopPropagation );
  934. assertEqualMarkup( getModelData( model ), modelTable( [
  935. [ '00', '01', '02' ],
  936. [ '10', `<horizontalLine></horizontalLine><paragraph>[]word ${ text }</paragraph>`, '12' ],
  937. [ '20', '21', '22' ]
  938. ] ) );
  939. } );
  940. it( 'should move the caret to the end of cell non-object content if the caret is in the last line of text', () => {
  941. setModelData( model, modelTable( [
  942. [ '00', '01', '02' ],
  943. [ '10', `<paragraph>${ text } word []word</paragraph><horizontalLine></horizontalLine>`, '12' ],
  944. [ '20', '21', '22' ]
  945. ] ) );
  946. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  947. sinon.assert.calledOnce( downArrowDomEvtDataStub.preventDefault );
  948. sinon.assert.calledOnce( downArrowDomEvtDataStub.stopPropagation );
  949. assertEqualMarkup( getModelData( model ), modelTable( [
  950. [ '00', '01', '02' ],
  951. [ '10', `<paragraph>${ text } word word[]</paragraph><horizontalLine></horizontalLine>`, '12' ],
  952. [ '20', '21', '22' ]
  953. ] ) );
  954. } );
  955. it( 'should not move the caret to the end of cell content if widget is selected in middle of a cell content', () => {
  956. const paragraph = `<paragraph>${ text }</paragraph>`;
  957. const hr = '<horizontalLine></horizontalLine>';
  958. setModelData( model, modelTable( [
  959. [ '00', '01', '02' ],
  960. [ '10', `${ paragraph }[${ hr }]${ paragraph }${ hr }`, '12' ],
  961. [ '20', '21', '22' ]
  962. ] ) );
  963. // Note: Two keydowns are necessary because the first one is handled by the WidgetTypeAround plugin
  964. // to activate the "fake caret".
  965. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  966. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  967. assertEqualMarkup( getModelData( model ), modelTable( [
  968. [ '00', '01', '02' ],
  969. [ '10', `${ paragraph }${ hr }<paragraph>[]${ text }</paragraph>${ hr }`, '12' ],
  970. [ '20', '21', '22' ]
  971. ] ) );
  972. } );
  973. it( 'should not move the caret to the end of cell content if widget is next to the selection', () => {
  974. const paragraph = `<paragraph>${ text }</paragraph>`;
  975. const hr = '<horizontalLine></horizontalLine>';
  976. setModelData( model, modelTable( [
  977. [ '00', '01', '02' ],
  978. [ '10', `${ paragraph }[]${ hr }`, '12' ],
  979. [ '20', '21', '22' ]
  980. ] ) );
  981. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  982. assertEqualMarkup( getModelData( model ), modelTable( [
  983. [ '00', '01', '02' ],
  984. [ '10', `${ paragraph }[${ hr }]`, '12' ],
  985. [ '20', '21', '22' ]
  986. ] ) );
  987. } );
  988. describe( 'when shift key is pressed', () => {
  989. beforeEach( () => {
  990. upArrowDomEvtDataStub.shiftKey = true;
  991. downArrowDomEvtDataStub.shiftKey = true;
  992. } );
  993. it( 'should expand selection to the beginning of the cell content', () => {
  994. setModelData( model, modelTable( [
  995. [ '00', '01', '02' ],
  996. [ '10', '<horizontalLine></horizontalLine><paragraph>foo[]bar</paragraph>', '12' ],
  997. [ '20', '21', '22' ]
  998. ] ) );
  999. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  1000. sinon.assert.calledOnce( upArrowDomEvtDataStub.preventDefault );
  1001. sinon.assert.calledOnce( upArrowDomEvtDataStub.stopPropagation );
  1002. assertEqualMarkup( getModelData( model ), modelTable( [
  1003. [ '00', '01', '02' ],
  1004. [ '10', '<horizontalLine></horizontalLine><paragraph>[foo]bar</paragraph>', '12' ],
  1005. [ '20', '21', '22' ]
  1006. ] ) );
  1007. } );
  1008. it( 'should expand selection to the end of the cell content', () => {
  1009. setModelData( model, modelTable( [
  1010. [ '00', '01', '02' ],
  1011. [ '10', '<paragraph>foo[]bar</paragraph><horizontalLine></horizontalLine>', '12' ],
  1012. [ '20', '21', '22' ]
  1013. ] ) );
  1014. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  1015. sinon.assert.calledOnce( downArrowDomEvtDataStub.preventDefault );
  1016. sinon.assert.calledOnce( downArrowDomEvtDataStub.stopPropagation );
  1017. assertEqualMarkup( getModelData( model ), modelTable( [
  1018. [ '00', '01', '02' ],
  1019. [ '10', '<paragraph>foo[bar]</paragraph><horizontalLine></horizontalLine>', '12' ],
  1020. [ '20', '21', '22' ]
  1021. ] ) );
  1022. } );
  1023. } );
  1024. } );
  1025. describe( 'contains image widget with caption and selection inside the caption', () => {
  1026. it( 'should move caret to the closest limit boundary', () => {
  1027. setModelData( model, modelTable( [
  1028. [ '00', '01', '02' ],
  1029. [ '10', `<paragraph>foo</paragraph><image src="${ imageUrl }"><caption>bar[]baz</caption></image>`, '12' ],
  1030. [ '20', '21', '22' ]
  1031. ] ) );
  1032. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  1033. sinon.assert.calledOnce( upArrowDomEvtDataStub.preventDefault );
  1034. sinon.assert.calledOnce( upArrowDomEvtDataStub.stopPropagation );
  1035. assertEqualMarkup( getModelData( model ), modelTable( [
  1036. [ '00', '01', '02' ],
  1037. [ '10', `<paragraph>foo</paragraph><image src="${ imageUrl }"><caption>[]barbaz</caption></image>`, '12' ],
  1038. [ '20', '21', '22' ]
  1039. ] ) );
  1040. } );
  1041. it( 'should not prevent default browser behavior when caret at the beginning of nested editable', () => {
  1042. setModelData( model, modelTable( [
  1043. [ '00', '01', '02' ],
  1044. [ '10', `<paragraph>foo</paragraph><image src="${ imageUrl }"><caption>[]barbaz</caption></image>`, '12' ],
  1045. [ '20', '21', '22' ]
  1046. ] ) );
  1047. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  1048. sinon.assert.notCalled( upArrowDomEvtDataStub.preventDefault );
  1049. sinon.assert.notCalled( upArrowDomEvtDataStub.stopPropagation );
  1050. } );
  1051. it( 'should move the caret to the first position of the image caption', () => {
  1052. setModelData( model, modelTable( [
  1053. [ '00', '01', '02' ],
  1054. [ '10', `<image src="${ imageUrl }"><caption>bar[]baz</caption></image><paragraph>foo</paragraph>`, '12' ],
  1055. [ '20', '21', '22' ]
  1056. ] ) );
  1057. editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
  1058. sinon.assert.calledOnce( upArrowDomEvtDataStub.preventDefault );
  1059. sinon.assert.calledOnce( upArrowDomEvtDataStub.stopPropagation );
  1060. assertEqualMarkup( getModelData( model ), modelTable( [
  1061. [ '00', '01', '02' ],
  1062. [ '10', `<image src="${ imageUrl }"><caption>[]barbaz</caption></image><paragraph>foo</paragraph>`, '12' ],
  1063. [ '20', '21', '22' ]
  1064. ] ) );
  1065. } );
  1066. it( 'should move caret to the end of image caption', () => {
  1067. setModelData( model, modelTable( [
  1068. [ '00', '01', '02' ],
  1069. [ '10', `<image src="${ imageUrl }"><caption>bar[]baz</caption></image><paragraph>foo</paragraph>`, '12' ],
  1070. [ '20', '21', '22' ]
  1071. ] ) );
  1072. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  1073. sinon.assert.calledOnce( downArrowDomEvtDataStub.preventDefault );
  1074. sinon.assert.calledOnce( downArrowDomEvtDataStub.stopPropagation );
  1075. assertEqualMarkup( getModelData( model ), modelTable( [
  1076. [ '00', '01', '02' ],
  1077. [ '10', `<image src="${ imageUrl }"><caption>barbaz[]</caption></image><paragraph>foo</paragraph>`, '12' ],
  1078. [ '20', '21', '22' ]
  1079. ] ) );
  1080. } );
  1081. it( 'should move caret to the end of image caption when caret is on the position next to the last one', () => {
  1082. setModelData( model, modelTable( [
  1083. [ '00', '01', '02' ],
  1084. [ '10', `<image src="${ imageUrl }"><caption>barba[]z</caption></image><paragraph>foo</paragraph>`, '12' ],
  1085. [ '20', '21', '22' ]
  1086. ] ) );
  1087. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  1088. sinon.assert.calledOnce( downArrowDomEvtDataStub.preventDefault );
  1089. sinon.assert.calledOnce( downArrowDomEvtDataStub.stopPropagation );
  1090. assertEqualMarkup( getModelData( model ), modelTable( [
  1091. [ '00', '01', '02' ],
  1092. [ '10', `<image src="${ imageUrl }"><caption>barbaz[]</caption></image><paragraph>foo</paragraph>`, '12' ],
  1093. [ '20', '21', '22' ]
  1094. ] ) );
  1095. } );
  1096. it( 'should not prevent default browser behavior when caret inside image caption when followed by a paragraph', () => {
  1097. setModelData( model, modelTable( [
  1098. [ '00', '01', '02' ],
  1099. [ '10', `<image src="${ imageUrl }"><caption>barbaz[]</caption></image><paragraph>foo</paragraph>`, '12' ],
  1100. [ '20', '21', '22' ]
  1101. ] ) );
  1102. editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
  1103. sinon.assert.notCalled( downArrowDomEvtDataStub.preventDefault );
  1104. sinon.assert.notCalled( downArrowDomEvtDataStub.stopPropagation );
  1105. } );
  1106. } );
  1107. } );
  1108. } );