8
0

documentfragment.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import NodeList from './nodelist.js';
  7. /**
  8. * DocumentFragment represents a part of Tree Model which does not have a common root but it's top level nodes
  9. * can be seen as siblings.
  10. *
  11. * @memberOf engine.model
  12. */
  13. export default class DocumentFragment {
  14. /**
  15. * Creates empty DocumentFragment.
  16. *
  17. * @param {engine.model.NodeSet} children List of nodes contained inside the DocumentFragment.
  18. */
  19. constructor( children ) {
  20. /**
  21. * List of nodes contained inside the DocumentFragment.
  22. *
  23. * @protected
  24. * @member {engine.model.NodeSet} engine.model.DocumentFragment#_children
  25. */
  26. this._children = new NodeList();
  27. if ( children ) {
  28. this.insertChildren( 0, children );
  29. }
  30. }
  31. /**
  32. * `DocumentFragment` iterator. Returns {@link engine.model.Node nodes} that are added to the `DocumentFragment`.
  33. */
  34. [ Symbol.iterator ]() {
  35. return this._children[ Symbol.iterator ]();
  36. }
  37. /**
  38. * The root of `DocumentFragment`. Returns itself. Added for compatibility reasons with {@link engine.model.Element}.
  39. *
  40. * @readonly
  41. * @type {engine.model.DocumentFragment}
  42. */
  43. get root() {
  44. return this;
  45. }
  46. /**
  47. * Returns path to the `DocumentFragment` This is always equal to empty array and is added for compatibility reasons
  48. * with {@link engine.model.Element}.
  49. *
  50. * @returns {Array} The path.
  51. */
  52. getPath() {
  53. return [];
  54. }
  55. /**
  56. * Gets child at the given index.
  57. *
  58. * @param {Number} index Index of child.
  59. * @returns {engine.model.Node} Child node.
  60. */
  61. getChild( index ) {
  62. return this._children.get( index );
  63. }
  64. /**
  65. * Gets the number of top-level elements of DocumentFragment.
  66. *
  67. * @returns {Number} The number of top-level elements.
  68. */
  69. getChildCount() {
  70. return this._children.length;
  71. }
  72. /**
  73. * Gets index of the given child node.
  74. *
  75. * @param {engine.model.Node} node Child node.
  76. * @returns {Number} Index of the child node.
  77. */
  78. getChildIndex( node ) {
  79. return this._children.indexOf( node );
  80. }
  81. /**
  82. * Inserts a child node or a list of child nodes at the end of this DocumentFragment.
  83. *
  84. * @param {engine.model.NodeSet} nodes The list of nodes to be inserted.
  85. */
  86. appendChildren( nodes ) {
  87. this.insertChildren( this.getChildCount(), nodes );
  88. }
  89. /**
  90. * Inserts a list of child nodes on the given index and sets the parent of these nodes to this DocumentFragment.
  91. *
  92. * @param {Number} index Position where nodes should be inserted.
  93. * @param {engine.model.NodeSet} nodes The list of nodes to be inserted.
  94. */
  95. insertChildren( index, nodes ) {
  96. let nodeList = new NodeList( nodes );
  97. for ( let node of nodeList._nodes ) {
  98. node.parent = this;
  99. }
  100. // Clean original DocumentFragment so it won't contain nodes that were added somewhere else.
  101. if ( nodes instanceof DocumentFragment ) {
  102. nodes._children = new NodeList();
  103. }
  104. this._children.insert( index, nodeList );
  105. }
  106. /**
  107. * Removes number of child nodes starting at the given index and set the parent of these nodes to `null`.
  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.model.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. }