Exclude annotation text from Node.__str__/__unicode__ (fixes #146)#152
Open
apoorvdarshan wants to merge 1 commit into
Open
Exclude annotation text from Node.__str__/__unicode__ (fixes #146)#152apoorvdarshan wants to merge 1 commit into
apoorvdarshan wants to merge 1 commit into
Conversation
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.
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.
Fixes #146
Root cause
Node.__str__andNode.__unicode__build the string value of an element byrecursing into every child node. For a spreadsheet cell that carries a
comment, the cell's children include an
office:annotationsubtree (thecomment's
dc:dateand 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
Observed on
master(wrong — comment text leaks in):With this change (correct — only the cell's own value):
Fix
Skip
office:annotationandoffice:annotation-endchildren when collectingthe string value in
Node.__str__/Node.__unicode__. The annotation elementsthemselves are left untouched and still serialize to XML through
toXml();only text extraction via
str()/unicode()changes. The check usesgetattr(c, "qname", None)so it is safe for text nodes, which have noqname. The two matched qnames are collected in a module-levelfrozensetfor 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.pywithtest_str_ignores_annotationandtest_unicode_ignores_annotation, which build a commented cell and assert thestring value equals
"Cell Value". Both tests fail on unmodifiedmaster(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_metageneratorandteststyles.py::...testAttributeForeignare present on pristinemasteraswell (unrelated to this change; they reproduce with the patch reverted).
Disclosure: prepared with AI assistance; reviewed and verified locally.