diff --git a/django/utils/html.py b/django/utils/html.py
index 5732350..2a20847 100644
a
|
b
|
from django.utils.functional import allow_lazy
|
16 | 16 | from django.utils import six |
17 | 17 | from django.utils.text import normalize_newlines |
18 | 18 | |
19 | | from .html_parser import HTMLParser |
| 19 | from .html_parser import HTMLParser, HTMLParseError |
20 | 20 | |
21 | 21 | |
22 | 22 | # Configuration for urlize() function. |
… |
… |
class MLStripper(HTMLParser):
|
136 | 136 | def strip_tags(value): |
137 | 137 | """Returns the given HTML with all tags stripped.""" |
138 | 138 | s = MLStripper() |
139 | | s.feed(value) |
140 | | data = s.get_data() |
141 | 139 | try: |
142 | | res = s.close() |
143 | | except Exception as e: |
144 | | data += s.rawdata |
145 | | return data |
| 140 | s.feed(value) |
| 141 | s.close() |
| 142 | except HTMLParseError: |
| 143 | data = value |
| 144 | else: |
| 145 | data = s.get_data() |
| 146 | return data.replace('<', '<').replace('>', '>') |
146 | 147 | strip_tags = allow_lazy(strip_tags) |
147 | 148 | |
148 | 149 | def remove_tags(html, tags): |
diff --git a/docs/ref/utils.txt b/docs/ref/utils.txt
index 14ae9aa..8de2e0f 100644
a
|
b
|
escaping HTML.
|
566 | 566 | If ``value`` is ``"<b>Joel</b> <button>is</button> a <span>slug</span>"`` the |
567 | 567 | return value will be ``"Joel is a slug"``. |
568 | 568 | |
| 569 | .. versionchanged:: 1.6 |
| 570 | |
| 571 | For improved safety, ``strip_tags`` is now parser-based. Any ``<`` or |
| 572 | ``>`` characters that are not part of a valid tag are replaced by their |
| 573 | encoded equivalents (``<`` and ``>``). |
| 574 | |
| 575 | |
569 | 576 | .. function:: remove_tags(value, tags) |
570 | 577 | |
571 | 578 | Removes a space-separated list of [X]HTML tag names from the output. |
diff --git a/tests/utils_tests/test_html.py b/tests/utils_tests/test_html.py
index c3e9f7c..78967d5 100644
a
|
b
|
class TestUtilsHtml(TestCase):
|
69 | 69 | ('<adf>a', 'a'), |
70 | 70 | ('</adf>a', 'a'), |
71 | 71 | ('<asdf><asdf>e', 'e'), |
72 | | ('hi, <f x', 'hi, <f x'), |
73 | | ('</fe', '</fe'), |
| 72 | ('hi, <f x', 'hi, <f x'), |
| 73 | ('234<235, right?', '234<235, right?'), |
| 74 | ('a4<a5 right?', 'a4<a5 right?'), |
| 75 | ('b7>b2!', 'b7>b2!'), |
| 76 | ('</fe', '</fe'), |
74 | 77 | ('<x>b<y>', 'b'), |
75 | 78 | ('a<p onclick="alert(\'<test>\')">b</p>c', 'abc'), |
76 | 79 | ('a<p a >b</p>c', 'abc'), |