element.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /**
  9. * Tree data model element.
  10. *
  11. * @class treeModel.Element
  12. */
  13. export default class Element extends Node {
  14. /**
  15. * Creates a tree data model element.
  16. *
  17. * This constructor should be used only internally by the document.
  18. *
  19. * @param {String} name Node name.
  20. * @param {Iterable} attrs Iterable collection of {@link treeModel.Attribute attributes}.
  21. * @param {treeModel.Node|treeModel.Text|treeModel.TextNode|treeModel.NodeList|String|Iterable} children List of nodes to be inserted
  22. * into created element. List of nodes can be of any type accepted by the {@link treeModel.NodeList} constructor.
  23. * @constructor
  24. */
  25. constructor( name, attrs, children ) {
  26. super( attrs );
  27. /**
  28. * Element name.
  29. *
  30. * @readonly
  31. * @property {String} name
  32. */
  33. this.name = name;
  34. /**
  35. * List of children nodes.
  36. *
  37. * @protected
  38. * @property {treeModel.NodeList} _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 {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 {treeModel.Node|treeModel.Text} node Child node.
  66. * @returns {Number} Index of the child node.
  67. */
  68. getChildIndex( node ) {
  69. return this._children.indexOf( node );
  70. }
  71. /**
  72. * Inserts a list of child nodes on the given index and sets the parent of these nodes to this element.
  73. *
  74. * Note that the list of children can be modified only in elements not yet attached to the document.
  75. * All attached nodes should be modified using the {@link treeModel.operation.InsertOperation}.
  76. *
  77. * @param {Number} index Position where nodes should be inserted.
  78. * @param {treeModel.Node|treeModel.Text|treeModel.NodeList|String|Iterable} nodes The list of nodes to be inserted.
  79. * The list of nodes can be of any type accepted by the {@link treeModel.NodeList} constructor.
  80. */
  81. insertChildren( index, nodes ) {
  82. let nodeList = new NodeList( nodes );
  83. for ( let node of nodeList._nodes ) {
  84. node.parent = this;
  85. }
  86. this._children.insert( index, nodeList );
  87. }
  88. /**
  89. * Removes number of child nodes starting at the given index and set the parent of these nodes to `null`.
  90. *
  91. * Note that the list of children can be modified only in elements not yet attached to the document.
  92. * All attached nodes should be modified using the {@link treeModel.operation.RemoveOperation}.
  93. *
  94. * @param {Number} index Position of the first node to remove.
  95. * @param {Number} number Number of nodes to remove.
  96. * @returns {treeModel.NodeList} The list of removed nodes.
  97. */
  98. removeChildren( index, number ) {
  99. let nodeList = this._children.remove( index, number );
  100. for ( let node of nodeList._nodes ) {
  101. node.parent = null;
  102. }
  103. return nodeList;
  104. }
  105. }