blockquotecommand.js 17 KB

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