element.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. CKEDITOR.define( [ 'document/node', 'document/nodelist' ], ( Node, NodeList ) => {
  7. /**
  8. * Tree data model element.
  9. *
  10. * @class document.Element
  11. */
  12. class Element extends Node {
  13. /**
  14. * Creates a tree data model element.
  15. *
  16. * This constructor should be used only internally by the document.
  17. *
  18. * @param {String} name Node name.
  19. * @param {Iterable} attrs Iterable collection of {@link document.Attribute attributes}.
  20. * @param {document.Node|document.Text|document.NodeList|String|Iterable} children List of nodes to be inserted
  21. * into created element. List of nodes can be of any type accepted by the {@link document.NodeList} constructor.
  22. * @constructor
  23. */
  24. constructor( name, attrs, children ) {
  25. super( attrs );
  26. /**
  27. * Element name.
  28. *
  29. * @readonly
  30. * @property {String} name
  31. */
  32. this.name = name;
  33. /**
  34. * List of children nodes.
  35. *
  36. * @protected
  37. * @property {document.NodeList} _children
  38. */
  39. this._children = new NodeList();
  40. if ( children ) {
  41. this.insertChildren( 0, children );
  42. }
  43. }
  44. /**
  45. * Inserts a list of child nodes on the given index and sets the parent of these nodes to this element.
  46. *
  47. * Note that the list of children can be modified only in elements not yet attached to the document.
  48. * All attached nodes should be modified using the {@link document.operation.InsertOperation}.
  49. *
  50. * @param {Number} index Position where nodes should be inserted.
  51. * @param {document.Node|document.Text|document.NodeList|String|Iterable} nodes The list of nodes to be inserted.
  52. * The list of nodes can be of any type accepted by the {@link document.NodeList} constructor.
  53. */
  54. insertChildren( index, nodes ) {
  55. this._children.insert( index, new NodeList( nodes ) );
  56. for ( let node of this._children ) {
  57. node.parent = this;
  58. }
  59. }
  60. /**
  61. * Removes number of child nodes starting at the given index and set the parent of these nodes to `null`.
  62. *
  63. * Note that the list of children can be modified only in elements not yet attached to the document.
  64. * All attached nodes should be modified using the {@link document.operation.RemoveOperation}.
  65. *
  66. * @param {Number} index Position of the first node to remove.
  67. * @param {Number} number Number of nodes to remove.
  68. * @returns {document.NodeList} The list of removed nodes.
  69. */
  70. removeChildren( index, number ) {
  71. for ( let i = index; i < index + number; i++ ) {
  72. this._children.get( i ).parent = null;
  73. }
  74. return this._children.remove( index, number );
  75. }
  76. /**
  77. * Gets child at the given index.
  78. *
  79. * @param {Number} index Index of child.
  80. * @returns {document.Node} Child node.
  81. */
  82. getChild( index ) {
  83. return this._children.get( index );
  84. }
  85. /**
  86. * Gets index of the given child node.
  87. *
  88. * @param {document.Node} node Child node.
  89. * @returns {Number} Index of the child node.
  90. */
  91. getChildIndex( node ) {
  92. return this._children.indexOf( node );
  93. }
  94. /**
  95. * Gets the number of element's children.
  96. *
  97. * @returns {Number} The number of element's children.
  98. */
  99. getChildCount() {
  100. return this._children.length;
  101. }
  102. }
  103. return Element;
  104. } );