« Adobe Player Security Update
» Strip Html Tags - with allowable tags

ActionScript, How to

Find Cursor Position in a HtmlText Object (RichTextEditor, TextArea, TextField) - UPDATE

26.03.08 | 11 Comments

Table of contents

  1. Find Cursor Position in a HtmlText Object (RichTextEditor, TextArea, TextField)
  2. Find Cursor Position in a HtmlText Object (RichTextEditor, TextArea, TextField) - UPDATE

In the previous article I came out with a function that calculates the position of the cursor in the htmlText which is different than the position in the normal text. Rick draw my attention in his comments to some flaws which are correct: the &amp; and other similar special chars are not counted correctly and the new lines are replaced by <p> and not counted.

For this new article I studied the problem and remodeled the calculateHtmlPosition function from the previous post and now more than one type of tag opening and ending are possible (see openTags and closeTags arrays). Here it is (comments are in the code):

public function calculateHtmlPosition(htmlstr:String, pos:int):int
{
    // we return -1 (not found) if the position is bad
    if (pos <= -1)
        return -1;
 
    // characters that appears when a tag starts
    var openTags:Array = ["<","&"];
    // characters that appears when a tag ends
    var closeTags:Array = [">",";"];
    // the tag should be replaced with
    // ex: &amp; is & and has 1 as length but normal 
    // tags have 0 length
    var tagReplaceLength:Array = [0,1];
    // flag to know when we are inside a tag
    var isInsideTag:Boolean = false;
    var cnt:int = 0;
    // the id of the tag opening found
    var tagId:int = -1;
    var tagContent:String = "";
 
    for (var i:int = 0; i < htmlstr.length; i++)
    {
        // if the counter passes the position specified
        // means that we reach the text position
        if (cnt>=pos) 
            break;
        // current char	
        var currentChar:String = htmlstr.charAt(i);
        // checking if the current char is in the open tag array
        for (var j:int = 0; j < openTags.length; j++)
        {
            if (currentChar == openTags[j])
            {
                // set flag
                isInsideTag = true;
                // store the tag open id
                tagId = j;
            }
        }
        if (!isInsideTag)
        {
            // increment the counter
            cnt++;
        } else {
            // store the tag content
            // needed afterwards to find new lines
            tagContent += currentChar;
        }
        if (currentChar == closeTags[tagId]) {
            // we ad the replace length 
            if (tagId > -1) cnt += tagReplaceLength[tagId];
            // if we encounter the </P> tag we increment the counter
            // because of new line character
            if (tagContent == "</P>") cnt++;
            // set flag 
            isInsideTag = false;
            // reset tag content
            tagContent = "";
        }
    }
    // return de position in html text
    return i;
}

The modified working example is bellow (pressing “Add _” button an underscore will be added at the cursor position):


Rick thanks for you comments and credit goes to you.

Future development can be done to be able to add over a selected part of text.

UPDATE: JBI had special situation and he explained it in his comment bellow, so please take a look - it may save you much trouble.

Popularity: 95%

Share and Enjoy:
  • Technorati
  • StumbleUpon
  • del.icio.us
  • NewsVine
  • Reddit
  • Digg
  • Furl
  • co.mments
  • blogmarks
  • Slashdot
  • DZone
  • Taggly
  • YahooMyWeb
  • connotea
  • Webride
RTE_test_update_source
RTE_test_update




Tags: , , ,

This post was written by Andrei Ionescu

Views: 3329

related

popular

11 Comments

have your say

Add your comment below, or trackback from your own site. Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

:

:


« Adobe Player Security Update
» Strip Html Tags - with allowable tags