alignmentediting.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  8. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. import AlignmentCommand from '../src/alignmentcommand';
  10. describe( 'AlignmentEditing', () => {
  11. let editor, doc;
  12. beforeEach( () => {
  13. return VirtualTestEditor
  14. .create( {
  15. plugins: [ AlignmentEditing, Paragraph ]
  16. } )
  17. .then( newEditor => {
  18. editor = newEditor;
  19. doc = editor.document;
  20. } );
  21. } );
  22. afterEach( () => {
  23. editor.destroy();
  24. } );
  25. it( 'adds alignment commands', () => {
  26. expect( editor.commands.get( 'alignLeft' ) ).to.be.instanceOf( AlignmentCommand );
  27. expect( editor.commands.get( 'alignRight' ) ).to.be.instanceOf( AlignmentCommand );
  28. expect( editor.commands.get( 'alignCenter' ) ).to.be.instanceOf( AlignmentCommand );
  29. expect( editor.commands.get( 'alignJustify' ) ).to.be.instanceOf( AlignmentCommand );
  30. } );
  31. it( 'allows for alignment in the $blocks', () => {
  32. expect( doc.schema.check( { name: '$block', inside: '$root', attributes: 'alignment' } ) ).to.be.true;
  33. } );
  34. describe( 'alignLeft', () => {
  35. it( 'adds converters to the data pipeline', () => {
  36. const data = '<p style="text-align:left;">x</p>';
  37. editor.setData( data );
  38. expect( getModelData( doc ) ).to.equal( '<paragraph alignment="left">[]x</paragraph>' );
  39. expect( editor.getData() ).to.equal( data );
  40. } );
  41. it( 'adds a converter to the view pipeline', () => {
  42. setModelData( doc, '<paragraph alignment="left">[]x</paragraph>' );
  43. expect( editor.getData() ).to.equal( '<p style="text-align:left;">x</p>' );
  44. } );
  45. } );
  46. describe( 'should work with broken styles', () => {
  47. it( 'should ignore empty style', () => {
  48. const data = '<p style="text-align:">x</p>';
  49. editor.setData( data );
  50. expect( getModelData( doc ) ).to.equal( '<paragraph>[]x</paragraph>' );
  51. expect( editor.getData() ).to.equal( '<p>x</p>' );
  52. } );
  53. it( 'should ignore not known style', () => {
  54. const data = '<p style="text-align:unset;">x</p>';
  55. editor.setData( data );
  56. expect( getModelData( doc ) ).to.equal( '<paragraph>[]x</paragraph>' );
  57. expect( editor.getData() ).to.equal( '<p>x</p>' );
  58. } );
  59. } );
  60. describe( 'alignCenter', () => {
  61. it( 'adds converters to the data pipeline', () => {
  62. const data = '<p style="text-align:center;">x</p>';
  63. editor.setData( data );
  64. expect( getModelData( doc ) ).to.equal( '<paragraph alignment="center">[]x</paragraph>' );
  65. expect( editor.getData() ).to.equal( data );
  66. } );
  67. it( 'adds a converter to the view pipeline', () => {
  68. setModelData( doc, '<paragraph alignment="center">[]x</paragraph>' );
  69. expect( editor.getData() ).to.equal( '<p style="text-align:center;">x</p>' );
  70. } );
  71. } );
  72. describe( 'alignRight', () => {
  73. it( 'adds converters to the data pipeline', () => {
  74. const data = '<p style="text-align:right;">x</p>';
  75. editor.setData( data );
  76. expect( getModelData( doc ) ).to.equal( '<paragraph alignment="right">[]x</paragraph>' );
  77. expect( editor.getData() ).to.equal( data );
  78. } );
  79. it( 'adds a converter to the view pipeline', () => {
  80. setModelData( doc, '<paragraph alignment="right">[]x</paragraph>' );
  81. expect( editor.getData() ).to.equal( '<p style="text-align:right;">x</p>' );
  82. } );
  83. } );
  84. describe( 'alignJustify', () => {
  85. it( 'adds converters to the data pipeline', () => {
  86. const data = '<p style="text-align:justify;">x</p>';
  87. editor.setData( data );
  88. expect( getModelData( doc ) ).to.equal( '<paragraph alignment="justify">[]x</paragraph>' );
  89. expect( editor.getData() ).to.equal( data );
  90. } );
  91. it( 'adds a converter to the view pipeline', () => {
  92. setModelData( doc, '<paragraph alignment="justify">[]x</paragraph>' );
  93. expect( editor.getData() ).to.equal( '<p style="text-align:justify;">x</p>' );
  94. } );
  95. } );
  96. } );