title.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. '<p><comment></comment>Ba<comment></comment>r</p>'
  458. );
  459. } );
  460. } );
  461. describe( 'placeholders', () => {
  462. let viewRoot;
  463. beforeEach( () => {
  464. viewRoot = editor.editing.view.document.getRoot();
  465. } );
  466. it( 'should attach placeholder placeholder to title and body', () => {
  467. setData( model,
  468. '<title><title-content>Foo</title-content></title>' +
  469. '<paragraph>Bar</paragraph>'
  470. );
  471. const title = viewRoot.getChild( 0 );
  472. const body = viewRoot.getChild( 1 );
  473. expect( title.getAttribute( 'data-placeholder' ) ).to.equal( 'Title' );
  474. expect( body.getAttribute( 'data-placeholder' ) ).to.equal( 'Body' );
  475. expect( title.hasClass( 'ck-placeholder' ) ).to.equal( false );
  476. expect( body.hasClass( 'ck-placeholder' ) ).to.equal( false );
  477. } );
  478. it( 'should show placeholder in empty title and body', () => {
  479. setData( model,
  480. '<title><title-content></title-content></title>' +
  481. '<paragraph></paragraph>'
  482. );
  483. const title = viewRoot.getChild( 0 );
  484. const body = viewRoot.getChild( 1 );
  485. expect( title.getAttribute( 'data-placeholder' ) ).to.equal( 'Title' );
  486. expect( body.getAttribute( 'data-placeholder' ) ).to.equal( 'Body' );
  487. expect( title.hasClass( 'ck-placeholder' ) ).to.equal( true );
  488. expect( body.hasClass( 'ck-placeholder' ) ).to.equal( true );
  489. } );
  490. it( 'should hide placeholder from body with more than one child elements', () => {
  491. setData( editor.model,
  492. '<title><title-content>Foo</title-content></title>' +
  493. '<paragraph></paragraph>' +
  494. '<paragraph></paragraph>'
  495. );
  496. const body = viewRoot.getChild( 1 );
  497. expect( body.getAttribute( 'data-placeholder' ) ).to.equal( 'Body' );
  498. expect( body.hasClass( 'ck-placeholder' ) ).to.equal( false );
  499. } );
  500. it( 'should hide placeholder from body with element other than paragraph', () => {
  501. setData( editor.model,
  502. '<title><title-content>Foo</title-content></title>' +
  503. '<heading1></heading1>'
  504. );
  505. const body = viewRoot.getChild( 1 );
  506. expect( body.hasAttribute( 'data-placeholder' ) ).to.equal( true );
  507. expect( body.hasClass( 'ck-placeholder' ) ).to.equal( false );
  508. } );
  509. it( 'should hide placeholder when title element become not empty', () => {
  510. setData( model,
  511. '<title><title-content></title-content></title>' +
  512. '<paragraph>[]</paragraph>'
  513. );
  514. expect( viewRoot.getChild( 0 ).hasClass( 'ck-placeholder' ) ).to.equal( true );
  515. model.change( writer => {
  516. writer.appendText( 'Bar', null, model.document.getRoot().getChild( 0 ).getChild( 0 ) );
  517. } );
  518. expect( viewRoot.getChild( 0 ).hasClass( 'ck-placeholder' ) ).to.equal( false );
  519. } );
  520. it( 'should hide placeholder when body element become not empty', () => {
  521. setData( model,
  522. '<title><title-content>Foo</title-content></title>' +
  523. '<paragraph></paragraph>'
  524. );
  525. expect( viewRoot.getChild( 1 ).hasClass( 'ck-placeholder' ) ).to.equal( true );
  526. model.change( writer => {
  527. writer.appendText( 'Bar', null, model.document.getRoot().getChild( 1 ) );
  528. } );
  529. expect( viewRoot.getChild( 1 ).hasClass( 'ck-placeholder' ) ).to.equal( false );
  530. } );
  531. it( 'should properly map the body placeholder in DOM when undoing', () => {
  532. const viewRoot = editor.editing.view.document.getRoot();
  533. const domConverter = editor.editing.view.domConverter;
  534. let bodyDomElement;
  535. setData( editor.model,
  536. '<title><title-content>[Foo</title-content></title>' +
  537. '<paragraph>Bar</paragraph>' +
  538. '<paragraph>Baz]</paragraph>'
  539. );
  540. editor.model.deleteContent( editor.model.document.selection );
  541. bodyDomElement = domConverter.mapViewToDom( viewRoot.getChild( 1 ) );
  542. expect( bodyDomElement.dataset.placeholder ).to.equal( 'Body' );
  543. expect( bodyDomElement.classList.contains( 'ck-placeholder' ) ).to.equal( true );
  544. editor.execute( 'undo' );
  545. bodyDomElement = domConverter.mapViewToDom( viewRoot.getChild( 1 ) );
  546. expect( bodyDomElement.dataset.placeholder ).to.equal( 'Body' );
  547. expect( bodyDomElement.classList.contains( 'ck-placeholder' ) ).to.equal( false );
  548. } );
  549. it( 'should use placeholder defined through EditorConfig as a Body placeholder', () => {
  550. const element = document.createElement( 'div' );
  551. document.body.appendChild( element );
  552. return ClassicTestEditor.create( element, {
  553. plugins: [ Title ],
  554. placeholder: 'Custom editor placeholder'
  555. } ).then( editor => {
  556. const viewRoot = editor.editing.view.document.getRoot();
  557. const title = viewRoot.getChild( 0 );
  558. const body = viewRoot.getChild( 1 );
  559. expect( title.getAttribute( 'data-placeholder' ) ).to.equal( 'Title' );
  560. expect( title.hasClass( 'ck-placeholder' ) ).to.equal( true );
  561. expect( body.getAttribute( 'data-placeholder' ) ).to.equal( 'Custom editor placeholder' );
  562. expect( body.hasClass( 'ck-placeholder' ) ).to.equal( true );
  563. return editor.destroy().then( () => element.remove() );
  564. } );
  565. } );
  566. } );
  567. describe( 'Tab press handling', () => {
  568. it( 'should handle tab key when the selection is at the beginning of the title', () => {
  569. setData( model,
  570. '<title><title-content>[]foo</title-content></title>' +
  571. '<paragraph>bar</paragraph>'
  572. );
  573. const eventData = getEventData( keyCodes.tab );
  574. editor.keystrokes.press( eventData );
  575. sinon.assert.calledOnce( eventData.preventDefault );
  576. sinon.assert.calledOnce( eventData.stopPropagation );
  577. expect( getData( model ) ).to.equal(
  578. '<title><title-content>foo</title-content></title>' +
  579. '<paragraph>[]bar</paragraph>'
  580. );
  581. } );
  582. it( 'should handle tab key when the selection is at the end of the title', () => {
  583. setData( model,
  584. '<title><title-content>foo[]</title-content></title>' +
  585. '<paragraph>bar</paragraph>'
  586. );
  587. const eventData = getEventData( keyCodes.tab );
  588. editor.keystrokes.press( eventData );
  589. sinon.assert.calledOnce( eventData.preventDefault );
  590. sinon.assert.calledOnce( eventData.stopPropagation );
  591. expect( getData( model ) ).to.equal(
  592. '<title><title-content>foo</title-content></title>' +
  593. '<paragraph>[]bar</paragraph>'
  594. );
  595. } );
  596. it( 'should not handle tab key when the selection is in the title and body', () => {
  597. setData( model,
  598. '<title><title-content>fo[o</title-content></title>' +
  599. '<paragraph>b]ar</paragraph>'
  600. );
  601. const eventData = getEventData( keyCodes.tab );
  602. editor.keystrokes.press( eventData );
  603. sinon.assert.notCalled( eventData.preventDefault );
  604. sinon.assert.notCalled( eventData.stopPropagation );
  605. expect( getData( model ) ).to.equal(
  606. '<title><title-content>fo[o</title-content></title>' +
  607. '<paragraph>b]ar</paragraph>'
  608. );
  609. } );
  610. it( 'should not handle tab key when the selection is in the body', () => {
  611. setData( model,
  612. '<title><title-content>foo</title-content></title>' +
  613. '<paragraph>[]bar</paragraph>'
  614. );
  615. const eventData = getEventData( keyCodes.tab );
  616. editor.keystrokes.press( eventData );
  617. sinon.assert.notCalled( eventData.preventDefault );
  618. sinon.assert.notCalled( eventData.stopPropagation );
  619. expect( getData( model ) ).to.equal(
  620. '<title><title-content>foo</title-content></title>' +
  621. '<paragraph>[]bar</paragraph>'
  622. );
  623. } );
  624. } );
  625. describe( 'Shift + Tab press handling', () => {
  626. it( 'should handle shift + tab keys when the selection is at the beginning of the body', () => {
  627. setData( model,
  628. '<title><title-content>foo</title-content></title>' +
  629. '<paragraph>[]bar</paragraph>'
  630. );
  631. const eventData = getEventData( keyCodes.tab, { shiftKey: true } );
  632. editor.keystrokes.press( eventData );
  633. sinon.assert.calledOnce( eventData.preventDefault );
  634. sinon.assert.calledOnce( eventData.stopPropagation );
  635. expect( getData( model ) ).to.equal(
  636. '<title><title-content>[]foo</title-content></title>' +
  637. '<paragraph>bar</paragraph>'
  638. );
  639. } );
  640. it( 'should not handle shift + tab keys when the selection is not at the beginning of the body', () => {
  641. setData( model,
  642. '<title><title-content>foo</title-content></title>' +
  643. '<paragraph>b[]ar</paragraph>'
  644. );
  645. const eventData = getEventData( keyCodes.tab, { shiftKey: true } );
  646. editor.keystrokes.press( eventData );
  647. sinon.assert.notCalled( eventData.preventDefault );
  648. sinon.assert.notCalled( eventData.stopPropagation );
  649. expect( getData( model ) ).to.equal(
  650. '<title><title-content>foo</title-content></title>' +
  651. '<paragraph>b[]ar</paragraph>'
  652. );
  653. } );
  654. it( 'should not handle shift + tab keys when the selection is not collapsed', () => {
  655. setData( model,
  656. '<title><title-content>foo</title-content></title>' +
  657. '<paragraph>[b]ar</paragraph>'
  658. );
  659. const eventData = getEventData( keyCodes.tab, { shiftKey: true } );
  660. editor.keystrokes.press( eventData );
  661. sinon.assert.notCalled( eventData.preventDefault );
  662. sinon.assert.notCalled( eventData.stopPropagation );
  663. expect( getData( model ) ).to.equal(
  664. '<title><title-content>foo</title-content></title>' +
  665. '<paragraph>[b]ar</paragraph>'
  666. );
  667. } );
  668. it( 'should not handle shift + tab keys when the selection is in the title', () => {
  669. setData( model,
  670. '<title><title-content>[]foo</title-content></title>' +
  671. '<paragraph>bar</paragraph>'
  672. );
  673. const eventData = getEventData( keyCodes.tab, { shiftKey: true } );
  674. editor.keystrokes.press( eventData );
  675. sinon.assert.notCalled( eventData.preventDefault );
  676. sinon.assert.notCalled( eventData.stopPropagation );
  677. expect( getData( model ) ).to.equal(
  678. '<title><title-content>[]foo</title-content></title>' +
  679. '<paragraph>bar</paragraph>'
  680. );
  681. } );
  682. } );
  683. } );
  684. function getEventData( keyCode, { shiftKey = false } = {} ) {
  685. return {
  686. keyCode,
  687. shiftKey,
  688. preventDefault: sinon.spy(),
  689. stopPropagation: sinon.spy()
  690. };
  691. }