Index: /CKEditor/trunk/_source/core/htmlparser/fragment.js
===================================================================
--- /CKEditor/trunk/_source/core/htmlparser/fragment.js	(revision 3605)
+++ /CKEditor/trunk/_source/core/htmlparser/fragment.js	(revision 3606)
@@ -117,4 +117,20 @@
 			}
 
+			// Rtrim empty spaces on block end boundary. (#3585)
+			if ( element._.isBlockLike )
+			{
+
+				var length = element.children.length,
+					lastChild = element.children[ length - 1 ],
+					text;
+				if ( lastChild && lastChild.type == CKEDITOR.NODE_TEXT )
+				{
+					if ( !( text = CKEDITOR.tools.rtrim( lastChild.value ) ) )
+						element.children.length = length -1;
+					else
+						lastChild.value = text;
+				}
+			}
+
 			target.add( element );
 
Index: /CKEditor/trunk/_source/plugins/justify/plugin.js
===================================================================
--- /CKEditor/trunk/_source/plugins/justify/plugin.js	(revision 3605)
+++ /CKEditor/trunk/_source/plugins/justify/plugin.js	(revision 3606)
@@ -67,38 +67,42 @@
 		exec : function( editor )
 		{
-			var selection = editor.getSelection(),
-				range = selection && selection.getRanges()[0];
+			var selection = editor.getSelection();
+			if ( !selection )
+				return;
+			
+			var bookmarks = selection.createBookmarks(),
+				ranges = selection.getRanges();
 
-			if ( !range )
-				return;
 
-			var bookmarks = selection.createBookmarks(),
-				cssClassName = this.cssClassName,
-				iterator = range.createIterator(),
+			var cssClassName = this.cssClassName,
+				iterator,
 				block;
-
-			while ( ( block = iterator.getNextParagraph() ) )
-			{
-				block.removeAttribute( 'align' );
-
-				if ( cssClassName )
+			for ( var i = ranges.length-1 ; i >= 0 ; i-- ) {
+				iterator = ranges[ i ].createIterator();
+				while ( ( block = iterator.getNextParagraph() ) )
 				{
-					// Remove any of the alignment classes from the className.
-					var className = block.$.className =
-						CKEDITOR.tools.ltrim( block.$.className.replace( this.cssClassRegex, '' ) );
-
-					// Append the desired class name.
-					if ( this.state == CKEDITOR.TRISTATE_OFF && !this.isDefaultAlign )
-						block.addClass( cssClassName );
-					else if ( !className )
-						block.removeAttribute( 'class' );
+					block.removeAttribute( 'align' );
+	
+					if ( cssClassName )
+					{
+						// Remove any of the alignment classes from the className.
+						var className = block.$.className =
+							CKEDITOR.tools.ltrim( block.$.className.replace( this.cssClassRegex, '' ) );
+	
+						// Append the desired class name.
+						if ( this.state == CKEDITOR.TRISTATE_OFF && !this.isDefaultAlign )
+							block.addClass( cssClassName );
+						else if ( !className )
+							block.removeAttribute( 'class' );
+					}
+					else
+					{
+						if ( this.state == CKEDITOR.TRISTATE_OFF && !this.isDefaultAlign )
+							block.setStyle( 'text-align', this.value );
+						else
+							block.removeStyle( 'text-align' );
+					}
 				}
-				else
-				{
-					if ( this.state == CKEDITOR.TRISTATE_OFF && !this.isDefaultAlign )
-						block.setStyle( 'text-align', this.value );
-					else
-						block.removeStyle( 'text-align' );
-				}
+				
 			}
 
Index: /CKEditor/trunk/_source/plugins/selection/plugin.js
===================================================================
--- /CKEditor/trunk/_source/plugins/selection/plugin.js	(revision 3605)
+++ /CKEditor/trunk/_source/plugins/selection/plugin.js	(revision 3606)
@@ -869,7 +869,33 @@
 		{
 			var retval = [],
-				ranges = this.getRanges();
-			for ( var i = 0 ; i < ranges.length ; i++ )
-				retval.push( ranges[i].createBookmark( serializable ) );
+				ranges = this.getRanges(),
+				length = ranges.length;
+			for ( var i = 0; i < length ; i++ )
+			{
+				// Updating the offset values for rest of ranges which have been mangled.
+				for ( var j = i + 1 ; j < length ; j++ )
+				{
+					retval.push( ranges[ i ].createBookmark( serializable, true ) );
+
+					var dirtyRange = ranges[ j ];
+					function updateDirtyRange( isRangeStart, isBookmarkStart )
+					{
+						if ( dirtyRange[ isRangeStart ?
+						      'startContainer' : 'endContainer' ].equals(
+								retval[ i ][ isBookmarkStart ?
+								   'startNode' : 'endNode' ].getParent() ) )
+							dirtyRange[ isRangeStart ? 'startOffset' : 'endOffset' ]++;
+					}
+
+					updateDirtyRange( false, true );
+					updateDirtyRange( false, false );
+					updateDirtyRange( true, true );
+					updateDirtyRange( true, false );
+				}
+			}
+
+			// Update ranges caches on dirty.
+			if ( dirtyRange )
+				this._.cache.ranges = ranges;
 			return retval;
 		},
Index: /CKEditor/trunk/_source/tests/core/htmlparser/fragment.html
===================================================================
--- /CKEditor/trunk/_source/tests/core/htmlparser/fragment.html	(revision 3605)
+++ /CKEditor/trunk/_source/tests/core/htmlparser/fragment.html	(revision 3606)
@@ -152,4 +152,23 @@
 		},
 
+		test_ticket_3585 : function()
+		{
+			testParser(	'<p><br />\t\r\n</p>',
+						'<p><br /></p>' );
+		},
+
+		test_ticket_3585_1 : function()
+		{
+			testParser(	'<p><br />text\t\r\n</p>',
+						'<p><br />text</p>' );
+		},
+
+
+		test_ticket_3585_2 : function()
+		{
+			testParser(	'<b>inline </b>\n<p>paragraph\t\r\n</p>\t\r\n',
+						'<p><b>inline </b></p><p>paragraph</p>' );
+		},
+
 		name : document.title
 	};
@@ -157,5 +176,5 @@
 
 // Uncomment the following to run a single test.
-// window.onload = tc.test_ticket_3195_2;
+// window.onload = tc.test_ticket_3585_2;
 
 	//]]>
