Skip to content

fix(epub): safely extract metadata text without crashing on None nodeValue or nested elements - #2247

Open
hsusul wants to merge 1 commit into
microsoft:mainfrom
hsusul:fix-epub-metadata-nodevalue-crash
Open

fix(epub): safely extract metadata text without crashing on None nodeValue or nested elements#2247
hsusul wants to merge 1 commit into
microsoft:mainfrom
hsusul:fix-epub-metadata-nodevalue-crash

Conversation

@hsusul

@hsusul hsusul commented Jul 27, 2026

Copy link
Copy Markdown

Summary

  • Fixes EpubConverter metadata extraction crash (AttributeError: 'NoneType' object has no attribute 'strip') when Dublin Core metadata elements in EPUB content.opf contain nested XML/HTML tags or produce a None nodeValue.
  • Recursively collects text node values across child nodes instead of assuming node.firstChild.nodeValue is a non-None string.

Problem

In xml.dom.minidom, all Node objects (including Element nodes) define a nodeValue property whose value is None for element nodes. EpubConverter._get_all_texts_from_nodes checked hasattr(node.firstChild, "nodeValue"), which always evaluates to True for element children (e.g. <dc:title><span>Structured</span> Title</dc:title>). Attempting to call .strip() on None raised AttributeError and caused the entire EpubConverter.convert() process to fail.

Root cause

Line 144 of _epub_converter.py assumed node.firstChild.nodeValue was a string if hasattr(node.firstChild, "nodeValue") returned true. When node.firstChild is an Element, nodeValue is None, raising AttributeError: 'NoneType' object has no attribute 'strip'.

Fix

Added _collect_node_text to 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 — PASS

Compatibility

100% backward compatible. Preserves valid text extraction for simple string metadata while preventing crashes on formatted or nested metadata elements in EPUB documents.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant