How would I convert a set of token ranges inside a jQuery selection, to a set of rangy ranges?
For example I have this:
<div class="test-input">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas
convallis dui id erat pellentesque et rhoncus nunc semper. Suspendisse
malesuada {hendrerit velit nec }tristique. Aliq{uam gravida mauris at
ligula venenatis rhoncus. Suspendisse inter}dum, nisi nec consectetur
pulvinar, lorem augue ornare felis, vel lacinia erat nibh in ve{lit.
</p>
<p>
Hendr}erit, felis ac fringilla lobortis, massa ligula aliquet justo, sit
amet tincidunt enim quam {sollicitudin} nisi. Maecenas ipsum augue,
commodo sit amet aliquet ut, laoreet ut nunc. Vestibulum ante ipsum
primis in {fauc}ibus orci luctus et ultrices posuere cubilia Curae;
Pellentesque tincidunt eros quis tellus laoreet ac dignissim turpis
luctus. Integer nunc est, {pulvinar ac tempor ac, pretium ut odio.
</p>
<p>
Pellentesque in arcu sit amet} odio scelerisque tincidunt. Lorem ipsum
dolor sit amet, consectetur adipiscing elit. Pellentesque habitant morbi
tristique senectus et netus et malesuada fames ac turpis egestas.
</p>
</div>
And I want to convert the text between {
and }
into ranges (and remove the tokens).
I tried using this:
function tokensToRanges(element) {
element = $(element);
var node = element.get(0);
var ranges = [];
do {
var text = $(node).text(),
start = text.indexOf('{'),
end = text.indexOf('}') - 1,
input = null;
input = node.innerHTML.replace('{', '').replace('}', '');
element.html(input);
var range = rangy.createRange();
range.selectCharacters(node, start, end);
ranges.push(range);
} while ($(node).text().indexOf('{') != -1);
return ranges;
}
But it the ranges are not correct. I think the selectCharacters
method ignores whitespace.
Also I would prefer not to use the TextRangeModule
if possible.