title.js 24 KB

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