So one of the simple applications I like to make in any new language is an HTML escaper. Since I'm playing with Flex right now, I figured I'd make one (and hopefully it'll be useful, since I'll be using it to post the code).
It took a couple of minutes, but here's the code:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" backgroundColor="#f6f6f6">
<mx:Script>
<![CDATA[
public function escapeHTML(str:String):String {
str = str.replace(/&/g, "&");
str = str.replace(/</g, "<");
str = str.replace(/>/g, ">");
return str;
}
public function outputClickHandler(event:Event):void {
System.setClipboard(output.text);
}
]]>
</mx:Script>
<mx:VBox>
<mx:Label text="Original" fontSize="14" fontWeight="bold" />
<mx:TextArea id="input" width="800" height="200" />
<mx:Label text="Escaped" fontSize="14" fontWeight="bold" />
<mx:TextArea id="output" text="{escapeHTML(input.text)}" click="outputClickHandler(event)" width="800" height="200" />
</mx:VBox>
</mx:Application>
The cool things about this little application are:
- The data binding
- Note that the second TextArea's text attribute is defined as text="{escapeHTML(input.text)}". What that means is that the two text fields are bound together, with the ActionScript function between them as a sort of filter. Any change you make in the first box will immediately show up in the second box.
- The event handler
- The second box has an event listener set inline by the click attribute. It calls the outputClickHandler() method, which simply sets the your clipboard to be the result of the escaping. That way you don't have to manually select everything when you're ready to copy the results.
This is by far the best HTML escaper program I've written, and it's all thanks to Flex. Awesome.
(Feel free to use the HTML Escaper Application to your heart's content.)
No comments:
Post a Comment