title.js 25 KB

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