utils.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import {
  6. getNodesAndText,
  7. jsonParseStringify,
  8. wrapInDelta,
  9. itemAt,
  10. getText,
  11. createRangeOnElementOnly
  12. } from '../../../tests/model/_utils/utils';
  13. import Model from '../../../src/model/model';
  14. import Range from '../../../src/model/range';
  15. import Element from '../../../src/model/element';
  16. import Text from '../../../src/model/text';
  17. import Node from '../../../src/model/node';
  18. import TextProxy from '../../../src/model/textproxy';
  19. import Operation from '../../../src/model/operation/operation';
  20. import Delta from '../../../src/model/delta/delta';
  21. describe( 'getNodesAndText', () => {
  22. let doc, root, div, p;
  23. beforeEach( () => {
  24. const model = new Model();
  25. doc = model.document;
  26. root = doc.createRoot();
  27. div = new Element( 'div', [], new Text( 'foobar' ) );
  28. p = new Element( 'p', [], new Text( 'abcxyz' ) );
  29. root.insertChildren( 0, [ div, p ] );
  30. } );
  31. it( 'reads two elements with text', () => {
  32. expect( getNodesAndText( Range.createIn( root ) ) ).to.equal( 'DIVfoobarDIVPabcxyzP' );
  33. } );
  34. } );
  35. describe( 'jsonParseStringify', () => {
  36. class Foo {
  37. constructor( ra ) {
  38. this.ra = ra;
  39. }
  40. }
  41. it( 'should return cleaned object', () => {
  42. const foo = new Foo( { bar: 'bar' } );
  43. const fooJsoned = jsonParseStringify( foo );
  44. expect( fooJsoned ).to.not.be.instanceOf( Foo );
  45. expect( fooJsoned ).to.deep.equal( { ra: { bar: 'bar' } } );
  46. } );
  47. } );
  48. describe( 'wrapInDelta', () => {
  49. it( 'should return given operation wrapped in a delta', () => {
  50. const op = new Operation( 0 );
  51. const wrapped = wrapInDelta( op );
  52. expect( wrapped ).to.equal( op );
  53. expect( wrapped.delta ).to.be.instanceof( Delta );
  54. } );
  55. } );
  56. describe( 'itemAt', () => {
  57. let foo, img, bar, element;
  58. beforeEach( () => {
  59. foo = new Text( 'foo' );
  60. img = new Element( 'image' );
  61. bar = new Text( 'bar' );
  62. element = new Element( 'p', null, [ foo, img, bar ] );
  63. } );
  64. it( 'should return element if it starts at given offset', () => {
  65. expect( itemAt( element, 3 ) ).to.equal( img );
  66. } );
  67. it( 'should return text proxy with one character if text node starts at given offset', () => {
  68. const text = itemAt( element, 4 );
  69. expect( text ).to.be.instanceof( TextProxy );
  70. expect( text.data ).to.equal( 'b' );
  71. expect( text.textNode ).to.equal( bar );
  72. } );
  73. it( 'should return text proxy with one character if text node occupies given offset', () => {
  74. const text = itemAt( element, 1 );
  75. expect( text ).to.be.instanceof( TextProxy );
  76. expect( text.data ).to.equal( 'o' );
  77. expect( text.textNode ).to.equal( foo );
  78. } );
  79. } );
  80. describe( 'getText', () => {
  81. it( 'should deeply visit each child of given element and concat text data of all visited text nodes', () => {
  82. const div = new Element( 'div', null, [
  83. new Element( 'p', null, [
  84. new Text( 'aaa', { bold: true } ),
  85. new Text( ' bbb' )
  86. ] ),
  87. new Text( 'ccc' ),
  88. new Node( { attr: 'value' } ),
  89. new Element( 'p', null, [
  90. new Text( 'ddd' )
  91. ] )
  92. ] );
  93. expect( getText( div ) ).to.equal( 'aaa bbbcccddd' );
  94. } );
  95. } );
  96. describe( 'createRangeOnElementOnly', () => {
  97. it( 'should create a range that contains only the given element', () => {
  98. const parent = new Element( 'parent' );
  99. const element = new Element( 'elem' );
  100. parent.appendChildren( element );
  101. const range = createRangeOnElementOnly( element );
  102. expect( Array.from( range.getItems() ) ).to.deep.equal( [ element ] );
  103. } );
  104. } );