(AS2)
I've got an html text field and I need to be able to display a lot of various special characters in it (this text is coming in via XML). Everything works fine EXCEPT for the less than character, "<"
The field needs to be html so I can do some formatting (line breaks, font size, colors, etc).
No matter what I do - define it as url encoded ( %3C ), as a character entity ( < ), escaped with a \, whatever, it won't display at all in the field. Flash just seems to ignore it.
I've tried escaping the value, and then unescaping in the actual field, but again, nothing. If I escape it, it will correctly show up as %3C, but that doesn't do me any good.
I can't imagine that it's not possible to have a < in an html text field in Flash, but after trying everything I can think of, I sure can't get it to work.
CDATA is my friend, but it doesn't solve the problem.
The character traces just fine in Flash. It's not breaking the XML file and it makes it into Flash without any problem. The problem is INSIDE Flash, displaying the character in an HTML text field.
is it embedded properly?
does the character exist in the font you're using? (I recently had a hell of a time with the Euro symbol not showing up, only to find the character didn't exist in the font, although I imagine a < should be ok)
Try recreating the problem in a simple FLA with just a text field in it to see if you can get it working there, then work out why it's not happening in your larger project.
If the text field isn't set to be HTML, the character shows up fine. The character is embedded in the font.
I'll play with a simplified version - always good advice.
I hate this sort of problem - I must have come across it a hundred times and I never learn what the hell is the right way to deal with textfields.
Just for shits and giggles, here's my actual code (in case I'm doing something obviously wrong that I'm not seeing):
private function createKeyText():Void{ var tmpChars:String = ""; var charCount:Number = 0; var charSize:Number = 9; for (var i:Number=0; i 0){ tmpChars += ""; } tmpChars += keyCharacters[i]; charCount++; } if (charCount == 1){ if (keyOther){ charSize = 8; } else { charSize = 13; } } keytext = this.createTextField("keyText_txt", this.getNextHighestDepth(),0,0,10,0); keytext.autoSize = true; keytext.antiAliasType = "advanced"; keytext.background = false; keytext.selectable = false; keytext.embedFonts = true; keytext.wordWrap = false; keytext.html = true; keytext.backgroundColor = null; keytext.multiline = true; keytext.htmlText = ""+tmpChars+""; keytext.setTextFormat(_root.aText); keytext.setTextFormat(_root.aText); keytext._x = Math.ceil((keyWidth/2)-(keytext._width/2)); keytext._y = Math.ceil((keyHeight/2)-(keytext._height/2)); keyTextColor = new Color(this.keytext); keyTextColor.setRGB(_root.skinArray["keyboard"]["keyboardKeyText"]); };
And I realize that using _root is likely frowned upon these days, especially in a class, but I'm just getting my toes wet with OOP and I'm not exactly up on the best practices just yet 
Are you using ' and "
keytext.htmlText = ""+tmpChars+"";
looks really dodgy
No, it's all double quotes with the ones that should end up in the string being escaped with a
Although I won't argue that it looks dodgy
But it works fine.
I much prefer to use double quotes for everything (escaping ones that need it) rather than mixing double and single. That's always felt really dodgy to me...
well each to there own
please post a cut down version and I am sure someone will take a look, nothing springs to mind at moment.
cheers ;j
"CDATA is my friend, but it doesn't solve the problem."
"It's not breaking the XML file and it makes it into Flash without any problem"
This is NOT what CDATA is for. CDATA is not a quick fix for weird parsing issues. It is a way of telling a parser how to handle subsections of data.
Let's assume, once you have your value from XML, even if you don't use CDATA in your xml (which dangerous when you are expecting <, > and & chars... but anyways...
So you, quite impossibly without CDATA, get the raw string "&%$#/<< >D>D" as a child in XML and you create a variable with that string as the value.
This is where the idea of a document object model shows its strength.
So, lets pretend you don't want left brackets, right brackets and ampersands to be read as HTML in your final destination, you want to just display them as is. Since HTML is a type of extensible mark up, you can hide special characters from the HTML parser with the XML object on the fly.
...
function killSpecialChars(str) { return new XML("<![CDATA["+str+"]]>"); } var tmpChars:String = killSpecialChars("&%$#/<< >D>D");
...
This will cause the XML parser to put parse friendly character equivalents in place of <,> and &. Luckily for us, Adobe decided to replace them with HTML compliant characters when a CDATA tag is encountered.
Your brackets will display after filtering as CDATA in Flash's XML object.
You can avoid all of this by using CDATA tags in text nodes within XML when you expect extended ascii.
Also, backslashing double quotes is a standard, and good practice if just for portability. Not all containers are going to like single and double quotes as a nesting method. Although I am a terrible offender with double and single quotes myself. XD
Originally posted by: persist You can avoid all of this by using CDATA tags in text nodes within XML when you expect extended ascii.
I am using CDATA tags in the text nodes within my XML, which is why I'm not following how this can be avoided.
Here's my XML (abridged):
<?xml version="1.0" encoding="UTF-8"?> <![CDATA[<]]> <![CDATA[,]]> <![CDATA[>]]> <![CDATA[.]]> <![CDATA[?]]> <![CDATA[/]]>
If I trace all of the values being read as they are parsed, I see the "<" show up fine. But once I try to put it in an html text field, no dice. I don't understand how I can make any more use of CDATA than I already am.
Nevermind. I got it.
I see what you were getting at, although it took me a bit.
So using CDATA, in Flash, at the point that I'm adding the character to the string that gets used in the html text field, is the trick.
Sorry for being dense. And thanks for your help.
not dense.
this shit is ridiculously layered. really. flash is an onion of one quirk inside the next.
XD
Originally posted by: JERKSTORE So using CDATA, in Flash, at the point that I'm adding the character to the string that gets used in the html text field, is the trick.
I for one did not get that either - first time I've heard of it too, but it makes perfect sense nwo it's staring me in the face.
Did that fix it for you though Jerkstore?
Yeah, it absolutely fixed the problem.
That's a really handy trick to know too, because I never would have guessed you could apply CDATA inside of Flash. I'll definitely be keeping this one in mind in the future.
If I trace all of the values being read as they are parsed, I see the "<" show up fine. But once I try to put it in an html text field, no dice. I don't understand how I can make any more use of CDATA than I already am.
Something is weird there.
See this:
str = '<?xml version="1.0" encoding="UTF-8"?><![CDATA[<]]><![CDATA[,]]>';
var n = new XML(); n.ignoreWhite = true; n.parseXML(str);
var keytext = this.createTextField("keyText_txt", this.getNextHighestDepth(),10,00,250,100); keytext.html = true; keytext.htmlText = "test: ";
function showTextNode(node) {
if(node.nodeType == "3"){
keytext.htmlText += "<p align=\"center\"><font size=\"14\">"+node.toString()+"</font></p>";
}
for (var i = 0; i<node.childNodes.length; i++) {
showTextNode(node.childNodes[i]);
}
}
showTextNode(n.firstChild);
This works fine. Anyways, I am glad its fixed. Whenever you need parsing tools for html, try and use the XML object to do the heavy lifting. Adobe calling it an XML() object and not a DOMHandler() object limits the perception of its abilities.
Is this for kiosk? Just wondering.
Yeah, i don't know what the problem is, but applying CDATA in Flash definitely fixed it.
This is for a website that does typing tutorials. This flash keyboard will follow along on screen as you type, showing which keys were depressed, and highlighting errors on the key if it's incorrect.
It was mostly done in Flash for aesthetic reasons, since it was proving to be a world of hurt to try and make it look right in IE using only CSS (of course).
You know what I'm not using that you are?
parseXML()
I'm simply loading the data into an XML object, and then accessing it from there.
Maybe that's the culprit.
Although it looks like in your example maybe parseXML turns the string into XML, which in my case wouldn't be necessary since it loads into an XML object?
It's all semi-black magic to me 
Very nice. You're leaving the HTML presentation layer in Flash, and not in the XML is a nice separation. I figure it was a for a virtual keyboard, which is why I thought it was a kiosk.
Leaving the XML as is will allow you to make a ajax version later anyways. Hehe.
I always enjoy helping you get your ideas working, you work on some cool stuff. I hope I don't come off snarky. I try to be thorough. In the past some people have complained I was being to snotty by just posting the code for a fix.
Anyways, good luck with the project, I am sure it rocks.
parseXML is automatically run by the onData event btw and the result is sent to the onLoad event automagically.