blockquoteediting.js 4.8 KB

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