8
0

modelconsumable.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: conversion */
  6. 'use strict';
  7. import ModelConsumable from '/ckeditor5/engine/conversion/modelconsumable.js';
  8. import ModelElement from '/ckeditor5/engine/model/element.js';
  9. import ModelTextProxy from '/ckeditor5/engine/model/textproxy.js';
  10. import ModelText from '/ckeditor5/engine/model/text.js';
  11. describe( 'ModelConsumable', () => {
  12. let modelConsumable, modelElement;
  13. beforeEach( () => {
  14. modelConsumable = new ModelConsumable();
  15. modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar' ) );
  16. } );
  17. describe( 'add', () => {
  18. it( 'should add consumable value from given element of given type', () => {
  19. modelConsumable.add( modelElement, 'type' );
  20. expect( modelConsumable.test( modelElement, 'type' ) ).to.be.true;
  21. } );
  22. it( 'should store multiple values for one element', () => {
  23. modelConsumable.add( modelElement, 'typeA' );
  24. modelConsumable.add( modelElement, 'typeB' );
  25. modelConsumable.add( modelElement, 'typeC' );
  26. expect( modelConsumable.test( modelElement, 'typeA' ) ).to.be.true;
  27. expect( modelConsumable.test( modelElement, 'typeB' ) ).to.be.true;
  28. expect( modelConsumable.test( modelElement, 'typeC' ) ).to.be.true;
  29. } );
  30. it( 'should correctly add text proxy instances', () => {
  31. const modelTextProxy = new ModelTextProxy( modelElement.getChild( 0 ), 1, 3 );
  32. modelConsumable.add( modelTextProxy, 'type' );
  33. expect( modelConsumable.test( modelTextProxy, 'type' ) ).to.be.true;
  34. } );
  35. } );
  36. describe( 'consume', () => {
  37. it( 'should remove consumable value of given type for given element and return true', () => {
  38. modelConsumable.add( modelElement, 'type' );
  39. const result = modelConsumable.consume( modelElement, 'type' );
  40. expect( result ).to.be.true;
  41. expect( modelConsumable.test( modelElement, 'type' ) ).to.be.false;
  42. } );
  43. it( 'should return false if given type of consumable was not added for given element', () => {
  44. const result = modelConsumable.consume( modelElement, 'type' );
  45. expect( result ).to.be.false;
  46. } );
  47. it( 'should correctly consume text proxy instances', () => {
  48. const proxy1To4 = new ModelTextProxy( modelElement.getChild( 0 ), 1, 3 );
  49. const proxy1To5 = new ModelTextProxy( modelElement.getChild( 0 ), 1, 4 );
  50. const proxyOther1To4 = new ModelTextProxy( new ModelText( 'abcdef' ), 1, 3 );
  51. modelConsumable.add( proxy1To4, 'type' );
  52. expect( modelConsumable.consume( proxy1To5, 'type' ) ).to.be.false;
  53. expect( modelConsumable.consume( proxyOther1To4, 'type' ) ).to.be.false;
  54. const equalProxy1To4 = new ModelTextProxy( modelElement.getChild( 0 ), 1, 3 );
  55. const result = modelConsumable.consume( equalProxy1To4, 'type' );
  56. expect( result ).to.be.true;
  57. expect( modelConsumable.test( proxy1To4, 'type' ) ).to.be.false;
  58. } );
  59. } );
  60. describe( 'revert', () => {
  61. it( 'should re-add consumable value if it was already consumed and return true', () => {
  62. modelConsumable.add( modelElement, 'type' );
  63. modelConsumable.consume( modelElement, 'type' );
  64. const result = modelConsumable.revert( modelElement, 'type' );
  65. expect( result ).to.be.true;
  66. expect( modelConsumable.test( modelElement, 'type' ) ).to.be.true;
  67. } );
  68. it( 'should return false if consumable value has not been yet consumed', () => {
  69. modelConsumable.add( modelElement, 'type' );
  70. const result = modelConsumable.revert( modelElement, 'type' );
  71. expect( result ).to.be.false;
  72. } );
  73. it( 'should return null if consumable value of given type has never been added for given element', () => {
  74. const result = modelConsumable.revert( modelElement, 'type' );
  75. expect( result ).to.be.null;
  76. } );
  77. it( 'should correctly revert text proxy instances', () => {
  78. const modelTextProxy = new ModelTextProxy( modelElement.getChild( 0 ), 1, 3 );
  79. modelConsumable.add( modelTextProxy, 'type' );
  80. modelConsumable.consume( modelTextProxy, 'type' );
  81. const result = modelConsumable.revert( modelTextProxy, 'type' );
  82. expect( result ).to.be.true;
  83. expect( modelConsumable.test( modelTextProxy, 'type' ) ).to.be.true;
  84. } );
  85. } );
  86. describe( 'test', () => {
  87. it( 'should return null if consumable value of given type has never been added for given element', () => {
  88. expect( modelConsumable.test( modelElement, 'typeA' ) ).to.be.null;
  89. modelConsumable.add( modelElement, 'typeA' );
  90. expect( modelConsumable.test( modelElement, 'typeB' ) ).to.be.null;
  91. } );
  92. it( 'should correctly test for text proxy instances', () => {
  93. const proxy1To4 = new ModelTextProxy( modelElement.getChild( 0 ), 1, 3 );
  94. const proxy1To5 = new ModelTextProxy( modelElement.getChild( 0 ), 1, 4 );
  95. const proxyOther1To4 = new ModelTextProxy( new ModelText( 'abcdef' ), 1, 3 );
  96. const equalProxy1To4 = new ModelTextProxy( modelElement.getChild( 0 ), 1, 3 );
  97. modelConsumable.add( proxy1To4, 'type' );
  98. expect( modelConsumable.test( proxy1To4, 'type' ) ).to.be.true;
  99. expect( modelConsumable.test( proxy1To4, 'otherType' ) ).to.be.null;
  100. expect( modelConsumable.test( proxy1To5, 'type' ) ).to.be.null;
  101. expect( modelConsumable.test( proxyOther1To4, 'type' ) ).to.be.null;
  102. expect( modelConsumable.test( equalProxy1To4, 'type' ) ).to.be.true;
  103. } );
  104. } );
  105. } );