title.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. /**
  2. * @license Copyright (c) 2003-2020, 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. stopPropagation() {},
  246. preventDefault() {}
  247. } );
  248. expect( getData( model ) ).to.equal(
  249. '<title><title-content>Title</title-content></title>' +
  250. '<paragraph>Body[]</paragraph>'
  251. );
  252. } );
  253. it( 'should not remove the extra paragraph when pasting to the editor with directly created body element', () => {
  254. setData( model,
  255. '<title><title-content>[]</title-content></title>' +
  256. '<paragraph></paragraph>'
  257. );
  258. const dataTransferMock = {
  259. getData: type => {
  260. if ( type === 'text/html' ) {
  261. return '<h1>Title</h1><p>Body</p>';
  262. }
  263. },
  264. types: [],
  265. files: []
  266. };
  267. editor.editing.view.document.fire( 'paste', {
  268. dataTransfer: dataTransferMock,
  269. stopPropagation() {},
  270. preventDefault() {}
  271. } );
  272. expect( getData( model ) ).to.equal(
  273. '<title><title-content>Title</title-content></title>' +
  274. '<paragraph>Body[]</paragraph>' +
  275. '<paragraph></paragraph>'
  276. );
  277. } );
  278. it( 'should remove the extra paragraph when pressing enter in the title', () => {
  279. setData( model, '<title><title-content>fo[]o</title-content></title>' );
  280. editor.execute( 'enter' );
  281. expect( getData( model ) ).to.equal(
  282. '<title><title-content>fo</title-content></title>' +
  283. '<paragraph>[]o</paragraph>'
  284. );
  285. } );
  286. it( 'should not remove the extra paragraph when pressing enter in the title when body is created directly', () => {
  287. setData( model,
  288. '<title><title-content>fo[]o</title-content></title>' +
  289. '<paragraph></paragraph>'
  290. );
  291. editor.execute( 'enter' );
  292. expect( getData( model ) ).to.equal(
  293. '<title><title-content>fo</title-content></title>' +
  294. '<paragraph>[]o</paragraph>' +
  295. '<paragraph></paragraph>'
  296. );
  297. } );
  298. } );
  299. describe( 'getTitle()', () => {
  300. it( 'should return content of a title element', () => {
  301. setData( model,
  302. '<title><title-content>Foo</title-content></title>' +
  303. '<paragraph>Bar</paragraph>'
  304. );
  305. expect( editor.plugins.get( 'Title' ).getTitle() ).to.equal( 'Foo' );
  306. } );
  307. it( 'should return content of an empty title element', () => {
  308. setData( model,
  309. '<title><title-content></title-content></title>' +
  310. '<paragraph>Bar</paragraph>'
  311. );
  312. expect( editor.plugins.get( 'Title' ).getTitle() ).to.equal( '' );
  313. } );
  314. it( 'should return marker - starts and ends inside a title', () => {
  315. editor.conversion.for( 'downcast' ).markerToElement( { model: 'comment', view: 'comment' } );
  316. setData( model, '<title><title-content>Foo Bar</title-content></title>' );
  317. const title = model.document.getRoot().getChild( 0 ).getChild( 0 );
  318. model.change( writer => {
  319. writer.addMarker( 'comment', {
  320. range: model.createRange( model.createPositionAt( title, 1 ), model.createPositionAt( title, 5 ) ),
  321. usingOperation: true
  322. } );
  323. } );
  324. expect( editor.plugins.get( 'Title' ).getTitle() ).to.equal(
  325. 'F<comment></comment>oo B<comment></comment>ar'
  326. );
  327. } );
  328. it( 'should return marker - starts inside a title ends inside a body', () => {
  329. editor.conversion.for( 'downcast' ).markerToElement( { model: 'comment', view: 'comment' } );
  330. setData( model,
  331. '<title><title-content>Foo Bar</title-content></title>' +
  332. '<paragraph>Biz</paragraph>'
  333. );
  334. const title = model.document.getRoot().getChild( 0 ).getChild( 0 );
  335. const body = model.document.getRoot().getChild( 1 );
  336. model.change( writer => {
  337. writer.addMarker( 'comment', {
  338. range: model.createRange( model.createPositionAt( title, 1 ), model.createPositionAt( body, 3 ) ),
  339. usingOperation: true
  340. } );
  341. } );
  342. expect( editor.plugins.get( 'Title' ).getTitle() ).to.equal(
  343. 'F<comment></comment>oo Bar<comment></comment>'
  344. );
  345. } );
  346. } );
  347. describe( 'getBody()', () => {
  348. it( 'should return all data except the title element', () => {
  349. setData( model,
  350. '<title><title-content>Foo</title-content></title>' +
  351. '<paragraph>Bar</paragraph>' +
  352. '<paragraph>Biz</paragraph>'
  353. );
  354. expect( editor.plugins.get( 'Title' ).getBody() ).to.equal( '<p>Bar</p><p>Biz</p>' );
  355. } );
  356. it( 'should return empty paragraph when body is empty', () => {
  357. setData( model, '<title><title-content>Foo</title-content></title>' );
  358. expect( editor.plugins.get( 'Title' ).getBody() ).to.equal( '<p>&nbsp;</p>' );
  359. } );
  360. it( 'should return marker - starts and ends inside a body', () => {
  361. editor.conversion.for( 'downcast' ).markerToElement( { model: 'comment', view: 'comment' } );
  362. setData( model,
  363. '<title><title-content></title-content></title>' +
  364. '<paragraph>Foo Bar</paragraph>'
  365. );
  366. const body = model.document.getRoot().getChild( 1 );
  367. model.change( writer => {
  368. writer.addMarker( 'comment', {
  369. range: model.createRange( model.createPositionAt( body, 1 ), model.createPositionAt( body, 5 ) ),
  370. usingOperation: true
  371. } );
  372. } );
  373. expect( editor.plugins.get( 'Title' ).getBody() ).to.equal(
  374. '<p>F<comment></comment>oo B<comment></comment>ar</p>'
  375. );
  376. } );
  377. it( 'should return marker - starts inside a title ends inside a body', () => {
  378. editor.conversion.for( 'downcast' ).markerToElement( { model: 'comment', view: 'comment' } );
  379. setData( model,
  380. '<title><title-content>Foo</title-content></title>' +
  381. '<paragraph>Bar</paragraph>'
  382. );
  383. const title = model.document.getRoot().getChild( 0 ).getChild( 0 );
  384. const body = model.document.getRoot().getChild( 1 );
  385. model.change( writer => {
  386. writer.addMarker( 'comment', {
  387. range: model.createRange( model.createPositionAt( title, 1 ), model.createPositionAt( body, 2 ) ),
  388. usingOperation: true
  389. } );
  390. } );
  391. expect( editor.plugins.get( 'Title' ).getBody() ).to.equal(
  392. '<comment></comment><p>Ba<comment></comment>r</p>'
  393. );
  394. } );
  395. it( 'should return marker - starts at the beginning of the body ends inside the body', () => {
  396. editor.conversion.for( 'downcast' ).markerToElement( { model: 'comment', view: 'comment' } );
  397. setData( model,
  398. '<title><title-content>Foo</title-content></title>' +
  399. '<paragraph>Bar</paragraph>'
  400. );
  401. const body = model.document.getRoot().getChild( 1 );
  402. model.change( writer => {
  403. writer.addMarker( 'comment', {
  404. range: model.createRange( model.createPositionAt( body, 0 ), model.createPositionAt( body, 2 ) ),
  405. usingOperation: true
  406. } );
  407. } );
  408. expect( editor.plugins.get( 'Title' ).getBody() ).to.equal(
  409. '<p><comment></comment>Ba<comment></comment>r</p>'
  410. );
  411. } );
  412. it( 'should do nothing when marker is fully out of the body range', () => {
  413. editor.conversion.for( 'downcast' ).markerToElement( { model: 'comment', view: 'comment' } );
  414. setData( model,
  415. '<title><title-content>Foo</title-content></title>' +
  416. '<paragraph>Bar</paragraph>'
  417. );
  418. const title = model.document.getRoot().getChild( 0 ).getChild( 0 );
  419. model.change( writer => {
  420. writer.addMarker( 'comment', {
  421. range: model.createRange( model.createPositionAt( title, 1 ), model.createPositionAt( title, 2 ) ),
  422. usingOperation: true
  423. } );
  424. } );
  425. expect( editor.plugins.get( 'Title' ).getBody() ).to.equal( '<p>Bar</p>' );
  426. } );
  427. } );
  428. describe( 'placeholders', () => {
  429. let viewRoot;
  430. beforeEach( () => {
  431. viewRoot = editor.editing.view.document.getRoot();
  432. } );
  433. it( 'should attach placeholder placeholder to title and body', () => {
  434. setData( model,
  435. '<title><title-content>Foo</title-content></title>' +
  436. '<paragraph>Bar</paragraph>'
  437. );
  438. const title = viewRoot.getChild( 0 );
  439. const body = viewRoot.getChild( 1 );
  440. expect( title.getAttribute( 'data-placeholder' ) ).to.equal( 'Type your title' );
  441. expect( body.getAttribute( 'data-placeholder' ) ).to.equal( 'Type or paste your content here.' );
  442. expect( title.hasClass( 'ck-placeholder' ) ).to.equal( false );
  443. expect( body.hasClass( 'ck-placeholder' ) ).to.equal( false );
  444. } );
  445. it( 'should show placeholder in empty title and body', () => {
  446. setData( model,
  447. '<title><title-content></title-content></title>' +
  448. '<paragraph></paragraph>'
  449. );
  450. const title = viewRoot.getChild( 0 );
  451. const body = viewRoot.getChild( 1 );
  452. expect( title.getAttribute( 'data-placeholder' ) ).to.equal( 'Type your title' );
  453. expect( body.getAttribute( 'data-placeholder' ) ).to.equal( 'Type or paste your content here.' );
  454. expect( title.hasClass( 'ck-placeholder' ) ).to.equal( true );
  455. expect( body.hasClass( 'ck-placeholder' ) ).to.equal( true );
  456. } );
  457. it( 'should hide placeholder from body with more than one child elements', () => {
  458. setData( editor.model,
  459. '<title><title-content>Foo</title-content></title>' +
  460. '<paragraph></paragraph>' +
  461. '<paragraph></paragraph>'
  462. );
  463. const body = viewRoot.getChild( 1 );
  464. expect( body.getAttribute( 'data-placeholder' ) ).to.equal( 'Type or paste your content here.' );
  465. expect( body.hasClass( 'ck-placeholder' ) ).to.equal( false );
  466. } );
  467. it( 'should hide placeholder from body with element other than paragraph', () => {
  468. setData( editor.model,
  469. '<title><title-content>Foo</title-content></title>' +
  470. '<heading1></heading1>'
  471. );
  472. const body = viewRoot.getChild( 1 );
  473. expect( body.hasAttribute( 'data-placeholder' ) ).to.equal( true );
  474. expect( body.hasClass( 'ck-placeholder' ) ).to.equal( false );
  475. } );
  476. it( 'should hide placeholder when title element become not empty', () => {
  477. setData( model,
  478. '<title><title-content></title-content></title>' +
  479. '<paragraph>[]</paragraph>'
  480. );
  481. expect( viewRoot.getChild( 0 ).hasClass( 'ck-placeholder' ) ).to.equal( true );
  482. model.change( writer => {
  483. writer.appendText( 'Bar', null, model.document.getRoot().getChild( 0 ).getChild( 0 ) );
  484. } );
  485. expect( viewRoot.getChild( 0 ).hasClass( 'ck-placeholder' ) ).to.equal( false );
  486. } );
  487. it( 'should hide placeholder when body element become not empty', () => {
  488. setData( model,
  489. '<title><title-content>Foo</title-content></title>' +
  490. '<paragraph></paragraph>'
  491. );
  492. expect( viewRoot.getChild( 1 ).hasClass( 'ck-placeholder' ) ).to.equal( true );
  493. model.change( writer => {
  494. writer.appendText( 'Bar', null, model.document.getRoot().getChild( 1 ) );
  495. } );
  496. expect( viewRoot.getChild( 1 ).hasClass( 'ck-placeholder' ) ).to.equal( false );
  497. } );
  498. it( 'should properly map the body placeholder in DOM when undoing', () => {
  499. const viewRoot = editor.editing.view.document.getRoot();
  500. const domConverter = editor.editing.view.domConverter;
  501. let bodyDomElement;
  502. setData( editor.model,
  503. '<title><title-content>[Foo</title-content></title>' +
  504. '<paragraph>Bar</paragraph>' +
  505. '<paragraph>Baz]</paragraph>'
  506. );
  507. editor.model.deleteContent( editor.model.document.selection );
  508. bodyDomElement = domConverter.mapViewToDom( viewRoot.getChild( 1 ) );
  509. expect( bodyDomElement.dataset.placeholder ).to.equal( 'Type or paste your content here.' );
  510. expect( bodyDomElement.classList.contains( 'ck-placeholder' ) ).to.equal( true );
  511. editor.execute( 'undo' );
  512. bodyDomElement = domConverter.mapViewToDom( viewRoot.getChild( 1 ) );
  513. expect( bodyDomElement.dataset.placeholder ).to.equal( 'Type or paste your content here.' );
  514. expect( bodyDomElement.classList.contains( 'ck-placeholder' ) ).to.equal( false );
  515. } );
  516. describe( 'custom placeholder defined using configuration', () => {
  517. let element, editor, model;
  518. beforeEach( () => {
  519. element = document.createElement( 'div' );
  520. document.body.appendChild( element );
  521. return ClassicTestEditor.create( element, {
  522. plugins: [ Title, Heading, BlockQuote, Clipboard, Image, ImageUpload, Enter, Undo ],
  523. title: {
  524. placeholder: 'foo'
  525. },
  526. placeholder: 'bar'
  527. } ).then( _editor => {
  528. editor = _editor;
  529. model = editor.model;
  530. } );
  531. } );
  532. afterEach( () => {
  533. return editor.destroy().then( () => element.remove() );
  534. } );
  535. it( 'should show custom placeholder in title and body', () => {
  536. const viewRoot = editor.editing.view.document.getRoot();
  537. setData( model,
  538. '<title><title-content></title-content></title>' +
  539. '<paragraph></paragraph>'
  540. );
  541. const title = viewRoot.getChild( 0 );
  542. const body = viewRoot.getChild( 1 );
  543. expect( title.getAttribute( 'data-placeholder' ) ).to.equal( 'foo' );
  544. expect( body.getAttribute( 'data-placeholder' ) ).to.equal( 'bar' );
  545. expect( title.hasClass( 'ck-placeholder' ) ).to.equal( true );
  546. expect( body.hasClass( 'ck-placeholder' ) ).to.equal( true );
  547. } );
  548. } );
  549. describe( 'custom placeholder defined using textarea attribte', () => {
  550. let element, editor, model;
  551. beforeEach( () => {
  552. element = document.createElement( 'textarea' );
  553. element.setAttribute( 'placeholder', 'bom' );
  554. document.body.appendChild( element );
  555. return ClassicTestEditor.create( element, {
  556. plugins: [ Title, Heading, BlockQuote, Clipboard, Image, ImageUpload, Enter, Undo ],
  557. title: {
  558. placeholder: 'foo'
  559. }
  560. } ).then( _editor => {
  561. editor = _editor;
  562. model = editor.model;
  563. } );
  564. } );
  565. afterEach( () => {
  566. return editor.destroy().then( () => element.remove() );
  567. } );
  568. it( 'should show custom placeholder in title and body', () => {
  569. const viewRoot = editor.editing.view.document.getRoot();
  570. setData( model,
  571. '<title><title-content></title-content></title>' +
  572. '<paragraph></paragraph>'
  573. );
  574. const title = viewRoot.getChild( 0 );
  575. const body = viewRoot.getChild( 1 );
  576. expect( title.getAttribute( 'data-placeholder' ) ).to.equal( 'foo' );
  577. expect( body.getAttribute( 'data-placeholder' ) ).to.equal( 'bom' );
  578. expect( title.hasClass( 'ck-placeholder' ) ).to.equal( true );
  579. expect( body.hasClass( 'ck-placeholder' ) ).to.equal( true );
  580. } );
  581. } );
  582. } );
  583. describe( 'Tab press handling', () => {
  584. it( 'should handle tab key when the selection is at the beginning of the title', () => {
  585. setData( model,
  586. '<title><title-content>[]foo</title-content></title>' +
  587. '<paragraph>bar</paragraph>'
  588. );
  589. const eventData = getEventData( keyCodes.tab );
  590. editor.keystrokes.press( eventData );
  591. sinon.assert.calledOnce( eventData.preventDefault );
  592. sinon.assert.calledOnce( eventData.stopPropagation );
  593. expect( getData( model ) ).to.equal(
  594. '<title><title-content>foo</title-content></title>' +
  595. '<paragraph>[]bar</paragraph>'
  596. );
  597. } );
  598. it( 'should handle tab key when the selection is at the end of the title', () => {
  599. setData( model,
  600. '<title><title-content>foo[]</title-content></title>' +
  601. '<paragraph>bar</paragraph>'
  602. );
  603. const eventData = getEventData( keyCodes.tab );
  604. editor.keystrokes.press( eventData );
  605. sinon.assert.calledOnce( eventData.preventDefault );
  606. sinon.assert.calledOnce( eventData.stopPropagation );
  607. expect( getData( model ) ).to.equal(
  608. '<title><title-content>foo</title-content></title>' +
  609. '<paragraph>[]bar</paragraph>'
  610. );
  611. } );
  612. it( 'should not handle tab key when the selection is in the title and body', () => {
  613. setData( model,
  614. '<title><title-content>fo[o</title-content></title>' +
  615. '<paragraph>b]ar</paragraph>'
  616. );
  617. const eventData = getEventData( keyCodes.tab );
  618. editor.keystrokes.press( eventData );
  619. sinon.assert.notCalled( eventData.preventDefault );
  620. sinon.assert.notCalled( eventData.stopPropagation );
  621. expect( getData( model ) ).to.equal(
  622. '<title><title-content>fo[o</title-content></title>' +
  623. '<paragraph>b]ar</paragraph>'
  624. );
  625. } );
  626. it( 'should not handle tab key when the selection is in the body', () => {
  627. setData( model,
  628. '<title><title-content>foo</title-content></title>' +
  629. '<paragraph>[]bar</paragraph>'
  630. );
  631. const eventData = getEventData( keyCodes.tab );
  632. editor.keystrokes.press( eventData );
  633. sinon.assert.notCalled( eventData.preventDefault );
  634. sinon.assert.notCalled( eventData.stopPropagation );
  635. expect( getData( model ) ).to.equal(
  636. '<title><title-content>foo</title-content></title>' +
  637. '<paragraph>[]bar</paragraph>'
  638. );
  639. } );
  640. } );
  641. describe( 'Shift + Tab press handling', () => {
  642. it( 'should handle shift + tab keys when the selection is at the beginning of the body', () => {
  643. setData( model,
  644. '<title><title-content>foo</title-content></title>' +
  645. '<paragraph>[]bar</paragraph>'
  646. );
  647. const eventData = getEventData( keyCodes.tab, { shiftKey: true } );
  648. editor.keystrokes.press( eventData );
  649. sinon.assert.calledOnce( eventData.preventDefault );
  650. sinon.assert.calledOnce( eventData.stopPropagation );
  651. expect( getData( model ) ).to.equal(
  652. '<title><title-content>[]foo</title-content></title>' +
  653. '<paragraph>bar</paragraph>'
  654. );
  655. } );
  656. it( 'should not handle shift + tab keys when the selection is not at the beginning of the body', () => {
  657. setData( model,
  658. '<title><title-content>foo</title-content></title>' +
  659. '<paragraph>b[]ar</paragraph>'
  660. );
  661. const eventData = getEventData( keyCodes.tab, { shiftKey: true } );
  662. editor.keystrokes.press( eventData );
  663. sinon.assert.notCalled( eventData.preventDefault );
  664. sinon.assert.notCalled( eventData.stopPropagation );
  665. expect( getData( model ) ).to.equal(
  666. '<title><title-content>foo</title-content></title>' +
  667. '<paragraph>b[]ar</paragraph>'
  668. );
  669. } );
  670. it( 'should not handle shift + tab keys when the selection is not collapsed', () => {
  671. setData( model,
  672. '<title><title-content>foo</title-content></title>' +
  673. '<paragraph>[b]ar</paragraph>'
  674. );
  675. const eventData = getEventData( keyCodes.tab, { shiftKey: true } );
  676. editor.keystrokes.press( eventData );
  677. sinon.assert.notCalled( eventData.preventDefault );
  678. sinon.assert.notCalled( eventData.stopPropagation );
  679. expect( getData( model ) ).to.equal(
  680. '<title><title-content>foo</title-content></title>' +
  681. '<paragraph>[b]ar</paragraph>'
  682. );
  683. } );
  684. it( 'should not handle shift + tab keys when the selection is in the title', () => {
  685. setData( model,
  686. '<title><title-content>[]foo</title-content></title>' +
  687. '<paragraph>bar</paragraph>'
  688. );
  689. const eventData = getEventData( keyCodes.tab, { shiftKey: true } );
  690. editor.keystrokes.press( eventData );
  691. sinon.assert.notCalled( eventData.preventDefault );
  692. sinon.assert.notCalled( eventData.stopPropagation );
  693. expect( getData( model ) ).to.equal(
  694. '<title><title-content>[]foo</title-content></title>' +
  695. '<paragraph>bar</paragraph>'
  696. );
  697. } );
  698. } );
  699. } );
  700. function getEventData( keyCode, { shiftKey = false } = {} ) {
  701. return {
  702. keyCode,
  703. shiftKey,
  704. preventDefault: sinon.spy(),
  705. stopPropagation: sinon.spy()
  706. };
  707. }