Skip to curriculum
Jump to the editor

17.7. Read the text between two nodes

Hard
Topics
35–60 min

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.

Signature

JAVASCRIPT
function textBetween(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.

Requirements

  1. 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.
  2. The points may be in different elements at any depth; element and text boundaries can be mixed.
  3. 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.
  4. When end is the same point as start or comes before it, the result is "".
  5. When the points are in different trees (no common root), the result is "".
  6. Trees that are not in the page (a detached element, a DocumentFragment) work the same way.
  7. An offset below 0 or greater than the node's length (characters, or child nodes) throws a DOMException named "IndexSizeError". Offsets are whole numbers.
  8. The tree is not changed: no text nodes are merged or split.
  9. It handles trees with thousands of text nodes.

Examples

JAVASCRIPT
// <p id="p">Version 2 adds <b>offline <i>drafts</i></b> and faster search.</p>
const p = document.getElementById("p");
const first = p.firstChild;               // "Version 2 adds "
const drafts = p.querySelector("i").firstChild;

textBetween({ node: first, offset: 8 }, { node: drafts, offset: 3 });
// "2 adds offline dra"
JAVASCRIPT
// <ul id="list"><li>one</li><li>two</li><li>three</li></ul>
textBetween({ node: list, offset: 1 }, { node: list, offset: 2 }); // "two"
textBetween({ node: list, offset: 0 }, { node: list, offset: 3 }); // "onetwothree"
JAVASCRIPT
textBetween({ node: drafts, offset: 3 }, { node: first, offset: 0 }); // "": end before start
textBetween({ node: first, offset: 99 }, { node: first, offset: 0 }); // throws IndexSizeError

Environment

  • 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.

Hints

  1. Can you write a function that says whether one boundary point comes before another? Think about the case where one point's node contains the other's.
  2. Apart from the two nodes that hold the points, can a text node be partly inside the stretch?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…