title.js 23 KB

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