DOM Walk

0.1.2 · abandoned · verified Sun Apr 19

dom-walk is a minimalistic, client-side utility library designed for iteratively traversing a DOM node and its children. Currently at version 0.1.2, it was last published approximately six years ago (as of early 2020), indicating it is no longer actively maintained. Despite its age and low version number, it reports a surprisingly high weekly download count, suggesting it is a transitive dependency in many projects or widely used in legacy applications. The library provides a single `walk` function that takes a list of nodes and a callback to execute for each node. Its simplicity and small footprint were advantageous in older web development contexts, but its lack of ongoing development and reliance on CommonJS make it less suitable for modern projects that prioritize ESM, TypeScript, and active community support. Alternatives or native DOM traversal methods are generally preferred for new development.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import and use the `walk` function to traverse DOM nodes, logging the type and content of each visited node.

import walk from 'dom-walk';

// Ensure this code runs in a browser environment with a DOM.
// For Node.js, you would need a JSDOM-like environment.

function setupAndWalkDOM() {
  // Create some dummy DOM structure for the example
  document.body.innerHTML = `
    <div id="root">
      <p>Paragraph 1</p>
      <div>
        <span>Span 1</span>
        <p>Paragraph 2</p>
      </div>
    </div>
  `;

  const nodesToWalk = document.body.childNodes;

  console.log('Starting DOM walk...');
  walk(nodesToWalk, function (node) {
    if (node.nodeType === Node.ELEMENT_NODE) {
      console.log("Visited element node:", node.tagName.toLowerCase(), 'id:', node.id);
    } else if (node.nodeType === Node.TEXT_NODE && node.textContent.trim().length > 0) {
      console.log("Visited text node:", node.textContent.trim());
    }
  });
  console.log('DOM walk complete.');
}

// Execute the setup and walk once the DOM is ready
if (document.readyState === 'loading') {
  document.addEventListener('DOMContentLoaded', setupAndWalkDOM);
} else {
  setupAndWalkDOM();
}

view raw JSON →