8
0

utils.js 3.1 KB

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