I’m trying to add classes to selected text in a document. The problem is that I don’t want the user to be able to select ALL the text on the page (using command+A for example…) So I would like to remove nodes from a range but I don’t know how to do that. This page has a lot of text / nested divs so using the added class for checking if it is in an element that is selectable would be very time consuming / slow.
I made a fiddle to explain what I’m trying to do a bit better.
http://jsfiddle.net/thomasjonas/BhKFt/20/
If you select all the text in this example the class is added to all the divs. I want it to only apply the css to the divs with the ‘yes’ class. Is there a nice, non-browser-crashing solution for this?
Thanks in advance!
The following is a little heavy-handed and inefficient because it checks each element with class “yes” intersects the selection. You could improve it by checking in advance whether the selection lies completely within a single element with the “yes” class, for example. It uses Rangy’s proprietary
intersection()method of range objects.Demo: http://jsfiddle.net/timdown/BhKFt/23/
Code:
It may be useful if there was some kind of filtering option to the options object passed into
rangy.createCssClassApplier(). I’ll have a think.