8
0

attributeelement.js 2.9 KB

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