Skip to content

Commit 58b1bac

Browse files
committed
Support for li/@value override.
1 parent 466d2d0 commit 58b1bac

3 files changed

Lines changed: 40 additions & 5 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@
55

66
/bin-testOutput/
77
/openhtmltopdf_2021.txt
8+
9+
#IntelliJ IDEA files
10+
.idea
11+
*.iml

src/main/java/org/docx4j/convert/in/xhtml/ListHelper.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@
7272
import org.w3c.dom.css.CSSPrimitiveValue;
7373
import org.w3c.dom.css.CSSValue;
7474

75+
76+
import static org.docx4j.com.google.common.base.Strings.isNullOrEmpty;
77+
7578
public class ListHelper {
7679

7780
public static Logger log = LoggerFactory.getLogger(ListHelper.class);
@@ -548,10 +551,11 @@ void addNumbering(P p, Element e, Map<String, PropertyValue> cssMap) {
548551

549552
NumberFormat specified = getNumberFormatFromCSSListStyleType(
550553
cssMap.get("list-style-type" ).getCssText());
551-
554+
final String valueAttribute = e.getAttribute("value");
552555
if (peekListItemStateStack().isFirstItem // and level already exists,
553556
|| numfmtExisting ==null
554-
|| numfmtExisting.getVal()!=specified ) {
557+
|| numfmtExisting.getVal()!=specified
558+
|| !isNullOrEmpty(valueAttribute)) {
555559

556560
// can't re-use..
557561

@@ -600,7 +604,7 @@ void addNumbering(P p, Element e, Map<String, PropertyValue> cssMap) {
600604
int ilvl = lvl.getIlvl().intValue();
601605
log.debug("concrete list points at abstract " + getConcreteList().getAbstractNumId().getVal().longValue());
602606
long newNumId = ndp.restart(getConcreteList().getNumId().longValue(), ilvl,
603-
/* restart at */ 1);
607+
/* restart at */ isNullOrEmpty(valueAttribute)?1:Integer.parseInt(valueAttribute));
604608
// retrieve it
605609
ListNumberingDefinition listDef = ndp.getInstanceListDefinitions().get(""+newNumId);
606610

src/test/java/org/docx4j/convert/in/xhtml/NumberingTest.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
import org.junit.Ignore;
5959
import org.junit.Test;
6060

61+
62+
import static org.junit.Assert.assertEquals;
63+
6164
public class NumberingTest {
6265

6366
private WordprocessingMLPackage wordMLPackage;
@@ -475,7 +478,32 @@ public void testUnorderedCssOnLiToIndent() throws Docx4JException {
475478
assertTrue( p.getPPr().getInd()==null);
476479

477480
}
478-
481+
482+
@Test public void testListItemValueOverridden() throws Docx4JException {
483+
this.addNumberingPart(wordMLPackage.getMainDocumentPart());
484+
this.addStylesPart(wordMLPackage.getMainDocumentPart());
485+
String xhtml= "<div>" +
486+
"<ol>"
487+
+"<li>Item 1</li>"
488+
+"<li value=\"1\">Second item with 1 as number</li>"
489+
+"<li>Item 2</li>"
490+
+"<li value=\"2\">Second item with 2 as number</li>"
491+
+"</ol>"+
492+
"</div>";
493+
List<Object> results = convert( xhtml, FormattingOption.CLASS_PLUS_OTHER);
494+
wordMLPackage.getMainDocumentPart().getContent().addAll(results);
495+
System.out.println(XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true));
496+
final P item1 = (P) results.get(0);
497+
final P secondItem1 = (P) results.get(1);
498+
final P item2 = (P) results.get(2);
499+
final P secondItem2 = (P) results.get(3);
500+
assertEquals(BigInteger.valueOf(2L),item1.getPPr().getNumPr().getNumId().getVal());
501+
assertEquals(BigInteger.valueOf(3L),secondItem1.getPPr().getNumPr().getNumId().getVal());
502+
assertEquals(BigInteger.valueOf(3L),item2.getPPr().getNumPr().getNumId().getVal());
503+
assertEquals(BigInteger.valueOf(4L),secondItem2.getPPr().getNumPr().getNumId().getVal());
504+
}
505+
506+
479507
// ===============================================================================
480508
// machinery / helpers
481509

@@ -929,7 +957,6 @@ public Numbering getNumbering() {
929957

930958
return numbering;
931959
}
932-
933960

934961

935962
}

0 commit comments

Comments
 (0)