From 8661cad2a82b66254fff564f4a514cbd67ad371d Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 10 Dec 2016 22:01:20 +0000 Subject: Remove self-closing elements from AST --- mammoth/conversion.py | 2 +- mammoth/html/__init__.py | 29 ++++++++--------------------- mammoth/html/nodes.py | 7 +++---- mammoth/images.py | 2 +- tests/conversion_tests.py | 2 +- tests/html/strip_empty_tests.py | 4 ++-- 6 files changed, 16 insertions(+), 30 deletions(-) diff --git a/mammoth/conversion.py b/mammoth/conversion.py index bbb4f86..9398c5c 100644 --- a/mammoth/conversion.py +++ b/mammoth/conversion.py @@ -179,7 +179,7 @@ class _DocumentConverter(documents.ElementVisitor): def visit_line_break(self, line_break): - return [html.self_closing_element("br")] + return [html.element("br")] def visit_note_reference(self, note_reference): self._note_references.append(note_reference) diff --git a/mammoth/html/__init__.py b/mammoth/html/__init__.py index 6e97ead..b397a81 100644 --- a/mammoth/html/__init__.py +++ b/mammoth/html/__init__.py @@ -1,5 +1,5 @@ from ..lists import flat_map -from .nodes import TextNode, Tag, Element, SelfClosingElement, ForceWrite, NodeVisitor +from .nodes import TextNode, Tag, Element, ForceWrite, NodeVisitor def text(value): @@ -24,13 +24,6 @@ def element(tag_names, attributes=None, children=None, collapsible=None, separat def collapsible_element(tag_names, attributes=None, children=None): return element(tag_names, attributes, children, collapsible=True) - - -def self_closing_element(tag_name, attributes=None): - if attributes is None: - attributes = {} - - return SelfClosingElement(tag_name, attributes) force_write = ForceWrite() @@ -53,14 +46,11 @@ class StripEmpty(NodeVisitor): def visit_element(self, element): children = strip_empty(element.children) - if len(children) == 0: + if len(children) == 0 and not element.is_void(): return [] else: return [Element(element.tag, children)] - def visit_self_closing_element(self, element): - return [element] - def visit_force_write(self, node): return [node] @@ -80,9 +70,6 @@ class _CollapseNode(NodeVisitor): def visit_element(self, element): return Element(element.tag, collapse(element.children)) - def visit_self_closing_element(self, element): - return element - def visit_force_write(self, node): return node @@ -133,12 +120,12 @@ class _NodeWriter(NodeVisitor): self._writer.text(node.value) def visit_element(self, element): - self._writer.start(element.tag_name, element.attributes) - self.visit_all(element.children) - self._writer.end(element.tag_name) - - def visit_self_closing_element(self, element): - self._writer.self_closing(element.tag_name, element.attributes) + if element.is_void(): + self._writer.self_closing(element.tag_name, element.attributes) + else: + self._writer.start(element.tag_name, element.attributes) + self.visit_all(element.children) + self._writer.end(element.tag_name) def visit_force_write(self, element): pass diff --git a/mammoth/html/nodes.py b/mammoth/html/nodes.py index ff31c8c..9c07259 100644 --- a/mammoth/html/nodes.py +++ b/mammoth/html/nodes.py @@ -47,11 +47,10 @@ class Element(Node): def separator(self): return self.tag.separator + _VOID_TAG_NAMES = set(["br", "hr", "img"]) -@cobble.data -class SelfClosingElement(Node): - tag_name = cobble.field() - attributes = cobble.field() + def is_void(self): + return not self.children and self.tag_name in self._VOID_TAG_NAMES @cobble.visitable diff --git a/mammoth/images.py b/mammoth/images.py index 4dc095a..297b0a2 100644 --- a/mammoth/images.py +++ b/mammoth/images.py @@ -7,7 +7,7 @@ def img_element(func): if image.alt_text: attributes["alt"] = image.alt_text - return [html.self_closing_element("img", attributes)] + return [html.element("img", attributes)] return convert_image diff --git a/tests/conversion_tests.py b/tests/conversion_tests.py index 0d08b20..71fbbc7 100644 --- a/tests/conversion_tests.py +++ b/tests/conversion_tests.py @@ -389,7 +389,7 @@ def images_have_alt_tags_if_available(): def can_define_custom_conversion_for_images(): def convert_image(image): with image.open() as image_file: - return [html.self_closing_element("img", {"alt": image_file.read().decode("ascii")})] + return [html.element("img", {"alt": image_file.read().decode("ascii")})] image = documents.image(alt_text=None, content_type="image/png", open=lambda: io.BytesIO(b"abc")) result = convert_document_element_to_html(image, convert_image=convert_image) diff --git a/tests/html/strip_empty_tests.py b/tests/html/strip_empty_tests.py index eb28f30..b72b50a 100644 --- a/tests/html/strip_empty_tests.py +++ b/tests/html/strip_empty_tests.py @@ -54,8 +54,8 @@ def empty_children_are_removed(): @istest def self_closing_elements_are_never_empty(): assert_equal( - [html.self_closing_element("br")], - html.strip_empty([html.self_closing_element("br")])) + [html.element("br")], + html.strip_empty([html.element("br")])) @istest -- cgit v1.2.3