Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8858117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:45:17+00:00 2026-06-14T14:45:17+00:00

How would I convert a set of token ranges inside a jQuery selection, to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:45:19+00:00Added an answer on June 14, 2026 at 2:45 pm

    selectCharacters() does not ignore all white space but it does ignore collapsed white space. For example, if a text node contains three consecutive space characters, only the first contributes to the character count. I may add an option to that method to switch that behaviour off.

    In answer to your question, Rangy’s test suite has a function that does something a bit like what you want, so I’ve adapted it below. Start and end range markers may appear in different nodes.

    Demo: http://jsfiddle.net/timdown/DdeFr/

    Code:

    function RangeInfo() {}
    
    RangeInfo.prototype = {
        setStart: function(node, offset) {
            this.sc = node;
            this.so = offset;
        },
        setEnd: function(node, offset) {
            this.ec = node;
            this.eo = offset;
        },
        toRange: function() {
            var range = rangy.createRange();
            range.setStart(this.sc, this.so);
            range.setEnd(this.ec, this.eo);
            return range;
        }
    };
    
    function getTextNodesIn(node) {
        var textNodes = [];
        function getTextNodes(node) {
            if (node.nodeType === 3) {
                textNodes.push(node);
            } else {
                for (var i = 0, l = node.childNodes.length; i < l; i++) {
                    getTextNodes(node.childNodes[i]);
                }
            }
        }
    
        getTextNodes(node);
        return textNodes;
    }
    
    function tokensToRanges(el) {
        var rangeInfos = [];
        var currentRangeInfo;
        var textNodes = getTextNodesIn(el);
    
        $.each(textNodes, function() {
            var searchStartIndex = 0;
            var searchIndex;
            while ( (searchIndex = this.data.indexOf(currentRangeInfo ? "}" : "{", searchStartIndex)) != -1 ) {
                // Remove the marker. Doing this breaks existing ranges
                // in this node, which is why we use RangeInfo objects
                // instead of ranges
                this.data = this.data.slice(0, searchIndex) + this.data.slice(searchIndex + 1);
                if (currentRangeInfo) {
                    currentRangeInfo.setEnd(this, searchIndex);
                    rangeInfos.push(currentRangeInfo);
                    currentRangeInfo = null;
                } else {
                    currentRangeInfo = new RangeInfo();
                    currentRangeInfo.setStart(this, searchIndex);
                }
                searchStartIndex = searchIndex;
            }
        });
    
        // Convert RangeInfos into ranges
        var ranges = [];
        $.each(rangeInfos, function() {
            ranges.push(this.toRange());
        });
    
        return ranges;
    }
    
    var ranges = tokensToRanges(document.body);
    var applier = rangy.createCssClassApplier("highlight");
    applier.applyToRanges(ranges);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to convert a set of C# classes (class library) to SQL tables
let's say I have a class that does some calculations. This set of calculations
From Sun's Java Tutorial , I would have thought this code would convert a
I would like to convert from a set to a List within a method
How would I convert this kind of expression in VB.NET? I'm stomped! bool exists=repo.Exists<Post>(x=>x.Title==My
how would you convert this array: Array ( [0] => Array ( [Contact] =>
How would I convert a BitmapImage to a System.Windows.Media.Brush ? I have a BitmapImage
I have a result set that is being returned using this code: while ($row
Given this 3D bar graph sample code , how would you convert the numerical
Has anyone had any luck with a vba macro that would convert this input:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.