Saving caretRange unaffectable by CssClassApplier

33 views
Skip to first unread message

Sascha Schmidt

unread,
Nov 5, 2013, 12:09:47 PM11/5/13
to ra...@googlegroups.com
Hi.
Is there a way to use rangy to save the caret position so that it won't be affected by the CssClassApplier. Right now I'm using
start = document.caretRangeFromPoint(touch.pageX, touch.pageY);
to get the start of my range. If I'm later using the CssClassApplier on text that lies before the position start shifts around in the text.
The use case is very similar to text selection by mouse.

Tim Down

unread,
Nov 6, 2013, 6:31:41 PM11/6/13
to rangy
Hi Sascha,

You could use the selection save and restore module, which works by inserting hidden markers element in the DOM. These marker elements are removed when you restore the selection. These markers would be unaffected by the class applier.

https://code.google.com/p/rangy/wiki/SelectionSaveRestoreModule

Tim


--
You received this message because you are subscribed to the Google Groups "rangy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rangy+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Sascha Schmidt

unread,
Nov 7, 2013, 12:59:12 PM11/7/13
to ra...@googlegroups.com, t...@timdown.co.uk
Hi Tim.
Thanks for you reply. This only seems to work for me a few times. I try to preserve the initial position by
rangy.restoreSelection(savedSel); 
var sel = rangy.getSelection();
savedSel = rangy.saveSelection(); 
and then continue with creating a range from sel and applying CSS to it. After 1 or 2 iterations I always get
Rangy warning: Module SaveRestore: Marker element has been removed. Cannot restore selection.
Any ideas on how to find out whats cause this?

greetings

Sascha Schmidt

unread,
Nov 8, 2013, 12:11:04 PM11/8/13
to ra...@googlegroups.com, t...@timdown.co.uk
Hi again.
So I managed to do it using the TextRange Module from rangy 1.3.
Strangely enough the result only seems to work if I use console.log somewhere during my highlighting process, but this should rater be a problem of my code than of rangy.
Thanks a lot.
Reply all
Reply to author
Forward
0 new messages