findattributerange.js 5.2 KB

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