清理HTML格式的内容以便在Flash中显示?

问题描述:

I want to display HTML formatted content from various sources inside a Flash Flex application. Flash supports HTML formatting in its text fields, however it is very limited compared to a web browser. Are there any scripts out there that will convert common HTML formatted text into a format that Flash can handle? My particular use cases are:

  • Displaying HTML formatted emails inside Flash
  • Displaying RTF files inside Flash (after running an RTF2HTML conversion on the server)
  • Displaying random HTML content copied and pasted from other sources into Flash

I'm open to code that runs either on the client or the server, but server is probably preferable.

我想在Flash Flex应用程序中显示各种来源的HTML格式内容。 Flash在其文本字段中支持HTML格式,但与Web浏览器相比,它非常有限。 是否有任何脚本可以将常见的HTML格式文本转换为Flash可以处理的格式? 我的特定用例是: p>

  • 在Flash中显示HTML格式的电子邮件 li>
  • 在Flash中显示RTF文件(在运行RTF2HTML转换后) () li> 客户端或服务器,但服务器可能更可取。 p> div>

The subset of html tags supported is quite poor and has not changed in forever:

<a>, <b>, <br>, <font>, <img>, <i>, <li>, <p>, <textformat>, <u>

This means is that regardless of conversion quality, html cannot be rendered as fully intended; you could also be giving up a significant portion of css styling if you replace unsupported tags with more basic ones.

That being said, http://simplehtmldom.sourceforge.net/ (PHP) would work with some tweaks and it's competent enough to cope with invalid markup as well (seeing how you're after processing content from various sources, I'd say this feature alone would save a lot of pain in the long run) - than replace

<h1>,...,<h6> => <b>
<strong>      => <b>
<em>          => <i>

and plaintext the rest of it into paragraphs you'd be surprised at how readable it would still be. You could be a bit fancy too like so:

<h1> => <b class="header1">

and add some css as appropriate (although flash css support is pretty limited too)

I've been saving this one for desert - you'll either love it or hate it but it would do the trick. Assuming your app is deployed in-browser (if not and I misread you, save me the embarrassment and stop reading right here) you could use an iframe to display your html, seriously.

JS<->AS communication is fairly straightforward and you could have it positioned over a predetermined area of your app, giving the illusion that it's part of it; just remember to set windowmode on the flash object/embed correctly so it does not render on top of other page elements, then increase the iframe z-index.

I would not be surprised if this is seen as an "ugly" approach, but it's beautiful on the inside - you'll end up with verbatim html and real css support. As for user interactions, you could even intercept link clicks etc. in the iframe and request an action from the movieclip.

AS3 HTML Parser Library is not quite what I'm looking for, since it does not convert the HTML but instead renders it within Flash, meaning that it wont be editable. But it may be useful in some cases that I only want to display and not edit text.

Another option is to look at several sample HTML that I'd like to be able to display, and then write regex to convert them to the format Flash/TLF expects. But I feel like that may be a huge endeavor, due to the wide range of HTML out there.

You can use HTMLPurifier and specify a whitelist of tags that you want to support.