Improved keyboard navigation in text surrounding tables
diff --git a/textLayout/src/flashx/textLayout/elements/ParagraphElement.as b/textLayout/src/flashx/textLayout/elements/ParagraphElement.as
index 0622c84..dbb01b8 100644
--- a/textLayout/src/flashx/textLayout/elements/ParagraphElement.as
+++ b/textLayout/src/flashx/textLayout/elements/ParagraphElement.as
@@ -805,6 +805,9 @@
 		public function findPreviousAtomBoundary(relativePosition:int):int
 		{
 			var tb:TextBlock = getTextBlockAtPosition(relativePosition);
+			if(!tb || !tb.content)
+				return relativePosition-1;
+			
 			var tbStart:int = getTextBlockStart(tb);
 			var textBlockPos:int = relativePosition - tbStart;
             var tl:TextLine = tb.getTextLineAtCharIndex(textBlockPos);
@@ -852,7 +855,11 @@
     				{
     					tl = tl.previousLine;
     					if (!tl)
-    						return -1;
+						{
+							if(tb != _textBlocks[0])
+								return relativePosition-1;
+							return -1;
+						}
     					// need this when 0x2028 line separator in use
     					if (tl.textBlockBeginIndex + tl.rawTextLength == textBlockPos)
     						return tl.textBlockBeginIndex + tl.rawTextLength - 1 + tbStart;
@@ -899,6 +906,8 @@
 		public function findNextAtomBoundary(relativePosition:int):int
 		{
 			var tb:TextBlock = getTextBlockAtPosition(relativePosition);
+			if(!tb || !tb.content)
+				return relativePosition+1;
 			var tbStart:int = getTextBlockStart(tb);
 			var textBlockPos:int = relativePosition - tbStart;
             var tl:TextLine = tb.getTextLineAtCharIndex(textBlockPos);
@@ -941,7 +950,11 @@
     				{
     					tl = tl.nextLine;
     					if (!tl)
-    						return -1;
+						{
+							if(tb != _textBlocks[_textBlocks.length-1])
+								return relativePosition+1;
+							return -1;
+						}
     					return tl.textBlockBeginIndex + tbStart;
     				}
     				while (++textBlockPos)
diff --git a/textLayout/src/flashx/textLayout/utils/NavigationUtil.as b/textLayout/src/flashx/textLayout/utils/NavigationUtil.as
index 091ceda..fea8549 100644
--- a/textLayout/src/flashx/textLayout/utils/NavigationUtil.as
+++ b/textLayout/src/flashx/textLayout/utils/NavigationUtil.as
@@ -25,6 +25,7 @@
 	
 	import flashx.textLayout.compose.IFlowComposer;
 	import flashx.textLayout.compose.TextFlowLine;
+	import flashx.textLayout.compose.TextFlowTableBlock;
 	import flashx.textLayout.container.ContainerController;
 	import flashx.textLayout.container.ScrollPolicy;
 	import flashx.textLayout.elements.FlowLeafElement;
@@ -337,6 +338,7 @@
 		{
 			var endIdx:int;
 			var targetTextLine:TextLine = targetFlowLine.getTextLine(true);
+			var blockOffset:int = targetFlowLine.paragraph.getTextBlockAbsoluteStart(targetTextLine.textBlock);
 			var currentTextLine:TextLine = curTextFlowLine.getTextLine(true);
 			var bidiRightToLeft:Boolean = ((currentTextLine.getAtomBidiLevel(atomIndex) % 2) != 0); 				
 			
@@ -412,7 +414,7 @@
 						paraSelectionIdx = leanRight ? targetTextLine.getAtomTextBlockEndIndex(atomIndex) : targetTextLine.getAtomTextBlockBeginIndex(atomIndex);							
 					}
 				}
-				endIdx = targetFlowLine.paragraph.getAbsoluteStart() + paraSelectionIdx;
+				endIdx = blockOffset + paraSelectionIdx;
 			}
 			return endIdx;
 		}
@@ -447,8 +449,8 @@
 				var lineStart:int = curTextFlowLine.absoluteStart;
 				var lineDelta:int = endIdx - lineStart;
 				var currentTextLine:TextLine = curTextFlowLine.getTextLine(true);
-				var para:ParagraphElement = curTextFlowLine.paragraph;
-				var atomIndex:int = currentTextLine.getAtomIndexAtCharIndex(endIdx - para.getAbsoluteStart());
+				var blockOffset:int = curTextFlowLine.paragraph.getTextBlockAbsoluteStart(currentTextLine.textBlock);
+				var atomIndex:int = currentTextLine.getAtomIndexAtCharIndex(endIdx - blockOffset);
 				var bidiRightToLeft:Boolean = ((currentTextLine.getAtomBidiLevel(atomIndex) % 2) != 0); 
 				var curPosRect:Rectangle = currentTextLine.getAtomBounds(atomIndex);
 				var currentTextLineX:Number = currentTextLine.x;
@@ -483,8 +485,11 @@
 				
 				//at this point, we have the global point of our current position.  Now adjust x or y to the
 				//baseline of the next line.
-				var nextFlowLine:TextFlowLine = textFlow.flowComposer.getLineAt(curLine + 1);
-				if (nextFlowLine.absoluteStart >= limitIdx)
+				var lineInc:int = 1;
+				var nextFlowLine:TextFlowLine = textFlow.flowComposer.getLineAt(curLine + lineInc);
+				while(nextFlowLine is TextFlowTableBlock)
+					nextFlowLine = textFlow.flowComposer.getLineAt(++lineInc + curLine);
+				if (!nextFlowLine || nextFlowLine.absoluteStart >= limitIdx)
 				{
 					if (!extendSelection)
 						range.activePosition = range.anchorPosition = textFlow.textLength - 1;
@@ -567,8 +572,8 @@
 				var lineStart:int = curTextFlowLine.absoluteStart;
 				var lineDelta:int = endIdx - lineStart;
 				var currentTextLine:TextLine = curTextFlowLine.getTextLine(true);
-				var para:ParagraphElement = curTextFlowLine.paragraph;
-				var atomIndex:int = currentTextLine.getAtomIndexAtCharIndex(endIdx - para.getAbsoluteStart());
+				var blockOffset:int = curTextFlowLine.paragraph.getTextBlockAbsoluteStart(currentTextLine.textBlock);
+				var atomIndex:int = currentTextLine.getAtomIndexAtCharIndex(endIdx - blockOffset);
 				var curPosRect:Rectangle = currentTextLine.getAtomBounds(atomIndex);
 				var currentTextLineX:Number = currentTextLine.x;
 				var curPosRectLeft:Number = curPosRect.left;
@@ -602,7 +607,19 @@
 				
 				//at this point, we have the global point of our current position.  Now adjust x or y to the
 				//baseline of the next line.
-				var prevFlowLine:TextFlowLine = textFlow.flowComposer.getLineAt(curLine - 1);
+				var lineInc:int = 1;
+				var prevFlowLine:TextFlowLine = textFlow.flowComposer.getLineAt(curLine - lineInc);
+				while(prevFlowLine is TextFlowTableBlock)
+					prevFlowLine = textFlow.flowComposer.getLineAt(curLine - (++lineInc));
+				if (!prevFlowLine)
+				{
+					if (!extendSelection)
+						range.activePosition = range.anchorPosition = 0;
+					else
+						range.activePosition = 0;
+					return true;
+				}
+
 				// get the last container so that we can make sure the previous line is in view.
 				var controller:ContainerController = textFlow.flowComposer.getControllerAt(textFlow.flowComposer.numControllers-1);
 				var firstPosInContainer:int = controller.absoluteStart;