indexof.js 583 B

12345678910111213141516171819202122232425
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module utils/dom/indexof
  7. */
  8. /**
  9. * Returns index of the node in the parent element.
  10. *
  11. * @param {Node} node Node which index is tested.
  12. * @returns {Number} Index of the node in the parent element. Returns 0 if node has no parent.
  13. */
  14. export default function indexOf( node ) {
  15. let index = 0;
  16. while ( node.previousSibling ) {
  17. node = node.previousSibling;
  18. index++;
  19. }
  20. return index;
  21. }