attributeelement.js 2.6 KB

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