Highlighters, comment threads on a document, "copy this quote" buttons and find-in-page all describe a stretch of text by two boundary points: a node and an offset where the stretch starts, and another where it ends. The two points can sit in different elements at different depths, so reading the text between them means walking the tree in document order and cutting the first and last text nodes.
Write textBetween(start, end) in textbetween.js. It returns what a Range from start to end would return from toString(), without using Range.
functiontextBetween(start, end) // returns a string
start, end: boundary points { node, offset }. In a text node or a comment, offset counts characters. In any other node (an element, a fragment), it counts child nodes: (list, 1) is the gap between the list's first and second child.
The result joins the text of every text node between the two points, in document order, with the first and last cut at the offsets.
The points may be in different elements at any depth; element and text boundaries can be mixed.
Only text nodes count: comments add nothing, though a comment can hold a boundary point. Text inside <script>, <style> and hidden elements counts, as in textContent.
When end is the same point as start or comes before it, the result is "".
When the points are in different trees (no common root), the result is "".
Trees that are not in the page (a detached element, a DocumentFragment) work the same way.
An offset below 0 or greater than the node's length (characters, or child nodes) throws a DOMException named "IndexSizeError". Offsets are whole numbers.
The tree is not changed: no text nodes are merged or split.
Plain JavaScript in a browser page; the tests call the global textBetween on trees they build and compare each result with a real Range.
Range, document.createRange and getSelection are not allowed: the tests switch them off during each call. compareDocumentPosition, contains and tree walking are fine.
Points in doctype nodes and in shadow trees are out of scope.