lyrics: Remove script tags (fix #1034)

This commit is contained in:
Adrian Sampson 2014-10-24 17:33:11 -07:00
parent 61bdbd6dd7
commit 0325fe2225
2 changed files with 10 additions and 6 deletions

View file

@ -329,10 +329,10 @@ def _scrape_strip_cruft(html, plain_text_out=False):
"""
html = unescape(html)
# Normalize EOL
html = html.replace('\r', '\n')
html = html.replace('\r', '\n') # Normalize EOL.
html = re.sub(r' +', ' ', html) # Whitespaces collapse.
html = BREAK_RE.sub('\n', html) # <br> eats up surrounding '\n'
html = BREAK_RE.sub('\n', html) # <br> eats up surrounding '\n'.
html = re.sub(r'<(script).*?</\1>(?s)', '', html) # Strip script tags.
if plain_text_out: # Strip remaining HTML tags
html = TAG_RE.sub('', html)

View file

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# This file is part of beets.
# Copyright 2014, Fabrice Laporte.
#
@ -131,7 +130,7 @@ class LyricsPluginTest(unittest.TestCase):
self.assertFalse(lyrics.is_lyrics(t))
def test_slugify(self):
text = u"http://site.com/çafe-au_lait(boisson)"
text = u"http://site.com/\xe7afe-au_lait(boisson)"
self.assertEqual(lyrics.slugify(text), 'http://site.com/cafe_au_lait')
def test_scrape_strip_cruft(self):
@ -144,6 +143,11 @@ class LyricsPluginTest(unittest.TestCase):
self.assertEqual(lyrics._scrape_strip_cruft(text, True),
"one\ntwo !\n\nfour")
def test_scrape_strip_scripts(self):
text = u"""foo<script>bar</script>baz"""
self.assertEqual(lyrics._scrape_strip_cruft(text, True),
"foobaz")
def test_scrape_merge_paragraphs(self):
text = u"one</p> <p class='myclass'>two</p><p>three"
self.assertEqual(lyrics._scrape_merge_paragraphs(text),
@ -263,7 +267,7 @@ class LyricsGooglePluginTest(unittest.TestCase):
except ImportError:
self.skipTest('Beautiful Soup 4 not available')
if sys.version_info[:3] < (2, 7, 3):
self.skipTest("Pythons built-in HTML parser is not good enough")
self.skipTest("Python's built-in HTML parser is not good enough")
lyrics.LyricsPlugin()
lyrics.fetch_url = MockFetchUrl()