nodelist.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: document */
  6. 'use strict';
  7. var modules = bender.amd.require(
  8. 'document/nodelist',
  9. 'document/character',
  10. 'document/text',
  11. 'document/attribute' );
  12. describe( 'constructor', function() {
  13. it( 'should change array of strings into a set of nodes', function() {
  14. var NodeList = modules[ 'document/nodelist' ];
  15. var Character = modules[ 'document/character' ];
  16. var nodeList = new NodeList( [ 'foo', new Character( 'x' ), 'bar' ] );
  17. expect( nodeList.length ).to.be.equal( 7 );
  18. expect( nodeList.get( 0 ).character ).to.be.equal( 'f' );
  19. expect( nodeList.get( 1 ).character ).to.be.equal( 'o' );
  20. expect( nodeList.get( 2 ).character ).to.be.equal( 'o' );
  21. expect( nodeList.get( 3 ).character ).to.be.equal( 'x' );
  22. expect( nodeList.get( 4 ).character ).to.be.equal( 'b' );
  23. expect( nodeList.get( 5 ).character ).to.be.equal( 'a' );
  24. expect( nodeList.get( 6 ).character ).to.be.equal( 'r' );
  25. } );
  26. it( 'should change string into a set of nodes', function() {
  27. var NodeList = modules[ 'document/nodelist' ];
  28. var nodeList = new NodeList( 'foo' );
  29. expect( nodeList.length ).to.be.equal( 3 );
  30. expect( nodeList.get( 0 ).character ).to.be.equal( 'f' );
  31. expect( nodeList.get( 1 ).character ).to.be.equal( 'o' );
  32. expect( nodeList.get( 2 ).character ).to.be.equal( 'o' );
  33. } );
  34. it( 'should change node into a set of nodes', function() {
  35. var NodeList = modules[ 'document/nodelist' ];
  36. var Character = modules[ 'document/character' ];
  37. var nodeList = new NodeList( new Character( 'x' ) );
  38. expect( nodeList.length ).to.be.equal( 1 );
  39. expect( nodeList.get( 0 ).character ).to.be.equal( 'x' );
  40. } );
  41. it( 'should change text with attribute into a set of nodes', function() {
  42. var NodeList = modules[ 'document/nodelist' ];
  43. var Text = modules[ 'document/text' ];
  44. var Attribute = modules[ 'document/attribute' ];
  45. var attrs = [ new Attribute( 'bold', true ) ];
  46. var nodeList = new NodeList( new Text( 'foo', attrs ) );
  47. expect( nodeList.length ).to.be.equal( 3 );
  48. expect( nodeList.get( 0 ).character ).to.be.equal( 'f' );
  49. expect( nodeList.get( 0 ).attrs ).to.be.deep.equal( attrs );
  50. expect( nodeList.get( 0 ).attrs ).not.to.be.equal( attrs );
  51. expect( nodeList.get( 1 ).character ).to.be.equal( 'o' );
  52. expect( nodeList.get( 1 ).attrs ).to.be.deep.equal( attrs );
  53. expect( nodeList.get( 1 ).attrs ).not.to.be.equal( attrs );
  54. expect( nodeList.get( 2 ).character ).to.be.equal( 'o' );
  55. expect( nodeList.get( 2 ).attrs ).to.be.deep.equal( attrs );
  56. expect( nodeList.get( 2 ).attrs ).not.to.be.equal( attrs );
  57. } );
  58. } );
  59. describe( 'insert', function() {
  60. it( 'should insert one nodelist into another', function() {
  61. var NodeList = modules[ 'document/nodelist' ];
  62. var outerList = new NodeList( 'foo' );
  63. var innerList = new NodeList( 'xxx' );
  64. outerList.insert( 2, innerList );
  65. expect( outerList.length ).to.be.equal( 6 );
  66. expect( outerList.get( 0 ).character ).to.be.equal( 'f' );
  67. expect( outerList.get( 1 ).character ).to.be.equal( 'o' );
  68. expect( outerList.get( 2 ).character ).to.be.equal( 'x' );
  69. expect( outerList.get( 3 ).character ).to.be.equal( 'x' );
  70. expect( outerList.get( 4 ).character ).to.be.equal( 'x' );
  71. expect( outerList.get( 5 ).character ).to.be.equal( 'o' );
  72. } );
  73. } );
  74. describe( 'remove', function() {
  75. it( 'should remove part of the nodelist', function() {
  76. var NodeList = modules[ 'document/nodelist' ];
  77. var nodeList = new NodeList( 'foobar' );
  78. nodeList.remove( 2, 3 );
  79. expect( nodeList.length ).to.be.equal( 3 );
  80. expect( nodeList.get( 0 ).character ).to.be.equal( 'f' );
  81. expect( nodeList.get( 1 ).character ).to.be.equal( 'o' );
  82. expect( nodeList.get( 2 ).character ).to.be.equal( 'r' );
  83. } );
  84. } );
  85. describe( 'iterator', function() {
  86. it( 'should iterate over all elements in the collection', function() {
  87. var NodeList = modules[ 'document/nodelist' ];
  88. var characters = 'foo';
  89. var nodeList = new NodeList( characters );
  90. var i = 0;
  91. for ( var node of nodeList ) {
  92. expect( node.character ).to.be.equal( characters[ i ] );
  93. i++;
  94. }
  95. expect( i ).to.be.equal( 3 );
  96. } );
  97. } );