npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

find-dom-nodes-by-content

v4.6.7

Published

Library that allows finding DOM nodes by their content.

Downloads

18

Readme

Easily locate DOM nodes containing specific content within a given context.

🌟 Features

  • Lightweight and Efficient: Minimal footprint with maximum performance.
  • XPath Evaluation: Utilizes XPath for precise node retrieval.
  • Caching: Optional caching mechanism for advanced scenarios.
  • Framework Agnostic: Works with any or no framework.

📦 Installation

# With npm
npm install find-dom-nodes-by-content

# With yarn
yarn add find-dom-nodes-by-content

🚀 Usage

Importing

Works with both ESM and CommonJS syntaxes:

// ESM
import {
  findDOMNodesByContent,
  exposeEvaluatedNodesCacheGlobally,
  clearEvaluatedNodesCache,
} from "find-dom-nodes-by-content";

// CommonJS
const {
  findDOMNodesByContent,
  exposeEvaluatedNodesCacheGlobally,
  clearEvaluatedNodesCache,
} = require("find-dom-nodes-by-content");

Finding DOM Nodes by Content

Retrieve an array of nodes based on content:

const nodes = findDOMNodesByContent({ text: "Hello" }, document.body);

📄 Docs

⚙️ findDOMNodesByContent()

Finds and returns DOM nodes containing the specified content by utilizing XPath. Users can fine-tune the search by providing various configuration options.

Parameters:

  • content (Object): Configuration for content search.

    • text (string): Content to search for within the DOM nodes.

    • textWrapper (string, optional, default = "): Wrapper for the content string in the XPath expression.

    • nodeType (string, optional, default = *): Node type to target in the search.

    • attributes (array, optional): Array of attribute objects to refine the search.

    • logicalOperator (string, optional, default = "and"): How multiple criteria should be combined.

    • nested (boolean, optional, default = false): Specifies whether to include nested nodes.

  • contextElement (Element | Document, optional): The context within which to conduct the search. Defaults to the entire document.

  • config (Object, optional, default = {})
    Detailed configuration object for refining the search:

    • expression (string, optional, default = '')
      An optional custom XPath expression. When provided, it takes precedence over the default content-based search.

    • XPathNSResolver (XPathNSResolver, optional, default = null)
      Namespace resolver for XPath when searching within XML documents with namespaces.

    • XPathResultType (number, optional, default = XPathResult.UNORDERED_NODE_ITERATOR_TYPE)
      Determines the type of result.

      • Examples:
        • XPathResult.UNORDERED_NODE_ITERATOR_TYPE: Returns an unordered iterator.
        • XPathResult.ORDERED_NODE_ITERATOR_TYPE: Returns an ordered iterator.
    • customXPathResult (XPathResult, optional, default = null)
      An optional custom XPath result object.

    • cache (Map, optional, default = null)
      A cache for storing and reusing results. Must be an instance of Map if used.

    • returnIterator (boolean, optional, default = false)
      Determines the return type:

      • true: Returns an XPathResult iterator.
      • false: Returns an array of matching nodes.

Returns: Either an array of DOM nodes or an XPathResult iterator, based on the configuration.

Throws: Specific errors based on the context like unsupported environment, no nodes matching criteria, or cache being not an instance of Map.

⚙️ exposeEvaluatedNodesCacheGlobally()

Exposes a given evaluated nodes cache to the global window object. This facilitates the exposure of a cache from the find-dom-nodes-by-content library to a broader scope for possible debugging or other purposes.

Parameters:

  • cache (Map): The evaluated nodes cache, which should be an instance of Map.
  • config (Object, optional): Configuration options like log.

Throws: Errors if the cache is already exposed or if the provided cache is not an instance of Map.

⚙️ clearEvaluatedNodesCache()

Clears the globally exposed evaluated nodes cache. Useful for ensuring memory efficiency and clearing any globally accessible cache references.

Parameters:

  • config (Object, optional): Configuration options like log.

Throws: An error if the cache is not currently exposed on the global window object.