Skip to content

Commit 12d8872

Browse files
authored
Merge pull request #809 from vkbo/item_tags
Make XML parser for items less picky
2 parents 960d196 + 40ff865 commit 12d8872

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

nw/core/item.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ def unpackXML(self, xItem):
106106
if "order" in xItem.attrib:
107107
self.setOrder(xItem.attrib["order"])
108108

109-
retStatus = True
110109
for xValue in xItem:
111110
if xValue.tag == "name":
112111
self.setName(xValue.text)
@@ -131,10 +130,12 @@ def unpackXML(self, xItem):
131130
elif xValue.tag == "cursorPos":
132131
self.setCursorPos(xValue.text)
133132
else:
133+
# Sliently skip as we may otherwise cause orphaned
134+
# items if an otherwise valid file is opened by a
135+
# version of novelWriter that doesn't know the tag.
134136
logger.error("Unknown tag '%s'" % xValue.tag)
135-
retStatus = False
136137

137-
return retStatus
138+
return True
138139

139140
@staticmethod
140141
def _subPack(xParent, name, attrib=None, text=None, none=True):

tests/test_core/test_core_item.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def testCoreItem_LayoutSetter(dummyGUI):
270270
# END Test testCoreItem_LayoutSetter
271271

272272
@pytest.mark.core
273-
def testCoreItem_XMLPackUnpack(dummyGUI):
273+
def testCoreItem_XMLPackUnpack(dummyGUI, caplog):
274274
"""Test packing and unpacking XML objects for the NWItem class.
275275
"""
276276
theProject = NWProject(dummyGUI)
@@ -370,31 +370,33 @@ def testCoreItem_XMLPackUnpack(dummyGUI):
370370
# Errors
371371

372372
## Not an Item
373-
xDummy = etree.SubElement(nwXML, "stuff")
374-
assert not theItem.unpackXML(xDummy)
373+
mockXml = etree.SubElement(nwXML, "stuff")
374+
assert theItem.unpackXML(mockXml) is False
375375

376376
## Item without Handle
377-
xDummy = etree.SubElement(nwXML, "item", attrib={"stuff": "nah"})
378-
assert not theItem.unpackXML(xDummy)
377+
mockXml = etree.SubElement(nwXML, "item", attrib={"stuff": "nah"})
378+
assert theItem.unpackXML(mockXml) is False
379379

380-
## Item with Invalid SubElement
381-
xDummy = etree.SubElement(nwXML, "item", attrib={"handle": "0123456789abc"})
382-
xParam = etree.SubElement(xDummy, "invalid")
380+
## Item with Invalid SubElement is Accepted w/Error
381+
mockXml = etree.SubElement(nwXML, "item", attrib={"handle": "0123456789abc"})
382+
xParam = etree.SubElement(mockXml, "invalid")
383383
xParam.text = "stuff"
384-
assert not theItem.unpackXML(xDummy)
384+
caplog.clear()
385+
assert theItem.unpackXML(mockXml) is True
386+
assert "Unknown tag 'invalid'" in caplog.text
385387

386388
# Pack Valid Item
387-
xDummy = etree.SubElement(nwXML, "group")
388-
theItem._subPack(xDummy, "subGroup", {"one": "two"}, "value", False)
389-
assert etree.tostring(xDummy, pretty_print=False, encoding="utf-8") == (
389+
mockXml = etree.SubElement(nwXML, "group")
390+
theItem._subPack(mockXml, "subGroup", {"one": "two"}, "value", False)
391+
assert etree.tostring(mockXml, pretty_print=False, encoding="utf-8") == (
390392
b"<group><subGroup one=\"two\">value</subGroup></group>"
391393
)
392394

393395
# Pack Not Allowed None
394-
xDummy = etree.SubElement(nwXML, "group")
395-
assert theItem._subPack(xDummy, "subGroup", {}, None, False) is None
396-
assert theItem._subPack(xDummy, "subGroup", {}, "None", False) is None
397-
assert etree.tostring(xDummy, pretty_print=False, encoding="utf-8") == (
396+
mockXml = etree.SubElement(nwXML, "group")
397+
assert theItem._subPack(mockXml, "subGroup", {}, None, False) is None
398+
assert theItem._subPack(mockXml, "subGroup", {}, "None", False) is None
399+
assert etree.tostring(mockXml, pretty_print=False, encoding="utf-8") == (
398400
b"<group/>"
399401
)
400402

0 commit comments

Comments
 (0)