nodelist.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 attr = new Attribute( 'bold', true );
  46. var nodeList = new NodeList( new Text( 'foo', [ attr ] ) );
  47. expect( nodeList.length ).to.be.equal( 3 );
  48. expect( nodeList.get( 0 ).character ).to.be.equal( 'f' );
  49. expect( nodeList.get( 0 ).getAttr( attr.key ) ).to.be.equal( attr.value );
  50. expect( nodeList.get( 1 ).character ).to.be.equal( 'o' );
  51. expect( nodeList.get( 1 ).getAttr( attr.key ) ).to.be.equal( attr.value );
  52. expect( nodeList.get( 2 ).character ).to.be.equal( 'o' );
  53. expect( nodeList.get( 2 ).getAttr( attr.key ) ).to.be.equal( attr.value );
  54. } );
  55. } );
  56. describe( 'insert', function() {
  57. it( 'should insert one nodelist into another', function() {
  58. var NodeList = modules[ 'document/nodelist' ];
  59. var outerList = new NodeList( 'foo' );
  60. var innerList = new NodeList( 'xxx' );
  61. outerList.insert( 2, innerList );
  62. expect( outerList.length ).to.be.equal( 6 );
  63. expect( outerList.get( 0 ).character ).to.be.equal( 'f' );
  64. expect( outerList.get( 1 ).character ).to.be.equal( 'o' );
  65. expect( outerList.get( 2 ).character ).to.be.equal( 'x' );
  66. expect( outerList.get( 3 ).character ).to.be.equal( 'x' );
  67. expect( outerList.get( 4 ).character ).to.be.equal( 'x' );
  68. expect( outerList.get( 5 ).character ).to.be.equal( 'o' );
  69. } );
  70. } );
  71. describe( 'remove', function() {
  72. it( 'should remove part of the nodelist', function() {
  73. var NodeList = modules[ 'document/nodelist' ];
  74. var nodeList = new NodeList( 'foobar' );
  75. nodeList.remove( 2, 3 );
  76. expect( nodeList.length ).to.be.equal( 3 );
  77. expect( nodeList.get( 0 ).character ).to.be.equal( 'f' );
  78. expect( nodeList.get( 1 ).character ).to.be.equal( 'o' );
  79. expect( nodeList.get( 2 ).character ).to.be.equal( 'r' );
  80. } );
  81. } );
  82. describe( 'iterator', function() {
  83. it( 'should iterate over all elements in the collection', function() {
  84. var NodeList = modules[ 'document/nodelist' ];
  85. var characters = 'foo';
  86. var nodeList = new NodeList( characters );
  87. var i = 0;
  88. for ( var node of nodeList ) {
  89. expect( node.character ).to.be.equal( characters[ i ] );
  90. i++;
  91. }
  92. expect( i ).to.be.equal( 3 );
  93. } );
  94. } );