Search code examples
javascripthtmlrangeselectionrangy

Rangy manipulation DOM in loop of ranges


I have a set of ranges, created from the tokensToRanges function in:

Convert tokens into selection ranges

Now I want to loop over each range, and apply an action to them. However I get an error:

Uncaught Error: Range error: Range is no longer valid after DOM mutation ([WrappedRange("foobar":6)])

Now I understand why I am getting the error, but I am wondering how can I loop over a set of ranges, and manipulate each one?


Solution

  • It's a tricky problem that I've run up against several time. Your options are basically:

    1. Convert the list of ranges to a list of range boundaries. For each pair of boundaries, create a range, do your DOM manipulation and adjust any of the remaining range boundaries that need adjusting.
    2. Refactor the code that generated your list of ranges to give you one range at a time.