margin.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 StylesMap, { StylesProcessor } from '../../../src/view/stylesmap';
  6. import { addMarginRules } from '../../../src/view/styles/margin';
  7. describe( 'Margin styles normalizer', () => {
  8. let styles, stylesProcessor;
  9. before( () => {
  10. stylesProcessor = new StylesProcessor();
  11. addMarginRules( stylesProcessor );
  12. } );
  13. beforeEach( () => {
  14. styles = new StylesMap( stylesProcessor );
  15. } );
  16. it( 'should set all margins (1 value defined)', () => {
  17. styles.setTo( 'margin:1px;' );
  18. expect( styles.getNormalized( 'margin' ) ).to.deep.equal( {
  19. top: '1px',
  20. right: '1px',
  21. bottom: '1px',
  22. left: '1px'
  23. } );
  24. } );
  25. it( 'should set all margins (2 values defined)', () => {
  26. styles.setTo( 'margin:1px .34cm;' );
  27. expect( styles.getNormalized( 'margin' ) ).to.deep.equal( {
  28. top: '1px',
  29. right: '.34cm',
  30. bottom: '1px',
  31. left: '.34cm'
  32. } );
  33. } );
  34. it( 'should set all margins (3 values defined)', () => {
  35. styles.setTo( 'margin:1px .34cm 90.1rem;' );
  36. expect( styles.getNormalized( 'margin' ) ).to.deep.equal( {
  37. top: '1px',
  38. right: '.34cm',
  39. bottom: '90.1rem',
  40. left: '.34cm'
  41. } );
  42. } );
  43. it( 'should set all margins (4 values defined)', () => {
  44. styles.setTo( 'margin:1px .34cm 90.1rem thick;' );
  45. expect( styles.getNormalized( 'margin' ) ).to.deep.equal( {
  46. top: '1px',
  47. right: '.34cm',
  48. bottom: '90.1rem',
  49. left: 'thick'
  50. } );
  51. } );
  52. it( 'should output inline style (1 value defined)', () => {
  53. styles.setTo( 'margin:1px;' );
  54. expect( styles.toString() ).to.equal( 'margin:1px;' );
  55. expect( styles.getAsString( 'margin' ) ).to.equal( '1px' );
  56. expect( styles.getAsString( 'margin-top' ) ).to.equal( '1px' );
  57. expect( styles.getAsString( 'margin-right' ) ).to.equal( '1px' );
  58. expect( styles.getAsString( 'margin-bottom' ) ).to.equal( '1px' );
  59. expect( styles.getAsString( 'margin-left' ) ).to.equal( '1px' );
  60. } );
  61. it( 'should output inline style (2 values defined)', () => {
  62. styles.setTo( 'margin:1px .34cm;' );
  63. expect( styles.toString() ).to.equal( 'margin:1px .34cm;' );
  64. expect( styles.getAsString( 'margin' ) ).to.equal( '1px .34cm' );
  65. expect( styles.getAsString( 'margin-top' ) ).to.equal( '1px' );
  66. expect( styles.getAsString( 'margin-right' ) ).to.equal( '.34cm' );
  67. expect( styles.getAsString( 'margin-bottom' ) ).to.equal( '1px' );
  68. expect( styles.getAsString( 'margin-left' ) ).to.equal( '.34cm' );
  69. } );
  70. it( 'should output inline style (3 values defined)', () => {
  71. styles.setTo( 'margin:1px .34cm 90.1rem;' );
  72. expect( styles.toString() ).to.equal( 'margin:1px .34cm 90.1rem;' );
  73. expect( styles.getAsString( 'margin' ) ).to.equal( '1px .34cm 90.1rem' );
  74. expect( styles.getAsString( 'margin-top' ) ).to.equal( '1px' );
  75. expect( styles.getAsString( 'margin-right' ) ).to.equal( '.34cm' );
  76. expect( styles.getAsString( 'margin-bottom' ) ).to.equal( '90.1rem' );
  77. expect( styles.getAsString( 'margin-left' ) ).to.equal( '.34cm' );
  78. } );
  79. it( 'should output inline style (3 values defined, only last different)', () => {
  80. styles.setTo( 'margin:1px 1px 90.1rem;' );
  81. expect( styles.toString() ).to.equal( 'margin:1px 1px 90.1rem;' );
  82. expect( styles.getAsString( 'margin' ) ).to.equal( '1px 1px 90.1rem' );
  83. expect( styles.getAsString( 'margin-top' ) ).to.equal( '1px' );
  84. expect( styles.getAsString( 'margin-right' ) ).to.equal( '1px' );
  85. expect( styles.getAsString( 'margin-bottom' ) ).to.equal( '90.1rem' );
  86. expect( styles.getAsString( 'margin-left' ) ).to.equal( '1px' );
  87. } );
  88. it( 'should output inline style (4 values defined)', () => {
  89. styles.setTo( 'margin:1px .34cm 90.1rem thick;' );
  90. expect( styles.toString() ).to.equal( 'margin:1px .34cm 90.1rem thick;' );
  91. expect( styles.getAsString( 'margin' ) ).to.equal( '1px .34cm 90.1rem thick' );
  92. expect( styles.getAsString( 'margin-top' ) ).to.equal( '1px' );
  93. expect( styles.getAsString( 'margin-right' ) ).to.equal( '.34cm' );
  94. expect( styles.getAsString( 'margin-bottom' ) ).to.equal( '90.1rem' );
  95. expect( styles.getAsString( 'margin-left' ) ).to.equal( 'thick' );
  96. } );
  97. it( 'should output inline style (4 values defined, only last different)', () => {
  98. styles.setTo( 'margin:1px 1px 1px thick;' );
  99. expect( styles.toString() ).to.equal( 'margin:1px 1px 1px thick;' );
  100. expect( styles.getAsString( 'margin' ) ).to.equal( '1px 1px 1px thick' );
  101. expect( styles.getAsString( 'margin-top' ) ).to.equal( '1px' );
  102. expect( styles.getAsString( 'margin-right' ) ).to.equal( '1px' );
  103. expect( styles.getAsString( 'margin-bottom' ) ).to.equal( '1px' );
  104. expect( styles.getAsString( 'margin-left' ) ).to.equal( 'thick' );
  105. } );
  106. describe( 'margin-*', () => {
  107. it( 'should set proper margin', () => {
  108. styles.setTo( 'margin-top:1px;' );
  109. expect( styles.getNormalized( 'margin' ) ).to.deep.equal( { top: '1px' } );
  110. expect( styles.getNormalized( 'margin-top' ) ).to.equal( '1px' );
  111. } );
  112. it( 'should merge margin with margin shorthand', () => {
  113. styles.setTo( 'margin: 2em;margin-top:1px;' );
  114. expect( styles.getNormalized( 'margin' ) ).to.deep.equal( {
  115. top: '1px',
  116. right: '2em',
  117. bottom: '2em',
  118. left: '2em'
  119. } );
  120. expect( styles.getNormalized( 'margin-top' ) ).to.equal( '1px' );
  121. expect( styles.getNormalized( 'margin-right' ) ).to.equal( '2em' );
  122. expect( styles.getNormalized( 'margin-bottom' ) ).to.equal( '2em' );
  123. expect( styles.getNormalized( 'margin-left' ) ).to.equal( '2em' );
  124. } );
  125. it( 'should output margin-top', () => {
  126. styles.setTo( 'margin-top:1px;' );
  127. expect( styles.toString() ).to.equal( 'margin-top:1px;' );
  128. expect( styles.getAsString( 'margin-top' ) ).to.equal( '1px' );
  129. } );
  130. it( 'should output margin-right', () => {
  131. styles.setTo( 'margin-right:1px;' );
  132. expect( styles.toString() ).to.equal( 'margin-right:1px;' );
  133. expect( styles.getAsString( 'margin-right' ) ).to.equal( '1px' );
  134. } );
  135. it( 'should output margin-bottom', () => {
  136. styles.setTo( 'margin-bottom:1px;' );
  137. expect( styles.toString() ).to.equal( 'margin-bottom:1px;' );
  138. expect( styles.getAsString( 'margin-bottom' ) ).to.equal( '1px' );
  139. } );
  140. it( 'should output margin-left', () => {
  141. styles.setTo( 'margin-left:1px;' );
  142. expect( styles.toString() ).to.equal( 'margin-left:1px;' );
  143. expect( styles.getAsString( 'margin-left' ) ).to.equal( '1px' );
  144. } );
  145. } );
  146. } );