8
0

utils.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * @license Copyright (c) 2003-2016, 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. } from '/tests/engine/model/_utils/utils.js';
  12. import Document from '/ckeditor5/engine/model/document.js';
  13. import Range from '/ckeditor5/engine/model/range.js';
  14. import Element from '/ckeditor5/engine/model/element.js';
  15. import Text from '/ckeditor5/engine/model/text.js';
  16. import Node from '/ckeditor5/engine/model/node.js';
  17. import TextProxy from '/ckeditor5/engine/model/textproxy.js';
  18. import Operation from '/ckeditor5/engine/model/operation/operation.js';
  19. import Delta from '/ckeditor5/engine/model/delta/delta.js';
  20. describe( 'getNodesAndText', () => {
  21. let doc, root, div, p;
  22. beforeEach( () => {
  23. doc = new Document();
  24. root = doc.createRoot();
  25. div = new Element( 'div', [], new Text( 'foobar' ) );
  26. p = new Element( 'p', [], new Text( 'abcxyz' ) );
  27. root.insertChildren( 0, [ div, p ] );
  28. } );
  29. it( 'reads two elements with text', () => {
  30. expect( getNodesAndText( Range.createFromElement( 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. let foo = new Foo( { bar: 'bar' } );
  41. let fooJsoned = jsonParseStringify( foo );
  42. expect( fooJsoned ).to.not.be.instanceOf( Foo );
  43. expect( fooJsoned ).to.deep.equal( { ra: { bar: 'bar' } } );
  44. } );
  45. } );
  46. describe( 'wrapInDelta', () => {
  47. it( 'should return given operation wrapped in a delta', () => {
  48. const op = new Operation( 0 );
  49. const wrapped = wrapInDelta( op );
  50. expect( wrapped ).to.equal( op );
  51. expect( wrapped.delta ).to.be.instanceof( Delta );
  52. } );
  53. } );
  54. describe( 'itemAt', () => {
  55. let foo, img, bar, element;
  56. beforeEach( () => {
  57. foo = new Text( 'foo' );
  58. img = new Element( 'image' );
  59. bar = new Text( 'bar' );
  60. element = new Element( 'p', null, [ foo, img, bar ] );
  61. } );
  62. it( 'should return element if it starts at given offset', () => {
  63. expect( itemAt( element, 3 ) ).to.equal( img );
  64. } );
  65. it( 'should return text proxy with one character if text node starts at given offset', () => {
  66. const text = itemAt( element, 4 );
  67. expect( text ).to.be.instanceof( TextProxy );
  68. expect( text.data ).to.equal( 'b' );
  69. expect( text.textNode ).to.equal( bar );
  70. } );
  71. it( 'should return text proxy with one character if text node occupies given offset', () => {
  72. const text = itemAt( element, 1 );
  73. expect( text ).to.be.instanceof( TextProxy );
  74. expect( text.data ).to.equal( 'o' );
  75. expect( text.textNode ).to.equal( foo );
  76. } );
  77. } );
  78. describe( 'getText', () => {
  79. it( 'should deeply visit each child of given element and concat text data of all visited text nodes', () => {
  80. const div = new Element( 'div', null, [
  81. new Element( 'p', null, [
  82. new Text( 'aaa', { bold: true } ),
  83. new Text( ' bbb' )
  84. ] ),
  85. new Text( 'ccc' ),
  86. new Node( { attr: 'value' } ),
  87. new Element( 'p', null, [
  88. new Text( 'ddd' )
  89. ] )
  90. ] );
  91. expect( getText( div ) ).to.equal( 'aaa bbbcccddd' );
  92. } );
  93. } );