element.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import Node from './node.js';
  7. import NodeList from './nodelist.js';
  8. import DocumentFragment from './documentfragment.js';
  9. import Range from './range.js';
  10. import toMap from '../../utils/tomap.js';
  11. /**
  12. * Tree data model element.
  13. *
  14. * @memberOf engine.treeModel
  15. */
  16. export default class Element extends Node {
  17. /**
  18. * Creates a tree data model element.
  19. *
  20. * @param {String} name Node name.
  21. * @param {Iterable} [attrs] Iterable collection of attributes.
  22. * @param {engine.treeModel.NodeSet} [children] List of nodes to be inserted.
  23. * into created element. List of nodes can be of any type accepted by the {@link engine.treeModel.NodeList} constructor.
  24. */
  25. constructor( name, attrs, children ) {
  26. super( attrs );
  27. /**
  28. * Element name.
  29. *
  30. * @readonly
  31. * @member {String} engine.treeModel.Element#name
  32. */
  33. this.name = name;
  34. /**
  35. * List of children nodes.
  36. *
  37. * @protected
  38. * @member {engine.treeModel.NodeList} engine.treeModel.Element#_children
  39. */
  40. this._children = new NodeList();
  41. if ( children ) {
  42. this.insertChildren( 0, children );
  43. }
  44. }
  45. /**
  46. * Gets child at the given index.
  47. *
  48. * @param {Number} index Index of child.
  49. * @returns {engine.treeModel.Node} Child node.
  50. */
  51. getChild( index ) {
  52. return this._children.get( index );
  53. }
  54. /**
  55. * Gets the number of element's children.
  56. *
  57. * @returns {Number} The number of element's children.
  58. */
  59. getChildCount() {
  60. return this._children.length;
  61. }
  62. /**
  63. * Gets index of the given child node.
  64. *
  65. * @param {engine.treeModel.Node} node Child node.
  66. * @returns {Number} Index of the child node.
  67. */
  68. getChildIndex( node ) {
  69. return this._children.indexOf( node );
  70. }
  71. /**
  72. * {@link engine.treeModel.Element#insert Inserts} a child node or a list of child nodes at the end of this node and sets
  73. * the parent of these nodes to this element.
  74. *
  75. * Note that the list of children can be modified only in elements not yet attached to the document.
  76. * All attached nodes should be modified using the {@link engine.treeModel.operation.InsertOperation}.
  77. *
  78. * @param {engine.treeModel.NodeSet} nodes The list of nodes to be inserted.
  79. */
  80. appendChildren( nodes ) {
  81. this.insertChildren( this.getChildCount(), nodes );
  82. }
  83. /**
  84. * Inserts a list of child nodes on the given index and sets the parent of these nodes to this element.
  85. *
  86. * Note that the list of children can be modified only in elements not yet attached to the document.
  87. * All attached nodes should be modified using the {@link engine.treeModel.operation.InsertOperation}.
  88. *
  89. * @param {Number} index Position where nodes should be inserted.
  90. * @param {engine.treeModel.NodeSet} nodes The list of nodes to be inserted.
  91. */
  92. insertChildren( index, nodes ) {
  93. let nodeList = new NodeList( nodes );
  94. for ( let node of nodeList._nodes ) {
  95. node.parent = this;
  96. }
  97. // Clean original DocumentFragment so it won't contain nodes that were added somewhere else.
  98. if ( nodes instanceof DocumentFragment ) {
  99. nodes._children = new NodeList();
  100. }
  101. this._children.insert( index, nodeList );
  102. }
  103. /**
  104. * Removes number of child nodes starting at the given index and set the parent of these nodes to `null`.
  105. *
  106. * Note that the list of children can be modified only in elements not yet attached to the document.
  107. * All attached nodes should be modified using the {@link engine.treeModel.operation.RemoveOperation}.
  108. *
  109. * @param {Number} index Position of the first node to remove.
  110. * @param {Number} [howMany=1] Number of nodes to remove.
  111. * @returns {engine.treeModel.NodeList} The list of removed nodes.
  112. */
  113. removeChildren( index, howMany = 1 ) {
  114. let nodeList = this._children.remove( index, howMany );
  115. for ( let node of nodeList._nodes ) {
  116. node.parent = null;
  117. }
  118. return nodeList;
  119. }
  120. /**
  121. * Sets attribute on the element. If attribute with the same key already is set, it overwrites its value.
  122. *
  123. * @param {String} key Key of attribute to set.
  124. * @param {*} value Attribute value.
  125. */
  126. setAttribute( key, value ) {
  127. this._attrs.set( key, value );
  128. }
  129. /**
  130. * Removes all attributes from the element and sets given attributes.
  131. *
  132. * @param {Iterable|Object} attrs Iterable object containing attributes to be set. See {@link engine.treeModel.Node#getAttributes}.
  133. */
  134. setAttributesTo( attrs ) {
  135. this._attrs = toMap( attrs );
  136. }
  137. /**
  138. * Removes an attribute with given key from the element.
  139. *
  140. * @param {String} key Key of attribute to remove.
  141. * @returns {Boolean} `true` if the attribute was set on the element, `false` otherwise.
  142. */
  143. removeAttribute( key ) {
  144. return this._attrs.delete( key );
  145. }
  146. /**
  147. * Removes all attributes from the element.
  148. */
  149. clearAttributes() {
  150. this._attrs.clear();
  151. }
  152. /**
  153. * Checks whether element is empty (has no children).
  154. *
  155. * @returns {Boolean}
  156. */
  157. isEmpty() {
  158. return this.getChildCount() === 0;
  159. }
  160. /**
  161. * Gets the text content of the element. The return value is created by concatenating all
  162. * text nodes in this element and its descendants.
  163. *
  164. * @returns {String}
  165. */
  166. getText() {
  167. let text = '';
  168. for ( let value of Range.createFromElement( this ) ) {
  169. if ( value.type == 'TEXT' ) {
  170. text += value.item.text;
  171. }
  172. }
  173. return text;
  174. }
  175. /**
  176. * Creates Element object from deserilized object, ie. from parsed JSON string.
  177. *
  178. * let deserialized = JSON.parse( JSON.stringify( someElementObject ) );
  179. * let element = NodeList.fromJSON( deserialized );
  180. *
  181. * @param object
  182. * @returns {engine.treeModel.Element}
  183. */
  184. static fromJSON( object ) {
  185. return new Element( object.name, object._attrs, NodeList.fromJSON( object._children ) );
  186. }
  187. }