element.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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' ], function( Node, NodeList ) {
  7. /**
  8. * Linear data element.
  9. *
  10. * @class document.Element
  11. */
  12. class Element extends Node {
  13. /**
  14. * Creates linear data 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} nodes List of nodes to be inserted.
  21. * List of nodes can be 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. * Insert a list of child nodes on the given index and set the parent of these node to this.
  46. *
  47. * Note that list of children can be modified only in elements not attached yet to the document.
  48. * All attached nodes should be modified using the {@link document.InsertOperation}.
  49. *
  50. * @param {Nuber} index Position where nodes should be inserted.
  51. * @param {document.Node|document.Text|document.NodeList|String|Iterable} nodes List of nodes to be inserted.
  52. * List of nodes can be any type accepted by the {@link document.NodeList} constructor.
  53. */
  54. insertChildren( index, nodes ) {
  55. this._children.insert( index, new NodeList( nodes ) );
  56. for ( var 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 node to null.
  62. *
  63. * Note that list of children can be modified only in elements not attached yet to the document.
  64. * All attached nodes should be modified using the {@link document.RemoveOperation}.
  65. *
  66. * @param {Number} index Position of the first node to remove.
  67. * @param {Number} number Number of nodes to remove.
  68. */
  69. removeChildren( index, number ) {
  70. for ( var i = index; i < index + number; i++ ) {
  71. this._children.get( i ).parent = null;
  72. }
  73. this._children.remove( index, number );
  74. }
  75. /**
  76. * Get child at given index.
  77. *
  78. * @param {Number} index Index of child.
  79. * @returns {document.Node} Child node.
  80. */
  81. getChild( index ) {
  82. return this._children.get( index );
  83. }
  84. /**
  85. * Get index of child node.
  86. *
  87. * @param {document.Node} node Child node.
  88. * @returns {Number} Index of child.
  89. */
  90. getChildIndex( node ) {
  91. return this._children.indexOf( node );
  92. }
  93. /**
  94. * Gets number of element's children.
  95. *
  96. * @returns {Number} Number of element's children.
  97. */
  98. getChildCount() {
  99. return this._children.length;
  100. }
  101. }
  102. return Element;
  103. } );