utils.js 2.7 KB

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