문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top