alignmentediting.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import AlignmentEditing from '../src/alignmentediting';
  6. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import ImageCaptionEngine from '@ckeditor/ckeditor5-image/src/imagecaption/imagecaptionengine';
  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 AlignmentCommand from '../src/alignmentcommand';
  11. describe( 'AlignmentEditing', () => {
  12. let editor, doc;
  13. beforeEach( () => {
  14. return VirtualTestEditor
  15. .create( {
  16. plugins: [ AlignmentEditing, Paragraph ]
  17. } )
  18. .then( newEditor => {
  19. editor = newEditor;
  20. doc = editor.document;
  21. } );
  22. } );
  23. afterEach( () => {
  24. editor.destroy();
  25. } );
  26. it( 'adds alignment commands', () => {
  27. expect( editor.commands.get( 'alignLeft' ) ).to.be.instanceOf( AlignmentCommand );
  28. expect( editor.commands.get( 'alignRight' ) ).to.be.instanceOf( AlignmentCommand );
  29. expect( editor.commands.get( 'alignCenter' ) ).to.be.instanceOf( AlignmentCommand );
  30. expect( editor.commands.get( 'alignJustify' ) ).to.be.instanceOf( AlignmentCommand );
  31. } );
  32. it( 'allows for alignment in $blocks', () => {
  33. expect( doc.schema.check( { name: '$block', inside: '$root', attributes: 'alignment' } ) ).to.be.true;
  34. } );
  35. describe( 'integration', () => {
  36. beforeEach( () => {
  37. return VirtualTestEditor
  38. .create( {
  39. plugins: [ AlignmentEditing, ImageCaptionEngine, Paragraph ]
  40. } )
  41. .then( newEditor => {
  42. editor = newEditor;
  43. doc = editor.document;
  44. } );
  45. } );
  46. it( 'disallows for alignment in the catpion', () => {
  47. expect( doc.schema.check( { name: '$block', inside: 'figcaption', attributes: 'alignment' } ) ).to.be.true;
  48. } );
  49. } );
  50. describe( 'alignLeft', () => {
  51. it( 'adds converters to the data pipeline', () => {
  52. const data = '<p style="text-align:left;">x</p>';
  53. editor.setData( data );
  54. expect( getModelData( doc ) ).to.equal( '<paragraph>[]x</paragraph>' );
  55. expect( editor.getData() ).to.equal( '<p>x</p>' );
  56. } );
  57. } );
  58. describe( 'alignCenter', () => {
  59. it( 'adds converters to the data pipeline', () => {
  60. const data = '<p style="text-align:center;">x</p>';
  61. editor.setData( data );
  62. expect( getModelData( doc ) ).to.equal( '<paragraph alignment="center">[]x</paragraph>' );
  63. expect( editor.getData() ).to.equal( data );
  64. } );
  65. it( 'adds a converter to the view pipeline', () => {
  66. setModelData( doc, '<paragraph alignment="center">[]x</paragraph>' );
  67. expect( editor.getData() ).to.equal( '<p style="text-align:center;">x</p>' );
  68. } );
  69. } );
  70. describe( 'alignRight', () => {
  71. it( 'adds converters to the data pipeline', () => {
  72. const data = '<p style="text-align:right;">x</p>';
  73. editor.setData( data );
  74. expect( getModelData( doc ) ).to.equal( '<paragraph alignment="right">[]x</paragraph>' );
  75. expect( editor.getData() ).to.equal( data );
  76. } );
  77. it( 'adds a converter to the view pipeline', () => {
  78. setModelData( doc, '<paragraph alignment="right">[]x</paragraph>' );
  79. expect( editor.getData() ).to.equal( '<p style="text-align:right;">x</p>' );
  80. } );
  81. } );
  82. describe( 'alignJustify', () => {
  83. it( 'adds converters to the data pipeline', () => {
  84. const data = '<p style="text-align:justify;">x</p>';
  85. editor.setData( data );
  86. expect( getModelData( doc ) ).to.equal( '<paragraph alignment="justify">[]x</paragraph>' );
  87. expect( editor.getData() ).to.equal( data );
  88. } );
  89. it( 'adds a converter to the view pipeline', () => {
  90. setModelData( doc, '<paragraph alignment="justify">[]x</paragraph>' );
  91. expect( editor.getData() ).to.equal( '<p style="text-align:justify;">x</p>' );
  92. } );
  93. } );
  94. describe( 'should work with broken styles', () => {
  95. it( 'should ignore empty style', () => {
  96. const data = '<p style="text-align:">x</p>';
  97. editor.setData( data );
  98. expect( getModelData( doc ) ).to.equal( '<paragraph>[]x</paragraph>' );
  99. expect( editor.getData() ).to.equal( '<p>x</p>' );
  100. } );
  101. it( 'should ignore not known style', () => {
  102. const data = '<p style="text-align:unset;">x</p>';
  103. editor.setData( data );
  104. expect( getModelData( doc ) ).to.equal( '<paragraph>[]x</paragraph>' );
  105. expect( editor.getData() ).to.equal( '<p>x</p>' );
  106. } );
  107. } );
  108. } );