Skip to content

Commit

Permalink
fix: Fixed the bug that caused output failure due to the lack of name…
Browse files Browse the repository at this point in the history
…spaces.
  • Loading branch information
bookfere committed May 9, 2024
1 parent c4808c7 commit f892863
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
10 changes: 5 additions & 5 deletions lib/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,11 @@ def _polish_translation(self, translation):

def _create_new_element(
self, name, content='', copy_attrs=True, excluding_attrs=[]):
# new_element = self.element.makeelement(
# get_name(self.element), nsmap={'xhtml': ns['x']})
# new_element.text = trim(translation)
new_element = etree.XML('<{0} xmlns="{1}">{2}</{0}>'.format(
name, ns['x'], trim(content)))
namespaces = ' '.join(
'xmlns%s="%s"' % ('' if name is None else ':' + name, value)
for name, value in self.element.nsmap.items())
new_element = etree.XML('<{0} {1}>{2}</{0}>'.format(
name, namespaces, trim(content)))
# Preserve all attributes from the original element.
if copy_attrs:
for name, value in self.element.items():
Expand Down
24 changes: 23 additions & 1 deletion tests/test_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ def test_get_content(self):
self.assertEqual(
'<img src="w3.jpg"></img>', self.element.reserve_elements[6])
self.assertEqual(
'<code>App\Http</code>', self.element.reserve_elements[7])
'<code>App\\Http</code>', self.element.reserve_elements[7])
self.assertEqual('<sup>[1]</sup>', self.element.reserve_elements[8])

def test_get_content_with_sub_sup(self):
Expand Down Expand Up @@ -583,6 +583,28 @@ def test_add_translation_with_linefeeds(self):
'A<br/><br/>B<br/>C</p>',
get_string(elements[1]))

def test_add_translation_with_extra_namespace(self):
xhtml = etree.XML(rb"""<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:epub="http://www.idpf.org/2007/ops" lang="en">
<head><title>Test Document</title></head>
<body><p>a<code><span epub:type="pagebreak"/>b</code></p></body>
</html>""")
element = PageElement(xhtml.find('.//x:p', namespaces=ns), 'p1')
element.reserve_pattern = create_xpath(('code',))
element.set_placeholder(Base.placeholder)
element.get_content()
element.add_translation('A{{id_00000}}')

elements = xhtml.findall('.//x:p', namespaces=ns)
self.assertEqual(2, len(elements))
self.assertEqual(
'<p xmlns="http://www.w3.org/1999/xhtml" '
'xmlns:epub="http://www.idpf.org/2007/ops" '
'dir="auto">A<code><span epub:type="pagebreak"/>b</code></p>',
get_string(elements[1]))

def test_add_translation_below(self):
self.element.position = 'next'
self.element.original_color = 'red'
Expand Down

0 comments on commit f892863

Please sign in to comment.