diff --git a/reviewboard/diffviewer/chunk_generator.py b/reviewboard/diffviewer/chunk_generator.py
index 49062f5fa2d3b5a52e9f331ecbbbc4abae4bf547..54d233b562a41acbf8969daee94676b52138a0be 100644
--- a/reviewboard/diffviewer/chunk_generator.py
+++ b/reviewboard/diffviewer/chunk_generator.py
@@ -1,6 +1,7 @@
 from __future__ import unicode_literals
 
 import fnmatch
+import functools
 import re
 
 from django.utils import six
@@ -106,7 +107,6 @@ class DiffChunkGenerator(object):
         # Chunk processing state.
         self._last_header = [None, None]
         self._last_header_index = [0, 0]
-        self._cur_meta = {}
         self._chunk_index = 0
 
     def make_cache_key(self):
@@ -255,12 +255,10 @@ class DiffChunkGenerator(object):
             new_lines = markup_b[j1:j2]
             num_lines = max(len(old_lines), len(new_lines))
 
-            self._cur_meta = meta
-            lines = map(self._diff_line,
+            lines = map(functools.partial(self._diff_line, tag, meta),
                         range(line_num, line_num + num_lines),
                         range(i1 + 1, i2 + 1), range(j1 + 1, j2 + 1),
                         a[i1:i2], b[j1:j2], old_lines, new_lines)
-            self._cur_meta = None
 
             counts[tag] += num_lines
 
@@ -337,7 +335,7 @@ class DiffChunkGenerator(object):
 
         return True
 
-    def _diff_line(self, v_line_num, old_line_num, new_line_num,
+    def _diff_line(self, tag, meta, v_line_num, old_line_num, new_line_num,
                    old_line, new_line, old_markup, new_markup):
         """Creates a single line in the diff viewer.
 
@@ -347,10 +345,13 @@ class DiffChunkGenerator(object):
         region information, syntax-highlighted HTML for the text,
         and other metadata.
         """
-        if (old_line and new_line and
-                len(old_line) <= self.STYLED_MAX_LINE_LEN and
-                len(new_line) <= self.STYLED_MAX_LINE_LEN and
-                old_line != new_line):
+        if (tag == 'replace' and
+            old_line and new_line and
+            len(old_line) <= self.STYLED_MAX_LINE_LEN and
+            len(new_line) <= self.STYLED_MAX_LINE_LEN and
+            old_line != new_line):
+            # Generate information on the regions that changed between the
+            # two lines.
             old_region, new_region = \
                 get_line_changed_regions(old_line, new_line)
         else:
@@ -359,7 +360,6 @@ class DiffChunkGenerator(object):
         old_markup = old_markup or ''
         new_markup = new_markup or ''
 
-        meta = self._cur_meta
         line_pair = (old_line_num, new_line_num)
 
         indentation_changes = meta.get('indentation_changes', {})
diff --git a/reviewboard/diffviewer/templatetags/difftags.py b/reviewboard/diffviewer/templatetags/difftags.py
index 415748b5c1db7cf71b438d22f0d3e50839b4e7be..324109b4c2865ab6b03eed967b00ee1b627bb6ac 100644
--- a/reviewboard/diffviewer/templatetags/difftags.py
+++ b/reviewboard/diffviewer/templatetags/difftags.py
@@ -18,8 +18,7 @@ register = template.Library()
 
 @register.filter
 def highlightregion(value, regions):
-    """
-    Highlights the specified regions of text.
+    """Highlights the specified regions of text.
 
     This is used to insert ``<span class="hl">...</span>`` tags in the
     text as specified by the ``regions`` variable.
@@ -27,7 +26,7 @@ def highlightregion(value, regions):
     if not regions:
         return value
 
-    s = ""
+    s = ''
 
     # We need to insert span tags into a string already consisting
     # of span tags. We have a list of ranges that our span tags should
@@ -43,45 +42,52 @@ def highlightregion(value, regions):
     # This code makes the assumption that the list of regions is sorted.
     # This is safe to assume in practice, but if we ever at some point
     # had reason to doubt it, we could always sort the regions up-front.
-    in_tag = in_entity = in_hl = False
+    in_hl = False
     i = j = r = 0
-    region = regions[r]
+    region_start, region_end = regions[r]
 
-    for i in range(len(value)):
-        if value[i] == "<":
-            in_tag = True
+    while i < len(value):
+        c = value[i]
 
+        if c == '<':
             if in_hl:
-                s += "</span>"
+                s += '</span>'
                 in_hl = False
-        elif value[i] == ">":
-            in_tag = False
-        elif value[i] == ';' and in_entity:
-            in_entity = False
-            j += 1
-        elif not in_tag and not in_entity:
-            if not in_hl and region[0] <= j < region[1]:
+
+            k = value.find('>', i)
+            assert k != -1
+
+            s += value[i:k + 1]
+            i = k
+        else:
+            if not in_hl and region_start <= j < region_end:
                 s += '<span class="hl">'
                 in_hl = True
 
-            if value[i] == '&':
-                in_entity = True
+            if c == '&':
+                k = value.find(';', i)
+                assert k != -1
+
+                s += value[i:k + 1]
+                i = k
+                j += 1
             else:
                 j += 1
+                s += c
 
-        s += value[i]
-
-        if j == region[1]:
-            r += 1
-
+        if j == region_end:
             if in_hl:
                 s += '</span>'
                 in_hl = False
 
+            r += 1
+
             if r == len(regions):
                 break
 
-            region = regions[r]
+            region_start, region_end = regions[r]
+
+        i += 1
 
     if i + 1 < len(value):
         s += value[i + 1:]
@@ -196,13 +202,19 @@ def diff_lines(file, chunk, standalone, line_fmt, anchor_fmt,
     num_lines = len(lines)
     chunk_index = chunk['index']
     change = chunk['change']
-    is_equal = (change == 'equal')
-    is_replace = (change == 'replace')
-    is_insert = (change == 'insert')
-    is_delete = (change == 'delete')
-
-    moved_from_prev_linenum = None
-    moved_to_prev_linenum = None
+    is_equal = False
+    is_replace = False
+    is_insert = False
+    is_delete = False
+
+    if change == 'equal':
+        is_equal = True
+    elif change == 'replace':
+        is_replace = True
+    elif change == 'insert':
+        is_insert = True
+    elif change == 'delete':
+        is_delete = True
 
     result = []
 
