documentfragment.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.treeModel
  12. */
  13. export default class DocumentFragment {
  14. /**
  15. * Creates empty DocumentFragment.
  16. *
  17. * @param {engine.treeModel.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.treeModel.NodeSet} engine.treeModel.DocumentFragment#_children
  25. */
  26. this._children = new NodeList();
  27. if ( children ) {
  28. this.insertChildren( 0, children );
  29. }
  30. }
  31. /**
  32. * Gets child at the given index.
  33. *
  34. * @param {Number} index Index of child.
  35. * @returns {engine.treeModel.Node} Child node.
  36. */
  37. getChild( index ) {
  38. return this._children.get( index );
  39. }
  40. /**
  41. * Gets the number of top-level elements of DocumentFragment.
  42. *
  43. * @returns {Number} The number of top-level elements.
  44. */
  45. getChildCount() {
  46. return this._children.length;
  47. }
  48. /**
  49. * Gets index of the given child node.
  50. *
  51. * @param {engine.treeModel.Node} node Child node.
  52. * @returns {Number} Index of the child node.
  53. */
  54. getChildIndex( node ) {
  55. return this._children.indexOf( node );
  56. }
  57. /**
  58. * Inserts a child node or a list of child nodes at the end of this DocumentFragment.
  59. *
  60. * @param {engine.treeModel.NodeSet} nodes The list of nodes to be inserted.
  61. */
  62. appendChildren( nodes ) {
  63. this.insertChildren( this.getChildCount(), nodes );
  64. }
  65. /**
  66. * Inserts a list of child nodes on the given index and sets the parent of these nodes to this DocumentFragment.
  67. *
  68. * @param {Number} index Position where nodes should be inserted.
  69. * @param {engine.treeModel.NodeSet} nodes The list of nodes to be inserted.
  70. */
  71. insertChildren( index, nodes ) {
  72. let nodeList = new NodeList( nodes );
  73. for ( let node of nodeList._nodes ) {
  74. node.parent = this;
  75. }
  76. // Clean original DocumentFragment so it won't contain nodes that were added somewhere else.
  77. if ( nodes instanceof DocumentFragment ) {
  78. nodes._children = new NodeList();
  79. }
  80. this._children.insert( index, nodeList );
  81. }
  82. /**
  83. * Removes number of child nodes starting at the given index and set the parent of these nodes to `null`.
  84. *
  85. * @param {Number} index Position of the first node to remove.
  86. * @param {Number} [howMany=1] Number of nodes to remove.
  87. * @returns {engine.treeModel.NodeList} The list of removed nodes.
  88. */
  89. removeChildren( index, howMany = 1 ) {
  90. let nodeList = this._children.remove( index, howMany );
  91. for ( let node of nodeList._nodes ) {
  92. node.parent = null;
  93. }
  94. return nodeList;
  95. }
  96. }