blockquoteediting.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 BlockQuoteEditing from '../src/blockquoteediting';
  6. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import ListEditing from '@ckeditor/ckeditor5-list/src/listediting';
  8. import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
  9. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  10. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  11. import BlockQuoteCommand from '../src/blockquotecommand';
  12. describe( 'BlockQuoteEditing', () => {
  13. let editor, model;
  14. beforeEach( () => {
  15. return VirtualTestEditor
  16. .create( {
  17. plugins: [ BlockQuoteEditing, Paragraph, BoldEditing ]
  18. } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. model = editor.model;
  22. } );
  23. } );
  24. afterEach( () => {
  25. return editor.destroy();
  26. } );
  27. it( 'should have pluginName', () => {
  28. expect( BlockQuoteEditing.pluginName ).to.equal( 'BlockQuoteEditing' );
  29. } );
  30. it( 'adds a blockQuote command', () => {
  31. expect( editor.commands.get( 'blockQuote' ) ).to.be.instanceOf( BlockQuoteCommand );
  32. } );
  33. it( 'allows for blockQuote in the $root', () => {
  34. expect( model.schema.checkChild( [ '$root' ], 'blockQuote' ) ).to.be.true;
  35. } );
  36. it( 'allows for $block in blockQuote', () => {
  37. expect( model.schema.checkChild( [ '$root', 'blockQuote' ], '$block' ) ).to.be.true;
  38. expect( model.schema.checkChild( [ '$root', 'blockQuote' ], 'paragraph' ) ).to.be.true;
  39. } );
  40. it( 'does not allow for blockQuote in blockQuote', () => {
  41. expect( model.schema.checkChild( [ '$root', 'blockQuote' ], 'blockQuote' ) ).to.be.false;
  42. } );
  43. it( 'does not break when checking an unregisterd item', () => {
  44. expect( model.schema.checkChild( [ '$root', 'blockQuote' ], 'foo' ) ).to.be.false;
  45. } );
  46. it( 'adds converters to the data pipeline', () => {
  47. const data = '<blockquote><p>x</p></blockquote>';
  48. editor.setData( data );
  49. expect( getModelData( model ) ).to.equal( '<blockQuote><paragraph>[]x</paragraph></blockQuote>' );
  50. expect( editor.getData() ).to.equal( data );
  51. } );
  52. it( 'adds a converter to the view pipeline', () => {
  53. setModelData( model, '<blockQuote><paragraph>x</paragraph></blockQuote>' );
  54. expect( editor.getData() ).to.equal( '<blockquote><p>x</p></blockquote>' );
  55. } );
  56. it( 'allows list items inside blockQuote', () => {
  57. return VirtualTestEditor
  58. .create( {
  59. plugins: [ BlockQuoteEditing, Paragraph, ListEditing ]
  60. } )
  61. .then( editor => {
  62. editor.setData( '<blockquote><ul><li>xx</li></ul></blockquote>' );
  63. expect( editor.getData() ).to.equal( '<blockquote><ul><li>xx</li></ul></blockquote>' );
  64. } );
  65. } );
  66. it( 'should remove empty blockQuote elements', () => {
  67. setModelData( model, '<blockQuote></blockQuote><paragraph>Foo</paragraph>' );
  68. expect( editor.getData() ).to.equal( '<p>Foo</p>' );
  69. } );
  70. it( 'should remove blockQuotes which became empty', () => {
  71. setModelData( model, '<blockQuote><paragraph>Foo</paragraph></blockQuote>' );
  72. model.change( writer => {
  73. const root = model.document.getRoot();
  74. const bq = root.getChild( 0 );
  75. writer.remove( writer.createRangeIn( bq ) );
  76. } );
  77. expect( editor.getData( { trim: 'none' } ) ).to.equal( '<p>&nbsp;</p>' ); // Autoparagraphed.
  78. } );
  79. it( 'should unwrap a blockQuote if it was inserted into another blockQuote', () => {
  80. setModelData( model, '<blockQuote><paragraph>Foo</paragraph></blockQuote>' );
  81. model.change( writer => {
  82. const root = model.document.getRoot();
  83. const bq = writer.createElement( 'blockQuote' );
  84. const p = writer.createElement( 'paragraph' );
  85. writer.insertText( 'Bar', p, 0 ); // <p>Bar</p>.
  86. writer.insert( p, bq, 0 ); // <blockquote><p>Bar</p></blockquote>.
  87. writer.insert( bq, root.getChild( 0 ), 1 ); // Insert after <p>Foo</p>.
  88. } );
  89. expect( editor.getData() ).to.equal( '<blockquote><p>Foo</p><p>Bar</p></blockquote>' );
  90. } );
  91. it( 'should unwrap nested blockQuote if it was wrapped into another blockQuote', () => {
  92. setModelData( model, '<blockQuote><paragraph>Foo</paragraph></blockQuote><paragraph>Bar</paragraph>' );
  93. model.change( writer => {
  94. const root = model.document.getRoot();
  95. writer.wrap( writer.createRangeIn( root ), 'blockQuote' );
  96. } );
  97. expect( editor.getData() ).to.equal( '<blockquote><p>Foo</p><p>Bar</p></blockquote>' );
  98. } );
  99. it( 'postfixer should do nothing on attribute change', () => {
  100. // This is strictly a 100% CC test.
  101. setModelData( model, '<blockQuote><paragraph>Foo</paragraph></blockQuote>' );
  102. model.change( writer => {
  103. const root = model.document.getRoot();
  104. const p = root.getChild( 0 ).getChild( 0 );
  105. writer.setAttribute( 'bold', true, writer.createRangeIn( p ) );
  106. } );
  107. expect( editor.getData() ).to.equal( '<blockquote><p><strong>Foo</strong></p></blockquote>' );
  108. } );
  109. } );