开发者

Can I get access to htmlText setter code in textArea in Flex?

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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜