Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.

hypothesis/dom-anchor-text-position

 
 

Repository files navigation

Text Position Anchor

License NPM Package Build Status Coverage Status

This library offers conversion between a DOM Range instance and a text position selector as defined by the Web Annotation Data Model.

For more information on Range see the documentation.

For more information on the fragment selector see the specification.

Installation

There are a few different ways to include the library.

With a CommonJS bundler, to require('dom-anchor-text-position'):

npm install dom-anchor-text-position

With a script tag, include one of the scripts from the dist directory. For this to work, you will also need to include the compatibility library node-iterator-shim.

With AMD loaders, these scripts should also work.

Usage

API Documentation

The module exposes a single constructor function, TextPositionAnchor.

new TextPositionAnchor(root, start, end)

This constructor creates a new TextPositionAnchor. The arguments describe textual offsets within the root Element. All arguments are required.

TextPositionAnchor.fromRange(root, range)

Provided with an existing Range instance this will return a TextPositionAnchor that stores the offsets of the beginning and end of the text selected by the range as measured from the beginning of the root Element.

TextPositionAnchor.fromSelector(root, selector)

Provided with an Object containing start and end keys this will return a TextPositionAnchor that corresponds to these offsets within the root Element.

TextPositionAnchor.prototype.toRange()

This method returns a Range object that selects the text corresponding to the substring of the text content of the anchor root over the interval [start, end).

TextPositionAnchor.prototype.toSelector()

This method returns an Object that has keys type, start, and end where type is "TextPositionSelector" and the start and end keys have values corresponding to the stored start and end offsets of the anchor.

About

Convert between DOM Range instances and text positions.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%