blockquotecommand.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /**
  2. * @license Copyright (c) 2003-2017, 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 buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  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.registerItem( 'paragraph', '$block' );
  23. model.schema.registerItem( 'heading', '$block' );
  24. model.schema.registerItem( 'widget' );
  25. model.schema.allow( { name: 'widget', inside: '$root' } );
  26. model.schema.allow( { name: '$text', inside: 'widget' } );
  27. model.schema.limits.add( 'widget' );
  28. buildModelConverter().for( editor.editing.modelToView )
  29. .fromElement( 'paragraph' )
  30. .toElement( 'p' );
  31. buildModelConverter().for( editor.editing.modelToView )
  32. .fromElement( 'heading' )
  33. .toElement( 'h' );
  34. buildModelConverter().for( editor.editing.modelToView )
  35. .fromElement( 'widget' )
  36. .toElement( 'widget' );
  37. command = editor.commands.get( 'blockQuote' );
  38. } );
  39. } );
  40. afterEach( () => {
  41. editor.destroy();
  42. } );
  43. it( 'is a command', () => {
  44. expect( BlockQuoteCommand.prototype ).to.be.instanceOf( Command );
  45. expect( command ).to.be.instanceOf( Command );
  46. } );
  47. describe( 'value', () => {
  48. it( 'is false when selection is not in a block quote', () => {
  49. setModelData( model, '<paragraph>x[]x</paragraph>' );
  50. expect( command ).to.have.property( 'value', false );
  51. } );
  52. it( 'is false when start of the selection is not in a block quote', () => {
  53. setModelData( model, '<paragraph>x[x</paragraph><blockQuote><paragraph>y]y</paragraph></blockQuote>' );
  54. expect( command ).to.have.property( 'value', false );
  55. } );
  56. it( 'is false when selection starts in a blockless space', () => {
  57. model.schema.allow( { name: '$text', inside: '$root' } );
  58. setModelData( model, 'x[]x' );
  59. expect( command ).to.have.property( 'value', false );
  60. } );
  61. it( 'is true when selection is in a block quote', () => {
  62. setModelData( model, '<blockQuote><paragraph>x[]x</paragraph></blockQuote>' );
  63. expect( command ).to.have.property( 'value', true );
  64. } );
  65. it( 'is true when selection starts in a block quote', () => {
  66. setModelData( model, '<blockQuote><paragraph>x[x</paragraph></blockQuote><paragraph>y]y</paragraph>' );
  67. expect( command ).to.have.property( 'value', true );
  68. } );
  69. } );
  70. describe( 'isEnabled', () => {
  71. it( 'is true when selection is in a block which can be wrapped with blockQuote', () => {
  72. setModelData( model, '<paragraph>x[]x</paragraph>' );
  73. expect( command ).to.have.property( 'isEnabled', true );
  74. } );
  75. it( 'is true when selection is in a block which is already in blockQuote', () => {
  76. setModelData( model, '<blockQuote><paragraph>x[]x</paragraph></blockQuote>' );
  77. expect( command ).to.have.property( 'isEnabled', true );
  78. } );
  79. it( 'is true when selection starts in a block which can be wrapped with blockQuote', () => {
  80. setModelData( model, '<paragraph>x[x</paragraph><widget>y]y</widget>' );
  81. expect( command ).to.have.property( 'isEnabled', true );
  82. } );
  83. it( 'is false when selection is in an element which cannot be wrapped with blockQuote (because it cannot be its child)', () => {
  84. setModelData( model, '<widget>x[]x</widget>' );
  85. expect( command ).to.have.property( 'isEnabled', false );
  86. } );
  87. it(
  88. 'is false when selection is in an element which cannot be wrapped with blockQuote' +
  89. '(because mQ is not allowed in its parent)',
  90. () => {
  91. model.schema.disallow( { name: 'blockQuote', inside: '$root' } );
  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.registerItem( 'fooBlock', '$block' );
  298. model.schema.disallow( { name: 'fooBlock', inside: 'blockQuote' } );
  299. buildModelConverter().for( editor.editing.modelToView )
  300. .fromElement( 'fooBlock' )
  301. .toElement( 'fooblock' );
  302. setModelData(
  303. model,
  304. '<paragraph>a[bc</paragraph>' +
  305. '<fooBlock>xx</fooBlock>' +
  306. '<paragraph>de]f</paragraph>'
  307. );
  308. editor.execute( 'blockQuote' );
  309. expect( getModelData( model ) ).to.equal(
  310. '<blockQuote>' +
  311. '<paragraph>a[bc</paragraph>' +
  312. '</blockQuote>' +
  313. '<fooBlock>xx</fooBlock>' +
  314. '<blockQuote>' +
  315. '<paragraph>de]f</paragraph>' +
  316. '</blockQuote>'
  317. );
  318. } );
  319. it( 'should not wrap a block which parent does not allow quote inside itself', () => {
  320. // blockQuote is not be allowed in fooWrapper, but fooBlock can be inside blockQuote.
  321. model.schema.registerItem( 'fooWrapper' );
  322. model.schema.registerItem( 'fooBlock', '$block' );
  323. model.schema.allow( { name: 'fooWrapper', inside: '$root' } );
  324. model.schema.allow( { name: 'fooBlock', inside: 'fooWrapper' } );
  325. buildModelConverter().for( editor.editing.modelToView )
  326. .fromElement( 'fooWrapper' )
  327. .toElement( 'foowrapper' );
  328. buildModelConverter().for( editor.editing.modelToView )
  329. .fromElement( 'fooBlock' )
  330. .toElement( 'fooblock' );
  331. setModelData(
  332. model,
  333. '<paragraph>a[bc</paragraph>' +
  334. '<fooWrapper><fooBlock>xx</fooBlock></fooWrapper>' +
  335. '<paragraph>de]f</paragraph>'
  336. );
  337. editor.execute( 'blockQuote' );
  338. expect( getModelData( model ) ).to.equal(
  339. '<blockQuote>' +
  340. '<paragraph>a[bc</paragraph>' +
  341. '</blockQuote>' +
  342. '<fooWrapper><fooBlock>xx</fooBlock></fooWrapper>' +
  343. '<blockQuote>' +
  344. '<paragraph>de]f</paragraph>' +
  345. '</blockQuote>'
  346. );
  347. } );
  348. } );
  349. describe( 'removing quote', () => {
  350. it( 'should unwrap a single block', () => {
  351. setModelData(
  352. model,
  353. '<paragraph>abc</paragraph>' +
  354. '<blockQuote><paragraph>x[]x</paragraph></blockQuote>' +
  355. '<paragraph>def</paragraph>'
  356. );
  357. editor.execute( 'blockQuote' );
  358. expect( getModelData( model ) ).to.equal(
  359. '<paragraph>abc</paragraph>' +
  360. '<paragraph>x[]x</paragraph>' +
  361. '<paragraph>def</paragraph>'
  362. );
  363. expect( getViewData( editor.editing.view ) ).to.equal(
  364. '<p>abc</p><p>x{}x</p><p>def</p>'
  365. );
  366. } );
  367. it( 'should unwrap multiple blocks', () => {
  368. setModelData(
  369. model,
  370. '<blockQuote>' +
  371. '<paragraph>a[bc</paragraph>' +
  372. '<paragraph>xx</paragraph>' +
  373. '<paragraph>de]f</paragraph>' +
  374. '</blockQuote>'
  375. );
  376. editor.execute( 'blockQuote' );
  377. expect( getModelData( model ) ).to.equal(
  378. '<paragraph>a[bc</paragraph>' +
  379. '<paragraph>xx</paragraph>' +
  380. '<paragraph>de]f</paragraph>'
  381. );
  382. expect( getViewData( editor.editing.view ) ).to.equal(
  383. '<p>a{bc</p><p>xx</p><p>de}f</p>'
  384. );
  385. } );
  386. it( 'should unwrap only the selected blocks - at the beginning', () => {
  387. setModelData(
  388. model,
  389. '<paragraph>xx</paragraph>' +
  390. '<blockQuote>' +
  391. '<paragraph>a[b]c</paragraph>' +
  392. '<paragraph>xx</paragraph>' +
  393. '</blockQuote>' +
  394. '<paragraph>yy</paragraph>'
  395. );
  396. editor.execute( 'blockQuote' );
  397. expect( getModelData( model ) ).to.equal(
  398. '<paragraph>xx</paragraph>' +
  399. '<paragraph>a[b]c</paragraph>' +
  400. '<blockQuote>' +
  401. '<paragraph>xx</paragraph>' +
  402. '</blockQuote>' +
  403. '<paragraph>yy</paragraph>'
  404. );
  405. expect( getViewData( editor.editing.view ) ).to.equal(
  406. '<p>xx</p><p>a{b}c</p><blockquote><p>xx</p></blockquote><p>yy</p>'
  407. );
  408. } );
  409. it( 'should unwrap only the selected blocks - at the end', () => {
  410. setModelData(
  411. model,
  412. '<blockQuote>' +
  413. '<paragraph>abc</paragraph>' +
  414. '<paragraph>x[x</paragraph>' +
  415. '</blockQuote>' +
  416. '<paragraph>de]f</paragraph>'
  417. );
  418. editor.execute( 'blockQuote' );
  419. expect( getModelData( model ) ).to.equal(
  420. '<blockQuote>' +
  421. '<paragraph>abc</paragraph>' +
  422. '</blockQuote>' +
  423. '<paragraph>x[x</paragraph>' +
  424. '<paragraph>de]f</paragraph>'
  425. );
  426. expect( getViewData( editor.editing.view ) ).to.equal(
  427. '<blockquote><p>abc</p></blockquote><p>x{x</p><p>de}f</p>'
  428. );
  429. } );
  430. it( 'should unwrap only the selected blocks - in the middle', () => {
  431. setModelData(
  432. model,
  433. '<paragraph>xx</paragraph>' +
  434. '<blockQuote>' +
  435. '<paragraph>abc</paragraph>' +
  436. '<paragraph>c[]de</paragraph>' +
  437. '<paragraph>fgh</paragraph>' +
  438. '</blockQuote>' +
  439. '<paragraph>xx</paragraph>'
  440. );
  441. editor.execute( 'blockQuote' );
  442. expect( getModelData( model ) ).to.equal(
  443. '<paragraph>xx</paragraph>' +
  444. '<blockQuote><paragraph>abc</paragraph></blockQuote>' +
  445. '<paragraph>c[]de</paragraph>' +
  446. '<blockQuote><paragraph>fgh</paragraph></blockQuote>' +
  447. '<paragraph>xx</paragraph>'
  448. );
  449. expect( getViewData( editor.editing.view ) ).to.equal(
  450. '<p>xx</p>' +
  451. '<blockquote><p>abc</p></blockquote>' +
  452. '<p>c{}de</p>' +
  453. '<blockquote><p>fgh</p></blockquote>' +
  454. '<p>xx</p>'
  455. );
  456. } );
  457. it( 'should remove multiple quotes', () => {
  458. setModelData(
  459. model,
  460. '<blockQuote><paragraph>a[bc</paragraph></blockQuote>' +
  461. '<paragraph>xx</paragraph>' +
  462. '<blockQuote><paragraph>def</paragraph><paragraph>ghi</paragraph></blockQuote>' +
  463. '<paragraph>yy</paragraph>' +
  464. '<blockQuote><paragraph>de]f</paragraph><paragraph>ghi</paragraph></blockQuote>'
  465. );
  466. editor.execute( 'blockQuote' );
  467. expect( getModelData( model ) ).to.equal(
  468. '<paragraph>a[bc</paragraph>' +
  469. '<paragraph>xx</paragraph>' +
  470. '<paragraph>def</paragraph><paragraph>ghi</paragraph>' +
  471. '<paragraph>yy</paragraph>' +
  472. '<paragraph>de]f</paragraph>' +
  473. '<blockQuote><paragraph>ghi</paragraph></blockQuote>'
  474. );
  475. expect( getViewData( editor.editing.view ) ).to.equal(
  476. '<p>a{bc</p>' +
  477. '<p>xx</p>' +
  478. '<p>def</p><p>ghi</p>' +
  479. '<p>yy</p>' +
  480. '<p>de}f</p>' +
  481. '<blockquote><p>ghi</p></blockquote>'
  482. );
  483. } );
  484. } );
  485. } );
  486. } );