You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.3 KiB

4 years ago
  1. import {dashToCamelCase} from './strings'
  2. /**
  3. * Get all the element attributes as object
  4. * @param {HTMLElement} element - DOM node we want to parse
  5. * @returns {Object} all the attributes found as a key value pairs
  6. */
  7. export function DOMattributesToObject(element) {
  8. return Array.from(element.attributes).reduce((acc, attribute) => {
  9. acc[dashToCamelCase(attribute.name)] = attribute.value
  10. return acc
  11. }, {})
  12. }
  13. /**
  14. * Move all the child nodes from a source tag to another
  15. * @param {HTMLElement} source - source node
  16. * @param {HTMLElement} target - target node
  17. * @returns {undefined} it's a void method ¯\_()_/¯
  18. */
  19. // Ignore this helper because it's needed only for svg tags
  20. export function moveChildren(source, target) {
  21. if (source.firstChild) {
  22. target.appendChild(source.firstChild)
  23. moveChildren(source, target)
  24. }
  25. }
  26. /**
  27. * Remove the child nodes from any DOM node
  28. * @param {HTMLElement} node - target node
  29. * @returns {undefined}
  30. */
  31. export function cleanNode(node) {
  32. clearChildren(node.childNodes)
  33. }
  34. /**
  35. * Clear multiple children in a node
  36. * @param {HTMLElement[]} children - direct children nodes
  37. * @returns {undefined}
  38. */
  39. export function clearChildren(children) {
  40. Array.from(children).forEach(n => n.parentNode && n.parentNode.removeChild(n))
  41. }