attributeelement.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import AttributeElement from 'ckeditor5-engine/src/view/attributeelement';
  6. import Element from 'ckeditor5-engine/src/view/element';
  7. import { parse } from 'ckeditor5-engine/src/dev-utils/view';
  8. describe( 'AttributeElement', () => {
  9. describe( 'constructor()', () => {
  10. it( 'should create element with default priority', () => {
  11. const el = new AttributeElement( 'strong' );
  12. expect( el ).to.be.an.instanceof( AttributeElement );
  13. expect( el ).to.be.an.instanceof( Element );
  14. expect( el ).to.have.property( 'name' ).that.equals( 'strong' );
  15. expect( el ).to.have.property( 'priority' ).that.equals( AttributeElement.DEFAULT_PRIORITY );
  16. } );
  17. } );
  18. describe( 'clone', () => {
  19. it( 'should clone element with priority', () => {
  20. const el = new AttributeElement( 'b' );
  21. el.priority = 7;
  22. const clone = el.clone();
  23. expect( clone ).to.not.equal( el );
  24. expect( clone.name ).to.equal( el.name );
  25. expect( clone.priority ).to.equal( el.priority );
  26. } );
  27. } );
  28. describe( 'isSimilar', () => {
  29. it( 'should return true if priorities are the same', () => {
  30. const b1 = new AttributeElement( 'b' );
  31. b1.priority = 7;
  32. const b2 = new AttributeElement( 'b' );
  33. b2.priority = 7;
  34. expect( b1.isSimilar( b2 ) ).to.be.true;
  35. } );
  36. it( 'should return false if priorities are different', () => {
  37. const b1 = new AttributeElement( 'b' );
  38. b1.priority = 7;
  39. const b2 = new AttributeElement( 'b' ); // default priority
  40. expect( b1.isSimilar( b2 ) ).to.be.false;
  41. } );
  42. } );
  43. describe( 'getFillerOffset', () => {
  44. it( 'should return position 0 if it is the only element in the container', () => {
  45. const { selection } = parse( '<container:p><attribute:b>[]</attribute:b></container:p>' );
  46. const attribute = selection.getFirstPosition().parent;
  47. expect( attribute.getFillerOffset() ).to.equals( 0 );
  48. } );
  49. it( 'should return position 0 if it is the only nested element in the container', () => {
  50. const { selection } = parse(
  51. '<container:p><attribute:b><attribute:i>[]</attribute:i></attribute:b></container:p>' );
  52. const attribute = selection.getFirstPosition().parent;
  53. expect( attribute.getFillerOffset() ).to.equals( 0 );
  54. } );
  55. it( 'should return null if element contains another element', () => {
  56. const attribute = parse( '<attribute:b><attribute:i></attribute:i></attribute:b>' );
  57. expect( attribute.getFillerOffset() ).to.be.null;
  58. } );
  59. it( 'should return null if element contains text', () => {
  60. const attribute = parse( '<attribute:b>text</attribute:b>' );
  61. expect( attribute.getFillerOffset() ).to.be.null;
  62. } );
  63. it( 'should return null if container element contains text', () => {
  64. const { selection } = parse( '<container:p><attribute:b>[]</attribute:b>foo</container:p>' );
  65. const attribute = selection.getFirstPosition().parent;
  66. expect( attribute.getFillerOffset() ).to.be.null;
  67. } );
  68. it( 'should return null if it is the parent contains text', () => {
  69. const { selection } = parse(
  70. '<container:p><attribute:b><attribute:i>[]</attribute:i>foo</attribute:b></container:p>' );
  71. const attribute = selection.getFirstPosition().parent;
  72. expect( attribute.getFillerOffset() ).to.be.null;
  73. } );
  74. it( 'should return null if there is no parent container element', () => {
  75. const { selection } = parse( '<attribute:b><attribute:i>[]</attribute:i>foo</attribute:b>' );
  76. const attribute = selection.getFirstPosition().parent;
  77. expect( attribute.getFillerOffset() ).to.be.null;
  78. } );
  79. } );
  80. } );