title.js 24 KB

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