findlinkrange.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import findLinkRange from 'ckeditor5-link/src/findlinkrange';
  6. import Document from 'ckeditor5-engine/src/model/document';
  7. import Range from 'ckeditor5-engine/src/model/range';
  8. import Position from 'ckeditor5-engine/src/model/position';
  9. import { setData } from 'ckeditor5-engine/src/dev-utils/model';
  10. describe( 'findLinkRange', () => {
  11. let document, root;
  12. beforeEach( () => {
  13. document = new Document();
  14. root = document.createRoot();
  15. document.schema.allow( { name: '$text', inside: '$root' } );
  16. document.schema.registerItem( 'p', '$block' );
  17. } );
  18. it( 'should find link range searching from the center of the link #1', () => {
  19. setData( document, '<$text linkHref="url">foobar</$text>' );
  20. const startPosition = new Position( root, [ 3 ] );
  21. const result = findLinkRange( startPosition, 'url' );
  22. expect( result ).to.instanceOf( Range );
  23. expect( result.isEqual( Range.createFromParentsAndOffsets( root, 0, root, 6 ) ) ).to.true;
  24. } );
  25. it( 'should find link range searching from the center of the link #2', () => {
  26. setData( document, 'abc <$text linkHref="url">foobar</$text> abc' );
  27. const startPosition = new Position( root, [ 7 ] );
  28. const result = findLinkRange( startPosition, 'url' );
  29. expect( result ).to.instanceOf( Range );
  30. expect( result.isEqual( Range.createFromParentsAndOffsets( root, 4, root, 10 ) ) ).to.true;
  31. } );
  32. it( 'should find link range searching from the beginning of the link #1', () => {
  33. setData( document, '<$text linkHref="url">foobar</$text>' );
  34. const startPosition = new Position( root, [ 0 ] );
  35. const result = findLinkRange( startPosition, 'url' );
  36. expect( result ).to.instanceOf( Range );
  37. expect( result.isEqual( Range.createFromParentsAndOffsets( root, 0, root, 6 ) ) ).to.true;
  38. } );
  39. it( 'should find link range searching from the beginning of the link #2', () => {
  40. setData( document, 'abc <$text linkHref="url">foobar</$text> abc' );
  41. const startPosition = new Position( root, [ 4 ] );
  42. const result = findLinkRange( startPosition, 'url' );
  43. expect( result ).to.instanceOf( Range );
  44. expect( result.isEqual( Range.createFromParentsAndOffsets( root, 4, root, 10 ) ) ).to.true;
  45. } );
  46. it( 'should find link range searching from the end of the link #1', () => {
  47. setData( document, '<$text linkHref="url">foobar</$text>' );
  48. const startPosition = new Position( root, [ 6 ] );
  49. const result = findLinkRange( startPosition, 'url' );
  50. expect( result ).to.instanceOf( Range );
  51. expect( result.isEqual( Range.createFromParentsAndOffsets( root, 0, root, 6 ) ) ).to.true;
  52. } );
  53. it( 'should find link range searching from the end of the link #2', () => {
  54. setData( document, 'abc <$text linkHref="url">foobar</$text> abc' );
  55. const startPosition = new Position( root, [ 10 ] );
  56. const result = findLinkRange( startPosition, 'url' );
  57. expect( result ).to.instanceOf( Range );
  58. expect( result.isEqual( Range.createFromParentsAndOffsets( root, 4, 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( document, '<$text linkHref="other">abc</$text><$text linkHref="url">foobar</$text><$text linkHref="other">abc</$text>' );
  62. const startPosition = new Position( root, [ 6 ] );
  63. const result = findLinkRange( startPosition, 'url' );
  64. expect( result ).to.instanceOf( Range );
  65. expect( result.isEqual( Range.createFromParentsAndOffsets( root, 3, 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( document, '<$text linkHref="other">abc</$text><$text linkHref="url">foobar</$text><$text linkHref="other">abc</$text>' );
  69. const startPosition = new Position( root, [ 3 ] );
  70. const result = findLinkRange( startPosition, 'url' );
  71. expect( result ).to.instanceOf( Range );
  72. expect( result.isEqual( Range.createFromParentsAndOffsets( root, 3, 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( document, '<$text linkHref="other">abc</$text><$text linkHref="url">foobar</$text><$text linkHref="other">abc</$text>' );
  76. const startPosition = new Position( root, [ 9 ] );
  77. const result = findLinkRange( startPosition, 'url' );
  78. expect( result ).to.instanceOf( Range );
  79. expect( result.isEqual( Range.createFromParentsAndOffsets( root, 3, root, 9 ) ) ).to.true;
  80. } );
  81. it( 'should find link range only inside current parent', () => {
  82. setData(
  83. document,
  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 = new Position( root, [ 1, 3 ] );
  89. const result = findLinkRange( startPosition, 'url' );
  90. expect( result ).to.instanceOf( Range );
  91. expect( result.isEqual( new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 6 ] ) ) ) ).to.true;
  92. } );
  93. } );