title.js 24 KB

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