title.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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. /* global document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import Title from '../src/title';
  8. import Heading from '../src/heading';
  9. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import BlockQuote from '@ckeditor/ckeditor5-block-quote/src/blockquote';
  12. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  13. import Image from '@ckeditor/ckeditor5-image/src/image';
  14. import ImageUpload from '@ckeditor/ckeditor5-image/src/imageupload';
  15. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  16. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  17. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  18. describe( 'Title', () => {
  19. let element, editor, model;
  20. beforeEach( () => {
  21. element = document.createElement( 'div' );
  22. document.body.appendChild( element );
  23. return ClassicTestEditor.create( element, {
  24. plugins: [ Title, Heading, BlockQuote, Clipboard, Image, ImageUpload, Enter, Undo ]
  25. } ).then( _editor => {
  26. editor = _editor;
  27. model = editor.model;
  28. } );
  29. } );
  30. afterEach( () => {
  31. return editor.destroy().then( () => element.remove() );
  32. } );
  33. it( 'should requires Paragraph plugin', () => {
  34. expect( Title.requires ).to.have.members( [ Paragraph ] );
  35. } );
  36. it( 'should have plugin name property', () => {
  37. expect( Title.pluginName ).to.equal( 'Title' );
  38. } );
  39. it( 'should set proper schema rules', () => {
  40. expect( model.schema.isRegistered( 'title' ) ).to.equal( true );
  41. expect( model.schema.isBlock( 'title' ) ).to.equal( true );
  42. expect( model.schema.isRegistered( 'title-content' ) ).to.equal( true );
  43. expect( model.schema.isBlock( 'title-content' ) ).to.equal( true );
  44. expect( model.schema.checkChild( 'title', '$text' ) ).to.equal( false );
  45. expect( model.schema.checkChild( 'title', '$block' ) ).to.equal( false );
  46. expect( model.schema.checkChild( 'title', 'title-content' ) ).to.equal( true );
  47. expect( model.schema.checkChild( '$root', 'title-content' ) ).to.equal( false );
  48. expect( model.schema.checkChild( '$root', 'title' ) ).to.equal( true );
  49. expect( model.schema.checkChild( '$block', 'title-content' ) ).to.equal( false );
  50. expect( model.schema.checkChild( 'title-content', '$text' ) ).to.equal( true );
  51. expect( model.schema.checkChild( 'title-content', '$block' ) ).to.equal( false );
  52. model.schema.extend( '$text', { allowAttributes: [ 'bold', 'foo' ] } );
  53. expect( model.schema.checkAttribute( [ 'title-content', '$text' ], 'bold' ) ).to.equal( false );
  54. expect( model.schema.checkAttribute( [ 'paragraph', '$text' ], 'bold' ) ).to.equal( true );
  55. expect( model.schema.checkAttribute( [ 'title-content', '$text' ], 'foo' ) ).to.equal( false );
  56. expect( model.schema.checkAttribute( [ 'paragraph', '$text' ], 'foo' ) ).to.equal( true );
  57. } );
  58. it( 'should convert title to h1', () => {
  59. setData( model,
  60. '<title><title-content>Foo</title-content></title>' +
  61. '<paragraph>Bar</paragraph>'
  62. );
  63. expect( editor.getData() ).to.equal( '<h1>Foo</h1><p>Bar</p>' );
  64. } );
  65. describe( 'model post-fixing', () => {
  66. it( 'should set title and content elements', () => {
  67. setData( model,
  68. '<title><title-content>Foo</title-content></title>' +
  69. '<paragraph>Bar</paragraph>'
  70. );
  71. expect( getData( model ) ).to.equal(
  72. '<title><title-content>[]Foo</title-content></title>' +
  73. '<paragraph>Bar</paragraph>'
  74. );
  75. } );
  76. it( 'should create a content element when only title has been set', () => {
  77. setData( model, '<title><title-content>Foo</title-content></title>' );
  78. expect( getData( model ) ).to.equal(
  79. '<title><title-content>[]Foo</title-content></title>' +
  80. '<paragraph></paragraph>'
  81. );
  82. } );
  83. it( 'should create a title and content elements when are missing', () => {
  84. setData( model, '' );
  85. expect( getData( model ) ).to.equal(
  86. '<title><title-content>[]</title-content></title>' +
  87. '<paragraph></paragraph>'
  88. );
  89. } );
  90. it( 'should change heading1 element to title when is set as a first root child', () => {
  91. setData( model,
  92. '<heading1>Foo</heading1>' +
  93. '<heading1>Bar</heading1>'
  94. );
  95. expect( getData( model ) ).to.equal(
  96. '<title><title-content>[]Foo</title-content></title>' +
  97. '<heading1>Bar</heading1>'
  98. );
  99. } );
  100. it( 'should change paragraph element to title when is set as a first root child', () => {
  101. setData( model,
  102. '<paragraph>Foo</paragraph>' +
  103. '<paragraph>Bar</paragraph>'
  104. );
  105. expect( getData( model ) ).to.equal(
  106. '<title><title-content>[]Foo</title-content></title>' +
  107. '<paragraph>Bar</paragraph>'
  108. );
  109. } );
  110. it( 'should have list of title-like elements', () => {
  111. expect( Array.from( Title.titleLikeElements ) ).to.have.members( [
  112. 'paragraph', 'heading1', 'heading2', 'heading3', 'heading4', 'heading5', 'heading6'
  113. ] );
  114. } );
  115. it( 'should change paragraph element to title when is set as a first root child', () => {
  116. setData( model,
  117. '<paragraph>Foo</paragraph>' +
  118. '<paragraph>Bar</paragraph>'
  119. );
  120. expect( getData( model ) ).to.equal(
  121. '<title><title-content>[]Foo</title-content></title>' +
  122. '<paragraph>Bar</paragraph>'
  123. );
  124. } );
  125. it( 'should change title element to a paragraph when is not a first root child #1', () => {
  126. setData( model,
  127. '<title><title-content>Foo</title-content></title>' +
  128. '<title><title-content>Bar</title-content></title>'
  129. );
  130. expect( getData( model ) ).to.equal(
  131. '<title><title-content>[]Foo</title-content></title>' +
  132. '<paragraph>Bar</paragraph>'
  133. );
  134. } );
  135. it( 'should change title element to a paragraph when is not a first root child #2', () => {
  136. setData( model,
  137. '<title><title-content>Foo</title-content></title>' +
  138. '<paragraph>Bar</paragraph>' +
  139. '<title><title-content>Biz</title-content></title>'
  140. );
  141. expect( getData( model ) ).to.equal(
  142. '<title><title-content>[]Foo</title-content></title>' +
  143. '<paragraph>Bar</paragraph>' +
  144. '<paragraph>Biz</paragraph>'
  145. );
  146. } );
  147. it( 'should move element after a title element when is not allowed to be a title', () => {
  148. setData( model,
  149. '<blockQuote><paragraph>Foo</paragraph></blockQuote>' +
  150. '<title><title-content>Bar</title-content></title>'
  151. );
  152. expect( getData( model ) ).to.equal(
  153. '<title><title-content>[]Bar</title-content></title>' +
  154. '<blockQuote><paragraph>Foo</paragraph></blockQuote>'
  155. );
  156. } );
  157. it( 'should create a missing title element before an element that cannot to be a title element', () => {
  158. setData( model, '<blockQuote><paragraph>Foo</paragraph></blockQuote>' );
  159. expect( getData( model ) ).to.equal(
  160. '<title><title-content>[]</title-content></title>' +
  161. '<blockQuote><paragraph>Foo</paragraph></blockQuote>'
  162. );
  163. } );
  164. it( 'should clear element from attributes when changing to title element', () => {
  165. model.schema.extend( '$text', { allowAttributes: 'bold' } );
  166. setData( model,
  167. '<paragraph>F<$text bold="true">o</$text>o</paragraph>' +
  168. '<paragraph>B<$text bold="true">a</$text>r</paragraph>'
  169. );
  170. expect( getData( model ) ).to.equal(
  171. '<title><title-content>[]Foo</title-content></title>' +
  172. '<paragraph>B<$text bold="true">a</$text>r</paragraph>'
  173. );
  174. } );
  175. } );
  176. describe( 'prevent extra paragraphing', () => {
  177. it( 'should remove the extra paragraph when pasting to the editor with body placeholder', () => {
  178. setData( model, '<title><title-content>[]</title-content></title>' );
  179. const dataTransferMock = {
  180. getData: type => {
  181. if ( type === 'text/html' ) {
  182. return '<h1>Title</h1><p>Body</p>';
  183. }
  184. },
  185. types: [],
  186. files: []
  187. };
  188. editor.editing.view.document.fire( 'paste', {
  189. dataTransfer: dataTransferMock,
  190. preventDefault() {}
  191. } );
  192. expect( getData( model ) ).to.equal(
  193. '<title><title-content>Title</title-content></title>' +
  194. '<paragraph>Body[]</paragraph>'
  195. );
  196. } );
  197. it( 'should not remove the extra paragraph when pasting to the editor with directly created body element', () => {
  198. setData( model,
  199. '<title><title-content>[]</title-content></title>' +
  200. '<paragraph></paragraph>'
  201. );
  202. const dataTransferMock = {
  203. getData: type => {
  204. if ( type === 'text/html' ) {
  205. return '<h1>Title</h1><p>Body</p>';
  206. }
  207. },
  208. types: [],
  209. files: []
  210. };
  211. editor.editing.view.document.fire( 'paste', {
  212. dataTransfer: dataTransferMock,
  213. preventDefault() {}
  214. } );
  215. expect( getData( model ) ).to.equal(
  216. '<title><title-content>Title</title-content></title>' +
  217. '<paragraph>Body[]</paragraph>' +
  218. '<paragraph></paragraph>'
  219. );
  220. } );
  221. it( 'should remove the extra paragraph when pressing enter in the title', () => {
  222. setData( model, '<title><title-content>fo[]o</title-content></title>' );
  223. editor.execute( 'enter' );
  224. expect( getData( model ) ).to.equal(
  225. '<title><title-content>fo</title-content></title>' +
  226. '<paragraph>[]o</paragraph>'
  227. );
  228. } );
  229. it( 'should not remove the extra paragraph when pressing enter in the title when body is created directly', () => {
  230. setData( model,
  231. '<title><title-content>fo[]o</title-content></title>' +
  232. '<paragraph></paragraph>'
  233. );
  234. editor.execute( 'enter' );
  235. expect( getData( model ) ).to.equal(
  236. '<title><title-content>fo</title-content></title>' +
  237. '<paragraph>[]o</paragraph>' +
  238. '<paragraph></paragraph>'
  239. );
  240. } );
  241. } );
  242. describe( 'setTitle()', () => {
  243. it( 'should set new content of a title element', () => {
  244. setData( model,
  245. '<title><title-content></title-content></title>' +
  246. '<paragraph>Bar</paragraph>'
  247. );
  248. editor.plugins.get( 'Title' ).setTitle( 'Biz' );
  249. expect( getData( model ) ).to.equal(
  250. '<title><title-content>[]Biz</title-content></title>' +
  251. '<paragraph>Bar</paragraph>'
  252. );
  253. } );
  254. it( 'should replace old content of a title element', () => {
  255. setData( model,
  256. '<title><title-content>Foo</title-content></title>' +
  257. '<paragraph>Bar</paragraph>'
  258. );
  259. editor.plugins.get( 'Title' ).setTitle( 'Biz' );
  260. expect( getData( model ) ).to.equal(
  261. '<title><title-content>[]Biz</title-content></title>' +
  262. '<paragraph>Bar</paragraph>'
  263. );
  264. } );
  265. it( 'should clear content of a title element', () => {
  266. setData( model,
  267. '<title><title-content>Foo</title-content></title>' +
  268. '<paragraph>Bar</paragraph>'
  269. );
  270. editor.plugins.get( 'Title' ).setTitle( '' );
  271. expect( getData( model ) ).to.equal(
  272. '<title><title-content>[]</title-content></title>' +
  273. '<paragraph>Bar</paragraph>'
  274. );
  275. } );
  276. it( 'should properly handle HTML element', () => {
  277. setData( model,
  278. '<title><title-content></title-content></title>' +
  279. '<paragraph>Bar</paragraph>'
  280. );
  281. editor.plugins.get( 'Title' ).setTitle( '<p>Foo</p>' );
  282. expect( getData( model ) ).to.equal(
  283. '<title><title-content>[]Foo</title-content></title>' +
  284. '<paragraph>Bar</paragraph>'
  285. );
  286. } );
  287. it( 'should properly handle multiple HTML elements', () => {
  288. setData( model,
  289. '<title><title-content>Foo</title-content></title>' +
  290. '<paragraph>Bar</paragraph>'
  291. );
  292. editor.plugins.get( 'Title' ).setTitle( '<p>Foo</p><p>bar</p>' );
  293. expect( getData( model ) ).to.equal(
  294. '<title><title-content>[]Foobar</title-content></title>' +
  295. '<paragraph>Bar</paragraph>'
  296. );
  297. } );
  298. it( 'should do nothing when setting empty content to the empty title', () => {
  299. setData( model,
  300. '<title><title-content></title-content></title>' +
  301. '<paragraph>Bar</paragraph>'
  302. );
  303. editor.plugins.get( 'Title' ).setTitle( '' );
  304. expect( getData( model ) ).to.equal(
  305. '<title><title-content>[]</title-content></title>' +
  306. '<paragraph>Bar</paragraph>'
  307. );
  308. } );
  309. } );
  310. describe( 'getTitle()', () => {
  311. it( 'should return content of a title element', () => {
  312. setData( model,
  313. '<title><title-content>Foo</title-content></title>' +
  314. '<paragraph>Bar</paragraph>'
  315. );
  316. expect( editor.plugins.get( 'Title' ).getTitle() ).to.equal( 'Foo' );
  317. } );
  318. it( 'should return content of an empty title element', () => {
  319. setData( model,
  320. '<title><title-content></title-content></title>' +
  321. '<paragraph>Bar</paragraph>'
  322. );
  323. expect( editor.plugins.get( 'Title' ).getTitle() ).to.equal( '' );
  324. } );
  325. } );
  326. describe( 'setBody()', () => {
  327. it( 'should set new content to body', () => {
  328. setData( model,
  329. '<title><title-content>Foo</title-content></title>' +
  330. '<paragraph>Bar</paragraph>'
  331. );
  332. editor.plugins.get( 'Title' ).setBody( 'Biz' );
  333. expect( getData( model ) ).to.equal(
  334. '<title><title-content>[]Foo</title-content></title>' +
  335. '<paragraph>Biz</paragraph>'
  336. );
  337. } );
  338. it( 'should set empty content to body', () => {
  339. setData( model,
  340. '<title><title-content>Foo</title-content></title>' +
  341. '<paragraph>Bar</paragraph>'
  342. );
  343. editor.plugins.get( 'Title' ).setBody( '' );
  344. expect( getData( model ) ).to.equal(
  345. '<title><title-content>[]Foo</title-content></title>' +
  346. '<paragraph></paragraph>'
  347. );
  348. } );
  349. it( 'should set html content to body', () => {
  350. setData( model,
  351. '<title><title-content>Foo</title-content>' +
  352. '</title><paragraph>Bar</paragraph>'
  353. );
  354. editor.plugins.get( 'Title' ).setBody( '<blockQuote>Bar</blockQuote><p>Biz</p>' );
  355. expect( getData( model ) ).to.equal(
  356. '<title><title-content>[]Foo</title-content></title>' +
  357. '<blockQuote><paragraph>Bar</paragraph></blockQuote>' +
  358. '<paragraph>Biz</paragraph>'
  359. );
  360. } );
  361. } );
  362. describe( 'getBody()', () => {
  363. it( 'should return all data except the title element', () => {
  364. setData( model,
  365. '<title><title-content>Foo</title-content></title>' +
  366. '<paragraph>Bar</paragraph>' +
  367. '<paragraph>Biz</paragraph>'
  368. );
  369. expect( editor.plugins.get( 'Title' ).getBody() ).to.equal( '<p>Bar</p><p>Biz</p>' );
  370. } );
  371. it( 'should return empty paragraph when body is empty', () => {
  372. setData( model, '<title><title-content>Foo</title-content></title>' );
  373. expect( editor.plugins.get( 'Title' ).getBody() ).to.equal( '<p>&nbsp;</p>' );
  374. } );
  375. } );
  376. describe( 'placeholders', () => {
  377. let viewRoot;
  378. beforeEach( () => {
  379. viewRoot = editor.editing.view.document.getRoot();
  380. } );
  381. it( 'should attach placeholder placeholder to title and body', () => {
  382. setData( model,
  383. '<title><title-content>Foo</title-content></title>' +
  384. '<paragraph>Bar</paragraph>'
  385. );
  386. const title = viewRoot.getChild( 0 );
  387. const body = viewRoot.getChild( 1 );
  388. expect( title.getAttribute( 'data-placeholder' ) ).to.equal( 'Title' );
  389. expect( body.getAttribute( 'data-placeholder' ) ).to.equal( 'Body' );
  390. expect( title.hasClass( 'ck-placeholder' ) ).to.equal( false );
  391. expect( body.hasClass( 'ck-placeholder' ) ).to.equal( false );
  392. } );
  393. it( 'should show placeholder in empty title and body', () => {
  394. setData( model,
  395. '<title><title-content></title-content></title>' +
  396. '<paragraph></paragraph>'
  397. );
  398. const title = viewRoot.getChild( 0 );
  399. const body = viewRoot.getChild( 1 );
  400. expect( title.getAttribute( 'data-placeholder' ) ).to.equal( 'Title' );
  401. expect( body.getAttribute( 'data-placeholder' ) ).to.equal( 'Body' );
  402. expect( title.hasClass( 'ck-placeholder' ) ).to.equal( true );
  403. expect( body.hasClass( 'ck-placeholder' ) ).to.equal( true );
  404. } );
  405. it( 'should hide placeholder from body with more than one child elements', () => {
  406. setData( editor.model,
  407. '<title><title-content>Foo</title-content></title>' +
  408. '<paragraph></paragraph>' +
  409. '<paragraph></paragraph>'
  410. );
  411. const body = viewRoot.getChild( 1 );
  412. expect( body.getAttribute( 'data-placeholder' ) ).to.equal( 'Body' );
  413. expect( body.hasClass( 'ck-placeholder' ) ).to.equal( false );
  414. } );
  415. it( 'should hide placeholder from body with element other than paragraph', () => {
  416. setData( editor.model,
  417. '<title><title-content>Foo</title-content></title>' +
  418. '<heading1></heading1>'
  419. );
  420. const body = viewRoot.getChild( 1 );
  421. expect( body.hasAttribute( 'data-placeholder' ) ).to.equal( true );
  422. expect( body.hasClass( 'ck-placeholder' ) ).to.equal( false );
  423. } );
  424. it( 'should hide placeholder when title element become not empty', () => {
  425. setData( model,
  426. '<title><title-content></title-content></title>' +
  427. '<paragraph>[]</paragraph>'
  428. );
  429. expect( viewRoot.getChild( 0 ).hasClass( 'ck-placeholder' ) ).to.equal( true );
  430. model.change( writer => {
  431. writer.appendText( 'Bar', null, model.document.getRoot().getChild( 0 ).getChild( 0 ) );
  432. } );
  433. expect( viewRoot.getChild( 0 ).hasClass( 'ck-placeholder' ) ).to.equal( false );
  434. } );
  435. it( 'should hide placeholder when body element become not empty', () => {
  436. setData( model,
  437. '<title><title-content>Foo</title-content></title>' +
  438. '<paragraph></paragraph>'
  439. );
  440. expect( viewRoot.getChild( 1 ).hasClass( 'ck-placeholder' ) ).to.equal( true );
  441. model.change( writer => {
  442. writer.appendText( 'Bar', null, model.document.getRoot().getChild( 1 ) );
  443. } );
  444. expect( viewRoot.getChild( 1 ).hasClass( 'ck-placeholder' ) ).to.equal( false );
  445. } );
  446. it( 'should properly map the body placeholder in DOM when undoing', () => {
  447. const viewRoot = editor.editing.view.document.getRoot();
  448. const domConverter = editor.editing.view.domConverter;
  449. let bodyDomElement;
  450. setData( editor.model,
  451. '<title><title-content>[Foo</title-content></title>' +
  452. '<paragraph>Bar</paragraph>' +
  453. '<paragraph>Baz]</paragraph>'
  454. );
  455. editor.model.deleteContent( editor.model.document.selection );
  456. bodyDomElement = domConverter.mapViewToDom( viewRoot.getChild( 1 ) );
  457. expect( bodyDomElement.dataset.placeholder ).to.equal( 'Body' );
  458. expect( bodyDomElement.classList.contains( 'ck-placeholder' ) ).to.be.true;
  459. editor.execute( 'undo' );
  460. bodyDomElement = domConverter.mapViewToDom( viewRoot.getChild( 1 ) );
  461. expect( bodyDomElement.dataset.placeholder ).to.equal( 'Body' );
  462. expect( bodyDomElement.classList.contains( 'ck-placeholder' ) ).to.be.false;
  463. } );
  464. it( 'should use placeholder defined through EditorConfig as a Body placeholder', () => {
  465. const element = document.createElement( 'div' );
  466. document.body.appendChild( element );
  467. return ClassicTestEditor.create( element, {
  468. plugins: [ Title ],
  469. placeholder: 'Custom editor placeholder'
  470. } ).then( editor => {
  471. const viewRoot = editor.editing.view.document.getRoot();
  472. const title = viewRoot.getChild( 0 );
  473. const body = viewRoot.getChild( 1 );
  474. expect( title.getAttribute( 'data-placeholder' ) ).to.equal( 'Title' );
  475. expect( title.hasClass( 'ck-placeholder' ) ).to.equal( true );
  476. expect( body.getAttribute( 'data-placeholder' ) ).to.equal( 'Custom editor placeholder' );
  477. expect( body.hasClass( 'ck-placeholder' ) ).to.equal( true );
  478. return editor.destroy().then( () => element.remove() );
  479. } );
  480. } );
  481. } );
  482. describe( 'Tab press handling', () => {
  483. it( 'should handle tab key when the selection is at the beginning of the title', () => {
  484. setData( model,
  485. '<title><title-content>[]foo</title-content></title>' +
  486. '<paragraph>bar</paragraph>'
  487. );
  488. const eventData = getEventData( keyCodes.tab );
  489. editor.keystrokes.press( eventData );
  490. sinon.assert.calledOnce( eventData.preventDefault );
  491. sinon.assert.calledOnce( eventData.stopPropagation );
  492. expect( getData( model ) ).to.equal(
  493. '<title><title-content>foo</title-content></title>' +
  494. '<paragraph>[]bar</paragraph>'
  495. );
  496. } );
  497. it( 'should handle tab key when the selection is at the end of the title', () => {
  498. setData( model,
  499. '<title><title-content>foo[]</title-content></title>' +
  500. '<paragraph>bar</paragraph>'
  501. );
  502. const eventData = getEventData( keyCodes.tab );
  503. editor.keystrokes.press( eventData );
  504. sinon.assert.calledOnce( eventData.preventDefault );
  505. sinon.assert.calledOnce( eventData.stopPropagation );
  506. expect( getData( model ) ).to.equal(
  507. '<title><title-content>foo</title-content></title>' +
  508. '<paragraph>[]bar</paragraph>'
  509. );
  510. } );
  511. it( 'should not handle tab key when the selection is in the title and body', () => {
  512. setData( model,
  513. '<title><title-content>fo[o</title-content></title>' +
  514. '<paragraph>b]ar</paragraph>'
  515. );
  516. const eventData = getEventData( keyCodes.tab );
  517. editor.keystrokes.press( eventData );
  518. sinon.assert.notCalled( eventData.preventDefault );
  519. sinon.assert.notCalled( eventData.stopPropagation );
  520. expect( getData( model ) ).to.equal(
  521. '<title><title-content>fo[o</title-content></title>' +
  522. '<paragraph>b]ar</paragraph>'
  523. );
  524. } );
  525. it( 'should not handle tab key when the selection is in the body', () => {
  526. setData( model,
  527. '<title><title-content>foo</title-content></title>' +
  528. '<paragraph>[]bar</paragraph>'
  529. );
  530. const eventData = getEventData( keyCodes.tab );
  531. editor.keystrokes.press( eventData );
  532. sinon.assert.notCalled( eventData.preventDefault );
  533. sinon.assert.notCalled( eventData.stopPropagation );
  534. expect( getData( model ) ).to.equal(
  535. '<title><title-content>foo</title-content></title>' +
  536. '<paragraph>[]bar</paragraph>'
  537. );
  538. } );
  539. } );
  540. describe( 'Shift + Tab press handling', () => {
  541. it( 'should handle shift + tab keys when the selection is at the beginning of the body', () => {
  542. setData( model,
  543. '<title><title-content>foo</title-content></title>' +
  544. '<paragraph>[]bar</paragraph>'
  545. );
  546. const eventData = getEventData( keyCodes.tab, { shiftKey: true } );
  547. editor.keystrokes.press( eventData );
  548. sinon.assert.calledOnce( eventData.preventDefault );
  549. sinon.assert.calledOnce( eventData.stopPropagation );
  550. expect( getData( model ) ).to.equal(
  551. '<title><title-content>[]foo</title-content></title>' +
  552. '<paragraph>bar</paragraph>'
  553. );
  554. } );
  555. it( 'should not handle shift + tab keys when the selection is not at the beginning of the body', () => {
  556. setData( model,
  557. '<title><title-content>foo</title-content></title>' +
  558. '<paragraph>b[]ar</paragraph>'
  559. );
  560. const eventData = getEventData( keyCodes.tab, { shiftKey: true } );
  561. editor.keystrokes.press( eventData );
  562. sinon.assert.notCalled( eventData.preventDefault );
  563. sinon.assert.notCalled( eventData.stopPropagation );
  564. expect( getData( model ) ).to.equal(
  565. '<title><title-content>foo</title-content></title>' +
  566. '<paragraph>b[]ar</paragraph>'
  567. );
  568. } );
  569. it( 'should not handle shift + tab keys when the selection is not collapsed', () => {
  570. setData( model,
  571. '<title><title-content>foo</title-content></title>' +
  572. '<paragraph>[b]ar</paragraph>'
  573. );
  574. const eventData = getEventData( keyCodes.tab, { shiftKey: true } );
  575. editor.keystrokes.press( eventData );
  576. sinon.assert.notCalled( eventData.preventDefault );
  577. sinon.assert.notCalled( eventData.stopPropagation );
  578. expect( getData( model ) ).to.equal(
  579. '<title><title-content>foo</title-content></title>' +
  580. '<paragraph>[b]ar</paragraph>'
  581. );
  582. } );
  583. it( 'should not handle shift + tab keys when the selection is in the title', () => {
  584. setData( model,
  585. '<title><title-content>[]foo</title-content></title>' +
  586. '<paragraph>bar</paragraph>'
  587. );
  588. const eventData = getEventData( keyCodes.tab, { shiftKey: true } );
  589. editor.keystrokes.press( eventData );
  590. sinon.assert.notCalled( eventData.preventDefault );
  591. sinon.assert.notCalled( eventData.stopPropagation );
  592. expect( getData( model ) ).to.equal(
  593. '<title><title-content>[]foo</title-content></title>' +
  594. '<paragraph>bar</paragraph>'
  595. );
  596. } );
  597. } );
  598. } );
  599. function getEventData( keyCode, { shiftKey = false } = {} ) {
  600. return {
  601. keyCode,
  602. shiftKey,
  603. preventDefault: sinon.spy(),
  604. stopPropagation: sinon.spy()
  605. };
  606. }