8
0

modelconsumable.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 ModelConsumable from '../../src/conversion/modelconsumable';
  6. import ModelElement from '../../src/model/element';
  7. import ModelTextProxy from '../../src/model/textproxy';
  8. import ModelText from '../../src/model/text';
  9. describe( 'ModelConsumable', () => {
  10. let modelConsumable, modelElement;
  11. beforeEach( () => {
  12. modelConsumable = new ModelConsumable();
  13. modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar' ) );
  14. } );
  15. describe( 'add', () => {
  16. it( 'should add consumable value from given element of given type', () => {
  17. modelConsumable.add( modelElement, 'type' );
  18. expect( modelConsumable.test( modelElement, 'type' ) ).to.be.true;
  19. } );
  20. it( 'should store multiple values for one element', () => {
  21. modelConsumable.add( modelElement, 'typeA' );
  22. modelConsumable.add( modelElement, 'typeB' );
  23. modelConsumable.add( modelElement, 'typeC' );
  24. expect( modelConsumable.test( modelElement, 'typeA' ) ).to.be.true;
  25. expect( modelConsumable.test( modelElement, 'typeB' ) ).to.be.true;
  26. expect( modelConsumable.test( modelElement, 'typeC' ) ).to.be.true;
  27. } );
  28. it( 'should correctly add text proxy instances', () => {
  29. const modelTextProxy = new ModelTextProxy( modelElement.getChild( 0 ), 1, 3 );
  30. modelConsumable.add( modelTextProxy, 'type' );
  31. expect( modelConsumable.test( modelTextProxy, 'type' ) ).to.be.true;
  32. } );
  33. it( 'should normalize type name', () => {
  34. modelConsumable.add( modelElement, 'foo:bar:baz:abc' );
  35. expect( modelConsumable.test( modelElement, 'foo:bar:baz:abc' ) ).to.be.true;
  36. expect( modelConsumable.test( modelElement, 'foo:bar:baz' ) ).to.be.true;
  37. expect( modelConsumable.test( modelElement, 'foo:bar' ) ).to.be.true;
  38. expect( modelConsumable.test( modelElement, 'foo:bar:xxx' ) ).to.be.true;
  39. expect( modelConsumable.test( modelElement, 'foo:xxx' ) ).to.be.null;
  40. } );
  41. } );
  42. describe( 'consume', () => {
  43. it( 'should remove consumable value of given type for given element and return true', () => {
  44. modelConsumable.add( modelElement, 'type' );
  45. const result = modelConsumable.consume( modelElement, 'type' );
  46. expect( result ).to.be.true;
  47. expect( modelConsumable.test( modelElement, 'type' ) ).to.be.false;
  48. } );
  49. it( 'should return false if given type of consumable was not added for given element', () => {
  50. const result = modelConsumable.consume( modelElement, 'type' );
  51. expect( result ).to.be.false;
  52. } );
  53. it( 'should correctly consume text proxy instances', () => {
  54. const proxy1To4 = new ModelTextProxy( modelElement.getChild( 0 ), 1, 3 );
  55. const proxy1To5 = new ModelTextProxy( modelElement.getChild( 0 ), 1, 4 );
  56. const proxyOther1To4 = new ModelTextProxy( new ModelText( 'abcdef' ), 1, 3 );
  57. modelConsumable.add( proxy1To4, 'type' );
  58. expect( modelConsumable.consume( proxy1To5, 'type' ) ).to.be.false;
  59. expect( modelConsumable.consume( proxyOther1To4, 'type' ) ).to.be.false;
  60. const equalProxy1To4 = new ModelTextProxy( modelElement.getChild( 0 ), 1, 3 );
  61. const result = modelConsumable.consume( equalProxy1To4, 'type' );
  62. expect( result ).to.be.true;
  63. expect( modelConsumable.test( proxy1To4, 'type' ) ).to.be.false;
  64. } );
  65. it( 'should normalize type name', () => {
  66. modelConsumable.add( modelElement, 'foo:bar:baz:abc' );
  67. const result = modelConsumable.consume( modelElement, 'foo:bar:baz' );
  68. expect( result ).to.be.true;
  69. expect( modelConsumable.test( modelElement, 'foo:bar:baz:abc' ) ).to.be.false;
  70. expect( modelConsumable.test( modelElement, 'foo:bar:baz' ) ).to.be.false;
  71. expect( modelConsumable.test( modelElement, 'foo:bar' ) ).to.be.false;
  72. } );
  73. } );
  74. describe( 'revert', () => {
  75. it( 'should re-add consumable value if it was already consumed and return true', () => {
  76. modelConsumable.add( modelElement, 'type' );
  77. modelConsumable.consume( modelElement, 'type' );
  78. const result = modelConsumable.revert( modelElement, 'type' );
  79. expect( result ).to.be.true;
  80. expect( modelConsumable.test( modelElement, 'type' ) ).to.be.true;
  81. } );
  82. it( 'should return false if consumable value has not been yet consumed', () => {
  83. modelConsumable.add( modelElement, 'type' );
  84. const result = modelConsumable.revert( modelElement, 'type' );
  85. expect( result ).to.be.false;
  86. } );
  87. it( 'should return null if consumable value of given type has never been added for given element', () => {
  88. const result = modelConsumable.revert( modelElement, 'type' );
  89. expect( result ).to.be.null;
  90. } );
  91. it( 'should correctly revert text proxy instances', () => {
  92. const modelTextProxy = new ModelTextProxy( modelElement.getChild( 0 ), 1, 3 );
  93. modelConsumable.add( modelTextProxy, 'type' );
  94. modelConsumable.consume( modelTextProxy, 'type' );
  95. const result = modelConsumable.revert( modelTextProxy, 'type' );
  96. expect( result ).to.be.true;
  97. expect( modelConsumable.test( modelTextProxy, 'type' ) ).to.be.true;
  98. } );
  99. it( 'should normalize type name', () => {
  100. modelConsumable.add( modelElement, 'foo:bar:baz:abc' );
  101. modelConsumable.consume( modelElement, 'foo:bar:baz' );
  102. const result = modelConsumable.revert( modelElement, 'foo:bar:baz' );
  103. expect( result ).to.be.true;
  104. expect( modelConsumable.test( modelElement, 'foo:bar:baz:abc' ) ).to.be.true;
  105. expect( modelConsumable.test( modelElement, 'foo:bar:baz' ) ).to.be.true;
  106. expect( modelConsumable.test( modelElement, 'foo:bar' ) ).to.be.true;
  107. } );
  108. } );
  109. describe( 'test', () => {
  110. it( 'should return null if consumable value of given type has never been added for given element', () => {
  111. expect( modelConsumable.test( modelElement, 'typeA' ) ).to.be.null;
  112. modelConsumable.add( modelElement, 'typeA' );
  113. expect( modelConsumable.test( modelElement, 'typeB' ) ).to.be.null;
  114. } );
  115. it( 'should correctly test for text proxy instances', () => {
  116. const proxy1To4 = new ModelTextProxy( modelElement.getChild( 0 ), 1, 3 );
  117. const proxy1To5 = new ModelTextProxy( modelElement.getChild( 0 ), 1, 4 );
  118. const proxyOther1To4 = new ModelTextProxy( new ModelText( 'abcdef' ), 1, 3 );
  119. const equalProxy1To4 = new ModelTextProxy( modelElement.getChild( 0 ), 1, 3 );
  120. modelConsumable.add( proxy1To4, 'type' );
  121. expect( modelConsumable.test( proxy1To4, 'type' ) ).to.be.true;
  122. expect( modelConsumable.test( proxy1To4, 'otherType' ) ).to.be.null;
  123. expect( modelConsumable.test( proxy1To5, 'type' ) ).to.be.null;
  124. expect( modelConsumable.test( proxyOther1To4, 'type' ) ).to.be.null;
  125. expect( modelConsumable.test( equalProxy1To4, 'type' ) ).to.be.true;
  126. } );
  127. } );
  128. } );