8
0

selectallcommand.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import SelectAllEditing from '../src/selectallediting';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import ImageEditing from '@ckeditor/ckeditor5-image/src/image/imageediting';
  9. import ImageCaptionEditing from '@ckeditor/ckeditor5-image/src/imagecaption/imagecaptionediting';
  10. import TableEditing from '@ckeditor/ckeditor5-table/src/tableediting';
  11. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. describe( 'SelectAllCommand', () => {
  13. let editor, model, command;
  14. beforeEach( () => {
  15. return ModelTestEditor
  16. .create( {
  17. plugins: [ SelectAllEditing, Paragraph, ImageEditing, ImageCaptionEditing, TableEditing ]
  18. } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. model = editor.model;
  22. command = editor.commands.get( 'selectAll' );
  23. } );
  24. } );
  25. afterEach( () => {
  26. return editor.destroy();
  27. } );
  28. describe( 'isEnabled', () => {
  29. it( 'should always be "true" because the command is stateless', () => {
  30. expect( command.isEnabled ).to.be.true;
  31. } );
  32. } );
  33. describe( 'execute()', () => {
  34. it( 'should select all (collapsed selection in a block with text)', () => {
  35. setData( model, '<paragraph>f[]oo</paragraph>' );
  36. editor.execute( 'selectAll' );
  37. expect( getData( model ) ).to.equal( '<paragraph>[foo]</paragraph>' );
  38. } );
  39. it( 'should select all (collapsed selection in a content with an object)', () => {
  40. setData( model, '<paragraph>fo[]o</paragraph><image src="foo.png"><caption></caption></image>' );
  41. editor.execute( 'selectAll' );
  42. expect( getData( model ) ).to.equal( '<paragraph>[foo</paragraph><image src="foo.png"><caption></caption></image>]' );
  43. } );
  44. it( 'should select all (selection on an object)', () => {
  45. setData( model, '<paragraph>foo</paragraph>[<image src="foo.png"><caption></caption></image>]' );
  46. editor.execute( 'selectAll' );
  47. expect( getData( model ) ).to.equal( '<paragraph>[foo</paragraph><image src="foo.png"><caption></caption></image>]' );
  48. } );
  49. it( 'should select all (collapsed selection in a nested editable)', () => {
  50. setData( model, '<paragraph>foo</paragraph><image src="foo.png"><caption>b[]ar</caption></image>' );
  51. editor.execute( 'selectAll' );
  52. expect( getData( model ) ).to.equal( '<paragraph>foo</paragraph><image src="foo.png"><caption>[bar]</caption></image>' );
  53. } );
  54. it( 'should select all (selection in a nested editable)', () => {
  55. setData( model, '<paragraph>foo</paragraph><image src="foo.png"><caption>b[ar]</caption></image>' );
  56. editor.execute( 'selectAll' );
  57. expect( getData( model ) ).to.equal( '<paragraph>foo</paragraph><image src="foo.png"><caption>[bar]</caption></image>' );
  58. } );
  59. it( 'should select all (selection within limit element)', () => {
  60. setData( model,
  61. '<paragraph>foo</paragraph>' +
  62. '<table>' +
  63. '<tableRow>' +
  64. '<tableCell>' +
  65. '<paragraph>foo</paragraph>' +
  66. '</tableCell>' +
  67. '[<tableCell>' +
  68. '<paragraph>bar</paragraph>' +
  69. '</tableCell>]' +
  70. '[<tableCell>' +
  71. '<paragraph>baz</paragraph>' +
  72. '</tableCell>]' +
  73. '</tableRow>' +
  74. '</table>'
  75. );
  76. editor.execute( 'selectAll' );
  77. expect( getData( model ) ).to.equal(
  78. '<paragraph>[foo</paragraph>' +
  79. '<table>' +
  80. '<tableRow>' +
  81. '<tableCell>' +
  82. '<paragraph>foo</paragraph>' +
  83. '</tableCell>' +
  84. '<tableCell>' +
  85. '<paragraph>bar</paragraph>' +
  86. '</tableCell>' +
  87. '<tableCell>' +
  88. '<paragraph>baz</paragraph>' +
  89. '</tableCell>' +
  90. '</tableRow>' +
  91. '</table>]'
  92. );
  93. } );
  94. it( 'should select all in the closest nested editable (nested editable inside another nested editable)', () => {
  95. setData( model,
  96. '<paragraph>foo</paragraph>' +
  97. '<table>' +
  98. '<tableRow>' +
  99. '<tableCell>' +
  100. '<paragraph>foo</paragraph>' +
  101. '<image src="foo.png"><caption>b[]ar</caption></image>' +
  102. '</tableCell>' +
  103. '</tableRow>' +
  104. '</table>'
  105. );
  106. editor.execute( 'selectAll' );
  107. expect( getData( model ) ).to.equal( '<paragraph>foo</paragraph>' +
  108. '<table>' +
  109. '<tableRow>' +
  110. '<tableCell>' +
  111. '<paragraph>foo</paragraph>' +
  112. '<image src="foo.png"><caption>[bar]</caption></image>' +
  113. '</tableCell>' +
  114. '</tableRow>' +
  115. '</table>'
  116. );
  117. } );
  118. it( 'should select all in the parent select-all-limit element (the entire editable is selected)', () => {
  119. setData( model, '<paragraph>foo</paragraph><image src="foo.png"><caption>[bar]</caption></image>' );
  120. editor.execute( 'selectAll' );
  121. expect( getData( model ) ).to.equal( '<paragraph>[foo</paragraph><image src="foo.png"><caption>bar</caption></image>]' );
  122. } );
  123. it( 'should select all in the parent sellect-all-limit element (consecutive execute() on a nested editable)', () => {
  124. setData( model,
  125. '<paragraph>foo</paragraph>' +
  126. '<table>' +
  127. '<tableRow>' +
  128. '<tableCell>' +
  129. '<paragraph>foo</paragraph>' +
  130. '<image src="foo.png"><caption>b[]ar</caption></image>' +
  131. '</tableCell>' +
  132. '</tableRow>' +
  133. '</table>'
  134. );
  135. editor.execute( 'selectAll' );
  136. editor.execute( 'selectAll' );
  137. expect( getData( model ) ).to.equal( '<paragraph>foo</paragraph>' +
  138. '<table>' +
  139. '<tableRow>' +
  140. '<tableCell>' +
  141. '<paragraph>[foo</paragraph>' +
  142. '<image src="foo.png"><caption>bar</caption></image>]' +
  143. '</tableCell>' +
  144. '</tableRow>' +
  145. '</table>'
  146. );
  147. editor.execute( 'selectAll' );
  148. expect( getData( model ) ).to.equal( '<paragraph>[foo</paragraph>' +
  149. '<table>' +
  150. '<tableRow>' +
  151. '<tableCell>' +
  152. '<paragraph>foo</paragraph>' +
  153. '<image src="foo.png"><caption>bar</caption></image>' +
  154. '</tableCell>' +
  155. '</tableRow>' +
  156. '</table>]'
  157. );
  158. } );
  159. it( 'should not change the selection (the entire editor is selected)', () => {
  160. setData( model,
  161. '<paragraph>[foo</paragraph>' +
  162. '<table>' +
  163. '<tableRow>' +
  164. '<tableCell>' +
  165. '<paragraph>foo</paragraph>' +
  166. '<image src="foo.png"><caption>bar</caption></image>' +
  167. '</tableCell>' +
  168. '</tableRow>' +
  169. '</table>]'
  170. );
  171. editor.execute( 'selectAll' );
  172. expect( getData( model ) ).to.equal( '<paragraph>[foo</paragraph>' +
  173. '<table>' +
  174. '<tableRow>' +
  175. '<tableCell>' +
  176. '<paragraph>foo</paragraph>' +
  177. '<image src="foo.png"><caption>bar</caption></image>' +
  178. '</tableCell>' +
  179. '</tableRow>' +
  180. '</table>]'
  181. );
  182. } );
  183. } );
  184. } );