title.js 28 KB

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