title.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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. it( 'should return marker - starts and ends inside a title', () => {
  381. editor.conversion.for( 'downcast' ).markerToElement( { model: 'comment', view: 'comment' } );
  382. setData( model, '<title><title-content>Foo Bar</title-content></title>' );
  383. const title = model.document.getRoot().getChild( 0 ).getChild( 0 );
  384. model.change( writer => {
  385. writer.addMarker( 'comment', {
  386. range: model.createRange( model.createPositionAt( title, 1 ), model.createPositionAt( title, 5 ) ),
  387. usingOperation: true
  388. } );
  389. } );
  390. expect( editor.plugins.get( 'Title' ).getTitle() ).to.equal(
  391. 'F<comment></comment>oo B<comment></comment>ar'
  392. );
  393. } );
  394. it( 'should return marker - starts inside a title ends inside a body', () => {
  395. editor.conversion.for( 'downcast' ).markerToElement( { model: 'comment', view: 'comment' } );
  396. setData( model,
  397. '<title><title-content>Foo Bar</title-content></title>' +
  398. '<paragraph>Biz</paragraph>'
  399. );
  400. const title = model.document.getRoot().getChild( 0 ).getChild( 0 );
  401. const body = model.document.getRoot().getChild( 1 );
  402. model.change( writer => {
  403. writer.addMarker( 'comment', {
  404. range: model.createRange( model.createPositionAt( title, 1 ), model.createPositionAt( body, 3 ) ),
  405. usingOperation: true
  406. } );
  407. } );
  408. expect( editor.plugins.get( 'Title' ).getTitle() ).to.equal(
  409. 'F<comment></comment>oo Bar<comment></comment>'
  410. );
  411. } );
  412. } );
  413. describe( 'setBody()', () => {
  414. it( 'should set new content to body', () => {
  415. setData( model,
  416. '<title><title-content>Foo</title-content></title>' +
  417. '<paragraph>Bar</paragraph>'
  418. );
  419. editor.plugins.get( 'Title' ).setBody( 'Biz' );
  420. expect( getData( model ) ).to.equal(
  421. '<title><title-content>[]Foo</title-content></title>' +
  422. '<paragraph>Biz</paragraph>'
  423. );
  424. } );
  425. it( 'should set empty content to body', () => {
  426. setData( model,
  427. '<title><title-content>Foo</title-content></title>' +
  428. '<paragraph>Bar</paragraph>'
  429. );
  430. editor.plugins.get( 'Title' ).setBody( '' );
  431. expect( getData( model ) ).to.equal(
  432. '<title><title-content>[]Foo</title-content></title>' +
  433. '<paragraph></paragraph>'
  434. );
  435. } );
  436. it( 'should set html content to body', () => {
  437. setData( model,
  438. '<title><title-content>Foo</title-content>' +
  439. '</title><paragraph>Bar</paragraph>'
  440. );
  441. editor.plugins.get( 'Title' ).setBody( '<blockQuote>Bar</blockQuote><p>Biz</p>' );
  442. expect( getData( model ) ).to.equal(
  443. '<title><title-content>[]Foo</title-content></title>' +
  444. '<blockQuote><paragraph>Bar</paragraph></blockQuote>' +
  445. '<paragraph>Biz</paragraph>'
  446. );
  447. } );
  448. } );
  449. describe( 'getBody()', () => {
  450. it( 'should return all data except the title element', () => {
  451. setData( model,
  452. '<title><title-content>Foo</title-content></title>' +
  453. '<paragraph>Bar</paragraph>' +
  454. '<paragraph>Biz</paragraph>'
  455. );
  456. expect( editor.plugins.get( 'Title' ).getBody() ).to.equal( '<p>Bar</p><p>Biz</p>' );
  457. } );
  458. it( 'should return empty paragraph when body is empty', () => {
  459. setData( model, '<title><title-content>Foo</title-content></title>' );
  460. expect( editor.plugins.get( 'Title' ).getBody() ).to.equal( '<p>&nbsp;</p>' );
  461. } );
  462. it( 'should return marker - starts and ends inside a body', () => {
  463. editor.conversion.for( 'downcast' ).markerToElement( { model: 'comment', view: 'comment' } );
  464. setData( model,
  465. '<title><title-content></title-content></title>' +
  466. '<paragraph>Foo Bar</paragraph>'
  467. );
  468. const body = model.document.getRoot().getChild( 1 );
  469. model.change( writer => {
  470. writer.addMarker( 'comment', {
  471. range: model.createRange( model.createPositionAt( body, 1 ), model.createPositionAt( body, 5 ) ),
  472. usingOperation: true
  473. } );
  474. } );
  475. expect( editor.plugins.get( 'Title' ).getBody() ).to.equal(
  476. '<p>F<comment></comment>oo B<comment></comment>ar</p>'
  477. );
  478. } );
  479. it( 'should return marker - starts inside a title ends inside a body', () => {
  480. editor.conversion.for( 'downcast' ).markerToElement( { model: 'comment', view: 'comment' } );
  481. setData( model,
  482. '<title><title-content>Foo</title-content></title>' +
  483. '<paragraph>Bar</paragraph>'
  484. );
  485. const title = model.document.getRoot().getChild( 0 ).getChild( 0 );
  486. const body = model.document.getRoot().getChild( 1 );
  487. model.change( writer => {
  488. writer.addMarker( 'comment', {
  489. range: model.createRange( model.createPositionAt( title, 1 ), model.createPositionAt( body, 2 ) ),
  490. usingOperation: true
  491. } );
  492. } );
  493. expect( editor.plugins.get( 'Title' ).getBody() ).to.equal(
  494. '<comment></comment><p>Ba<comment></comment>r</p>'
  495. );
  496. } );
  497. it( 'should return marker - starts at the beginning of the body ends inside the body', () => {
  498. editor.conversion.for( 'downcast' ).markerToElement( { model: 'comment', view: 'comment' } );
  499. setData( model,
  500. '<title><title-content>Foo</title-content></title>' +
  501. '<paragraph>Bar</paragraph>'
  502. );
  503. const body = model.document.getRoot().getChild( 1 );
  504. model.change( writer => {
  505. writer.addMarker( 'comment', {
  506. range: model.createRange( model.createPositionAt( body, 0 ), model.createPositionAt( body, 2 ) ),
  507. usingOperation: true
  508. } );
  509. } );
  510. expect( editor.plugins.get( 'Title' ).getBody() ).to.equal(
  511. '<p><comment></comment>Ba<comment></comment>r</p>'
  512. );
  513. } );
  514. it( 'should do nothing when marker is fully out of the body range', () => {
  515. editor.conversion.for( 'downcast' ).markerToElement( { model: 'comment', view: 'comment' } );
  516. setData( model,
  517. '<title><title-content>Foo</title-content></title>' +
  518. '<paragraph>Bar</paragraph>'
  519. );
  520. const title = model.document.getRoot().getChild( 0 ).getChild( 0 );
  521. model.change( writer => {
  522. writer.addMarker( 'comment', {
  523. range: model.createRange( model.createPositionAt( title, 1 ), model.createPositionAt( title, 2 ) ),
  524. usingOperation: true
  525. } );
  526. } );
  527. expect( editor.plugins.get( 'Title' ).getBody() ).to.equal( '<p>Bar</p>' );
  528. } );
  529. } );
  530. describe( 'placeholders', () => {
  531. let viewRoot;
  532. beforeEach( () => {
  533. viewRoot = editor.editing.view.document.getRoot();
  534. } );
  535. it( 'should attach placeholder placeholder to title and body', () => {
  536. setData( model,
  537. '<title><title-content>Foo</title-content></title>' +
  538. '<paragraph>Bar</paragraph>'
  539. );
  540. const title = viewRoot.getChild( 0 );
  541. const body = viewRoot.getChild( 1 );
  542. expect( title.getAttribute( 'data-placeholder' ) ).to.equal( 'Title' );
  543. expect( body.getAttribute( 'data-placeholder' ) ).to.equal( 'Body' );
  544. expect( title.hasClass( 'ck-placeholder' ) ).to.equal( false );
  545. expect( body.hasClass( 'ck-placeholder' ) ).to.equal( false );
  546. } );
  547. it( 'should show placeholder in empty title and body', () => {
  548. setData( model,
  549. '<title><title-content></title-content></title>' +
  550. '<paragraph></paragraph>'
  551. );
  552. const title = viewRoot.getChild( 0 );
  553. const body = viewRoot.getChild( 1 );
  554. expect( title.getAttribute( 'data-placeholder' ) ).to.equal( 'Title' );
  555. expect( body.getAttribute( 'data-placeholder' ) ).to.equal( 'Body' );
  556. expect( title.hasClass( 'ck-placeholder' ) ).to.equal( true );
  557. expect( body.hasClass( 'ck-placeholder' ) ).to.equal( true );
  558. } );
  559. it( 'should hide placeholder from body with more than one child elements', () => {
  560. setData( editor.model,
  561. '<title><title-content>Foo</title-content></title>' +
  562. '<paragraph></paragraph>' +
  563. '<paragraph></paragraph>'
  564. );
  565. const body = viewRoot.getChild( 1 );
  566. expect( body.getAttribute( 'data-placeholder' ) ).to.equal( 'Body' );
  567. expect( body.hasClass( 'ck-placeholder' ) ).to.equal( false );
  568. } );
  569. it( 'should hide placeholder from body with element other than paragraph', () => {
  570. setData( editor.model,
  571. '<title><title-content>Foo</title-content></title>' +
  572. '<heading1></heading1>'
  573. );
  574. const body = viewRoot.getChild( 1 );
  575. expect( body.hasAttribute( 'data-placeholder' ) ).to.equal( true );
  576. expect( body.hasClass( 'ck-placeholder' ) ).to.equal( false );
  577. } );
  578. it( 'should hide placeholder when title element become not empty', () => {
  579. setData( model,
  580. '<title><title-content></title-content></title>' +
  581. '<paragraph>[]</paragraph>'
  582. );
  583. expect( viewRoot.getChild( 0 ).hasClass( 'ck-placeholder' ) ).to.equal( true );
  584. model.change( writer => {
  585. writer.appendText( 'Bar', null, model.document.getRoot().getChild( 0 ).getChild( 0 ) );
  586. } );
  587. expect( viewRoot.getChild( 0 ).hasClass( 'ck-placeholder' ) ).to.equal( false );
  588. } );
  589. it( 'should hide placeholder when body element become not empty', () => {
  590. setData( model,
  591. '<title><title-content>Foo</title-content></title>' +
  592. '<paragraph></paragraph>'
  593. );
  594. expect( viewRoot.getChild( 1 ).hasClass( 'ck-placeholder' ) ).to.equal( true );
  595. model.change( writer => {
  596. writer.appendText( 'Bar', null, model.document.getRoot().getChild( 1 ) );
  597. } );
  598. expect( viewRoot.getChild( 1 ).hasClass( 'ck-placeholder' ) ).to.equal( false );
  599. } );
  600. it( 'should properly map the body placeholder in DOM when undoing', () => {
  601. const viewRoot = editor.editing.view.document.getRoot();
  602. const domConverter = editor.editing.view.domConverter;
  603. let bodyDomElement;
  604. setData( editor.model,
  605. '<title><title-content>[Foo</title-content></title>' +
  606. '<paragraph>Bar</paragraph>' +
  607. '<paragraph>Baz]</paragraph>'
  608. );
  609. editor.model.deleteContent( editor.model.document.selection );
  610. bodyDomElement = domConverter.mapViewToDom( viewRoot.getChild( 1 ) );
  611. expect( bodyDomElement.dataset.placeholder ).to.equal( 'Body' );
  612. expect( bodyDomElement.classList.contains( 'ck-placeholder' ) ).to.equal( true );
  613. editor.execute( 'undo' );
  614. bodyDomElement = domConverter.mapViewToDom( viewRoot.getChild( 1 ) );
  615. expect( bodyDomElement.dataset.placeholder ).to.equal( 'Body' );
  616. expect( bodyDomElement.classList.contains( 'ck-placeholder' ) ).to.equal( false );
  617. } );
  618. it( 'should use placeholder defined through EditorConfig as a Body placeholder', () => {
  619. const element = document.createElement( 'div' );
  620. document.body.appendChild( element );
  621. return ClassicTestEditor.create( element, {
  622. plugins: [ Title ],
  623. placeholder: 'Custom editor placeholder'
  624. } ).then( editor => {
  625. const viewRoot = editor.editing.view.document.getRoot();
  626. const title = viewRoot.getChild( 0 );
  627. const body = viewRoot.getChild( 1 );
  628. expect( title.getAttribute( 'data-placeholder' ) ).to.equal( 'Title' );
  629. expect( title.hasClass( 'ck-placeholder' ) ).to.equal( true );
  630. expect( body.getAttribute( 'data-placeholder' ) ).to.equal( 'Custom editor placeholder' );
  631. expect( body.hasClass( 'ck-placeholder' ) ).to.equal( true );
  632. return editor.destroy().then( () => element.remove() );
  633. } );
  634. } );
  635. } );
  636. describe( 'Tab press handling', () => {
  637. it( 'should handle tab key when the selection is at the beginning of the title', () => {
  638. setData( model,
  639. '<title><title-content>[]foo</title-content></title>' +
  640. '<paragraph>bar</paragraph>'
  641. );
  642. const eventData = getEventData( keyCodes.tab );
  643. editor.keystrokes.press( eventData );
  644. sinon.assert.calledOnce( eventData.preventDefault );
  645. sinon.assert.calledOnce( eventData.stopPropagation );
  646. expect( getData( model ) ).to.equal(
  647. '<title><title-content>foo</title-content></title>' +
  648. '<paragraph>[]bar</paragraph>'
  649. );
  650. } );
  651. it( 'should handle tab key when the selection is at the end of the title', () => {
  652. setData( model,
  653. '<title><title-content>foo[]</title-content></title>' +
  654. '<paragraph>bar</paragraph>'
  655. );
  656. const eventData = getEventData( keyCodes.tab );
  657. editor.keystrokes.press( eventData );
  658. sinon.assert.calledOnce( eventData.preventDefault );
  659. sinon.assert.calledOnce( eventData.stopPropagation );
  660. expect( getData( model ) ).to.equal(
  661. '<title><title-content>foo</title-content></title>' +
  662. '<paragraph>[]bar</paragraph>'
  663. );
  664. } );
  665. it( 'should not handle tab key when the selection is in the title and body', () => {
  666. setData( model,
  667. '<title><title-content>fo[o</title-content></title>' +
  668. '<paragraph>b]ar</paragraph>'
  669. );
  670. const eventData = getEventData( keyCodes.tab );
  671. editor.keystrokes.press( eventData );
  672. sinon.assert.notCalled( eventData.preventDefault );
  673. sinon.assert.notCalled( eventData.stopPropagation );
  674. expect( getData( model ) ).to.equal(
  675. '<title><title-content>fo[o</title-content></title>' +
  676. '<paragraph>b]ar</paragraph>'
  677. );
  678. } );
  679. it( 'should not handle tab key when the selection is in the body', () => {
  680. setData( model,
  681. '<title><title-content>foo</title-content></title>' +
  682. '<paragraph>[]bar</paragraph>'
  683. );
  684. const eventData = getEventData( keyCodes.tab );
  685. editor.keystrokes.press( eventData );
  686. sinon.assert.notCalled( eventData.preventDefault );
  687. sinon.assert.notCalled( eventData.stopPropagation );
  688. expect( getData( model ) ).to.equal(
  689. '<title><title-content>foo</title-content></title>' +
  690. '<paragraph>[]bar</paragraph>'
  691. );
  692. } );
  693. } );
  694. describe( 'Shift + Tab press handling', () => {
  695. it( 'should handle shift + tab keys when the selection is at the beginning of the body', () => {
  696. setData( model,
  697. '<title><title-content>foo</title-content></title>' +
  698. '<paragraph>[]bar</paragraph>'
  699. );
  700. const eventData = getEventData( keyCodes.tab, { shiftKey: true } );
  701. editor.keystrokes.press( eventData );
  702. sinon.assert.calledOnce( eventData.preventDefault );
  703. sinon.assert.calledOnce( eventData.stopPropagation );
  704. expect( getData( model ) ).to.equal(
  705. '<title><title-content>[]foo</title-content></title>' +
  706. '<paragraph>bar</paragraph>'
  707. );
  708. } );
  709. it( 'should not handle shift + tab keys when the selection is not at the beginning of the body', () => {
  710. setData( model,
  711. '<title><title-content>foo</title-content></title>' +
  712. '<paragraph>b[]ar</paragraph>'
  713. );
  714. const eventData = getEventData( keyCodes.tab, { shiftKey: true } );
  715. editor.keystrokes.press( eventData );
  716. sinon.assert.notCalled( eventData.preventDefault );
  717. sinon.assert.notCalled( eventData.stopPropagation );
  718. expect( getData( model ) ).to.equal(
  719. '<title><title-content>foo</title-content></title>' +
  720. '<paragraph>b[]ar</paragraph>'
  721. );
  722. } );
  723. it( 'should not handle shift + tab keys when the selection is not collapsed', () => {
  724. setData( model,
  725. '<title><title-content>foo</title-content></title>' +
  726. '<paragraph>[b]ar</paragraph>'
  727. );
  728. const eventData = getEventData( keyCodes.tab, { shiftKey: true } );
  729. editor.keystrokes.press( eventData );
  730. sinon.assert.notCalled( eventData.preventDefault );
  731. sinon.assert.notCalled( eventData.stopPropagation );
  732. expect( getData( model ) ).to.equal(
  733. '<title><title-content>foo</title-content></title>' +
  734. '<paragraph>[b]ar</paragraph>'
  735. );
  736. } );
  737. it( 'should not handle shift + tab keys when the selection is in the title', () => {
  738. setData( model,
  739. '<title><title-content>[]foo</title-content></title>' +
  740. '<paragraph>bar</paragraph>'
  741. );
  742. const eventData = getEventData( keyCodes.tab, { shiftKey: true } );
  743. editor.keystrokes.press( eventData );
  744. sinon.assert.notCalled( eventData.preventDefault );
  745. sinon.assert.notCalled( eventData.stopPropagation );
  746. expect( getData( model ) ).to.equal(
  747. '<title><title-content>[]foo</title-content></title>' +
  748. '<paragraph>bar</paragraph>'
  749. );
  750. } );
  751. } );
  752. } );
  753. function getEventData( keyCode, { shiftKey = false } = {} ) {
  754. return {
  755. keyCode,
  756. shiftKey,
  757. preventDefault: sinon.spy(),
  758. stopPropagation: sinon.spy()
  759. };
  760. }