Skip to content

Exclude annotation text from Node.__str__/__unicode__ (fixes #146)#152

Open
apoorvdarshan wants to merge 1 commit into
eea:masterfrom
apoorvdarshan:fix/issue-146-str-skips-annotation
Open

Exclude annotation text from Node.__str__/__unicode__ (fixes #146)#152
apoorvdarshan wants to merge 1 commit into
eea:masterfrom
apoorvdarshan:fix/issue-146-str-skips-annotation

Conversation

@apoorvdarshan

Copy link
Copy Markdown

Fixes #146

Root cause

Node.__str__ and Node.__unicode__ build the string value of an element by
recursing into every child node. For a spreadsheet cell that carries a
comment, the cell's children include an office:annotation subtree (the
comment's dc:date and paragraphs) in addition to the actual value paragraph.
Because nothing distinguished the annotation from real content, its text was
concatenated into the cell's string value.

Reproduction

from odf import office, table, text, dc

cell = table.TableCell(valuetype="string")

annotation = office.Annotation()
annotation.addElement(dc.Date(text=u"2025-05-12T00:00:00"))
p1 = text.P(); p1.addElement(text.Span(text=u"On Day2, did thing."))
annotation.addElement(p1)
p2 = text.P(); p2.addElement(text.Span(text=u"-Commenter"))
annotation.addElement(p2)
cell.addElement(annotation)

cell.addElement(text.P(text=u"Cell Value"))

print(repr(str(cell)))

Observed on master (wrong — comment text leaks in):

'2025-05-12T00:00:00On Day2, did thing.-CommenterCell Value'

With this change (correct — only the cell's own value):

'Cell Value'

Fix

Skip office:annotation and office:annotation-end children when collecting
the string value in Node.__str__/Node.__unicode__. The annotation elements
themselves are left untouched and still serialize to XML through toXml();
only text extraction via str()/unicode() changes. The check uses
getattr(c, "qname", None) so it is safe for text nodes, which have no
qname. The two matched qnames are collected in a module-level frozenset
for clarity and cheap membership testing.

This is a localized change to the two text-collection methods plus one import;
it does not alter serialization or any element construction.

Tests

Added tests/testannotation.py with test_str_ignores_annotation and
test_unicode_ignores_annotation, which build a commented cell and assert the
string value equals "Cell Value". Both tests fail on unmodified master
(they produce the concatenated string above) and pass with this change.

The rest of the existing suite continues to pass (115 passed, 1 skipped in my
run). Two pre-existing failures in testload.py::...test_metagenerator and
teststyles.py::...testAttributeForeign are present on pristine master as
well (unrelated to this change; they reproduce with the patch reverted).

Disclosure: prepared with AI assistance; reviewed and verified locally.

Node.__str__ and Node.__unicode__ collect the string value of an element
by recursing into every child, including office:annotation (cell comment)
subtrees. As a result str(cell) on a spreadsheet cell that carries a
comment returned the comment's date and body concatenated with the cell
value, e.g. "2025-05-12T00:00:00On Day2, did thing.-CommenterCell Value"
instead of "Cell Value".

Skip office:annotation and office:annotation-end children when building
the string value so the returned text reflects only the cell's own
content. The annotation elements are untouched and still serialize to XML
via toXml(); only text extraction changes.
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.

odf.element.Node.__str__ includes annotation text in the node contents.

1 participant