hpq is a utility library designed to parse HTML strings or existing DOM elements and query their content into a structured JavaScript object. The current stable version is 1.4.0, released in March 2023, indicating a mature project with a maintenance-focused release cadence, primarily addressing minor improvements and type definitions. It operates by allowing developers to define an object shape, where values are matcher functions (e.g., `attr`, `text`, `query`) that extract specific data points from the HTML. Its key differentiator is this declarative, object-shape-driven approach to data extraction, inspired by tools like gdom, making it particularly useful for screen scraping or data transformation within a browser environment.
npm install hpqVerified import paths — ran on the pinned version, not inferred.
Demonstrates parsing HTML to extract specific attributes and text content into a structured object using various matcher functions.
When running in Node.js, integrate a DOM simulation library like `jsdom`. For example, create a JSDOM instance and pass its `window.document` to a function that uses hpq, or configure global `window` and `document`.
Implement explicit checks for `undefined` or empty arrays in your consuming code if non-existence of elements is a possibility and requires specific handling.
Ensure that the HTML input is well-formed. Consider pre-processing HTML with a robust HTML parser if dealing with potentially malformed external sources.
Install `jsdom` (`npm install jsdom`) and configure it to provide a global `document` and `window` object before calling hpq functions. Example: `const { JSDOM } = require('jsdom'); const dom = new JSDOM('<!DOCTYPE html><html><body></body></html>'); global.document = dom.window.document;`Verify that the `source` passed to `hpq.parse` is a valid HTML string or a DOM `Element` and that subsequent selectors within `query` calls correctly target existing elements.
No dependency data recorded yet.