8
0

title.js 25 KB

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