diff --git a/src/calibre/ebooks/oeb/polish/container.py b/src/calibre/ebooks/oeb/polish/container.py index 2321a56463..f8580235c2 100644 --- a/src/calibre/ebooks/oeb/polish/container.py +++ b/src/calibre/ebooks/oeb/polish/container.py @@ -345,12 +345,23 @@ class Container(object): # {{{ self.remove_from_xml(elem) self.dirty(self.opf_name) if removed: + for spine in self.opf_xpath('//opf:spine'): + tocref = spine.attrib.get('toc', None) + if tocref and tocref in removed: + spine.attrib.pop('toc', None) + self.dirty(self.opf_name) + for item in self.opf_xpath('//opf:spine/opf:itemref[@idref]'): idref = item.get('idref') if idref in removed: self.remove_from_xml(item) self.dirty(self.opf_name) + for meta in self.opf_xpath('//opf:meta[@name="cover" and @content]'): + if meta.get('content') in removed: + self.remove_from_xml(meta) + self.dirty(self.opf_name) + for item in self.opf_xpath('//opf:guide/opf:reference[@href]'): if self.href_to_name(item.get('href'), self.opf_name) == name: self.remove_from_xml(item) diff --git a/src/calibre/ebooks/oeb/polish/tests/container.py b/src/calibre/ebooks/oeb/polish/tests/container.py index e65f25fe6f..609766b6fd 100644 --- a/src/calibre/ebooks/oeb/polish/tests/container.py +++ b/src/calibre/ebooks/oeb/polish/tests/container.py @@ -57,3 +57,19 @@ class ContainerTests(BaseTest): for c in (c1, c2): c.commit(outpath=x) + def test_file_removal(self): + ' Test removal of files from the container ' + book = get_simple_book() + c = get_container(book, tdir=self.tdir) + files = ('toc.ncx', 'cover.png', 'titlepage.xhtml') + self.assertIn('titlepage.xhtml', {x[0] for x in c.spine_names}) + self.assertTrue(c.opf_xpath('//opf:meta[@name="cover"]')) + for x in files: + c.remove_item(x) + self.assertIn(c.opf_name, c.dirtied) + self.assertNotIn('titlepage.xhtml', {x[0] for x in c.spine_names}) + self.assertFalse(c.opf_xpath('//opf:meta[@name="cover"]')) + raw = c.serialize_item(c.opf_name).decode('utf-8') + for x in files: + self.assertNotIn(x, raw) +