Flex TextFlowUtil.importFromString ignoring whitespace in some cases. Is this a bug?
In the following complete functional Flex application the line breaks between the two links ought to be preserved when importing the input text into the TextFlow:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark"
minWidth="955" minHeight="600"
creationComplete="application1_creationCompleteHandler(event)"
>
<fx:Script>
<![CDATA[
import flashx.textLayout.formats.WhiteSpaceCollapse;
import mx.events.FlexEvent;
import spark.utils.TextFlowUtil;
protected function application1_creationCompleteHandler(event : FlexEvent) : void
{
input.text = "<a href='#'>link1</a>\n<a href='#'>link2</a>";
}
protected function button1_clickHandler(event : MouseEvent) : void
{
output.textFlow = TextFlowUtil.importFromString(input.text, WhiteSpaceCollapse.PRESERVE);
}
]]>
</fx:Script>
<s:RichEditableText text=""
id="input"
width="266" height="215"
x="10" y="30"
/>
<s:Label text="Input"
开发者_运维知识库 x="10" y="10"
/>
<s:Label text="Output"
x="8" y="286"
/>
<s:Button x="10" y="253"
click="button1_clickHandler(event)"
label="Import"
/>
<s:RichEditableText id="output"
width="399" height="212"
x="10" y="306"
/>
</s:Application>
But if you click "Import" you will see the two links created in the same line (no break).
This changes if you add any non-whitespace character between the links in the source text. Meaning, if you just add a "*" between the links the line breaks will be preserved.
What gives? Is this a Flex bug?
You may need to adjust the global XML parsing settings. These are accessible as static properties of the XML
class. My hunch is that XML.ignoreWhitespace
is true
at the time of parsing. Try the following:
XML.ignoreWhitespace = false;
output.textFlow = TextFlowUtil.importFromString(input.text, WhiteSpaceCollapse.PRESERVE);
This is the way to solve this bug, and there is no need for changing any XML settings, simple and it works:
Exporting the TextFlow
:
var richTextXML:xml = new XML("<"+"richTextXML"+"/>");
richTextXML.appendChild(getCdataXMl());
private function getCdataXMl():xml {
var textFlowStr:String = TextConverter.export(textFlow,TextConverter.TEXT_FIELD_HTML_FORMAT, ConversionType.STRING_TYPE).toString();
var textFlowXMl:xml = new XMl("<![CDATA["+textFlowStr+"]]>");
return textFlowXMl;
}
Importing TextFlow
from XML:
var htmlTextInStr:String = richTextXMl.text();
var importtedTextFlow:TextFlow = TextConverter.importToFlow(htmlTextInStr,TextConverter.TEXT_FIELD_HTML_FORMAT);
textArea.textFlow = importtedTextFlow;
精彩评论