blockquotecommand.js 17 KB

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