attributelist.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel */
  6. 'use strict';
  7. import coreTestUtils from '/tests/core/_utils/utils.js';
  8. import AttributeList from '/ckeditor5/core/treemodel/attributelist.js';
  9. import Attribute from '/ckeditor5/core/treemodel/attribute.js';
  10. const getIteratorCount = coreTestUtils.getIteratorCount;
  11. describe( 'AttributeList', () => {
  12. let list, attrFooBar;
  13. beforeEach( () => {
  14. list = new AttributeList();
  15. attrFooBar = new Attribute( 'foo', 'bar' );
  16. } );
  17. describe( 'setAttr', () => {
  18. it( 'should insert an attribute', () => {
  19. list.setAttr( attrFooBar );
  20. expect( getIteratorCount( list.getAttrs() ) ).to.equal( 1 );
  21. expect( list.getAttr( attrFooBar.key ) ).to.equal( attrFooBar.value );
  22. } );
  23. it( 'should overwrite attribute with the same key', () => {
  24. list.setAttr( attrFooBar );
  25. expect( getIteratorCount( list.getAttrs() ) ).to.equal( 1 );
  26. expect( list.getAttr( 'foo' ) ).to.equal( 'bar' );
  27. let attrFooXyz = new Attribute( 'foo', 'xyz' );
  28. list.setAttr( attrFooXyz );
  29. expect( getIteratorCount( list.getAttrs() ) ).to.equal( 1 );
  30. expect( list.getAttr( 'foo' ) ).to.equal( 'xyz' );
  31. } );
  32. } );
  33. describe( 'setAttrsTo', () => {
  34. it( 'should remove all attributes and set passed ones', () => {
  35. list.setAttr( attrFooBar );
  36. let attrs = [ new Attribute( 'abc', true ), new Attribute( 'xyz', false ) ];
  37. list.setAttrsTo( attrs );
  38. expect( getIteratorCount( list.getAttrs() ) ).to.equal( 2 );
  39. expect( list.getAttr( 'foo' ) ).to.be.null;
  40. expect( list.getAttr( 'abc' ) ).to.be.true;
  41. expect( list.getAttr( 'xyz' ) ).to.be.false;
  42. } );
  43. it( 'should copy attributes array, not pass by reference', () => {
  44. let attrs = [ new Attribute( 'attr', true ) ];
  45. list.setAttrsTo( attrs );
  46. attrs.pop();
  47. expect( getIteratorCount( list.getAttrs() ) ).to.equal( 1 );
  48. } );
  49. } );
  50. describe( 'getAttr', () => {
  51. beforeEach( () => {
  52. list.setAttr( attrFooBar );
  53. } );
  54. it( 'should return attribute value if key of previously set attribute has been passed', () => {
  55. expect( list.getAttr( 'foo' ) ).to.equal( attrFooBar.value );
  56. } );
  57. it( 'should return null if attribute with given key has not been found', () => {
  58. expect( list.getAttr( 'bar' ) ).to.be.null;
  59. } );
  60. } );
  61. describe( 'removeAttr', () => {
  62. it( 'should remove an attribute', () => {
  63. let attrA = new Attribute( 'a', 'A' );
  64. let attrB = new Attribute( 'b', 'B' );
  65. let attrC = new Attribute( 'c', 'C' );
  66. list.setAttr( attrA );
  67. list.setAttr( attrB );
  68. list.setAttr( attrC );
  69. list.removeAttr( attrB.key );
  70. expect( getIteratorCount( list.getAttrs() ) ).to.equal( 2 );
  71. expect( list.getAttr( attrA.key ) ).to.equal( attrA.value );
  72. expect( list.getAttr( attrC.key ) ).to.equal( attrC.value );
  73. expect( list.getAttr( attrB.key ) ).to.be.null;
  74. } );
  75. } );
  76. describe( 'hasAttr', () => {
  77. it( 'should check attribute by key', () => {
  78. list.setAttr( attrFooBar );
  79. expect( list.hasAttr( 'foo' ) ).to.be.true;
  80. } );
  81. it( 'should return false if attribute was not found by key', () => {
  82. expect( list.hasAttr( 'bar' ) ).to.be.false;
  83. } );
  84. it( 'should check attribute by object', () => {
  85. list.setAttr( attrFooBar );
  86. expect( list.hasAttr( attrFooBar ) ).to.be.true;
  87. } );
  88. it( 'should return false if attribute was not found by object', () => {
  89. expect( list.hasAttr( attrFooBar ) ).to.be.false;
  90. } );
  91. } );
  92. describe( 'getAttrs', () => {
  93. it( 'should return all set attributes', () => {
  94. let attrA = new Attribute( 'a', 'A' );
  95. let attrB = new Attribute( 'b', 'B' );
  96. let attrC = new Attribute( 'c', 'C' );
  97. list.setAttrsTo( [
  98. attrA,
  99. attrB,
  100. attrC
  101. ] );
  102. list.removeAttr( attrB.key );
  103. let attrsIt = list.getAttrs();
  104. let attrs = [];
  105. for ( let attr of attrsIt ) {
  106. attrs.push( attr );
  107. }
  108. expect( [ attrA, attrC ] ).to.deep.equal( attrs );
  109. } );
  110. } );
  111. } );