Skip to content

Fix rich-text bold/italic/underline parsing per ECMA-376 - #448

Open
dev-aliawan wants to merge 1 commit into
justkawal:mainfrom
dev-aliawan:medpulse-fixes
Open

dev-aliawan wants to merge 1 commit into
justkawal:mainfrom
dev-aliawan:medpulse-fixes

Conversation

@dev-aliawan

Copy link
Copy Markdown

Summary

SharedString.textSpan in lib/src/sharedStrings/shared_strings.dart misparses three explicit-off OOXML boolean markers in rich-text runs:

  • <b val="0"/> imported as bold=true
  • <i val="0"/> imported as italic=true
  • <u val="none"/> imported as Underline.Single

Root cause

The getBool helper used inside textSpan:

bool getBool(XmlElement element) {
  return bool.tryParse(element.getAttribute('val') ?? '') ?? true;
}

Per the Dart SDK, bool.tryParse only accepts the literal strings "true" / "false" (https://api.dart.dev/dart-core/bool/tryParse.html). Excel writes <b val="0"/> to mean explicitly-off — XSD boolean's lexical space allows "1" and "0" in addition to "true"/"false" — so bool.tryParse("0") returns null, the ?? true default fires, and the run is misread as bold on.

The underline switch-case has a sibling bug: any non-"double" value is mapped to Underline.Single, which forces explicit <u val="none"/> runs to import underlined.

Spec reference

Reference implementation

openpyxl handles this correctly in descriptors/base.py:

class Bool(Convertible):
    def __set__(self, instance, value):
        if isinstance(value, str):
            if value in ('false', 'f', '0'):
                value = False
        super().__set__(instance, value)

This PR mirrors that behavior.

Impact

Downstream users importing xlsx files authored with Excel's "bold default + un-bold specific runs" pattern currently see every un-bolded run rendered as bold. Verified against a real 23-question content file (50+ <b val="0"/> runs) — flags now match openpyxl ground truth exactly.

Testing

Fix was verified end-to-end against real content. Happy to add unit tests matching the project's preferred style if you'd like — I didn't see existing coverage for the rich-text parsing path.

The previous implementation of getBool in SharedString.textSpan used
bool.tryParse, which only accepts the literal strings 'true' and 'false'
per the Dart core library. Excel writes <b val="0"/> to mean explicitly
off (W3C XSD boolean lexical space allows 1/0 in addition to true/false;
see ECMA-376 §18.8.2), so bool.tryParse('0') returned null and the
?? true fallback misread every explicit-off run as bold on. Same for
italic.

Fix getBool to match the spec + openpyxl's shipping implementation:
- val absent → true (spec default)
- val in {false, f, 0, off} (case-insensitive) → false
- everything else → true

Also fix the sibling underline bug: <u val="none"/> was previously
misread as single underline because the switch only tested for 'double'.
Per ECMA-376 §18.4.13, ST_UnderlineValues includes 'none' as an explicit
off state — honor it.
dev-aliawan added a commit to dev-aliawan/excel_community that referenced this pull request Apr 18, 2026
The previous implementation of getBool in SharedString.textSpan used
bool.tryParse, which only accepts the literal strings 'true' and 'false'
per the Dart core library. Excel writes <b val="0"/> to mean explicitly
off (W3C XSD boolean lexical space allows 1/0 in addition to true/false;
see ECMA-376 §18.8.2), so bool.tryParse('0') returned null and the
?? true fallback misread every explicit-off run as bold on. Same for
italic.

Fix getBool to match the spec + openpyxl's shipping implementation:
- val absent → true (spec default)
- val in {false, f, 0, off} (case-insensitive) → false
- everything else → true

Also fix the sibling underline bug: <u val="none"/> was previously
misread as single underline because the switch only tested for 'double'.
Per ECMA-376 §18.4.13, ST_UnderlineValues includes 'none' as an explicit
off state — honor it.

Inherited from justkawal/excel as of the community fork's creation; same
fix filed upstream as justkawal/excel#448.
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