I get an error (don't thing that is in rangy) when y use setEnd in some cases

150 views
Skip to first unread message

Matías Basilico

unread,
Oct 3, 2012, 8:47:19 AM10/3/12
to ra...@googlegroups.com

I read documentation, from DOM w3c too...I couldn't yet finish to do a union between range objects that is succesfull in all cases.

I get an error:
Error: IndexSizeError: Index or size is negative or greater than the allowed amount
when I use setEnd in a Range Object.

My scenario is that when I detect that two Ranges intersects I want to do an union. I try with range.union but fails in some cases so I went with the setEnd and setStart methods, but they fails too in some cases.

I think the problem is that sometimes the (endContainer, endOffset) or (startContainer, startOffset) are in "character system" or in "DOM element system" but I'm not sure...Could be that the case that sometimes I'm comparing different types of points? In such a case how can I normalize that so I can do the union succesfull in all cases?.

Thanks and I hope you could me throw some light :)

Matías Basilico

unread,
Oct 3, 2012, 10:23:13 AM10/3/12
to ra...@googlegroups.com
Maybe I need the new TextRange Module ??

I mean that module unifies the offset unit into character ??

Nico Burns

unread,
Oct 3, 2012, 10:25:47 AM10/3/12
to ra...@googlegroups.com
For your use case, the normal range functions (that you were trying to use) seem more appropriate. Have you got the detection bit working, and what is the error that you are getting?

Matías Basilico

unread,
Oct 3, 2012, 10:37:01 AM10/3/12
to ra...@googlegroups.com
Hi Nico, thanks for your reply.


Here a bit of code I try:

                var isIntersection = xRange.intersectsRange(xRangeActual);
                var isBefore = (xRange.compareBoundaryPoints(Range.START_TO_END, xRangeActual) == -1);
                var isAfter = (xRange.compareBoundaryPoints(Range.END_TO_START, xRangeActual) == 1);                    
               
                if (isIntersection) {
                    if (isBefore) {
                        xRange.setEnd(xRangeActual.endContainer,xRangeActual.endOffset);                       
                    }
                    else {
                        xRange.setStart(xRangeActual.startContainer,xRangeActual.startOffset);                           
                    }
                   
                    var sel = rangy.getSelection();
                    sel.setSingleRange(xRange);
                    xRangeToProcess = {"range":xRange, "selectedString": sel.getRangeAt(0).toString()};
                    sel.removeAllRanges();
                }   


when I get this situation (from Logs):
xRange.startContainer = [object Text]
xRange.startContainer.nodeType = 3

xRange.endContainer = [object HTMLSpanElement]
xRange.endContainer.nodeType = 1

I get the error I mention before:

Error: IndexSizeError: Index or size is negative or greater than the allowed amount

To put in context, I'm doing a firefox jetpack plugin but I don't think that put some rare condition, other intersection detection work well.

When I make the "union" I modify the DOM with some styles (span) to show the user some feedback, may be the DOM modification affect that?

Thanks!

Matías Basilico

unread,
Oct 3, 2012, 8:56:28 PM10/3/12
to ra...@googlegroups.com


Perphaps If I convert the RangyRange object's I collect with every selection to TextRange objects with toCharacterRange, that will help with the intersection detection and union's ??

Still I don't have the solution to detect all the cases so I can grab all the selection from the user and give them feedback.

Nico Burns

unread,
Oct 4, 2012, 9:26:34 AM10/4/12
to ra...@googlegroups.com
I don't think a TextRange will help here, they are just a wrapper around the other ranges anyway. If you are modifying the DOM *before* the union, then that is probably your problem, if it's afterwards then it shouldn't be a problem...

Tim Down

unread,
Oct 4, 2012, 7:20:43 PM10/4/12
to ra...@googlegroups.com
I agree with Nico. I suspect your Rangy ranges are getting out of sync
with the DOM. If you create your Rangy ranges, do some DOM
manipulation and then try and do stuff with those ranges, things can
definitely go wrong because Rangy doesn't update its ranges when the
DOM changes. A Rangy range has an isValid() method you can check, or
better, you can create new ranges after your DOM manipulation and work
with them instead.

Tim

Matías Basilico

unread,
Oct 5, 2012, 7:05:15 AM10/5/12
to ra...@googlegroups.com, t...@timdown.co.uk


Thanks for the comments Nico and Tim, they helped me a lot.
I'm doing that, modifing the DOM before the operations with ranges.

Now I have the last scenario of range inclusions that I not managing very well, but keep researching a bit If I don't find anything usefull "I  will come back" :) jaja

Thanks for your help.

Regards

Nico Burns

unread,
Oct 5, 2012, 8:45:24 AM10/5/12
to ra...@googlegroups.com
In order to avoid problems, you could try using the "Save and Restore" module: http://code.google.com/p/rangy/wiki/SelectionSaveRestoreModule

Save the selection before you modify the DOM, and restore it afterwards. This should work fairly well unless you are reordering DOM elements (which it doesn't sound like you are).
Reply all
Reply to author
Forward
0 new messages