nodelist.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. );
  13. describe( 'NodeList', function() {
  14. var NodeList, Character, Text, Attribute;
  15. before( function() {
  16. NodeList = modules[ 'document/nodelist' ];
  17. Character = modules[ 'document/character' ];
  18. Text = modules[ 'document/text' ];
  19. Attribute = modules[ 'document/attribute' ];
  20. } );
  21. describe( 'constructor', function() {
  22. it( 'should change array of strings into a set of nodes', function() {
  23. var nodeList = new NodeList( [ 'foo', new Character( 'x' ), 'bar' ] );
  24. expect( nodeList.length ).to.equal( 7 );
  25. expect( nodeList.get( 0 ).character ).to.equal( 'f' );
  26. expect( nodeList.get( 1 ).character ).to.equal( 'o' );
  27. expect( nodeList.get( 2 ).character ).to.equal( 'o' );
  28. expect( nodeList.get( 3 ).character ).to.equal( 'x' );
  29. expect( nodeList.get( 4 ).character ).to.equal( 'b' );
  30. expect( nodeList.get( 5 ).character ).to.equal( 'a' );
  31. expect( nodeList.get( 6 ).character ).to.equal( 'r' );
  32. } );
  33. it( 'should change string into a set of nodes', function() {
  34. var nodeList = new NodeList( 'foo' );
  35. expect( nodeList.length ).to.equal( 3 );
  36. expect( nodeList.get( 0 ).character ).to.equal( 'f' );
  37. expect( nodeList.get( 1 ).character ).to.equal( 'o' );
  38. expect( nodeList.get( 2 ).character ).to.equal( 'o' );
  39. } );
  40. it( 'should change node into a set of nodes', function() {
  41. var nodeList = new NodeList( new Character( 'x' ) );
  42. expect( nodeList.length ).to.equal( 1 );
  43. expect( nodeList.get( 0 ).character ).to.equal( 'x' );
  44. } );
  45. it( 'should change text with attribute into a set of nodes', function() {
  46. var attr = new Attribute( 'bold', true );
  47. var nodeList = new NodeList( new Text( 'foo', [ attr ] ) );
  48. expect( nodeList.length ).to.equal( 3 );
  49. expect( nodeList.get( 0 ).character ).to.equal( 'f' );
  50. expect( nodeList.get( 0 ).getAttr( attr.key ) ).to.equal( attr.value );
  51. expect( nodeList.get( 1 ).character ).to.equal( 'o' );
  52. expect( nodeList.get( 1 ).getAttr( attr.key ) ).to.equal( attr.value );
  53. expect( nodeList.get( 2 ).character ).to.equal( 'o' );
  54. expect( nodeList.get( 2 ).getAttr( attr.key ) ).to.equal( attr.value );
  55. } );
  56. } );
  57. describe( 'insert', function() {
  58. it( 'should insert one nodelist into another', function() {
  59. var outerList = new NodeList( 'foo' );
  60. var innerList = new NodeList( 'xxx' );
  61. outerList.insert( 2, innerList );
  62. expect( outerList.length ).to.equal( 6 );
  63. expect( outerList.get( 0 ).character ).to.equal( 'f' );
  64. expect( outerList.get( 1 ).character ).to.equal( 'o' );
  65. expect( outerList.get( 2 ).character ).to.equal( 'x' );
  66. expect( outerList.get( 3 ).character ).to.equal( 'x' );
  67. expect( outerList.get( 4 ).character ).to.equal( 'x' );
  68. expect( outerList.get( 5 ).character ).to.equal( 'o' );
  69. } );
  70. } );
  71. describe( 'remove', function() {
  72. it( 'should remove part of the nodelist', function() {
  73. var nodeList = new NodeList( 'foobar' );
  74. nodeList.remove( 2, 3 );
  75. expect( nodeList.length ).to.equal( 3 );
  76. expect( nodeList.get( 0 ).character ).to.equal( 'f' );
  77. expect( nodeList.get( 1 ).character ).to.equal( 'o' );
  78. expect( nodeList.get( 2 ).character ).to.equal( 'r' );
  79. } );
  80. } );
  81. describe( 'iterator', function() {
  82. it( 'should iterate over all elements in the collection', function() {
  83. var characters = 'foo';
  84. var nodeList = new NodeList( characters );
  85. var i = 0;
  86. for ( var node of nodeList ) {
  87. expect( node.character ).to.equal( characters[ i ] );
  88. i++;
  89. }
  90. expect( i ).to.equal( 3 );
  91. } );
  92. } );
  93. } );