I have a Flex/Actionscript 3 application that displays RSS feeds in a Text element. It strips out any HTML formatting present, but it's not handling HTML special entity codes properly -- it's rendering &mdash
as the literal string instead of replacing it with an em-dash, etc. Is there any systematic way I can make it handle those codes properly, or am I going to need to manually replace those strings regex style?
text
and htmlText
handle a limited subset of the characters properly with no further tweaking -- <
, >
,  
, etc. (In fact, text
actually handles them a little better than htmlText
, as the latter treats <
as a literal less-than character.) But there's a much larger set of characters (°
, &mdash
, and many others) that aren't handled properly -- at least, not by default.
Here is one way:
static private var entityMap:Object = { ' ':' ', '¡':'¡', '¢':'¢', '£':'£', '¤':'¤', '¥':'¥', '¦':'¦', '§':'§', '¨':'¨', '©':'©', 'ª':'ª', '«':'«', '¬':'¬', '­':'­', '®':'®', '¯':'¯', '°':'°', '±':'±', '²':'²', '³':'³', '´':'´', 'µ':'µ', '¶':'¶', '·':'·', '¸':'¸', '¹':'¹', 'º':'º', '»':'»', '¼':'¼', '½':'½', '¾':'¾', '¿':'¿', 'À':'À', 'Á':'Á', 'Â':'Â', 'Ã':'Ã', 'Ä':'Ä', 'Å':'Å', 'Æ':'Æ', 'Ç':'Ç', 'È':'È', 'É':'É', 'Ê':'Ê', 'Ë':'Ë', 'Ì':'Ì', 'Í':'Í', 'Î':'Î', 'Ï':'Ï', 'Ð':'Ð', 'Ñ':'Ñ', 'Ò':'Ò', 'Ó':'Ó', 'Ô':'Ô', 'Õ':'Õ', 'Ö':'Ö', '×':'×', 'Ø':'Ø', 'Ù':'Ù', 'Ú':'Ú', 'Û':'Û', 'Ü':'Ü', 'Ý':'Ý', 'Þ':'Þ', 'ß':'ß', 'à':'à', 'á':'á', 'â':'â', 'ã':'ã', 'ä':'ä', 'å':'å', 'æ':'æ', 'ç':'ç', 'è':'è', 'é':'é', 'ê':'ê', 'ë':'ë', 'ì':'ì', 'í':'í', 'î':'î', 'ï':'ï', 'ð':'ð', 'ñ':'ñ', 'ò':'ò', 'ó':'ó', 'ô':'ô', 'õ':'õ', 'ö':'ö', '÷':'÷', 'ø':'ø', 'ù':'ù', 'ú':'ú', 'û':'û', 'ü':'ü', 'ý':'ý', 'þ':'þ', 'ÿ':'ÿ', 'ƒ':'ƒ', 'Α':'Α', 'Β':'Β', 'Γ':'Γ', 'Δ':'Δ', 'Ε':'Ε', 'Ζ':'Ζ', 'Η':'Η', 'Θ':'Θ', 'Ι':'Ι', 'Κ':'Κ', 'Λ':'Λ', 'Μ':'Μ', 'Ν':'Ν', 'Ξ':'Ξ', 'Ο':'Ο', 'Π':'Π', 'Ρ':'Ρ', 'Σ':'Σ', 'Τ':'Τ', 'Υ':'Υ', 'Φ':'Φ', 'Χ':'Χ', 'Ψ':'Ψ', 'Ω':'Ω', 'α':'α', 'β':'β', 'γ':'γ', 'δ':'δ', 'ε':'ε', 'ζ':'ζ', 'η':'η', 'θ':'θ', 'ι':'ι', 'κ':'κ', 'λ':'λ', 'μ':'μ', 'ν':'ν', 'ξ':'ξ', 'ο':'ο', 'π':'π', 'ρ':'ρ', 'ς':'ς', 'σ':'σ', 'τ':'τ', 'υ':'υ', 'φ':'φ', 'χ':'χ', 'ψ':'ψ', 'ω':'ω', 'ϑ':'ϑ', 'ϒ':'ϒ', 'ϖ':'ϖ', '•':'•', '…':'…', '′':'′', '″':'″', '‾':'‾', '⁄':'⁄', '℘':'℘', 'ℑ':'ℑ', 'ℜ':'ℜ', '™':'™', 'ℵ':'ℵ', '←':'←', '↑':'↑', '→':'→', '↓':'↓', '↔':'↔', '↵':'↵', '⇐':'⇐', '⇑':'⇑', '⇒':'⇒', '⇓':'⇓', '⇔':'⇔', '∀':'∀', '∂':'∂', '∃':'∃', '∅':'∅', '∇':'∇', '∈':'∈', '∉':'∉', '∋':'∋', '∏':'∏', '∑':'∑', '−':'−', '∗':'∗', '√':'√', '∝':'∝', '∞':'∞', '∠':'∠', '∧':'∧', '∨':'∨', '∩':'∩', '∪':'∪', '∫':'∫', '∴':'∴', '∼':'∼', '≅':'≅', '≈':'≈', '≠':'≠', '≡':'≡', '≤':'≤', '≥':'≥', '⊂':'⊂', '⊃':'⊃', '⊄':'⊄', '⊆':'⊆', '⊇':'⊇', '⊕':'⊕', '⊗':'⊗', '⊥':'⊥', '⋅':'⋅', '⌈':'⌈', '⌉':'⌉', '⌊':'⌊', '⌋':'⌋', '⟨':'〈', '⟩':'〉', '◊':'◊', '♠':'♠', '♣':'♣', '♥':'♥', '♦':'♦', '"':'"', '&':'&', '<':'<', '>':'>', 'Œ':'Œ', 'œ':'œ', 'Š':'Š', 'š':'š', 'Ÿ':'Ÿ', 'ˆ':'ˆ', '˜':'˜', ' ':' ', ' ':' ', ' ':' ', '‌':'‌', '‍':'‍', '‎':'‎', '‏':'‏', '–':'–', '—':'—', '‘':'‘', '’':'’', '‚':'‚', '“':'“', '”':'”', '„':'„', '†':'†', '‡':'‡', '‰':'‰', '‹':'‹', '›':'›', '€':'€' };
static public function convertEntities(str:String):String
{
var re:RegExp = /&\w*;/g
var entitiesFound:Array = str.match(re);
var entitiesConverted:Object = {};
var len:int = entitiesFound.length;
var oldEntity:String;
var newEntity:String;
for (var i:int = 0; i < len; i++)
{
oldEntity = entitiesFound[i];
newEntity = entityMap[oldEntity];
if (newEntity && !entitiesConverted[oldEntity])
{
str = str.split(oldEntity).join(newEntity);
entitiesConverted[oldEntity] = true;
}
}
return str;
}
Credit where credit is due: this code was optimised by an acquaintance of mine (Tim Hawkins) which was modified from the original Flash HTML Entities Suck.
You can try using the htmlText
property to set your text, rather than the text
property.
This should convert any html entities to there correct symbols.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With