cognitive-engineering-lab / rust-book

The Rust Programming Language: Experimental Edition
https://rust-book.cs.brown.edu
Other
504 stars 82 forks source link

Sometimes text highlighting extends to the bottom of the page when reloading. #114

Closed DemonQilin closed 11 months ago

DemonQilin commented 11 months ago

Description of the problem:

Sometimes when highlighting text and reloading the page, the target text is no longer highlighted but instead all text following it is highlighted to the end.

I don't have a clear individual replication path, but I copied my highlights from the localStorage to Chrome, Firefox and Edge and in all of them the same error happened.

By having the highlights and visiting the following sections:

The pages look like this: image

And the following error appears in the console: image

Suggested fix:

I have identified that the error occurs within the highlightIsValid function of the utils.ts file on line 67, where the innerHTML of the first element of the dom_nodes array is readed but this array is empty because the validation of line 64 uses the statement if (!dom_nodes) return false; which only validates that it is not falsy but not its length.

Therefore it should be corrected by replacing this validation with:

export const highlightIsValid = (highlighter: Highlighter, highlight: HighlightSource) => {
  /*other code*/

  // if highlight has no DOM node, filter
  if (!dom_node?.length) return false;
};
willcrichton commented 11 months ago

This will be fixed in #117. Thanks for finding the issue!