8
0

utils.js 3.1 KB

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