title.js 24 KB

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