views:

33

answers:

1

Hi... My XHtml that id displayed to the user will be something like below one..

<html iTag="d1e1" version="-//W3C//DTD XHTML 1.1//EN">
 <head iTag="d1e3">
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
  <title iTag="d1e5">Every document must have a title</title> 
  </head>
  <body iTag="d1e9">
  <h1 iTag="d1e11" align="center">Very Important Heading</h1> 
  <b itag="dle13">Hi. Welcome to
   Java world.</b> 
  <p iTag="d1e15">Since this is just a sample, I won't put much text here.</p> 
  </body>
  </html>

If the user is selecting something like "to Java world.Since" on the screen, So selectin begin will be the position of 't'. I need to calculate selection begin with respect to the immediate parent of it alone which means within "b" tag with itag name "dle13", the position of 't'. The same way, selection end is there in the "p" tag with iTag name "dle15".I need seelction end with respect to this tag alone.

For that we are using the below java script code..

if (document.selection) {   
            alert("IE");         
            txt = document.selection.createRange().text;
            var range_all = document.body.createTextRange();
            if (txt!='' )
            {
                // Finding start position
             range = document.selection.createRange();
                range.collapse(true); 
                startItag = range.parentElement().getAttribute("iTag");
             var startParentElement =  range.parentElement();                 
             if(startItag == '' || startItag == null)
             {
                startItag = range.parentElement().parentNode.getAttribute("iTag");
                startParentElement = range.parentElement().parentNode;
             }
             //alert("parentId:" + parentId);
             alert("parent node name for start:" + range.parentElement().nodeName);
                range_all.moveToElementText(startParentElement);
                for (var sel_start = 0; range_all.compareEndPoints('StartToStart', range) < 0; sel_start ++)
                    range_all.moveStart('character', 1);
                range = document.selection.createRange();
                range.collapse(false); 
                range_all = document.body.createTextRange();
                endItag = range.parentElement().getAttribute("iTag");
             var endParentElement =  range.parentElement();                 
             if(endItag == '' || endItag == null)
             {
                endItag = range.parentElement().parentNode.getAttribute("iTag");
                endParentElement = range.parentElement().parentNode;
             }
             alert("parent node name for end:" + range.parentElement().nodeName);
                range_all.moveToElementText(endParentElement);
                for (var sel_end = 0; range_all.compareEndPoints('StartToEnd', range) < 0; sel_end ++)
                  range_all.moveStart('character', 1);
                document.getElementById("startIndex").value= sel_start ;   
                document.getElementById("endIndex").value= sel_end ; 
                document.getElementById("startItag").value= startItag ;   
                document.getElementById("endItag").value= endItag ; 
                alert("sel_start:" + sel_start);
                alert("sel_end:" + sel_end);
             }

But this start and end idex calculations are ignoring the "new line('\r\n')" I need to hav an index with considering '\r\n' as well..

Request you for the help to achive it..

A: 

I'm developing a cross-browser range and selection library that will do this and more: http://code.google.com/p/rangy. There should be a release within a few weeks. In the meantime, you could try IERange: http://code.google.com/p/ierange, which provides a wrapper for IE's TextRange with the same API as the DOM Range found in other browsers.

UPDATE

An early version of Rangy is now available at http://code.google.com/p/rangy

Tim Down