findlinkrange.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import findLinkRange from '../src/findlinkrange';
  6. import Model from '@ckeditor/ckeditor5-engine/src/model/model';
  7. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  8. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. describe( 'findLinkRange', () => {
  10. let model, document, root;
  11. beforeEach( () => {
  12. model = new Model();
  13. document = model.document;
  14. root = document.createRoot();
  15. model.schema.extend( '$text', { allowIn: '$root' } );
  16. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  17. } );
  18. it( 'should find link range searching from the center of the link #1', () => {
  19. setData( model, '<$text linkHref="url">foobar</$text>' );
  20. const startPosition = model.createPositionAt( root, [ 3 ] );
  21. const result = findLinkRange( startPosition, 'url', model );
  22. expect( result ).to.instanceOf( Range );
  23. expect( result.isEqual( model.createRange( model.createPositionAt( root, 0 ), model.createPositionAt( root, 6 ) ) ) ).to.true;
  24. } );
  25. it( 'should find link range searching from the center of the link #2', () => {
  26. setData( model, 'abc <$text linkHref="url">foobar</$text> abc' );
  27. const startPosition = model.createPositionAt( root, [ 7 ] );
  28. const result = findLinkRange( startPosition, 'url', model );
  29. expect( result ).to.instanceOf( Range );
  30. expect( result.isEqual( model.createRange( model.createPositionAt( root, 4 ), model.createPositionAt( root, 10 ) ) ) ).to.true;
  31. } );
  32. it( 'should find link range searching from the beginning of the link #1', () => {
  33. setData( model, '<$text linkHref="url">foobar</$text>' );
  34. const startPosition = model.createPositionAt( root, [ 0 ] );
  35. const result = findLinkRange( startPosition, 'url', model );
  36. expect( result ).to.instanceOf( Range );
  37. expect( result.isEqual( model.createRange( model.createPositionAt( root, 0 ), model.createPositionAt( root, 6 ) ) ) ).to.true;
  38. } );
  39. it( 'should find link range searching from the beginning of the link #2', () => {
  40. setData( model, 'abc <$text linkHref="url">foobar</$text> abc' );
  41. const startPosition = model.createPositionAt( root, [ 4 ] );
  42. const result = findLinkRange( startPosition, 'url', model );
  43. expect( result ).to.instanceOf( Range );
  44. expect( result.isEqual( model.createRange( model.createPositionAt( root, 4 ), model.createPositionAt( root, 10 ) ) ) ).to.true;
  45. } );
  46. it( 'should find link range searching from the end of the link #1', () => {
  47. setData( model, '<$text linkHref="url">foobar</$text>' );
  48. const startPosition = model.createPositionAt( root, [ 6 ] );
  49. const result = findLinkRange( startPosition, 'url', model );
  50. expect( result ).to.instanceOf( Range );
  51. expect( result.isEqual( model.createRange( model.createPositionAt( root, 0 ), model.createPositionAt( root, 6 ) ) ) ).to.true;
  52. } );
  53. it( 'should find link range searching from the end of the link #2', () => {
  54. setData( model, 'abc <$text linkHref="url">foobar</$text> abc' );
  55. const startPosition = model.createPositionAt( root, [ 10 ] );
  56. const result = findLinkRange( startPosition, 'url', model );
  57. expect( result ).to.instanceOf( Range );
  58. expect( result.isEqual( model.createRange( model.createPositionAt( root, 4 ), model.createPositionAt( root, 10 ) ) ) ).to.true;
  59. } );
  60. it( 'should find link range when link stick to other link searching from the center of the link', () => {
  61. setData( model, '<$text linkHref="other">abc</$text><$text linkHref="url">foobar</$text><$text linkHref="other">abc</$text>' );
  62. const startPosition = model.createPositionAt( root, [ 6 ] );
  63. const result = findLinkRange( startPosition, 'url', model );
  64. expect( result ).to.instanceOf( Range );
  65. expect( result.isEqual( model.createRange( model.createPositionAt( root, 3 ), model.createPositionAt( root, 9 ) ) ) ).to.true;
  66. } );
  67. it( 'should find link range when link stick to other link searching from the beginning of the link', () => {
  68. setData( model, '<$text linkHref="other">abc</$text><$text linkHref="url">foobar</$text><$text linkHref="other">abc</$text>' );
  69. const startPosition = model.createPositionAt( root, [ 3 ] );
  70. const result = findLinkRange( startPosition, 'url', model );
  71. expect( result ).to.instanceOf( Range );
  72. expect( result.isEqual( model.createRange( model.createPositionAt( root, 3 ), model.createPositionAt( root, 9 ) ) ) ).to.true;
  73. } );
  74. it( 'should find link range when link stick to other link searching from the end of the link', () => {
  75. setData( model, '<$text linkHref="other">abc</$text><$text linkHref="url">foobar</$text><$text linkHref="other">abc</$text>' );
  76. const startPosition = model.createPositionAt( root, [ 9 ] );
  77. const result = findLinkRange( startPosition, 'url', model );
  78. expect( result ).to.instanceOf( Range );
  79. expect( result.isEqual( model.createRange( model.createPositionAt( root, 3 ), model.createPositionAt( root, 9 ) ) ) ).to.true;
  80. } );
  81. it( 'should find link range only inside current parent', () => {
  82. setData(
  83. model,
  84. '<p><$text linkHref="url">foobar</$text></p>' +
  85. '<p><$text linkHref="url">foobar</$text></p>' +
  86. '<p><$text linkHref="url">foobar</$text></p>'
  87. );
  88. const startPosition = model.createPositionAt( root.getNodeByPath( [ 1 ] ), 3 );
  89. const result = findLinkRange( startPosition, 'url', model );
  90. expect( result ).to.instanceOf( Range );
  91. const expectedRange = model.createRange(
  92. model.createPositionAt( root.getNodeByPath( [ 1 ] ), 0 ),
  93. model.createPositionAt( root.getNodeByPath( [ 1 ] ), 6 )
  94. );
  95. expect( result.isEqual( expectedRange ) ).to.true;
  96. } );
  97. } );