blockquotecommand.js 17 KB

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