attributeelement.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module engine/view/attributeelement
  7. */
  8. import Element from './element';
  9. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  10. // Default attribute priority.
  11. const DEFAULT_PRIORITY = 10;
  12. /**
  13. * Attribute elements are used to represent formatting elements in the view (think – `<b>`, `<span style="font-size: 2em">`, etc.).
  14. * Most often they are created when downcasting model text attributes.
  15. *
  16. * Editing engine does not define a fixed HTML DTD. This is why a feature developer needs to choose between various
  17. * types (container element, {@link module:engine/view/attributeelement~AttributeElement attribute element},
  18. * {@link module:engine/view/emptyelement~EmptyElement empty element}, etc) when developing a feature.
  19. *
  20. * To create a new attribute element instance use the
  21. * {@link module:engine/view/downcastwriter~DowncastWriter#createAttributeElement `DowncastWriter#createAttributeElement()`} method.
  22. *
  23. * @extends module:engine/view/element~Element
  24. */
  25. export default class AttributeElement extends Element {
  26. /**
  27. * Creates an attribute element.
  28. *
  29. * @see module:engine/view/downcastwriter~DowncastWriter#createAttributeElement
  30. * @see module:engine/view/element~Element
  31. * @protected
  32. */
  33. constructor( name, attrs, children ) {
  34. super( name, attrs, children );
  35. /**
  36. * Returns block {@link module:engine/view/filler filler} offset or `null` if block filler is not needed.
  37. *
  38. * @method #getFillerOffset
  39. * @returns {Number|null} Block filler offset or `null` if block filler is not needed.
  40. */
  41. this.getFillerOffset = getFillerOffset;
  42. /**
  43. * Element priority. Decides in what order elements are wrapped by {@link module:engine/view/downcastwriter~DowncastWriter}.
  44. *
  45. * @protected
  46. * @member {Number}
  47. */
  48. this._priority = DEFAULT_PRIORITY;
  49. /**
  50. * Element identifier. If set, it is used by {@link module:engine/view/element~Element#isSimilar},
  51. * and then two elements are considered similar if, and only if they have the same `_id`.
  52. *
  53. * @protected
  54. * @member {String|Number}
  55. */
  56. this._id = null;
  57. /**
  58. * Keeps all the attribute elements that have the same {@link module:engine/view/attributeelement~AttributeElement#id ids}
  59. * and still exist in the view tree.
  60. *
  61. * This property is managed by {@link module:engine/view/downcastwriter~DowncastWriter}.
  62. *
  63. * @protected
  64. * @member {Set.<module:engine/view/attributeelement~AttributeElement>|null}
  65. */
  66. this._clonesGroup = null;
  67. }
  68. /**
  69. * Element priority. Decides in what order elements are wrapped by {@link module:engine/view/downcastwriter~DowncastWriter}.
  70. *
  71. * @readonly
  72. * @type {Number}
  73. */
  74. get priority() {
  75. return this._priority;
  76. }
  77. /**
  78. * Element identifier. If set, it is used by {@link module:engine/view/element~Element#isSimilar},
  79. * and then two elements are considered similar if, and only if they have the same `id`.
  80. *
  81. * @readonly
  82. * @type {String|Number}
  83. */
  84. get id() {
  85. return this._id;
  86. }
  87. /**
  88. * Returns all {@link module:engine/view/attributeelement~AttributeElement attribute elements} that has the
  89. * same {@link module:engine/view/attributeelement~AttributeElement#id id} and are in the view tree (were not removed).
  90. *
  91. * Note: If this element has been removed from the tree, returned set will not include it.
  92. *
  93. * Throws {@link module:utils/ckeditorerror~CKEditorError attribute-element-get-elements-with-same-id-no-id}
  94. * if this element has no `id`.
  95. *
  96. * @returns {Set.<module:engine/view/attributeelement~AttributeElement>} Set containing all the attribute elements
  97. * with the same `id` that were added and not removed from the view tree.
  98. */
  99. getElementsWithSameId() {
  100. if ( this.id === null ) {
  101. /**
  102. * Cannot get elements with the same id for an attribute element without id.
  103. *
  104. * @error attribute-element-get-elements-with-same-id-no-id
  105. */
  106. throw new CKEditorError(
  107. 'attribute-element-get-elements-with-same-id-no-id: ' +
  108. 'Cannot get elements with the same id for an attribute element without id.',
  109. this
  110. );
  111. }
  112. return new Set( this._clonesGroup );
  113. }
  114. /**
  115. * Checks whether this object is of the given.
  116. *
  117. * attributeElement.is( 'attributeElement' ); // -> true
  118. * attributeElement.is( 'element' ); // -> true
  119. * attributeElement.is( 'node' ); // -> true
  120. * attributeElement.is( 'view:attributeElement' ); // -> true
  121. * attributeElement.is( 'view:element' ); // -> true
  122. * attributeElement.is( 'view:node' ); // -> true
  123. *
  124. * attributeElement.is( 'model:element' ); // -> false
  125. * attributeElement.is( 'documentFragment' ); // -> false
  126. *
  127. * Assuming that the object being checked is an attribute element, you can also check its
  128. * {@link module:engine/view/attributeelement~AttributeElement#name name}:
  129. *
  130. * attributeElement.is( 'b' ); // -> true if this is a bold element
  131. * attributeElement.is( 'attributeElement', 'b' ); // -> same as above
  132. * text.is( 'b' ); -> false
  133. *
  134. * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
  135. *
  136. * @param {String} type Type to check when `name` parameter is present.
  137. * Otherwise, it acts like the `name` parameter.
  138. * @param {String} [name] Element name.
  139. * @returns {Boolean}
  140. */
  141. is( type, name = null ) {
  142. const cutType = type && type.replace( /^view:/, '' );
  143. if ( !name ) {
  144. return cutType == 'attributeElement' || super.is( type );
  145. } else {
  146. return ( cutType == 'attributeElement' && name == this.name ) || super.is( type, name );
  147. }
  148. }
  149. /**
  150. * Checks if this element is similar to other element.
  151. *
  152. * If none of elements has set {@link module:engine/view/attributeelement~AttributeElement#id}, then both elements
  153. * should have the same name, attributes and priority to be considered as similar. Two similar elements can contain
  154. * different set of children nodes.
  155. *
  156. * If at least one element has {@link module:engine/view/attributeelement~AttributeElement#id} set, then both
  157. * elements have to have the same {@link module:engine/view/attributeelement~AttributeElement#id} value to be
  158. * considered similar.
  159. *
  160. * Similarity is important for {@link module:engine/view/downcastwriter~DowncastWriter}. For example:
  161. *
  162. * * two following similar elements can be merged together into one, longer element,
  163. * * {@link module:engine/view/downcastwriter~DowncastWriter#unwrap} checks similarity of passed element and processed element to
  164. * decide whether processed element should be unwrapped,
  165. * * etc.
  166. *
  167. * @param {module:engine/view/element~Element} otherElement
  168. * @returns {Boolean}
  169. */
  170. isSimilar( otherElement ) {
  171. // If any element has an `id` set, just compare the ids.
  172. if ( this.id !== null || otherElement.id !== null ) {
  173. return this.id === otherElement.id;
  174. }
  175. return super.isSimilar( otherElement ) && this.priority == otherElement.priority;
  176. }
  177. /**
  178. * Clones provided element with priority.
  179. *
  180. * @protected
  181. * @param {Boolean} deep If set to `true` clones element and all its children recursively. When set to `false`,
  182. * element will be cloned without any children.
  183. * @returns {module:engine/view/attributeelement~AttributeElement} Clone of this element.
  184. */
  185. _clone( deep ) {
  186. const cloned = super._clone( deep );
  187. // Clone priority too.
  188. cloned._priority = this._priority;
  189. // And id too.
  190. cloned._id = this._id;
  191. return cloned;
  192. }
  193. }
  194. /**
  195. * Default attribute priority.
  196. *
  197. * @member {Number} module:engine/view/attributeelement~AttributeElement.DEFAULT_PRIORITY
  198. */
  199. AttributeElement.DEFAULT_PRIORITY = DEFAULT_PRIORITY;
  200. // Returns block {@link module:engine/view/filler~Filler filler} offset or `null` if block filler is not needed.
  201. //
  202. // @returns {Number|null} Block filler offset or `null` if block filler is not needed.
  203. function getFillerOffset() {
  204. // <b>foo</b> does not need filler.
  205. if ( nonUiChildrenCount( this ) ) {
  206. return null;
  207. }
  208. let element = this.parent;
  209. // <p><b></b></p> needs filler -> <p><b><br></b></p>
  210. while ( element && element.is( 'attributeElement' ) ) {
  211. if ( nonUiChildrenCount( element ) > 1 ) {
  212. return null;
  213. }
  214. element = element.parent;
  215. }
  216. if ( !element || nonUiChildrenCount( element ) > 1 ) {
  217. return null;
  218. }
  219. // Render block filler at the end of element (after all ui elements).
  220. return this.childCount;
  221. }
  222. // Returns total count of children that are not {@link module:engine/view/uielement~UIElement UIElements}.
  223. //
  224. // @param {module:engine/view/element~Element} element
  225. // @returns {Number}
  226. function nonUiChildrenCount( element ) {
  227. return Array.from( element.getChildren() ).filter( element => !element.is( 'uiElement' ) ).length;
  228. }