Fix bug #986 to better handle a both a correctly behaving lxml, as well as a badly behaving lxml

This commit is contained in:
Raoul Snyman 2022-02-04 11:39:44 -07:00
parent bc872e0e87
commit c0e8cef731
2 changed files with 40 additions and 2 deletions

View File

@ -683,8 +683,7 @@ class OpenLyrics(object):
string_lines = etree.tostring(lines) string_lines = etree.tostring(lines)
# lxml 4.6.x or some recent version of libxml2 seems to add the rest of the song to the string, so split on the # lxml 4.6.x or some recent version of libxml2 seems to add the rest of the song to the string, so split on the
# closing verse tag, and then add it back in # closing verse tag, and then add it back in
string_lines, _ = string_lines.split(b'</lines>', 1) string_lines = string_lines.split(b'</lines>', 1)[0] + b'</lines>'
string_lines += b'</lines>'
element = etree.XML(string_lines) element = etree.XML(string_lines)
# OpenLyrics 0.8 uses <br/> for new lines. Append text from "lines" element to verse text. # OpenLyrics 0.8 uses <br/> for new lines. Append text from "lines" element to verse text.

View File

@ -37,6 +37,14 @@ RESULT_TAGS = [{"temporary": False, "protected": False, "desc": "z", "start tag"
{"temporary": False, "end tag": "{/c}", "desc": "c", "start tag": "{c}", {"temporary": False, "end tag": "{/c}", "desc": "c", "start tag": "{c}",
"start html": "<span class=\"chord\" style=\"display:none\"><strong>", "end html": "</strong></span>", "start html": "<span class=\"chord\" style=\"display:none\"><strong>", "end html": "</strong></span>",
"protected": False}] "protected": False}]
VERSE_LINES_07_XML = '<lines>\
<line>Amazing grace, how sweet the sound</line>\
<line>That saved a wretch like me</line>\
</lines>'
VERSE_LINES_08_XML = '<lines>\
Amazing grace, how sweet the sound<br/>\
That saved a wretch like me\
</lines>'
AUTHOR_XML = '<properties>\ AUTHOR_XML = '<properties>\
<authors>\ <authors>\
<author type="words">Test Author1</author>\ <author type="words">Test Author1</author>\
@ -65,6 +73,37 @@ def test_songxml_get_verses_invalid_xml():
assert result == [] assert result == []
def test_process_verse_lines_v07():
"""
Test that the _process_verse_lines method correctly processes the verse lines with v0.7 OpenLyrics
"""
# GIVEN: Some lyrics XML and version 0.7 of OpenLyrics
open_lyrics = OpenLyrics(MagicMock())
lines = objectify.fromstring(VERSE_LINES_07_XML)
# WHEN: The lyrics of a verse are processed
result = open_lyrics._process_verse_lines(lines, '0.7')
# THEN: The results should be correct
assert result == 'Amazing grace, how sweet the sound\nThat saved a wretch like me'
def test_process_verse_lines_v08():
"""
Test that the _process_verse_lines method correctly processes the verse lines with v0.8 OpenLyrics
"""
# GIVEN: Some lyrics XML and version 0.8 of OpenLyrics
open_lyrics = OpenLyrics(MagicMock())
lines = objectify.fromstring(VERSE_LINES_08_XML)
# WHEN: The lyrics of a verse are processed
result = open_lyrics._process_verse_lines(lines, '0.8')
# THEN: The results should be correct
assert result == ' Amazing grace, how sweet the sound'\
' That saved a wretch like me '
def test_process_formatting_tags(settings): def test_process_formatting_tags(settings):
""" """
Test that _process_formatting_tags works Test that _process_formatting_tags works