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

dom-seek

v5.1.1

Published

Text traversal for HTML documents.

Downloads

9,479

Readme

DOM Seek

License NPM Package Build Status Coverage Status

POSIX has lseek(2). Now the browser has dom-seek.

This library can answer two questions:

  • What is the offset of a given TextNode within a text?

  • Which TextNode within a text contains the given offset?

Installation

Using npm:

npm install dom-seek

Usage

seek(iter, where)

Adjust the position of a NodeIterator by an offset measured in text code units or to the position immediately before a target node.

If the whatToShow attribute of iter is any value other than NodeFilter.SHOW_TEXT, throw an InvalidStateError DOMException.

If where is a positive integer, seek the iterator forward until the sum of the text code unit lengths of all nodes that the iterator traverses is as close as possible to where without exceeding it.

If where is a negative integer, seek the iterator backward until the sum of the text code unit lengths of all nodes that the iterator traverses is as close as possible to the positive value of where without exceeding it.

If where is a node, seek the iterator forward or backward until its pointer is positioned immediately before the target node.

If where is any other value, throw a TypeError exception.

Return the number of text code units between the initial and final iterator positions. This number will be negative when the traversal causes the iterator to traverse backward in document order.

If the where argument specifies a target beyond the bounds of the root attribute of the iterator, throw a RangError exception.

After this function returns, the pointerBeforeReferencNode property of the iterator should be true. The function may return a value less than where if returning where exactly would result in the iterator pointing after the last text node that its root node contains.

Browser Support

Use the dom-node-iterator module for a portable NodeIterator polyfill if targeting browsers that lack a full implementation that includes the referenceNode and pointerBeforeReferenceNode properties.

Example

Often, when searching for text strings in HTML documents, authors will traverse a document and look at the text of the leaf Elements. However, when the search pattern is split across element boundaries, the problem is harder.

Below is an example of using seek to highlight a string in a document, even if that string is split across element boundaries.

var text = 'ipsum';

// Find the text.
var offset = document.body.textContent.indexOf(text);
var length = text.length;

// Create a NodeIterator.
var iter = document.createNodeIterator(document.body, NodeFilter.SHOW_TEXT);

// Seek the iterator forward by some amount, splitting the text node that
// contains the destination if it does not fall exactly at a text node boundary.
function split(where) {
  var count = seek(iter, where);
  var remainder = where - count;

  if (remainder) {
    // Split the text at the offset
    iter.referenceNode.splitText(remainder);

    // Seek to the exact offset
    seek(iter, remainder);
  }

  return iter.referenceNode;
}

// Find split points
var start = split(offset);
var end = split(length);

// Walk backwards, collecting all the nodes
var nodes = [end];
while (iter.referenceNode !== start) {
  nodes.unshift(iter.previousNode());
}

// Highlight all the nodes.
for (var i = 0 ; i < nodes.length ; i++) {
  var node = nodes[i];

  // Create a highlight
  var highlight = document.createElement('mark');

  // Wrap it around the text node
  node.parentNode.replaceChild(highlight, node);
  highlight.appendChild(node);
}