I'm sorry I can't answer these questions right away: I'm at work right
now and anything requiring me to looking at it for more than a couple
of minutes has to wait until my free time. I will do my best to look
at it this evening (UK time).
Tim
You can do this by essentially reversing the original process. It's
annoyingly fiddly though. Example code is below. Demo is here:
http://jsfiddle.net/URms2/
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="currentrelease/rangy-core.js" type="text/javascript"></script>
<script src="currentrelease/rangy-cssclassapplier.js"
type="text/javascript"></script>
<script src="http://code.jquery.com/jquery-1.7.1.js"
type="text/javascript"></script>
<script src="currentrelease/rangy-selectionsaverestore.js"
type="text/javascript"></script>
<style>
.spans {
border: solid green 1px;
}
</style>
<script type="text/javascript">
function makeSelectionRed() {
var savedSel;
rangy.init();
var randomCssClass = "rangyTemp_" + (+new Date());
var classApplier = rangy.createCssClassApplier(randomCssClass, true);
classApplier.applyToSelection();
// Now use jQuery to add the CSS colour and remove the class
$("." + randomCssClass)
.css({"background-color": "red" })
.removeClass(randomCssClass)
.addClass("spans");
}
function getSelectionContainerElement() {
var sel = rangy.getSelection(), selectionContainerElement = null;
if (sel.rangeCount) {
selectionContainerElement =
sel.getRangeAt(0).commonAncestorContainer;
if (selectionContainerElement.nodeType == 3) {
selectionContainerElement =
selectionContainerElement.parentNode;
}
}
return selectionContainerElement;
}
function resetSel() {
rangy.init();
var randomCssClass = "spans";
var sel = rangy.getSelection();
if (sel.rangeCount) {
var selectionContainerElement =
sel.getRangeAt(0).commonAncestorContainer;
if (selectionContainerElement.nodeType == 3) {
selectionContainerElement =
selectionContainerElement.parentNode;
}
var $redSpans =
$(selectionContainerElement).find("*").andSelf().filter(function() {
return
/^(red|rgb\(255,\s*0,\s*0\))$/i.test($(this).css("background-color"));
});
// Add a class and remove the background colour using jQuery
$redSpans.addClass(randomCssClass).css("background-color", "");
// Now unapply class to selection using Rangy
var classApplier =
rangy.createCssClassApplier(randomCssClass, true);
classApplier.undoToSelection();
// Finally, add the red background back in for the
remaining elements with the random class
$("." + randomCssClass).css({"background-color": "red" })
}
}
</script>
</head>
<body>
<form id="form1" action="ravi.html">
<div>
asdasdasdasdasdklasdnasd kjsdhskjsah dhdasjdhasdkjashdk
adhasjhda
<input type="button" onclick="makeSelectionRed()" value="make red">
<input type="button" onclick="resetSel()" value="reset"></div>
</form>
</body>
</html>