fix(epub): safely extract metadata text without crashing on None nodeValue or nested elements - #2247
Open
hsusul wants to merge 1 commit into
Open
fix(epub): safely extract metadata text without crashing on None nodeValue or nested elements#2247hsusul wants to merge 1 commit into
hsusul wants to merge 1 commit into
Conversation
…Value or nested elements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
EpubConvertermetadata extraction crash (AttributeError: 'NoneType' object has no attribute 'strip') when Dublin Core metadata elements in EPUBcontent.opfcontain nested XML/HTML tags or produce aNonenodeValue.node.firstChild.nodeValueis a non-None string.Problem
In
xml.dom.minidom, allNodeobjects (includingElementnodes) define anodeValueproperty whose value isNonefor element nodes.EpubConverter._get_all_texts_from_nodescheckedhasattr(node.firstChild, "nodeValue"), which always evaluates toTruefor element children (e.g.<dc:title><span>Structured</span> Title</dc:title>). Attempting to call.strip()onNoneraisedAttributeErrorand caused the entireEpubConverter.convert()process to fail.Root cause
Line 144 of
_epub_converter.pyassumednode.firstChild.nodeValuewas a string ifhasattr(node.firstChild, "nodeValue")returned true. Whennode.firstChildis anElement,nodeValueisNone, raisingAttributeError: 'NoneType' object has no attribute 'strip'.Fix
Added
_collect_node_textto recursively collect text content across text and CDATA child nodes under any metadata element, stripping whitespace and filtering out empty metadata entries.Testing
pytest -k test_epub_metadata_nodevalue— PASS (1 passed)ruff check src/markitdown/converters/_epub_converter.py tests/test_module_misc.py— PASS (0 errors)mypy src/markitdown/converters/_epub_converter.py --ignore-missing-imports— PASS (Success)git diff --check— PASSCompatibility
100% backward compatible. Preserves valid text extraction for simple string metadata while preventing crashes on formatted or nested metadata elements in EPUB documents.