title.js 28 KB

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