Index: /FCKeditor/trunk/editor/_source/classes/fckdomrange.js
===================================================================
--- /FCKeditor/trunk/editor/_source/classes/fckdomrange.js	(revision 806)
+++ /FCKeditor/trunk/editor/_source/classes/fckdomrange.js	(revision 807)
@@ -841,4 +841,11 @@
 
 		return retval ;
+	},
+
+	GetText : function()
+	{
+		if ( ! this._Range )
+			return '' ;
+		return this._Range.toString() ;
 	}
 } ;
Index: /FCKeditor/trunk/editor/_source/classes/fckw3crange.js
===================================================================
--- /FCKeditor/trunk/editor/_source/classes/fckw3crange.js	(revision 806)
+++ /FCKeditor/trunk/editor/_source/classes/fckw3crange.js	(revision 807)
@@ -451,8 +451,20 @@
 		var startNode = this.startContainer ;
 		var endNode = this.endContainer ;
-		if ( startNode.firstChild && this.startOffset )
-			startNode = startNode.childNodes[this.startOffset] ;
-		if ( endNode.firstChild && this.endOffset )
-			endNode = endNode.childNodes[this.endOffset] ;
+
+		// note that it often occurs that startOffset/endOffset goes beyond the end of a container.
+		if ( startNode.firstChild )
+		{
+			if ( startNode.childNodes[this.startOffset] == null )
+				startNode = startNode.lastChild ;
+			else
+				startNode = startNode.childNodes[this.startOffset] ;
+		}
+		if ( endNode.firstChild )
+		{
+			if ( endNode.childNodes[this.endOffset] == null )
+				endNode = endNode.lastChild ;
+			else
+				endNode = endNode.childNodes[this.endOffset] ;
+		}
 
 		// Use an array for joining strings for performance.
Index: /FCKeditor/trunk/editor/_source/commandclasses/fckjustifycommands.js
===================================================================
--- /FCKeditor/trunk/editor/_source/commandclasses/fckjustifycommands.js	(revision 806)
+++ /FCKeditor/trunk/editor/_source/commandclasses/fckjustifycommands.js	(revision 807)
@@ -19,5 +19,5 @@
  * == END LICENSE ==
  *
- * FCKJustifyCommand Class: controls text justification.
+ * FCKJustifyCommand Class: controls block justification.
  */
 
@@ -35,13 +35,26 @@
 		range.MoveToSelection() ;
 		var bookmark = range.CreateBookmark() ;
-		range.Expand( 'block_contents' ) ;
-		if ( range.StartContainer == range.Window.document.body )
-			range.FixBlock( true ) ;
-		range.StartContainer.removeAttribute( 'align' ) ;
+		var paragraphs = range.GetParagraphs() ;
 
-		if ( this.CurrentState == FCK_TRISTATE_OFF)
-			range.StartContainer.style.textAlign = this.AlignVaue ;
-		else if ( this.CurrentState == FCK_TRISTATE_ON )
-			range.StartContainer.style.textAlign = '' ;
+		while ( paragraphs.length > 0 )
+		{
+			range = paragraphs.shift() ;
+			if ( range.GetText().search( /^[\r\n\t ]*$/ ) == 0 )
+				continue ;
+
+			if ( range.StartContainer == range.Window.document.body )
+				range.FixBlock( true ) ;
+			range.StartContainer.removeAttribute( 'align' ) ;
+
+			if ( this.CurrentState == FCK_TRISTATE_OFF)
+				range.StartContainer.style.textAlign = this.AlignVaue ;
+			else if ( this.CurrentState == FCK_TRISTATE_ON )
+				range.StartContainer.style.textAlign = '' ;
+
+			var nextNode = range.EndNode ;
+			while ( nextNode.lastChild )
+				nextNode = nextNode.lastChild ;
+			currentNode = nextNode = FCKDomTools.GetNextSourceNode( nextNode ) ;
+		}
 
 		range.MoveToBookmark( bookmark ) ;
@@ -58,12 +71,5 @@
 		}
 
-		var parentEl = FCKSelection.GetParentElement() ;
-		var nearestBlock = parentEl ;
-		while ( nearestBlock )
-		{
-			if ( FCKListsLib.BlockBoundaries[nearestBlock.nodeName.toLowerCase()] )
-				break ;
-			nearestBlock = nearestBlock.parentNode ;
-		}
+		var nearestBlock = FCKSelection.GetParentBlock() ;
 
 		if ( ! nearestBlock )
Index: /FCKeditor/trunk/editor/_source/internals/fckdomtools.js
===================================================================
--- /FCKeditor/trunk/editor/_source/internals/fckdomtools.js	(revision 806)
+++ /FCKeditor/trunk/editor/_source/internals/fckdomtools.js	(revision 807)
@@ -316,9 +316,40 @@
 		while ( node )
 		{
-			parents.splice( 0, 0, node ) ;
+			parents.unshift( node ) ;
 			node = node.parentNode ;
 		}
 
 		return parents ;
+	},
+
+	GetCommonParents : function( node1, node2 )
+	{
+		var p1 = this.GetParents( node1 ) ;
+		var p2 = this.GetParents( node2 ) ;
+		var retval = [] ;
+		for ( var i = 0 ; i < p1.length ; i++ )
+		{
+			if ( p1[i] == p2[i] )
+				retval.push( p1[i] ) ;
+		}
+		return retval ;
+	},
+
+	GetCommonParentNode : function( node1, node2, tagList )
+	{
+		var tagMap = {} ;
+		if ( ! tagList.pop )
+			tagList = [ tagList ] ;
+		while ( tagList.length > 0 )
+			tagMap[tagList.pop().toLowerCase()] = 1 ;
+
+		var commonParents = this.GetCommonParents( node1, node2 ) ;
+		var currentParent = null ;
+		while ( ( currentParent = commonParents.pop() ) )
+		{
+			if ( tagMap[currentParent.nodeName.toLowerCase()] )
+				return currentParent ;
+		}
+		return null ;
 	},
 
Index: /FCKeditor/trunk/editor/_source/internals/fckselection.js
===================================================================
--- /FCKeditor/trunk/editor/_source/internals/fckselection.js	(revision 806)
+++ /FCKeditor/trunk/editor/_source/internals/fckselection.js	(revision 807)
@@ -22,4 +22,16 @@
  */
 
-var FCKSelection = FCK.Selection = new Object() ;
-
+var FCKSelection = FCK.Selection = 
+{
+	GetParentBlock : function()
+	{
+		var retval = this.GetParentElement() ;
+		while ( retval )
+		{
+			if ( FCKListsLib.BlockBoundaries[retval.nodeName.toLowerCase()] )
+				break ;
+			retval = retval.parentNode ;
+		}
+		return retval ;
+	}
+} ;
