From c95ff2d0c356c2f6485b875cbd2e42e2270018a2 Mon Sep 17 00:00:00 2001 From: Aditya Bhatt Date: Thu, 10 Apr 2014 15:51:39 +0200 Subject: [PATCH] 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. --- webodf/lib/odf/ListStylesToCss.js | 32 ++++++---- webodf/tests/odf/ListStyleToCssTests.js | 32 +++++++++- webodf/tests/odf/layouttests.xml | 83 ++++++++++++++++++++++++- 3 files changed, 133 insertions(+), 14 deletions(-) diff --git a/webodf/lib/odf/ListStylesToCss.js b/webodf/lib/odf/ListStylesToCss.js index 493211dd..c896d9e3 100644 --- a/webodf/lib/odf/ListStylesToCss.js +++ b/webodf/lib/odf/ListStylesToCss.js @@ -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 , // 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 - 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 @@ } }; }; -}()); \ No newline at end of file +}()); + diff --git a/webodf/tests/odf/ListStyleToCssTests.js b/webodf/tests/odf/ListStyleToCssTests.js index 75bc01fe..f16c228d 100644 --- a/webodf/tests/odf/ListStyleToCssTests.js +++ b/webodf/tests/odf/ListStyleToCssTests.js @@ -130,6 +130,34 @@ odf.ListStyleToCssTests = function ListStyleToCssTests(runner) { } + function style_WithNo_list_level_poperties() { + applyListStyles( + '' + + '' + + '' + + '', + ""); + 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( + '' + + '' + + '' + + '' + + '' + + '', + ""); + 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 () { diff --git a/webodf/tests/odf/layouttests.xml b/webodf/tests/odf/layouttests.xml index 61fbd8ee..736dc6d7 100644 --- a/webodf/tests/odf/layouttests.xml +++ b/webodf/tests/odf/layouttests.xml @@ -229,6 +229,87 @@ + + + + + + + + + + + + + + + + + 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. + + + 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. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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. + + + 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. + + + + + + + + + + + + + + + + + + + + + + @@ -340,4 +421,4 @@ - \ No newline at end of file +