I'm using the TextConverter class to export a TextFlow to HTML and it's exporting HTML and Font tags in the export. Is there a way to prevent this so it only outputs the content without this?
Here is my AS3:
var output:String = TextConverter.export(RichText(textComponent).textFlow, TextConverter.TEXT_FIELD_HTML_FORMAT, ConversionType.STRING_TYPE);
Example output:
<HTML><BODY><P ALIGN="left"><FONT FACE="Arial" SIZE="12" COLOR="#000000"
LETTERSPACING="0" KERNING="1">Here is some content.
</FONT></P></BODY></HTML>
Also, isn't valid HTML in lowercase? Update, it looks like it doesn't matter except if you are using XHTML and then it should be uppercase.
It looks like TextFieldHtmlExporter.as is hardcoding in the HTML and BODY tags.
tlf_internal function exportToXML(textFlow:TextFlow) : XML {
var html:XML = <HTML/>;
if (textFlow.numChildren != 0)
{
if (textFlow.getChildAt(0).typeName != "BODY")
{
var body:XML = <BODY/>;
html.appendChild(body);
exportChildren(textFlow,body);
}
else
exportChildren(textFlow,html);
}
return html;
}
How would you handle this? Would this work:
exportChildren(textFlow, <root/>);
I want to get rid of that first "FONT" tag because it doesn't reflect the formatting in the RichText component. I'll probably have to rewrite the class since it uses uppercase all over the place.
It looks like I can get export to XML and then get the forth firstChild:
var test:Object = TextConverter.export(RichText(component).textFlow, TextConverter.TEXT_FIELD_HTML_FORMAT, ConversionType.XML_TYPE);
That creates this XML:
<HTML>
<BODY>
<P ALIGN="left">
<FONT FACE="Arial" SIZE="12" COLOR="#000000" LETTERSPACING="0" KERNING="1">
My text content
</FONT>
</P>
</BODY>
</HTML>
UPDATE:
A very hacky short cut is to get the actual paragraph content not the html wrapper you can use this but it is very fragile:
//layoutOutput += TextConverter.export(RichText(componentInstance).textFlow, TextConverter.TEXT_FIELD_HTML_FORMAT, ConversionType.STRING_TYPE);
var test:Object = TextConverter.export(RichText(componentInstance).textFlow, TextConverter.TEXT_FIELD_HTML_FORMAT, ConversionType.XML_TYPE);
var content:XML = test.children()[0].children()[0].children()[0].children()[0];
if (content) {
layoutOutput += content.toXMLString();
}
The classes that do the importing and exporting are:
TextFieldHtmlImporter, TextFieldHtmlExporter
Those classes look pretty easy to modify. You would use this code somewhere in your application:
TextConverter.removeFormat(TextFieldHtmlExporter);
TextConverter.addFormat(MyNewTextFieldHtmlExporter);
Here is a simple solution that you can extend and modify.
Create the following class,
TextFieldHTMLExporter2.as:The previous class corrects the font size issue. There are many more things that can be updated. If I post the class I'll add a link to it here.
Note:
The font size bug is that it when the text size is 12 px in height, it sets
SIZE="12"which is not the same asstyle="font-size:12px". If you are going to set theSIZEattribute then it needs to be converted to a equivalent value. See this question.