Skip to content

Commit

Permalink
Convert checkbox document elements to HTML checkboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilliamson committed Nov 30, 2024
1 parent 1a0d6b7 commit a2d7d04
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,18 @@ private String generateHref(Hyperlink hyperlink) {
}
}

@Override
public List<HtmlNode> visit(Checkbox checkbox, Context context) {
Map<String, String> attributes = new HashMap<>();
attributes.put("type", "checkbox");

if (checkbox.checked()) {
attributes.put("checked", "checked");
}

return list(Html.element("input", attributes));
}

@Override
public List<HtmlNode> visit(Bookmark bookmark, Context context) {
return list(Html.element("a", map("id", generateId(bookmark.getName())), list(Html.FORCE_WRITE)));
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/org/zwobble/mammoth/internal/documents/Checkbox.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.zwobble.mammoth.internal.documents;

public class Checkbox implements DocumentElement {
private final boolean checked;

public Checkbox(boolean checked) {
this.checked = checked;
}

public boolean checked() {
return this.checked;
}

@Override
public <T, U> T accept(DocumentElementVisitor<T, U> visitor, U context) {
return visitor.visit(this, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public interface DocumentElementVisitor<T, U> {
T visit(TableCell tableCell, U context);

T visit(Hyperlink hyperlink, U context);
T visit(Checkbox checkbox, U context);
T visit(Bookmark bookmark, U context);
T visit(NoteReference noteReference, U context);
T visit(CommentReference commentReference, U context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,22 @@ public void hyperlinkTargetFrameIsUsedAsAnchorTarget() {
deepEquals(list(Html.collapsibleElement("a", map("href", "#doc-42-start", "target", "_blank"), list(Html.text("Hello"))))));
}

@Test
public void uncheckedCheckboxIsConvertedToUncheckedCheckboxInput() {
assertThat(
convertToHtml(checkbox(false)),
deepEquals(list(Html.element("input", map("type", "checkbox"))))
);
}

@Test
public void checkedCheckboxIsConvertedToCheckedCheckboxInput() {
assertThat(
convertToHtml(checkbox(true)),
deepEquals(list(Html.element("input", map("type", "checkbox", "checked", "checked"))))
);
}

@Test
public void bookmarksAreConvertedToAnchorsWithIds() {
assertThat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,8 @@ public static Argument<Optional<String>> withAnchor(String anchor) {
public static Argument<Optional<String>> withTargetFrame(String targetFrame) {
return arg(TARGET_FRAME, Optional.of(targetFrame));
}

public static Checkbox checkbox(boolean checked) {
return new Checkbox(checked);
}
}

0 comments on commit a2d7d04

Please sign in to comment.