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 dc7a67c commit fe7708b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions mammoth/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ def visit_hyperlink(self, hyperlink, context):
return [html.collapsible_element("a", attributes, nodes)]


def visit_checkbox(self, checkbox, context):
attributes = {"type": "checkbox"}

if checkbox.checked:
attributes["checked"] = "checked"

return [html.element("input", attributes)]


def visit_bookmark(self, bookmark, context):
element = html.collapsible_element(
"a",
Expand Down
6 changes: 6 additions & 0 deletions mammoth/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ class Hyperlink(HasChildren):
anchor = cobble.field()
target_frame = cobble.field()

@cobble.data
class Checkbox(Element):
checked = cobble.field()

checkbox = Checkbox

@cobble.data
class Table(HasChildren):
style_id = cobble.field()
Expand Down
14 changes: 14 additions & 0 deletions tests/conversion_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,20 @@ def test_hyperlink_target_frame_is_used_as_anchor_target():
assert_equal('<a href="#start" target="_blank">Hello</a>', result.value)


def test_unchecked_checkbox_is_converted_to_unchecked_checkbox_input():
result = convert_document_element_to_html(
documents.checkbox(checked=False),
)
assert_equal('<input type="checkbox" />', result.value)


def test_checked_checkbox_is_converted_to_checked_checkbox_input():
result = convert_document_element_to_html(
documents.checkbox(checked=True),
)
assert_equal('<input checked="checked" type="checkbox" />', result.value)


def test_bookmarks_are_converted_to_anchors_with_ids():
result = convert_document_element_to_html(
documents.bookmark(name="start"),
Expand Down

0 comments on commit fe7708b

Please sign in to comment.