8
0

blockquoteediting.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * @license Copyright (c) 2003-2019, 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( 'adds a blockQuote command', () => {
  28. expect( editor.commands.get( 'blockQuote' ) ).to.be.instanceOf( BlockQuoteCommand );
  29. } );
  30. it( 'allows for blockQuote in the $root', () => {
  31. expect( model.schema.checkChild( [ '$root' ], 'blockQuote' ) ).to.be.true;
  32. } );
  33. it( 'allows for $block in blockQuote', () => {
  34. expect( model.schema.checkChild( [ '$root', 'blockQuote' ], '$block' ) ).to.be.true;
  35. expect( model.schema.checkChild( [ '$root', 'blockQuote' ], 'paragraph' ) ).to.be.true;
  36. } );
  37. it( 'does not allow for blockQuote in blockQuote', () => {
  38. expect( model.schema.checkChild( [ '$root', 'blockQuote' ], 'blockQuote' ) ).to.be.false;
  39. } );
  40. it( 'does not break when checking an unregisterd item', () => {
  41. expect( model.schema.checkChild( [ '$root', 'blockQuote' ], 'foo' ) ).to.be.false;
  42. } );
  43. it( 'adds converters to the data pipeline', () => {
  44. const data = '<blockquote><p>x</p></blockquote>';
  45. editor.setData( data );
  46. expect( getModelData( model ) ).to.equal( '<blockQuote><paragraph>[]x</paragraph></blockQuote>' );
  47. expect( editor.getData() ).to.equal( data );
  48. } );
  49. it( 'adds a converter to the view pipeline', () => {
  50. setModelData( model, '<blockQuote><paragraph>x</paragraph></blockQuote>' );
  51. expect( editor.getData() ).to.equal( '<blockquote><p>x</p></blockquote>' );
  52. } );
  53. it( 'allows list items inside blockQuote', () => {
  54. return VirtualTestEditor
  55. .create( {
  56. plugins: [ BlockQuoteEditing, Paragraph, ListEditing ]
  57. } )
  58. .then( editor => {
  59. editor.setData( '<blockquote><ul><li>xx</li></ul></blockquote>' );
  60. expect( editor.getData() ).to.equal( '<blockquote><ul><li>xx</li></ul></blockquote>' );
  61. } );
  62. } );
  63. it( 'should remove empty blockQuote elements', () => {
  64. setModelData( model, '<blockQuote></blockQuote><paragraph>Foo</paragraph>' );
  65. expect( editor.getData() ).to.equal( '<p>Foo</p>' );
  66. } );
  67. it( 'should remove blockQuotes which became empty', () => {
  68. setModelData( model, '<blockQuote><paragraph>Foo</paragraph></blockQuote>' );
  69. model.change( writer => {
  70. const root = model.document.getRoot();
  71. const bq = root.getChild( 0 );
  72. writer.remove( writer.createRangeIn( bq ) );
  73. } );
  74. expect( editor.getData( { trim: 'none' } ) ).to.equal( '<p>&nbsp;</p>' ); // Autoparagraphed.
  75. } );
  76. it( 'should unwrap a blockQuote if it was inserted into another blockQuote', () => {
  77. setModelData( model, '<blockQuote><paragraph>Foo</paragraph></blockQuote>' );
  78. model.change( writer => {
  79. const root = model.document.getRoot();
  80. const bq = writer.createElement( 'blockQuote' );
  81. const p = writer.createElement( 'paragraph' );
  82. writer.insertText( 'Bar', p, 0 ); // <p>Bar</p>.
  83. writer.insert( p, bq, 0 ); // <blockquote><p>Bar</p></blockquote>.
  84. writer.insert( bq, root.getChild( 0 ), 1 ); // Insert after <p>Foo</p>.
  85. } );
  86. expect( editor.getData() ).to.equal( '<blockquote><p>Foo</p><p>Bar</p></blockquote>' );
  87. } );
  88. it( 'should unwrap nested blockQuote if it was wrapped into another blockQuote', () => {
  89. setModelData( model, '<blockQuote><paragraph>Foo</paragraph></blockQuote><paragraph>Bar</paragraph>' );
  90. model.change( writer => {
  91. const root = model.document.getRoot();
  92. writer.wrap( writer.createRangeIn( root ), 'blockQuote' );
  93. } );
  94. expect( editor.getData() ).to.equal( '<blockquote><p>Foo</p><p>Bar</p></blockquote>' );
  95. } );
  96. it( 'postfixer should do nothing on attribute change', () => {
  97. // This is strictly a 100% CC test.
  98. setModelData( model, '<blockQuote><paragraph>Foo</paragraph></blockQuote>' );
  99. model.change( writer => {
  100. const root = model.document.getRoot();
  101. const p = root.getChild( 0 ).getChild( 0 );
  102. writer.setAttribute( 'bold', true, writer.createRangeIn( p ) );
  103. } );
  104. expect( editor.getData() ).to.equal( '<blockquote><p><strong>Foo</strong></p></blockquote>' );
  105. } );
  106. } );