element.js 3.3 KB

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