8
0

getlasttextline.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 Model from '@ckeditor/ckeditor5-engine/src/model/model';
  6. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import getText from '../../src/utils/getlasttextline';
  8. describe( 'utils', () => {
  9. let model, doc, root;
  10. beforeEach( () => {
  11. model = new Model();
  12. doc = model.document;
  13. root = doc.createRoot();
  14. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  15. model.schema.register( 'softBreak', { allowWhere: '$text', isInline: true } );
  16. model.schema.extend( '$text', { allowAttributes: [ 'bold', 'italic' ] } );
  17. } );
  18. describe( 'getText()', () => {
  19. it( 'should return all text from passed range', () => {
  20. setModelData( model, '<paragraph>foobar[]baz</paragraph>' );
  21. testOutput(
  22. model.createRangeIn( root.getChild( 0 ) ),
  23. 'foobarbaz',
  24. [ 0, 0 ],
  25. [ 0, 9 ] );
  26. } );
  27. it( 'should limit the output text to the given range', () => {
  28. setModelData( model, '<paragraph>foobar[]baz</paragraph>' );
  29. const testRange = model.createRange(
  30. model.createPositionAt( root.getChild( 0 ), 1 ),
  31. model.document.selection.focus
  32. );
  33. testOutput(
  34. testRange,
  35. 'oobar',
  36. [ 0, 1 ],
  37. [ 0, 6 ] );
  38. } );
  39. it( 'should limit the output to the last inline element text constrain in given range', () => {
  40. setModelData( model, '<paragraph>foo<softBreak></softBreak>bar<softBreak></softBreak>baz[]</paragraph>' );
  41. const testRange = model.createRange(
  42. model.createPositionAt( root.getChild( 0 ), 0 ),
  43. model.document.selection.focus
  44. );
  45. testOutput(
  46. testRange,
  47. 'baz',
  48. [ 0, 8 ],
  49. [ 0, 11 ] );
  50. } );
  51. it( 'should return text from text nodes with attributes', () => {
  52. setModelData( model,
  53. '<paragraph>' +
  54. '<$text bold="true">foo</$text>' +
  55. '<$text bold="true" italic="true">bar</$text>' +
  56. '<$text italic="true">baz</$text>[]' +
  57. '</paragraph>'
  58. );
  59. testOutput(
  60. model.createRangeIn( root.getChild( 0 ) ),
  61. 'foobarbaz',
  62. [ 0, 0 ],
  63. [ 0, 9 ] );
  64. } );
  65. } );
  66. function testOutput( range1, expectedText, startPath, endPath ) {
  67. const { text, range } = getText( range1, model );
  68. expect( text ).to.equal( expectedText );
  69. expect( range.start.path ).to.deep.equal( startPath );
  70. expect( range.end.path ).to.deep.equal( endPath );
  71. }
  72. } );