element.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/model/element
  7. */
  8. import Node from './node.js';
  9. import NodeList from './nodelist.js';
  10. import Text from './text.js';
  11. import isIterable from '../../utils/isiterable.js';
  12. /**
  13. * Model element. Type of {@link engine.model.Node node} that has a {@link engine.model.Element#name name} and
  14. * {@link engine.model.Element#getChildren child nodes}.
  15. *
  16. * **Important**: see {@link engine.model.Node} to read about restrictions using `Element` and `Node` API.
  17. *
  18. * @memberOf engine.model
  19. */
  20. export default class Element extends Node {
  21. /**
  22. * Creates a model element.
  23. *
  24. * @param {String} name Element's name.
  25. * @param {Object} [attrs] Element's attributes. See {@link utils.toMap} for a list of accepted values.
  26. * @param {engine.model.Node|Iterable.<engine.model.Node>} [children] One or more nodes to be inserted as children of
  27. * created element.
  28. */
  29. constructor( name, attrs, children ) {
  30. super( attrs );
  31. /**
  32. * Element name.
  33. *
  34. * @member {String} engine.model.Element#name
  35. */
  36. this.name = name;
  37. /**
  38. * List of children nodes.
  39. *
  40. * @private
  41. * @member {engine.model.NodeList} engine.model.Element#_children
  42. */
  43. this._children = new NodeList();
  44. if ( children ) {
  45. this.insertChildren( 0, children );
  46. }
  47. }
  48. /**
  49. * Number of this element's children.
  50. *
  51. * @readonly
  52. * @type {Number}
  53. */
  54. get childCount() {
  55. return this._children.length;
  56. }
  57. /**
  58. * Sum of {engine.model.Node#offsetSize offset sizes} of all of this element's children.
  59. *
  60. * @readonly
  61. * @type {Number}
  62. */
  63. get maxOffset() {
  64. return this._children.maxOffset;
  65. }
  66. /**
  67. * Is `true` if there are no nodes inside this element, `false` otherwise.
  68. *
  69. * @readonly
  70. * @type {Boolean}
  71. */
  72. get isEmpty() {
  73. return this.childCount === 0;
  74. }
  75. /**
  76. * Gets the child at the given index.
  77. *
  78. * @param {Number} index Index of child.
  79. * @returns {engine.model.Node} Child node.
  80. */
  81. getChild( index ) {
  82. return this._children.getNode( index );
  83. }
  84. /**
  85. * Returns an iterator that iterates over all of this element's children.
  86. *
  87. * @returns {Iterable.<engine.model.Node>}
  88. */
  89. getChildren() {
  90. return this._children[ Symbol.iterator ]();
  91. }
  92. /**
  93. * Returns an index of the given child node. Returns `null` if given node is not a child of this element.
  94. *
  95. * @param {engine.model.Node} node Child node to look for.
  96. * @returns {Number} Child node's index in this element.
  97. */
  98. getChildIndex( node ) {
  99. return this._children.getNodeIndex( node );
  100. }
  101. /**
  102. * Returns the starting offset of given child. Starting offset is equal to the sum of
  103. * {engine.model.Node#offsetSize offset sizes} of all node's siblings that are before it. Returns `null` if
  104. * given node is not a child of this element.
  105. *
  106. * @param {engine.model.Node} node Child node to look for.
  107. * @returns {Number} Child node's starting offset.
  108. */
  109. getChildStartOffset( node ) {
  110. return this._children.getNodeStartOffset( node );
  111. }
  112. /**
  113. * Creates a copy of this element and returns it. Created element has same name and attributes as original element.
  114. * If clone is not deep, children of copied element are references to the same nodes as in original element.
  115. * If clone is deep, original element's children are also cloned.
  116. *
  117. * @param {Boolean} [deep=false] Decides whether children of this element should also be cloned (`true`) or not (`false`).
  118. */
  119. clone( deep = false ) {
  120. const children = deep ?
  121. Array.from( this._children ).map( ( node ) => node.clone() ) :
  122. Array.from( this._children );
  123. return new Element( this.name, this.getAttributes(), children );
  124. }
  125. /**
  126. * Returns index of a node that occupies given offset. If given offset is too low, returns `0`. If given offset is
  127. * too high, returns {@link engine.model.Element#getChildCount index after last child}.
  128. *
  129. * const textNode = new Text( 'foo' );
  130. * const pElement = new Element( 'p' );
  131. * const divElement = new Element( [ textNode, pElement ] );
  132. * divElement.offsetToIndex( -1 ); // Returns 0, because offset is too low.
  133. * divElement.offsetToIndex( 0 ); // Returns 0, because offset 0 is taken by `textNode` which is at index 0.
  134. * divElement.offsetToIndex( 1 ); // Returns 0, because `textNode` has `offsetSize` equal to 3, so it occupies offset 1 too.
  135. * divElement.offsetToIndex( 2 ); // Returns 0.
  136. * divElement.offsetToIndex( 3 ); // Returns 1.
  137. * divElement.offsetToIndex( 4 ); // Returns 2. There are no nodes at offset 4, so last available index is returned.
  138. *
  139. * @param {Number} offset Offset to look for.
  140. * @returns {Number}
  141. */
  142. offsetToIndex( offset ) {
  143. return this._children.offsetToIndex( offset );
  144. }
  145. /**
  146. * {@link engine.model.Element#insertChildren Inserts} one or more nodes at the end of this element.
  147. *
  148. * @param {engine.model.Node|Iterable.<engine.model.Node>} nodes Nodes to be inserted.
  149. */
  150. appendChildren( nodes ) {
  151. this.insertChildren( this.childCount, nodes );
  152. }
  153. /**
  154. * Inserts one or more nodes at the given index and sets {@link engine.model.Node#parent parent} of these nodes
  155. * to this element.
  156. *
  157. * @param {Number} index Index at which nodes should be inserted.
  158. * @param {engine.model.Node|Iterable.<engine.model.Node>} nodes Nodes to be inserted.
  159. */
  160. insertChildren( index, nodes ) {
  161. nodes = normalize( nodes );
  162. for ( let node of nodes ) {
  163. node.parent = this;
  164. }
  165. this._children.insertNodes( index, nodes );
  166. }
  167. /**
  168. * Removes one or more nodes starting at the given index and sets {@link engine.model.Node#parent parent} of these nodes to `null`.
  169. *
  170. * @param {Number} index Index of the first node to remove.
  171. * @param {Number} [howMany=1] Number of nodes to remove.
  172. * @returns {Array.<engine.model.Node>} Array containing removed nodes.
  173. */
  174. removeChildren( index, howMany = 1 ) {
  175. const nodes = this._children.removeNodes( index, howMany );
  176. for ( let node of nodes ) {
  177. node.parent = null;
  178. }
  179. return nodes;
  180. }
  181. /**
  182. * Converts `Element` instance to plain object and returns it. Takes care of converting all of this element's children.
  183. *
  184. * @returns {Object} `Element` instance converted to plain object.
  185. */
  186. toJSON() {
  187. let json = super.toJSON();
  188. json.name = this.name;
  189. if ( this._children.length > 0 ) {
  190. json.children = [];
  191. for ( let node of this._children ) {
  192. json.children.push( node.toJSON() );
  193. }
  194. }
  195. return json;
  196. }
  197. /**
  198. * Creates an `Element` instance from given plain object (i.e. parsed JSON string).
  199. * Converts `Element` children to proper nodes.
  200. *
  201. * @param {Object} json Plain object to be converted to `Element`.
  202. * @returns {engine.model.Element} `Element` instance created using given plain object.
  203. */
  204. static fromJSON( json ) {
  205. let children = null;
  206. if ( json.children ) {
  207. children = [];
  208. for ( let child of json.children ) {
  209. if ( child.name ) {
  210. // If child has name property, it is an Element.
  211. children.push( Element.fromJSON( child ) );
  212. } else {
  213. // Otherwise, it is a Text node.
  214. children.push( Text.fromJSON( child ) );
  215. }
  216. }
  217. }
  218. return new Element( json.name, json.attributes, children );
  219. }
  220. }
  221. // Converts strings to Text and non-iterables to arrays.
  222. //
  223. // @param {String|engine.model.Node|Iterable.<String|engine.model.Node>}
  224. // @return {Iterable.<engine.model.Node>}
  225. function normalize( nodes ) {
  226. // Separate condition because string is iterable.
  227. if ( typeof nodes == 'string' ) {
  228. return [ new Text( nodes ) ];
  229. }
  230. if ( !isIterable( nodes ) ) {
  231. nodes = [ nodes ];
  232. }
  233. // Array.from to enable .map() on non-arrays.
  234. return Array.from( nodes ).map( ( node ) => typeof node == 'string' ? new Text( node ) : node );
  235. }