title.js 25 KB

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