attributeelement.js 3.6 KB

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