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.
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.
The module exposes a single constructor function, TextPositionAnchor
.
This constructor creates a new TextPositionAnchor
. The arguments describe
textual offsets within the root Element
. All arguments are required.
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
.
Provided with an Object
containing start
and end
keys this will return
a TextPositionAnchor
that corresponds to these offsets within the root
Element
.
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).
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.