title.js 26 KB

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