Flex mx:RichTextEditor to HTML

Here’s a modification I made to the example http://code.google.com/p/flex-richtexteditor-html-utils/ to convert RTE to HTML. I added some logic to handle

    ’s and
  • ’s a bit better.

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" backgroundColor="#ffffff" 
    	creationComplete="run()"
     viewSourceURL="srcview/index.html">
    	<mx:Form width="100%" height="100%">
    		<mx:RichTextEditor width="100%" height="300" id="editor"  text="Hello World!"/>
    		<mx:FormItem label="RichTextEditor.htmlText" width="100%">
    			<mx:Text id="richHtml" text="Text" width="100%" height="150"/>
    		</mx:FormItem>
    		<mx:FormItem label="Html" width="100%">
    			<mx:Button label="Update" click="update();"/>
    			<mx:Text id="html" text="Text" width="100%" height="299"/>
    		</mx:FormItem>
    	</mx:Form>
    	<mx:Label text="(c) Firma JAKUBIAK, Antoni Jakubiak 2007"/>
     
    	<mx:Script>
    		<![CDATA[
    			import mx.controls.Alert;
     
    			private function update():void {
    				richHtml.text = editor.htmlText;
    				html.text = richTextEditorToHtml( editor.htmlText );
    			}
     
    			private function run():void {
    				update();				
    			}
     
    			private function richTextEditorToHtml(str:String):String {
    				// Create XML document
    				var xml:XML = XML("<BODY>"+str+"</BODY>");
     
    				// temporary
    				var t1:XML;
    				var t2:XML;
     
    				// Remove all TEXTFORMAT
    				for( t1 = xml..TEXTFORMAT[0]; t1 != null; t1 = xml..TEXTFORMAT[0] ) {				
    					if(t1.child("LI").length() != 0){
    						trace(t1.childIndex());
    						delete t1.@LEADING;
    						t1.setName("UL");						
    					}else{
    						t1.parent().replace( t1.childIndex(), t1.children() );
    					}										
    				}
     
    				// Find all ALIGN
    				for each ( t1 in xml..@ALIGN ) {
    					t2 = t1.parent();
    					t2.@STYLE = "text-align: " + t1 + "; " + t2.@STYLE;
    					delete t2.@ALIGN;
    				}
     
    				// Find all FACE
    				for each ( t1 in xml..@FACE ) {
    					t2 = t1.parent();
    					t2.@STYLE = "font-family: " + t1 + "; " + t2.@STYLE;
    					delete t2.@FACE;
    				}
     
    				// Find all SIZE 
    				for each ( t1 in xml..@SIZE ) {
    					t2 = t1.parent();
    					t2.@STYLE = "font-size: " + t1 + "px; " + t2.@STYLE;
    					delete t2.@SIZE;
    				}
     
    				// Find all COLOR 
    				for each ( t1 in xml..@COLOR ) {
    					t2 = t1.parent();
    					t2.@STYLE = "color: " + t1 + "; " + t2.@STYLE;
    					delete t2.@COLOR;
    				}
     
    				// Find all LETTERSPACING 
    				for each ( t1 in xml..@LETTERSPACING ) {
    					t2 = t1.parent();
    					t2.@STYLE = "letter-spacing: " + t1 + "px; " + t2.@STYLE;
    					delete t2.@LETTERSPACING;
    				}
     
    				// Find all KERNING
    				for each ( t1 in xml..@KERNING ) {
    					t2 = t1.parent();
    					// ? css 
    					delete t2.@KERNING;
    				}
     
    				//Group adjacent LI's together 
    				var str:String = xml.children().toXMLString();
    				var pattern:RegExp = /<\/UL>\s*<UL>/ixg;
                   	str = str.replace(pattern, "");
     
    				return str;
    			}	
     
    		]]>
    	</mx:Script>
     
    </mx:Application>