8
0

blockquotecommand.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import BlockQuoteEngine from '../src/blockquoteengine';
  6. import BlockQuoteCommand from '../src/blockquotecommand';
  7. import { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
  8. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  9. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  11. import Command from '@ckeditor/ckeditor5-core/src/command';
  12. describe( 'BlockQuoteCommand', () => {
  13. let editor, model, command;
  14. beforeEach( () => {
  15. return VirtualTestEditor
  16. .create( {
  17. plugins: [ BlockQuoteEngine ]
  18. } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. model = editor.model;
  22. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  23. model.schema.register( 'heading', { inheritAllFrom: '$block' } );
  24. model.schema.register( 'widget' );
  25. model.schema.extend( 'widget', {
  26. allowIn: '$root',
  27. isLimit: true
  28. } );
  29. model.schema.extend( '$text', { allowIn: 'widget' } );
  30. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'paragraph', view: 'p' } ) );
  31. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'heading', view: 'h' } ) );
  32. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'widget', view: 'widget' } ) );
  33. command = editor.commands.get( 'blockQuote' );
  34. } );
  35. } );
  36. afterEach( () => {
  37. editor.destroy();
  38. } );
  39. it( 'is a command', () => {
  40. expect( BlockQuoteCommand.prototype ).to.be.instanceOf( Command );
  41. expect( command ).to.be.instanceOf( Command );
  42. } );
  43. describe( 'value', () => {
  44. it( 'is false when selection is not in a block quote', () => {
  45. setModelData( model, '<paragraph>x[]x</paragraph>' );
  46. expect( command ).to.have.property( 'value', false );
  47. } );
  48. it( 'is false when start of the selection is not in a block quote', () => {
  49. setModelData( model, '<paragraph>x[x</paragraph><blockQuote><paragraph>y]y</paragraph></blockQuote>' );
  50. expect( command ).to.have.property( 'value', false );
  51. } );
  52. it( 'is false when selection starts in a blockless space', () => {
  53. model.schema.extend( '$text', { allowIn: '$root' } );
  54. setModelData( model, 'x[]x' );
  55. expect( command ).to.have.property( 'value', false );
  56. } );
  57. it( 'is true when selection is in a block quote', () => {
  58. setModelData( model, '<blockQuote><paragraph>x[]x</paragraph></blockQuote>' );
  59. expect( command ).to.have.property( 'value', true );
  60. } );
  61. it( 'is true when selection starts in a block quote', () => {
  62. setModelData( model, '<blockQuote><paragraph>x[x</paragraph></blockQuote><paragraph>y]y</paragraph>' );
  63. expect( command ).to.have.property( 'value', true );
  64. } );
  65. } );
  66. describe( 'isEnabled', () => {
  67. it( 'is true when selection is in a block which can be wrapped with blockQuote', () => {
  68. setModelData( model, '<paragraph>x[]x</paragraph>' );
  69. expect( command ).to.have.property( 'isEnabled', true );
  70. } );
  71. it( 'is true when selection is in a block which is already in blockQuote', () => {
  72. setModelData( model, '<blockQuote><paragraph>x[]x</paragraph></blockQuote>' );
  73. expect( command ).to.have.property( 'isEnabled', true );
  74. } );
  75. it( 'is true when selection starts in a block which can be wrapped with blockQuote', () => {
  76. setModelData( model, '<paragraph>x[x</paragraph><widget>y]y</widget>' );
  77. expect( command ).to.have.property( 'isEnabled', true );
  78. } );
  79. it( 'is false when selection is in an element which cannot be wrapped with blockQuote (because it cannot be its child)', () => {
  80. setModelData( model, '<widget>x[]x</widget>' );
  81. expect( command ).to.have.property( 'isEnabled', false );
  82. } );
  83. it(
  84. 'is false when selection is in an element which cannot be wrapped with blockQuote' +
  85. '(because mQ is not allowed in its parent)',
  86. () => {
  87. model.schema.addChildCheck( ( ctx, childDef ) => {
  88. if ( childDef.name == 'blockQuote' ) {
  89. return false;
  90. }
  91. } );
  92. setModelData( model, '<paragraph>x[]x</paragraph>' );
  93. expect( command ).to.have.property( 'isEnabled', false );
  94. }
  95. );
  96. // https://github.com/ckeditor/ckeditor5-engine/issues/826
  97. // it( 'is false when selection starts in an element which cannot be wrapped with blockQuote', () => {
  98. // setModelData( model, '<widget>x[x</widget><paragraph>y]y</paragraph>' );
  99. // expect( command ).to.have.property( 'isEnabled', false );
  100. // } );
  101. } );
  102. describe( 'execute()', () => {
  103. describe( 'applying quote', () => {
  104. it( 'should wrap a single block', () => {
  105. setModelData(
  106. model,
  107. '<paragraph>abc</paragraph>' +
  108. '<paragraph>x[]x</paragraph>' +
  109. '<paragraph>def</paragraph>'
  110. );
  111. editor.execute( 'blockQuote' );
  112. expect( getModelData( model ) ).to.equal(
  113. '<paragraph>abc</paragraph>' +
  114. '<blockQuote><paragraph>x[]x</paragraph></blockQuote>' +
  115. '<paragraph>def</paragraph>'
  116. );
  117. expect( getViewData( editor.editing.view ) ).to.equal(
  118. '<p>abc</p><blockquote><p>x{}x</p></blockquote><p>def</p>'
  119. );
  120. } );
  121. it( 'should wrap multiple blocks', () => {
  122. setModelData(
  123. model,
  124. '<heading>a[bc</heading>' +
  125. '<paragraph>xx</paragraph>' +
  126. '<paragraph>de]f</paragraph>'
  127. );
  128. editor.execute( 'blockQuote' );
  129. expect( getModelData( model ) ).to.equal(
  130. '<blockQuote>' +
  131. '<heading>a[bc</heading>' +
  132. '<paragraph>xx</paragraph>' +
  133. '<paragraph>de]f</paragraph>' +
  134. '</blockQuote>'
  135. );
  136. expect( getViewData( editor.editing.view ) ).to.equal(
  137. '<blockquote><h>a{bc</h><p>xx</p><p>de}f</p></blockquote>'
  138. );
  139. } );
  140. it( 'should merge with an existing quote', () => {
  141. setModelData(
  142. model,
  143. '<heading>a[bc</heading>' +
  144. '<blockQuote><paragraph>x]x</paragraph><paragraph>yy</paragraph></blockQuote>' +
  145. '<paragraph>def</paragraph>'
  146. );
  147. editor.execute( 'blockQuote' );
  148. expect( getModelData( model ) ).to.equal(
  149. '<blockQuote>' +
  150. '<heading>a[bc</heading>' +
  151. '<paragraph>x]x</paragraph>' +
  152. '<paragraph>yy</paragraph>' +
  153. '</blockQuote>' +
  154. '<paragraph>def</paragraph>'
  155. );
  156. expect( getViewData( editor.editing.view ) ).to.equal(
  157. '<blockquote><h>a{bc</h><p>x}x</p><p>yy</p></blockquote><p>def</p>'
  158. );
  159. } );
  160. it( 'should not merge with a quote preceding the current block', () => {
  161. setModelData(
  162. model,
  163. '<blockQuote><paragraph>abc</paragraph></blockQuote>' +
  164. '<paragraph>x[]x</paragraph>'
  165. );
  166. editor.execute( 'blockQuote' );
  167. expect( getModelData( model ) ).to.equal(
  168. '<blockQuote><paragraph>abc</paragraph></blockQuote>' +
  169. '<blockQuote><paragraph>x[]x</paragraph></blockQuote>'
  170. );
  171. expect( getViewData( editor.editing.view ) ).to.equal(
  172. '<blockquote><p>abc</p></blockquote>' +
  173. '<blockquote><p>x{}x</p></blockquote>'
  174. );
  175. } );
  176. it( 'should not merge with a quote following the current block', () => {
  177. setModelData(
  178. model,
  179. '<paragraph>x[]x</paragraph>' +
  180. '<blockQuote><paragraph>abc</paragraph></blockQuote>'
  181. );
  182. editor.execute( 'blockQuote' );
  183. expect( getModelData( model ) ).to.equal(
  184. '<blockQuote><paragraph>x[]x</paragraph></blockQuote>' +
  185. '<blockQuote><paragraph>abc</paragraph></blockQuote>'
  186. );
  187. expect( getViewData( editor.editing.view ) ).to.equal(
  188. '<blockquote><p>x{}x</p></blockquote>' +
  189. '<blockquote><p>abc</p></blockquote>'
  190. );
  191. } );
  192. it( 'should merge with an existing quote (more blocks)', () => {
  193. setModelData(
  194. model,
  195. '<heading>a[bc</heading>' +
  196. '<paragraph>def</paragraph>' +
  197. '<blockQuote><paragraph>x]x</paragraph></blockQuote>' +
  198. '<paragraph>ghi</paragraph>'
  199. );
  200. editor.execute( 'blockQuote' );
  201. expect( getModelData( model ) ).to.equal(
  202. '<blockQuote>' +
  203. '<heading>a[bc</heading>' +
  204. '<paragraph>def</paragraph>' +
  205. '<paragraph>x]x</paragraph>' +
  206. '</blockQuote>' +
  207. '<paragraph>ghi</paragraph>'
  208. );
  209. expect( getViewData( editor.editing.view ) ).to.equal(
  210. '<blockquote><h>a{bc</h><p>def</p><p>x}x</p></blockquote><p>ghi</p>'
  211. );
  212. } );
  213. it( 'should not wrap non-block content', () => {
  214. setModelData(
  215. model,
  216. '<paragraph>a[bc</paragraph>' +
  217. '<widget>xx</widget>' +
  218. '<paragraph>de]f</paragraph>'
  219. );
  220. editor.execute( 'blockQuote' );
  221. expect( getModelData( model ) ).to.equal(
  222. '<blockQuote>' +
  223. '<paragraph>a[bc</paragraph>' +
  224. '</blockQuote>' +
  225. '<widget>xx</widget>' +
  226. '<blockQuote>' +
  227. '<paragraph>de]f</paragraph>' +
  228. '</blockQuote>'
  229. );
  230. expect( getViewData( editor.editing.view ) ).to.equal(
  231. '<blockquote><p>a{bc</p></blockquote><widget>xx</widget><blockquote><p>de}f</p></blockquote>'
  232. );
  233. } );
  234. it( 'should correctly wrap and merge groups of blocks', () => {
  235. setModelData(
  236. model,
  237. '<paragraph>a[bc</paragraph>' +
  238. '<widget>xx</widget>' +
  239. '<paragraph>def</paragraph>' +
  240. '<blockQuote><paragraph>ghi</paragraph></blockQuote>' +
  241. '<widget>yy</widget>' +
  242. '<paragraph>jk]l</paragraph>'
  243. );
  244. editor.execute( 'blockQuote' );
  245. expect( getModelData( model ) ).to.equal(
  246. '<blockQuote><paragraph>a[bc</paragraph></blockQuote>' +
  247. '<widget>xx</widget>' +
  248. '<blockQuote><paragraph>def</paragraph><paragraph>ghi</paragraph></blockQuote>' +
  249. '<widget>yy</widget>' +
  250. '<blockQuote><paragraph>jk]l</paragraph></blockQuote>'
  251. );
  252. expect( getViewData( editor.editing.view ) ).to.equal(
  253. '<blockquote><p>a{bc</p></blockquote>' +
  254. '<widget>xx</widget>' +
  255. '<blockquote><p>def</p><p>ghi</p></blockquote>' +
  256. '<widget>yy</widget>' +
  257. '<blockquote><p>jk}l</p></blockquote>'
  258. );
  259. } );
  260. it( 'should correctly merge a couple of subsequent quotes', () => {
  261. setModelData(
  262. model,
  263. '<paragraph>x</paragraph>' +
  264. '<paragraph>a[bc</paragraph>' +
  265. '<blockQuote><paragraph>def</paragraph></blockQuote>' +
  266. '<paragraph>ghi</paragraph>' +
  267. '<blockQuote><paragraph>jkl</paragraph></blockQuote>' +
  268. '<paragraph>mn]o</paragraph>' +
  269. '<paragraph>y</paragraph>'
  270. );
  271. editor.execute( 'blockQuote' );
  272. expect( getModelData( model ) ).to.equal(
  273. '<paragraph>x</paragraph>' +
  274. '<blockQuote>' +
  275. '<paragraph>a[bc</paragraph>' +
  276. '<paragraph>def</paragraph>' +
  277. '<paragraph>ghi</paragraph>' +
  278. '<paragraph>jkl</paragraph>' +
  279. '<paragraph>mn]o</paragraph>' +
  280. '</blockQuote>' +
  281. '<paragraph>y</paragraph>'
  282. );
  283. expect( getViewData( editor.editing.view ) ).to.equal(
  284. '<p>x</p>' +
  285. '<blockquote>' +
  286. '<p>a{bc</p>' +
  287. '<p>def</p>' +
  288. '<p>ghi</p>' +
  289. '<p>jkl</p>' +
  290. '<p>mn}o</p>' +
  291. '</blockquote>' +
  292. '<p>y</p>'
  293. );
  294. } );
  295. it( 'should not wrap a block which can not be in a quote', () => {
  296. // blockQuote is allowed in root, but fooBlock can not be inside blockQuote.
  297. model.schema.register( 'fooBlock', { inheritAllFrom: '$block' } );
  298. model.schema.addChildCheck( ( ctx, childDef ) => {
  299. if ( ctx.endsWith( 'blockQuote' ) && childDef.name == 'fooBlock' ) {
  300. return false;
  301. }
  302. } );
  303. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'fooBlock', view: 'fooblock' } ) );
  304. setModelData(
  305. model,
  306. '<paragraph>a[bc</paragraph>' +
  307. '<fooBlock>xx</fooBlock>' +
  308. '<paragraph>de]f</paragraph>'
  309. );
  310. editor.execute( 'blockQuote' );
  311. expect( getModelData( model ) ).to.equal(
  312. '<blockQuote>' +
  313. '<paragraph>a[bc</paragraph>' +
  314. '</blockQuote>' +
  315. '<fooBlock>xx</fooBlock>' +
  316. '<blockQuote>' +
  317. '<paragraph>de]f</paragraph>' +
  318. '</blockQuote>'
  319. );
  320. } );
  321. it( 'should not wrap a block which parent does not allow quote inside itself', () => {
  322. // blockQuote is not be allowed in fooWrapper, but fooBlock can be inside blockQuote.
  323. model.schema.register( 'fooWrapper' );
  324. model.schema.register( 'fooBlock', { inheritAllFrom: '$block' } );
  325. model.schema.extend( 'fooWrapper', { allowIn: '$root' } );
  326. model.schema.extend( 'fooBlock', { allowIn: 'fooWrapper' } );
  327. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'fooWrapper', view: 'foowrapper' } ) );
  328. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'fooBlock', view: 'fooblock' } ) );
  329. setModelData(
  330. model,
  331. '<paragraph>a[bc</paragraph>' +
  332. '<fooWrapper><fooBlock>xx</fooBlock></fooWrapper>' +
  333. '<paragraph>de]f</paragraph>'
  334. );
  335. editor.execute( 'blockQuote' );
  336. expect( getModelData( model ) ).to.equal(
  337. '<blockQuote>' +
  338. '<paragraph>a[bc</paragraph>' +
  339. '</blockQuote>' +
  340. '<fooWrapper><fooBlock>xx</fooBlock></fooWrapper>' +
  341. '<blockQuote>' +
  342. '<paragraph>de]f</paragraph>' +
  343. '</blockQuote>'
  344. );
  345. } );
  346. } );
  347. describe( 'removing quote', () => {
  348. it( 'should unwrap a single block', () => {
  349. setModelData(
  350. model,
  351. '<paragraph>abc</paragraph>' +
  352. '<blockQuote><paragraph>x[]x</paragraph></blockQuote>' +
  353. '<paragraph>def</paragraph>'
  354. );
  355. editor.execute( 'blockQuote' );
  356. expect( getModelData( model ) ).to.equal(
  357. '<paragraph>abc</paragraph>' +
  358. '<paragraph>x[]x</paragraph>' +
  359. '<paragraph>def</paragraph>'
  360. );
  361. expect( getViewData( editor.editing.view ) ).to.equal(
  362. '<p>abc</p><p>x{}x</p><p>def</p>'
  363. );
  364. } );
  365. it( 'should unwrap multiple blocks', () => {
  366. setModelData(
  367. model,
  368. '<blockQuote>' +
  369. '<paragraph>a[bc</paragraph>' +
  370. '<paragraph>xx</paragraph>' +
  371. '<paragraph>de]f</paragraph>' +
  372. '</blockQuote>'
  373. );
  374. editor.execute( 'blockQuote' );
  375. expect( getModelData( model ) ).to.equal(
  376. '<paragraph>a[bc</paragraph>' +
  377. '<paragraph>xx</paragraph>' +
  378. '<paragraph>de]f</paragraph>'
  379. );
  380. expect( getViewData( editor.editing.view ) ).to.equal(
  381. '<p>a{bc</p><p>xx</p><p>de}f</p>'
  382. );
  383. } );
  384. it( 'should unwrap only the selected blocks - at the beginning', () => {
  385. setModelData(
  386. model,
  387. '<paragraph>xx</paragraph>' +
  388. '<blockQuote>' +
  389. '<paragraph>a[b]c</paragraph>' +
  390. '<paragraph>xx</paragraph>' +
  391. '</blockQuote>' +
  392. '<paragraph>yy</paragraph>'
  393. );
  394. editor.execute( 'blockQuote' );
  395. expect( getModelData( model ) ).to.equal(
  396. '<paragraph>xx</paragraph>' +
  397. '<paragraph>a[b]c</paragraph>' +
  398. '<blockQuote>' +
  399. '<paragraph>xx</paragraph>' +
  400. '</blockQuote>' +
  401. '<paragraph>yy</paragraph>'
  402. );
  403. expect( getViewData( editor.editing.view ) ).to.equal(
  404. '<p>xx</p><p>a{b}c</p><blockquote><p>xx</p></blockquote><p>yy</p>'
  405. );
  406. } );
  407. it( 'should unwrap only the selected blocks - at the end', () => {
  408. setModelData(
  409. model,
  410. '<blockQuote>' +
  411. '<paragraph>abc</paragraph>' +
  412. '<paragraph>x[x</paragraph>' +
  413. '</blockQuote>' +
  414. '<paragraph>de]f</paragraph>'
  415. );
  416. editor.execute( 'blockQuote' );
  417. expect( getModelData( model ) ).to.equal(
  418. '<blockQuote>' +
  419. '<paragraph>abc</paragraph>' +
  420. '</blockQuote>' +
  421. '<paragraph>x[x</paragraph>' +
  422. '<paragraph>de]f</paragraph>'
  423. );
  424. expect( getViewData( editor.editing.view ) ).to.equal(
  425. '<blockquote><p>abc</p></blockquote><p>x{x</p><p>de}f</p>'
  426. );
  427. } );
  428. it( 'should unwrap only the selected blocks - in the middle', () => {
  429. setModelData(
  430. model,
  431. '<paragraph>xx</paragraph>' +
  432. '<blockQuote>' +
  433. '<paragraph>abc</paragraph>' +
  434. '<paragraph>c[]de</paragraph>' +
  435. '<paragraph>fgh</paragraph>' +
  436. '</blockQuote>' +
  437. '<paragraph>xx</paragraph>'
  438. );
  439. editor.execute( 'blockQuote' );
  440. expect( getModelData( model ) ).to.equal(
  441. '<paragraph>xx</paragraph>' +
  442. '<blockQuote><paragraph>abc</paragraph></blockQuote>' +
  443. '<paragraph>c[]de</paragraph>' +
  444. '<blockQuote><paragraph>fgh</paragraph></blockQuote>' +
  445. '<paragraph>xx</paragraph>'
  446. );
  447. expect( getViewData( editor.editing.view ) ).to.equal(
  448. '<p>xx</p>' +
  449. '<blockquote><p>abc</p></blockquote>' +
  450. '<p>c{}de</p>' +
  451. '<blockquote><p>fgh</p></blockquote>' +
  452. '<p>xx</p>'
  453. );
  454. } );
  455. it( 'should remove multiple quotes', () => {
  456. setModelData(
  457. model,
  458. '<blockQuote><paragraph>a[bc</paragraph></blockQuote>' +
  459. '<paragraph>xx</paragraph>' +
  460. '<blockQuote><paragraph>def</paragraph><paragraph>ghi</paragraph></blockQuote>' +
  461. '<paragraph>yy</paragraph>' +
  462. '<blockQuote><paragraph>de]f</paragraph><paragraph>ghi</paragraph></blockQuote>'
  463. );
  464. editor.execute( 'blockQuote' );
  465. expect( getModelData( model ) ).to.equal(
  466. '<paragraph>a[bc</paragraph>' +
  467. '<paragraph>xx</paragraph>' +
  468. '<paragraph>def</paragraph><paragraph>ghi</paragraph>' +
  469. '<paragraph>yy</paragraph>' +
  470. '<paragraph>de]f</paragraph>' +
  471. '<blockQuote><paragraph>ghi</paragraph></blockQuote>'
  472. );
  473. expect( getViewData( editor.editing.view ) ).to.equal(
  474. '<p>a{bc</p>' +
  475. '<p>xx</p>' +
  476. '<p>def</p><p>ghi</p>' +
  477. '<p>yy</p>' +
  478. '<p>de}f</p>' +
  479. '<blockquote><p>ghi</p></blockquote>'
  480. );
  481. } );
  482. } );
  483. } );
  484. } );