Probable bug in JsonOperationConsumer.getDefaultOutlineLevelFromStyleHierarchy
Static analysis (yes, I'm actually working on that ticket too) showed that the method JsonOperationConsumer.getDefaultOutlineLevelFromStyleHierarchy() does either not work or is overly complex. Look at the comments I added in the following snippet:
private static int getDefaultOutlineLevelFromStyleHierarchy(
TextParagraphElementBase paragraphBaseElement) {
OdfStyle style = paragraphBaseElement.getDocumentStyle();
int outlineLevel = getStyleOutlineLevel(style);
if (style != null) {
// if no outline Level was set, but there is a parentComponentElement
if (outlineLevel == NO_OUTLINE_LEVEL) { // <-- the if is only entered when outlineLevel == NO_OUTLINE_LEVEL
OdfStyleBase parentStyle = null;
style.getParentStyle();
while (outlineLevel == NO_OUTLINE_LEVEL) { // <-- this condition is always true because outlineLevel is never updated
outlineLevel = getStyleOutlineLevel(parentStyle); // <-- this will return the old value because parentStyle is null
if (outlineLevel != NO_OUTLINE_LEVEL) {
break;
}
if (parentStyle instanceof OdfStyle) { // <-- here, parentStyle is still null, so instanceOf will return false and parentStyle is not updated
parentStyle = ((OdfStyle) parentStyle).getParentStyle();
} else {
break; // <-- this break will always be reached in the first iteration
}
if (parentStyle == null) {
break;
}
}
}
}
return outlineLevel;
}
I think maybe this:
OdfStyleBase parentStyle = null;
style.getParentStyle();
should really be
OdfStyleBase parentStyle = style.getParentStyle();
But as I don't have a test case for this, I'd rather have someone else check what is the correct thing to do here.
Just some background on the ODF styling. Similar to HTML/CSS, there is a style hierarchy.
- Closest to the text (overwriting everything like inline HTML) is the automatic styles or hard formatting.
When you select in LibreOffice some character and press the bold button!
It is done without XML-style elements like HTML once did, but only by referenced styles. These automatic styles are typically in the
content.xml(only those from hard-formatted content in the header and footers are in the styles.xml file)
<office:automatic-styles>
<style:style style:name="co1" style:family="table-column">
<style:table-column-properties fo:break-before="auto" style:column-width="2.267cm"/>
</style:style>
"co1" is the name of a table column. The styles of lists and tables are always in automatic (or hard) styles in LibreOffice. 2. There can only be one automatic style, but this can inherit from a template style like
<style:style style:name="Result2" style:family="table-cell" style:parent-style-name="Result" style:data-style-name="N108"/>
</office:styles>
The template style can have a parent style. Above it is style:parent-style-name="Result"
So starting from a potential automatic style, there is an arbitrary long list of parent ancestors.
3. But above all, style and even if no style is set, there is the default style for each style:family, like
<office:styles>
<style:default-style style:family="table-cell">
<style:table-cell-properties style:decimal-places="2"/>
<style:paragraph-properties style:tab-stop-distance="1.27cm"/>
<style:text-properties style:font-name="Arial" fo:font-size="10pt" fo:language="en" fo:country="US" style:font-name-asian="Lucida Sans Unicode" style:font-size-asian="10pt" style:language-asian="zxx" style:country-asian="none" style:font-name-complex="Tahoma" style:font-size-complex="10pt" style:language-complex="zxx" style:country-complex="none"/>
</style:default-style>
Aside from the style inheritance itself, there can be a paragraph <text:p> with text formatting properties and a span <text:span> with text properties, where the embedded element is inheriting from the parent element.
In theory, there can be any arbitrary number of nested span elements, like a text ABA within a large span with text colour yellow and the B in a nested span with text colour red. I normalised this into a list of spans, which have nesting when I dealt with change operations - exchanging formatting between web offices.
I will dive into this issue earliest next week...and will explain/fix it.. just wanted to give some background...