diff --git a/reviewboard/diffviewer/chunk_generator.py b/reviewboard/diffviewer/chunk_generator.py
index 4f40664dbb23e16640dd44511a9f64a5ace0219d..8dd2df137c843d3f74e77519f30bf210f6598dfd 100644
--- a/reviewboard/diffviewer/chunk_generator.py
+++ b/reviewboard/diffviewer/chunk_generator.py
@@ -376,16 +376,56 @@ class DiffChunkGenerator(object):
         a way that makes it clear how many spaces or tabs were used.
         """
         if is_indent:
-            new_markup = '<span class="indent">%s</span>%s' % (
-                self._serialize_indentation(new_markup[:raw_indent_len]),
-                new_markup[raw_indent_len:])
+            new_markup = self._wrap_indentation_chars(
+                'indent',
+                new_markup,
+                raw_indent_len,
+                self._serialize_indentation)
         else:
-            old_markup = '<span class="unindent">%s</span>%s' % (
-                self._serialize_unindentation(old_markup[:raw_indent_len]),
-                old_markup[raw_indent_len:])
+            old_markup = self._wrap_indentation_chars(
+                'unindent',
+                old_markup,
+                raw_indent_len,
+                self._serialize_unindentation)
 
         return old_markup, new_markup
 
+    def _wrap_indentation_chars(self, class_name, markup, raw_indent_len,
+                                serializer):
+        """Wraps characters in a string with indentation markers.
+
+        This will insert the indentation markers and its wrapper in the
+        markup string. It's careful not to interfere with any tags that
+        may be used to highlight that line.
+        """
+        start_pos = 0
+
+        # There may be a tag wrapping this whitespace. If so, we need to
+        # find where the actual whitespace chars begin.
+        while markup[start_pos] == '<':
+            end_tag_pos = markup.find('>', start_pos + 1)
+
+            # We'll only reach this if some corrupted HTML was generated.
+            # We want to know about that.
+            assert end_tag_pos != -1
+
+            start_pos = end_tag_pos + 1
+
+        end_pos = start_pos + raw_indent_len
+
+        indentation = markup[start_pos:end_pos]
+
+        if indentation.strip() != '':
+            # There may be other things in here we didn't expect. It's not
+            # a straight sequence of characters. Give up on highlighting it.
+            return markup
+
+        return '%s<span class="%s">%s</span>%s' % (
+            markup[:start_pos],
+            class_name,
+            serializer(indentation),
+            markup[end_pos:])
+
     def _serialize_indentation(self, chars):
         """Serializes an indentation string into an HTML representation.
 
diff --git a/reviewboard/diffviewer/tests.py b/reviewboard/diffviewer/tests.py
index 5a555ebb4a8e310145a54d1e23f222a557a57b96..1771784f6322e3b0b44e4363eb58f6529ef2a3a9 100644
--- a/reviewboard/diffviewer/tests.py
+++ b/reviewboard/diffviewer/tests.py
@@ -1170,6 +1170,27 @@ class DiffChunkGeneratorTests(TestCase):
             self.generator._highlight_indentation('', '        foo', True, 4),
             ('', '<span class="indent">&gt;&gt;&gt;&gt;</span>    foo'))
 
+    def test_highlight_indent_with_adjacent_tag(self):
+        """Testing DiffChunkGenerator._highlight_indentation
+        with indentation and adjacent tag wrapping whitespace
+        """
+        self.assertEqual(
+            self.generator._highlight_indentation(
+                '',
+                '<span class="s"> </span>foo',
+                True, 1),
+            ('',
+             '<span class="s"><span class="indent">&gt;</span></span>foo'))
+
+    def test_highlight_indent_with_unexpected_chars(self):
+        """Testing DiffChunkGenerator._highlight_indentation
+        with indentation and unexpected markup chars
+        """
+        self.assertEqual(
+            self.generator._highlight_indentation(
+                '', ' <span>  </span> foo', True, 4),
+            ('', ' <span>  </span> foo'))
+
     def test_highlight_unindent(self):
         """Testing DiffChunkGenerator._highlight_indentation
         with unindentation
@@ -1178,6 +1199,27 @@ class DiffChunkGeneratorTests(TestCase):
             self.generator._highlight_indentation('        foo', '', False, 4),
             ('<span class="unindent">&lt;&lt;&lt;&lt;</span>    foo', ''))
 
+    def test_highlight_unindent_with_adjacent_tag(self):
+        """Testing DiffChunkGenerator._highlight_indentation
+        with unindentation and adjacent tag wrapping whitespace
+        """
+        self.assertEqual(
+            self.generator._highlight_indentation(
+                '<span class="s"> </span>foo',
+                '',
+                False, 1),
+            ('<span class="s"><span class="unindent">&lt;</span></span>foo',
+             ''))
+
+    def test_highlight_unindent_with_unexpected_chars(self):
+        """Testing DiffChunkGenerator._highlight_indentation
+        with unindentation and unexpected markup chars
+        """
+        self.assertEqual(
+            self.generator._highlight_indentation(
+                ' <span>  </span> foo', '', False, 4),
+            (' <span>  </span> foo', ''))
+
 
 class DiffOpcodeGeneratorTests(TestCase):
     """Unit tests for DiffOpcodeGenerator."""
