8
0

findlinkrange.js 5.0 KB

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