#6 Flex Hack – RichTextEditor to output XHTML 1.1 valid

Filed under Flex, Flex 3, Flex Beyound basic, Flex Hacks

The RTE is a great component but for some reason Adobe didn’t compete with best standards to prioritize or has their own way to not include a valid xhtml 1.1 code output to their current RTE. I hope with the next Flex 4 and Text Layout Framework things get better for RTE and really get a little bit more features.

Asides that Keep us with out lovely Flex 3 and enjoy this hack.

Level: intermediate

Requirements : Flex 3.2 SDK

Problems: RTE output a HTML 4.0 style and for some reason is not very acceptable when you are integrating with other technologies like Ajax. For this reason this hack got a simple two methods to do the job, removing that part of there’s no magic in the Regex uses , some elements are still missing like img, didn’t find a way to handle that. But in overall you can do a bunch things already.

The code bellow represents a two static methods that does the job, where you can really on this particular case or if you wish you can create your component or a class for this propose.




	/g;
                       str = str.replace(pattern, "

"); pattern = /

/g; str = str.replace(pattern, "

"); pattern = /

/g; str = str.replace(pattern, "

"); pattern = /<\/p>/g; str = str.replace(pattern, "

"); pattern = //g; str = str.replace(pattern, ""); pattern = /color:(.*?);/g; str = str.replace(pattern, "COLOR=\"$1\" "); pattern = /font-size:(.*?)px;/g; str = str.replace(pattern, "SIZE=\"$1\" "); pattern = /font-family:(.*?);/g; str = str.replace(pattern, "FACE=\"$1\" "); pattern = /text-align:(.*?);/g; str = str.replace(pattern, "ALIGN=\"$1\" "); pattern = /<\/span.*?>/g; str = str.replace(pattern, ""); pattern= /<\/li>
  • /g; str = str.replace(pattern, "
  • "); pattern= /<\/li><\/ul>/g; str = str.replace(pattern, "
  • "); pattern= /
    • /g; str = str.replace(pattern, "
    • "); pattern = //g; str = str.replace(pattern, ""); pattern = /<\/em>/g; str = str.replace(pattern, ""); pattern = //g; str = str.replace(pattern, ""); pattern = /<\/strong>/g; str = str.replace(pattern, ""); pattern = //g; str = str.replace(pattern, ""); pattern = /<\/u>/g; str = str.replace(pattern, ""); // Remove extra white space pattern = / /g; str = str.replace(pattern, " "); return str; } public static function convertToXHtml(str:String):String { var pattern:RegExp; pattern = //g; str = str.replace(pattern, ""); pattern = /<\/TEXTFORMAT.*?>/g; str = str.replace(pattern, ""); pattern = /

      /g; str = str.replace(pattern, "

      "); pattern = /

      /g; str = str.replace(pattern, "

      "); pattern = /

      /g; str = str.replace(pattern, "

      "); pattern = /<\/P>/g; str = str.replace(pattern, " "); pattern = //g; str = str.replace(pattern, ""); pattern = /COLOR=\"(.*?)\"/g; str = str.replace(pattern, "color:$1;"); pattern = /SIZE=\"(.*?)\"/g; str = str.replace(pattern, "font-size:$1px;"); pattern = /FACE=\"(.*?)\"/g; str = str.replace(pattern, "font-family:$1;"); pattern = /ALIGN=\"(.*?)\"/g; str = str.replace(pattern, "text-align:$1;"); pattern = /LETTERSPACING=\".*?\"/g; str = str.replace(pattern, ""); pattern = /KERNING=\".*?\"/g; str = str.replace(pattern, ""); pattern = /<\/FONT.*?>/g; str = str.replace(pattern, ""); pattern= /<\/LI>

    • /g; str = str.replace(pattern, "
    • "); pattern= /<\/LI>/g; str = str.replace(pattern, "
    "); pattern= /
  • /g; str = str.replace(pattern, "
    • "); pattern = //g; str = str.replace(pattern, ""); pattern = /<\/I>/g; str = str.replace(pattern, ""); pattern = //g; str = str.replace(pattern, ""); pattern = /<\/B>/g; str = str.replace(pattern, ""); pattern = //g; str = str.replace(pattern, ""); pattern = /<\/U>/g; str = str.replace(pattern, ""); return str; } ]]>
  • Research found at:http://thanksmister.com/?p=17=4

    6 Comments

    1. Posted January 12, 2009 at 3:33 pm | Permalink

      Thanks for the pingback, great post!

      -Mister

    2. Posted January 12, 2009 at 4:28 pm | Permalink

      I found a way to insert and make the image Tag XHTML compatible, will post a link on my blog soon

    3. Posted January 13, 2009 at 11:29 am | Permalink

      Here is a link to the post http://anotherflava.com/?p=243

    4. Posted March 11, 2009 at 1:18 pm | Permalink

      it looks like on line 84 you strip out altogether. I think you meant to replace with

      Jeremy

    5. Posted March 11, 2009 at 1:24 pm | Permalink

      it looks like on line 84 you strip out </P> altogether. I think you meant to replace </P> with </p>

      pattern = /<\/P>/g;
      str = str.replace(pattern, “</p>”);

      great post. thanks.

      Jeremy

    6. Posted January 18, 2010 at 6:21 pm | Permalink

      Great post!

      Thank you for the help.

    4 Trackbacks

    1. [...] component in flex does not produce XHTML compatible syntax. Igor Costor recently posted this hack Flex Hack – RichTextEditor to output XHTML 1.1 valid  , based on Thankmister work http://thanksmister.com/?p=17=4 another version can be found @ [...]

    2. [...] #6 Flex Hack – RichTextEditor to output XHTML 1.1… (from Igor Costa) [...]

    3. [...] Flex 3 came with works, and it works well… It also produces extra html that we dont need. Igor Costa provided the methods in his blog post, I’m simply taking those methods and adding getters and [...]

    4. [...] other day, someone asked me how they could re-use the code sample from Igor Costa’s post about getting the Rich Text Editor to output XHTML. Whenever you want to add functionality to an existing control, you just need to extend it. [...]

    Post a Comment

    Your email is never published nor shared. Required fields are marked *

    *
    *