views:

30

answers:

2

Greetings, I can see that when I set htmlText in a textarea control, the text property contains the html free version of the text. So there is a parser somewhere that is ripping of html from the content, which would be very usefull for my purposes. However, based on the flex source code, the setting of html is done in UITextField.as, which is the type of the textfield member of TextArea. The line that does the work is: super.htmlText = value; in function override public function set htmlText(value:String):void

Trying to follow the class hieararchy, I end up in FlexTextField class, which extends flash player's textfield class.

It appears the functionality I am after is in flash player. So is there any way of accessing this html cleaning function? Am I missing something here?

Best Regards Seref

+1  A: 

Nope, there is no way to access code in the Flash Player native classes.

However, you should be able to use a Regex to easily parse out all HTML Tags in your content.

Although, not tested, here is one option that came up in Google. You can just use that in Flex. So, you can probably do something like this:

var regEx : RegExp = new RegExp('<(.|\n)*?>');
var noHTMLText = htmlText.replace(regEx , '');

Of course, this was written in the browser. More info on Regex in Flex.

www.Flextras.com
Thanks, just a quick q: as far as I know, there is no replace all in actionscript 3, so the replace below should rather be a split(...).join(""), right?
To replace all in ActionScript w/ regex, I believe you can specify the 'g' flag. Like this: new RegExp('<(.|\n)*?>','g');
www.Flextras.com
Ah, this is handy, thanks!
+1  A: 

In case regex fails you, here is something you might want to check out:

var t:String = "asd <span>This is <font>some</font> text <b> :) </b> </span> \nand more";
function stripTags(x:XML):String {
  var t:String = "";
  var children:XMLList = x.children();
  var child:XML;
  for(var i:Number = 0; i < children.length(); i++){
    child = children[i];
    if(child.nodeKind() == "text")
      t += child.toString();
    else if(child.nodeKind() == "element")
      t += stripTags(child);
  }
  return t;
}
var x:XML = new XML("<root>" + t + "</root>");
XML.ignoreWhitespace = false;
var s:String = stripTags(x);
trace(s);

PS: I haven't tested this ActionScript code, here is the equivalent JavaScript code that I tested and found working. I assume it would work in ActionScript since both follow ECMAScript.

var t = "asd <span>This is <font>some</font> text <b> :) </b> </span> \nand more";
function stripTags(str){
  function strip(x){
    var t = "";
    var children = x.children();
    var child;
    for(var i = 0; i < children.length(); i++){
      child = children[i];
      if(child.nodeKind() == "text")
        t += child.toString();
      else if(child.nodeKind() == "element")
        t += strip(child);
    }
    return t;
  }
  var xml = new XML("<root>" + str + "</root>");
  XML.ignoreWhitespace = false;
  return strip(xml);
}
var s = stripTags(t);
console.log(s);

output:

asd This is some text :)  
and more
Amarghosh
Thanks, I think this should work just fine for me.