diff --git a/scrapy/tests/test_utils_markup.py b/scrapy/tests/test_utils_markup.py index f51350d90..b363106fd 100644 --- a/scrapy/tests/test_utils_markup.py +++ b/scrapy/tests/test_utils_markup.py @@ -87,6 +87,10 @@ class UtilsMarkupTest(unittest.TestCase): self.assertEqual(remove_tags(u'

texty

', which_ones=('b',)), u'

texty

') + # text with empty tags + self.assertEqual(remove_tags(u'a
b
c'), u'abc') + self.assertEqual(remove_tags(u'a
b
c', which_ones=('br',)), u'abc') + def test_remove_tags_with_content(self): # make sure it always return unicode assert isinstance(remove_tags_with_content('no tags'), unicode) @@ -105,6 +109,9 @@ class UtilsMarkupTest(unittest.TestCase): self.assertEqual(remove_tags_with_content(u'not will removedi will removed', which_ones=('i',)), u'not will removed') + # text with empty tags + self.assertEqual(remove_tags_with_content(u'
a
', which_ones=('br',)), u'a') + def test_replace_escape_chars(self): # make sure it always return unicode assert isinstance(replace_escape_chars('no ec'), unicode) diff --git a/scrapy/utils/markup.py b/scrapy/utils/markup.py index 5e2e0b1c1..7904267da 100644 --- a/scrapy/utils/markup.py +++ b/scrapy/utils/markup.py @@ -85,7 +85,7 @@ def remove_tags(text, which_ones=(), encoding=None): if is empty remove all tags. """ if which_ones: - tags = ['<%s>|<%s .*?>|' % (tag, tag, tag) for tag in which_ones] + tags = ['<%s/?>|<%s .*?>|' % (tag, tag, tag) for tag in which_ones] regex = '|'.join(tags) else: regex = '<.*?>' @@ -101,7 +101,7 @@ def remove_tags_with_content(text, which_ones=(), encoding=None): """ text = str_to_unicode(text, encoding) if which_ones: - tags = '|'.join(['<%s.*?' % (tag, tag) for tag in which_ones]) + tags = '|'.join([r'<%s.*?|<%s\s*/>' % (tag, tag, tag) for tag in which_ones]) retags = re.compile(tags, re.DOTALL | re.IGNORECASE) text = retags.sub(u'', text) return text