attributeelement.js 3.6 KB

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