StyleSheet’s method getStyle doesn’t return null for non-existing style

I have just learned that getStyle() method of flash.text.StyleSheet class doesn’t return null value when requested style doesn’t exists. Documentation states that it should, but in fact it returns empty object. Which could be fine for tests but you could have a style with empty declaration block which would create false positive result. This error appears to be for some time, check screen grabs from different players starting from fp9.

The solution is to extend StyleSheet class and override the getStyle method, then we can use the styleNames property to check if it contains requested style, if not return null otherwise call super method.

package
{
	import flash.text.StyleSheet;
	
	/**
	 * Fixes issue with getStyle method returning empty object for non-existing style.
	 *
	 * @author Lukasz 'Severiaan' Grela
	 */
	public class FixedStyleSheet extends StyleSheet
	{
		/**
		 * Variable storing the style name used in the test function
		 */
		protected var m_sStyleNameCondition:String;
		public function FixedStyleSheet()
		{
			super();
			
		}
		/**
		 * Patch to fix issue with not returning null value for non-existing styles
		 * @param	styleName
		 * @return
		 */
		override public function getStyle(styleName:String):Object
		{
			m_sStyleNameCondition = styleName;
			if (styleNames.some(searchForStyleName, null))
			{
				var o:Object = super.getStyle(styleName);
				return o;
			}
			
			return null;
		}
		
		protected function searchForStyleName(item:*,index:int,list:Array):Boolean
		{
			return item == m_sStyleNameCondition;
		}
	}
}

Edit:
I have recently found that this issue was reported in JIRA, give your vote here and here.

This entry was posted in actionscript, bugs, flash and tagged , , , , , , , . Bookmark the permalink.