Fix crash when certain list style elements are not present.

Specifically:

1. No style:list-level-properties is present.
2. No style:list-level-label-alignment child of that is present.
   In this case, it simply falls back to default zero margins and
   bullet widths.

Previously this would simply not load documents which did not come
with this optional element in a list style.

Layout tests and CSS generation tests are added for both cases.
This commit is contained in:
Aditya Bhatt
2014-07-11 12:13:55 +02:00
parent d3a26ea1e1
commit c95ff2d0c3
3 changed files with 133 additions and 14 deletions
+20 -12
View File
@@ -170,9 +170,9 @@
var selector = 'text|list[text|style-name="' + name + '"]',
level = node.getAttributeNS(textns, "level"),
listItemRule,
listLevelProps = /**@type{!Element}*/(node.getElementsByTagNameNS(stylens, "list-level-properties")[0]),
listLevelPositionSpaceMode = listLevelProps.getAttributeNS(textns, "list-level-position-and-space-mode"),
listLevelLabelAlign = /**@type{!Element}*/(listLevelProps.getElementsByTagNameNS(stylens, "list-level-label-alignment")[0]),
listLevelProps,
listLevelPositionSpaceMode,
listLevelLabelAlign,
listIndent,
textAlign,
bulletWidth,
@@ -181,6 +181,13 @@
followedBy,
leftOffset;
// style:list-level-properties is an optional element. Since the rest of this function
// depends on its existence, return from it if it is not found.
listLevelProps = /**@type{!Element|undefined}*/(node.getElementsByTagNameNS(stylens, "list-level-properties")[0]);
listLevelPositionSpaceMode = listLevelProps && listLevelProps.getAttributeNS(textns, "list-level-position-and-space-mode");
listLevelLabelAlign = /**@type{!Element|undefined}*/(listLevelProps) &&
/**@type{!Element|undefined}*/(listLevelProps.getElementsByTagNameNS(stylens, "list-level-label-alignment")[0]);
// calculate CSS selector based on list level
level = level && parseInt(level, 10);
while (level > 1) {
@@ -190,7 +197,7 @@
// TODO: fo:text-align is only an optional attribute with <style:list-level-properties>,
// needs to be found what should be done if not present. For now falling back to "left"
textAlign = listLevelProps.getAttributeNS(fons, "text-align") || "left";
textAlign = (listLevelProps && listLevelProps.getAttributeNS(fons, "text-align")) || "left";
// convert the start and end text alignments to left and right as
// IE does not support the start and end values for text alignment
switch (textAlign) {
@@ -207,18 +214,18 @@
// TODO: fetch the margin and indent from the paragraph style if it is defined there
// http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#element-style_list-level-label-alignment
// for now just fallback to "0px" if not defined on <style:list-level-label-alignment>
listIndent = listLevelLabelAlign.getAttributeNS(fons, "margin-left") || "0px";
bulletIndent = listLevelLabelAlign.getAttributeNS(fons, "text-indent") || "0px";
followedBy = listLevelLabelAlign.getAttributeNS(textns, "label-followed-by");
listIndent = (listLevelLabelAlign && listLevelLabelAlign.getAttributeNS(fons, "margin-left")) || "0px";
bulletIndent = (listLevelLabelAlign && listLevelLabelAlign.getAttributeNS(fons, "text-indent")) || "0px";
followedBy = listLevelLabelAlign && listLevelLabelAlign.getAttributeNS(textns, "label-followed-by");
leftOffset = convertToPxValue(listIndent);
} else {
// this block is entered if list-level-position-and-space-mode
// has the value label-width-and-position or is not present
// TODO: fallback values should be read from parent styles or (system) defaults
listIndent = listLevelProps.getAttributeNS(textns, "space-before") || "0px";
bulletWidth = listLevelProps.getAttributeNS(textns, "min-label-width") || "0px";
labelDistance = listLevelProps.getAttributeNS(textns, "min-label-distance") || "0px";
listIndent = (listLevelProps && listLevelProps.getAttributeNS(textns, "space-before")) || "0px";
bulletWidth = (listLevelProps && listLevelProps.getAttributeNS(textns, "min-label-width")) || "0px";
labelDistance = (listLevelProps && listLevelProps.getAttributeNS(textns, "min-label-distance")) || "0px";
leftOffset = convertToPxValue(listIndent) + convertToPxValue(bulletWidth);
}
@@ -252,7 +259,7 @@
}
} else {
listItemRule += 'min-width: ' + bulletWidth + ';';
listItemRule += 'margin-left: -' + bulletWidth + ';';
listItemRule += 'margin-left: ' + (parseFloat(bulletWidth) === 0 ? '' : '-') + bulletWidth + ';';
listItemRule += 'padding-right: ' + labelDistance + ';';
}
// Content needs to be on a new line if it contains slashes due to a bug in older versions of webkit
@@ -311,4 +318,5 @@
}
};
};
}());
}());
+31 -1
View File
@@ -130,6 +130,34 @@ odf.ListStyleToCssTests = function ListStyleToCssTests(runner) {
}
function style_WithNo_list_level_poperties() {
applyListStyles(
'<text:list-style style:name="lijst">' +
'<text:list-level-style-bullet text:bullet-char="-" text:level="1">' +
'</text:list-level-style-bullet>' +
'</text:list-style>',
"");
r.shouldBe(t, "t.styleSheet.cssRules.length", "3");
r.shouldBe(t, "t.styleSheet.cssRules.rules[0].ruleText", "'text|list[text|style-name=\"lijst\"] > text|list-item{margin-left: 0px;}'");
r.shouldBe(t, "t.styleSheet.cssRules.rules[1].ruleText", "'text|list[text|style-name=\"lijst\"] > text|list-item > text|list{margin-left: 0px;}'");
r.shouldBe(t, "t.styleSheet.cssRules.rules[2].ruleText", "'text|list[text|style-name=\"lijst\"] > text|list-item > *:not(text|list):first-child:before{text-align: left;counter-increment:list;display: inline-block;min-width: 0px;margin-left: 0px;padding-right: 0px;\\ncontent: \"-\";\\n}'");
}
function style_list_level_properties_WithNo_style_list_level_label_alignment() {
applyListStyles(
'<text:list-style style:name="lijst">' +
'<text:list-level-style-bullet text:bullet-char="-" text:level="1">' +
'<style:list-level-properties text:list-level-position-and-space-mode="label-alignment">' +
'</style:list-level-properties>' +
'</text:list-level-style-bullet>' +
'</text:list-style>',
"");
r.shouldBe(t, "t.styleSheet.cssRules.length", "3");
r.shouldBe(t, "t.styleSheet.cssRules.rules[0].ruleText", "'text|list[text|style-name=\"lijst\"] > text|list-item{margin-left: 0px;}'");
r.shouldBe(t, "t.styleSheet.cssRules.rules[1].ruleText", "'text|list[text|style-name=\"lijst\"] > text|list-item > text|list{margin-left: 0px;}'");
r.shouldBe(t, "t.styleSheet.cssRules.rules[2].ruleText", "'text|list[text|style-name=\"lijst\"] > text|list-item > *:not(text|list):first-child:before{text-align: left;counter-increment:list;display: inline-block;margin-left: 0px;\\ncontent: \"-\";\\n}'");
}
/**
* WebKit, Chrome + FF all have radically different ways of joining & quoting CSS content.
* The tests being created depend on a predictable string being parsed out for comparison, hence
@@ -357,7 +385,9 @@ odf.ListStyleToCssTests = function ListStyleToCssTests(runner) {
style_list_level_label_alignment_WithNo_fo_margin_left,
numberedListPrefixes,
numberedListSuffixes,
bulletCharacters
bulletCharacters,
style_WithNo_list_level_poperties,
style_list_level_properties_WithNo_style_list_level_label_alignment
]);
};
this.asyncTests = function () {
+82 -1
View File
@@ -229,6 +229,87 @@
</check>
</layoutchecks>
</test>
<test name="list-level-properties-missing">
<input>
<office:document-styles>
<office:automatic-styles>
<text:list-style style:name="L1">
<text:list-level-style-number style:num-format="1" style:num-suffix=")" text:level="1">
</text:list-level-style-number>
<text:list-level-style-number style:num-format="a" style:num-letter-sync="true" style:num-suffix=")" text:level="2">
</text:list-level-style-number>
</text:list-style>
</office:automatic-styles>
</office:document-styles>
<office:text>
<text:list text:style-name="L1" xml:id="list432847329805709832">
<text:list-item>
<text:p text:style-name="P1">This is a numbering test, This is a numbering test, This is a numbering test, This is a numbering test, This is a numbering test, This is a numbering test, This is a numbering test.</text:p>
<text:list>
<text:list-item>
<text:p text:style-name="P1">This is a numbering test, This is a numbering test, This is a numbering test, This is a numbering test, This is a numbering test, This is a numbering test, This is a numbering test.</text:p>
</text:list-item>
</text:list>
</text:list-item>
</text:list>
</office:text>
</input>
<layoutchecks>
<check xpath="(.//text:list)[1]">
<marginLeft value="0px"/>
</check>
<check xpath="(.//text:list)[2]">
<marginLeft value="0px"/>
</check>
</layoutchecks>
</test>
<test name="list-label-alignment-missing">
<input>
<office:document-styles>
<office:automatic-styles>
<text:list-style style:name="L1">
<text:list-level-style-number style:num-format="1" style:num-suffix="." text:level="1" text:style-name="Numbering_20_Symbols">
<style:list-level-properties text:list-level-position-and-space-mode="label-alignment">
</style:list-level-properties>
</text:list-level-style-number>
<text:list-level-style-number style:num-format="1" style:num-suffix="." text:display-levels="2" text:level="2" text:style-name="Numbering_20_Symbols">
<style:list-level-properties text:list-level-position-and-space-mode="label-alignment">
</style:list-level-properties>
</text:list-level-style-number>
</text:list-style>
</office:automatic-styles>
</office:document-styles>
<office:text>
<text:list text:style-name="L1" xml:id="list555894327569445">
<text:list-item>
<text:p text:style-name="P1">This is a numbering test, This is a numbering test, This is a numbering test, This is a numbering test, This is a numbering test, This is a numbering test, This is a numbering test.</text:p>
<text:list>
<text:list-item>
<text:p text:style-name="P1">This is a numbering test, This is a numbering test, This is a numbering test, This is a numbering test, This is a numbering test, This is a numbering test, This is a numbering test.</text:p>
</text:list-item>
</text:list>
</text:list-item>
</text:list>
</office:text>
</input>
<layoutchecks>
<check xpath="(.//text:list)[1]">
<marginLeft value="0px"/>
</check>
<check xpath="(.//text:list)[2]">
<marginLeft value="0px"/>
</check>
<check xpath="(.//text:list-item)[1]">
<marginLeft value="0px"/>
</check>
<check xpath="(.//text:list-item)[2]">
<marginLeft value="0px"/>
</check>
</layoutchecks>
</test>
<test name="label-alignment-list">
<input>
<office:document-styles>
@@ -340,4 +421,4 @@
</check>
</layoutchecks>
</test>
</layouttests>
</layouttests>