Skip to content

Commit

Permalink
Parse run highlights
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilliamson committed Jun 13, 2024
1 parent c63abd5 commit 9b1601c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.8.0

* Support parsing of run highlights.

# 1.7.1

* Switch the precedence of numbering properties in paragraph properties and the
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/zwobble/mammoth/internal/documents/Run.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Optional;

public class Run implements DocumentElement, HasChildren {
private final Optional<String> highlight;
private final boolean isBold;
private final boolean isItalic;
private final boolean isUnderline;
Expand All @@ -15,6 +16,7 @@ public class Run implements DocumentElement, HasChildren {
private final List<DocumentElement> children;

public Run(
Optional<String> highlight,
boolean isBold,
boolean isItalic,
boolean isUnderline,
Expand All @@ -25,6 +27,7 @@ public Run(
Optional<Style> style,
List<DocumentElement> children
) {
this.highlight = highlight;
this.isBold = isBold;
this.isItalic = isItalic;
this.isUnderline = isUnderline;
Expand All @@ -36,6 +39,10 @@ public Run(
this.children = children;
}

public Optional<String> getHighlight() {
return highlight;
}

public boolean isBold() {
return isBold;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ private ReadResult readRun(XmlElement element) {
}

return new Run(
readHighlight(properties),
isBold(properties),
isItalic(properties),
isUnderline(properties),
Expand All @@ -201,6 +202,10 @@ private Optional<HyperlinkComplexField> currentHyperlinkComplexField() {
return tryGetLast(lazyFilter(this.complexFieldStack, HyperlinkComplexField.class));
}

private Optional<String> readHighlight(XmlElementLike properties) {
return readVal(properties, "w:highlight");
}

private boolean isBold(XmlElementLike properties) {
return readBooleanElement(properties, "w:b");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
public class DocumentElementMakers {
private static final ArgumentKey<Optional<Style>> STYLE = new ArgumentKey<>("style");
private static final ArgumentKey<Optional<NumberingLevel>> NUMBERING = new ArgumentKey<>("numbering");
private static final ArgumentKey<Optional<String>> HIGHLIGHT = new ArgumentKey<>("highlight");
private static final ArgumentKey<Boolean> BOLD = new ArgumentKey<>("bold");
private static final ArgumentKey<Boolean> ITALIC = new ArgumentKey<>("italic");
private static final ArgumentKey<Boolean> UNDERLINE = new ArgumentKey<>("underline");
Expand Down Expand Up @@ -118,6 +119,7 @@ public static Paragraph paragraphWithText(String text) {
public static Run run(Object... args) {
Arguments arguments = new Arguments(args);
return new Run(
arguments.get(HIGHLIGHT, Optional.empty()),
arguments.get(BOLD, false),
arguments.get(ITALIC, false),
arguments.get(UNDERLINE, false),
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,26 @@ public void runIsSubscriptIfVerticalAlignmentPropertyIsSetToSubscript() {
hasProperty("verticalAlignment", equalTo(VerticalAlignment.SUBSCRIPT)));
}

@Test
public void runHasNoHighlightByDefault() {
XmlElement element = runXmlWithProperties();

DocumentElement result = readSuccess(bodyReader(), element);

assertThat(result, hasProperty("highlight", equalTo(Optional.empty())));
}

@Test
public void runHasHighlightReadFromProperties() {
XmlElement element = runXmlWithProperties(
element("w:highlight", map("w:val", "yellow"))
);

DocumentElement result = readSuccess(bodyReader(), element);

assertThat(result, hasProperty("highlight", equalTo(Optional.of("yellow"))));
}

@Test
public void canReadTabElement() {
XmlElement element = element("w:tab");
Expand Down

0 comments on commit 9b1601c

Please sign in to comment.