diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-11-29 22:05:08 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-11-29 22:05:08 +0000 |
| commit | 1b5c951358623ddb97a237efab72479d9dd9c4ca (patch) | |
| tree | c66c0fbdf9b3a2333de0c2418f4acb86618e1298 | |
| parent | 475077337689abeed1db21a832d26dbc2ae88672 (diff) | |
Add separator html.Element
| -rw-r--r-- | mammoth/html/__init__.py | 26 | ||||
| -rw-r--r-- | mammoth/html/nodes.py | 1 | ||||
| -rw-r--r-- | tests/html/collapse_tests.py | 18 |
3 files changed, 37 insertions, 8 deletions
diff --git a/mammoth/html/__init__.py b/mammoth/html/__init__.py index 889ec68..cf84043 100644 --- a/mammoth/html/__init__.py +++ b/mammoth/html/__init__.py @@ -6,14 +6,14 @@ def text(value): return TextNode(value) -def element(tag_names, attributes=None, children=None, collapsible=False): +def element(tag_names, attributes=None, children=None, collapsible=False, separator=None): if not isinstance(tag_names, list): tag_names = [tag_names] if attributes is None: attributes = {} if children is None: children = [] - return Element(tag_names, attributes, children, collapsible=collapsible) + return Element(tag_names, attributes, children, collapsible=collapsible, separator=separator) def collapsible_element(tag_names, attributes=None, children=None): @@ -50,11 +50,15 @@ class StripEmpty(NodeVisitor): if len(children) == 0: return [] else: - return [Element( - element.tag_names, - element.attributes, - children, - collapsible=element.collapsible)] + return [ + Element( + element.tag_names, + element.attributes, + children, + collapsible=element.collapsible, + separator=element.separator, + ) + ] def visit_self_closing_element(self, element): return [element] @@ -80,7 +84,9 @@ class _CollapseNode(NodeVisitor): element.tag_names, element.attributes, collapse(element.children), - collapsible=element.collapsible) + collapsible=element.collapsible, + separator=element.separator, + ) def visit_self_closing_element(self, element): return element @@ -110,8 +116,12 @@ def _try_collapse(collapsed, node): if not _is_match(last, node): return False + if node.separator: + last.children.append(text(node.separator)) + for child in node.children: _collapsing_add(last.children, child) + return True def _is_match(first, second): diff --git a/mammoth/html/nodes.py b/mammoth/html/nodes.py index a5a5bde..3b961f2 100644 --- a/mammoth/html/nodes.py +++ b/mammoth/html/nodes.py @@ -16,6 +16,7 @@ class Element(Node): attributes = cobble.field() children = cobble.field() collapsible = cobble.field() + separator = cobble.field() @property def tag_name(self): diff --git a/tests/html/collapse_tests.py b/tests/html/collapse_tests.py index 52cecd2..5dc7e6c 100644 --- a/tests/html/collapse_tests.py +++ b/tests/html/collapse_tests.py @@ -105,3 +105,21 @@ def element_with_choice_of_tag_names_can_collapse_into_previous_element_if_it_ha html.collapsible_element(["ul", "ol"]), html.collapsible_element("ol") ])) + + +@istest +def when_separator_is_present_then_separator_is_prepended_to_collapsed_element(): + assert_equal( + [ + html.element("pre", collapsible=False, children=[ + html.text("Hello"), + html.text("\n"), + html.text(" the"), + html.text("re") + ]) + ], + html.collapse([ + html.element("pre", collapsible=False, children=[html.text("Hello")]), + html.element("pre", collapsible=True, separator="\n", children=[html.text(" the"), html.text("re")]), + ]), + ) |
