attributeelement.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Element from './element.js';
  6. // Default attribute priority.
  7. const DEFAULT_PRIORITY = 10;
  8. /**
  9. * Attributes are elements which define document presentation. They are mostly elements like `<b>` or `<span>`.
  10. * Attributes can be broken and merged by the {@link engine.view.writer view writer}.
  11. *
  12. * Editing engine does not define fixed HTML DTD. This is why the type of the {@link engine.view.Element} need to
  13. * be defined by the feature developer. Creating an element you should use {@link engine.view.ContainerElement}
  14. * class or `AttributeElement`.
  15. *
  16. * @memberOf engine.view
  17. * @extends engine.view.Element
  18. */
  19. export default class AttributeElement extends Element {
  20. /**
  21. * Creates a attribute element.
  22. *
  23. * @see engine.view.Element
  24. */
  25. constructor( name, attrs, children ) {
  26. super( name, attrs, children );
  27. /**
  28. * Element priority. Attributes have to have the same priority to be
  29. * {@link engine.view.Element#isSimilar similar}. Setting different priorities on similar
  30. * nodes may prevent merging, e.g. two `<abbr>` nodes next each other shouldn't be merged.
  31. *
  32. * @member {Number} engine.view.AttributeElement#priority
  33. */
  34. this.priority = DEFAULT_PRIORITY;
  35. }
  36. /**
  37. * Clones provided element with priority.
  38. *
  39. * @param {Boolean} deep If set to `true` clones element and all its children recursively. When set to `false`,
  40. * element will be cloned without any children.
  41. * @returns {engine.view.AttributeElement} Clone of this element.
  42. */
  43. clone( deep ) {
  44. const cloned = super.clone( deep );
  45. // Clone priority too.
  46. cloned.priority = this.priority;
  47. return cloned;
  48. }
  49. /**
  50. * Checks if this element is similar to other element.
  51. * Both elements should have the same name, attributes and priority to be considered as similar.
  52. * Two similar elements can contain different set of children nodes.
  53. *
  54. * @param {engine.view.Element} otherElement
  55. * @returns {Boolean}
  56. */
  57. isSimilar( otherElement ) {
  58. return super.isSimilar( otherElement ) && this.priority == otherElement.priority;
  59. }
  60. /**
  61. * Returns block {@link engine.view.filler filler} offset or `null` if a block filler is not needed.
  62. *
  63. * @returns {Number|null} Block filler offset or `null` if block filler is not needed.
  64. */
  65. getFillerOffset() {
  66. // <b>foo</b> does not need filler.
  67. if ( this.childCount ) {
  68. return null;
  69. }
  70. let element = this.parent;
  71. // <p><b></b></p> needs filler -> <p><b><br></b></p>
  72. while ( element instanceof AttributeElement ) {
  73. if ( element.childCount > 1 ) {
  74. return null;
  75. }
  76. element = element.parent;
  77. }
  78. if ( !element || element.childCount > 1 ) {
  79. return null;
  80. }
  81. return 0;
  82. }
  83. }
  84. /**
  85. * Default attribute priority.
  86. *
  87. * @member {Number} engine.view.AttributeElement.DEFAULT_PRIORITY
  88. */
  89. AttributeElement.DEFAULT_PRIORITY = DEFAULT_PRIORITY;