Ordered Lists in TextField.htmlText

One thing thats kept bugging me with TextField.htmlText was the lack of support for ordered list (<ol><li></li></ol>) in a textfield with htmlText applied to it. I decided to write a simple class that strips a html string from the (<ol><li></li></ol>) and arrange the list(s) in numerical order with tabs instead. I know this isn’t the prettiest, but hey it works.

/**
 * com.deviouswork.util.HTMLUtil
 * @version 1.0.0
 * @author Joakim Roos | j  at deviouswork dot com
 * 
 * Copyright (c) 2009 Deviouswork
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package com.deviouswork.util {
 
	/**
	 * HTMLUtil
	 */
 
	public class HTMLUtil {		
 
		public static function orderedList(source : String, tabs:int = 2) : String {				
			if(source.indexOf("<ol>") == -1) {
				return source;
			} else {
				var tab_str : String = "";
				while(tabs--) {
					tab_str += "\t";
				}
				var orderedList_array : Array = source.split("</ol><ol>");
				var numberedList_str:String = orderedList_array[0];
				for(var i:uint = 1; i < orderedList_array.length; i++) {
					var listItem_array : Array = String(orderedList_array[i]).split("<li>").slice(1);
					for(var j:uint = 0; j < listItem_array.length; j++) {
						var list_str:String = String(j+1) + "." + tab_str + HTMLUtil.replace(listItem_array[j],"[\n]", "<br>");
						list_str = HTMLUtil.remove(list_str, "[\r]");
						list_str = HTMLUtil.replace(list_str, "<br />", "<br />" + tab_str);
						list_str = HTMLUtil.replace(list_str, "<br />" +tab_str + "\t", "<br /><br />");					
						numberedList_str += list_str;
					}
				}
				HTMLUtil.remove(numberedList_str, "</ol>");
				return numberedList_str;
			}
		}
 
		private static function remove(source:String, remove:String):String {
			return HTMLUtil.replace(source, remove, '');
		}
 
 
		private static function replace(source:String, remove:String, replace:String):String {
			var pattern:RegExp = new RegExp(remove, 'g');
			return source.replace(pattern, replace);
		}
	}
}

No Trackbacks

You can leave a trackback using this URL: http://www.deviouswork.com/2009/04/21/ordered-lists-in-textfieldhtmltext/trackback/

2 Comments

  1. derek

    any useage samples?

    Posted 6 May, 2009 at 20:38 | Permalink
  2. Dan

    Nice work around. I’ve always hated flash not being about to handle ordered lists.

    Although, i found an logic error which is easily fixed:
    for(var i:uint = 1; i < orderedList_array.length; i++)

    should be:
    for(var i:uint = 0; i < orderedList_array.length; i++)

    Cheers,

    Posted 23 August, 2010 at 2:45 | Permalink

Post a Comment

Your email is never shared. Required fields are marked *

*
*