8
0

utils.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 Document from '../../../src/model/document';
  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. doc = new Document();
  25. root = doc.createRoot();
  26. div = new Element( 'div', [], new Text( 'foobar' ) );
  27. p = new Element( 'p', [], new Text( 'abcxyz' ) );
  28. root.insertChildren( 0, [ div, p ] );
  29. } );
  30. it( 'reads two elements with text', () => {
  31. expect( getNodesAndText( Range.createIn( root ) ) ).to.equal( 'DIVfoobarDIVPabcxyzP' );
  32. } );
  33. } );
  34. describe( 'jsonParseStringify', () => {
  35. class Foo {
  36. constructor( ra ) {
  37. this.ra = ra;
  38. }
  39. }
  40. it( 'should return cleaned object', () => {
  41. const foo = new Foo( { bar: 'bar' } );
  42. const fooJsoned = jsonParseStringify( foo );
  43. expect( fooJsoned ).to.not.be.instanceOf( Foo );
  44. expect( fooJsoned ).to.deep.equal( { ra: { bar: 'bar' } } );
  45. } );
  46. } );
  47. describe( 'wrapInDelta', () => {
  48. it( 'should return given operation wrapped in a delta', () => {
  49. const op = new Operation( 0 );
  50. const wrapped = wrapInDelta( op );
  51. expect( wrapped ).to.equal( op );
  52. expect( wrapped.delta ).to.be.instanceof( Delta );
  53. } );
  54. } );
  55. describe( 'itemAt', () => {
  56. let foo, img, bar, element;
  57. beforeEach( () => {
  58. foo = new Text( 'foo' );
  59. img = new Element( 'image' );
  60. bar = new Text( 'bar' );
  61. element = new Element( 'p', null, [ foo, img, bar ] );
  62. } );
  63. it( 'should return element if it starts at given offset', () => {
  64. expect( itemAt( element, 3 ) ).to.equal( img );
  65. } );
  66. it( 'should return text proxy with one character if text node starts at given offset', () => {
  67. const text = itemAt( element, 4 );
  68. expect( text ).to.be.instanceof( TextProxy );
  69. expect( text.data ).to.equal( 'b' );
  70. expect( text.textNode ).to.equal( bar );
  71. } );
  72. it( 'should return text proxy with one character if text node occupies given offset', () => {
  73. const text = itemAt( element, 1 );
  74. expect( text ).to.be.instanceof( TextProxy );
  75. expect( text.data ).to.equal( 'o' );
  76. expect( text.textNode ).to.equal( foo );
  77. } );
  78. } );
  79. describe( 'getText', () => {
  80. it( 'should deeply visit each child of given element and concat text data of all visited text nodes', () => {
  81. const div = new Element( 'div', null, [
  82. new Element( 'p', null, [
  83. new Text( 'aaa', { bold: true } ),
  84. new Text( ' bbb' )
  85. ] ),
  86. new Text( 'ccc' ),
  87. new Node( { attr: 'value' } ),
  88. new Element( 'p', null, [
  89. new Text( 'ddd' )
  90. ] )
  91. ] );
  92. expect( getText( div ) ).to.equal( 'aaa bbbcccddd' );
  93. } );
  94. } );
  95. describe( 'createRangeOnElementOnly', () => {
  96. it( 'should create a range that contains only the given element', () => {
  97. const parent = new Element( 'parent' );
  98. const element = new Element( 'elem' );
  99. parent.appendChildren( element );
  100. const range = createRangeOnElementOnly( element );
  101. expect( Array.from( range.getItems() ) ).to.deep.equal( [ element ] );
  102. } );
  103. } );