diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-01-25 15:26:01 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-01-25 15:26:01 +0000 |
| commit | b5606471d456a4d08ac18a97b58e353938ce39f9 (patch) | |
| tree | 289c3671a5ec3126f0f3052d1f2725f909e34e69 | |
| parent | b09f43356b49922fc7c03a998f696af69a7ffb26 (diff) | |
Allow empty HTML paths
| -rw-r--r-- | mammoth/html_paths.py | 2 | ||||
| -rw-r--r-- | mammoth/style_reader/html_path_reader.py | 13 | ||||
| -rw-r--r-- | tests/style_reader_tests.py | 8 |
3 files changed, 21 insertions, 2 deletions
diff --git a/mammoth/html_paths.py b/mammoth/html_paths.py index 887288b..1428015 100644 --- a/mammoth/html_paths.py +++ b/mammoth/html_paths.py @@ -15,3 +15,5 @@ def element(names, class_names=None, fresh=None): HtmlPath = collections.namedtuple("HtmlPath", ["elements"]) HtmlPathElement = collections.namedtuple("HtmlPathElement", ["names", "class_names", "fresh"]) + +empty = path([]) diff --git a/mammoth/style_reader/html_path_reader.py b/mammoth/style_reader/html_path_reader.py index b508f59..20666d4 100644 --- a/mammoth/style_reader/html_path_reader.py +++ b/mammoth/style_reader/html_path_reader.py @@ -9,6 +9,13 @@ def read_html_path(string): def read_html_path_node(path_node): + if path_node.children: + return _read_html_path_elements_node(path_node.children[0]) + else: + return html_paths.empty + + +def _read_html_path_elements_node(path_node): elements = [ _read_element_node(child) for child in _repeated_children_with_separator(path_node, has_whitespace=True) @@ -60,7 +67,9 @@ def _repeated_children_with_separator(node, has_whitespace): grammar_text = r""" -html_path = element (whitespace* ">" whitespace* element)* +html_path = html_path_elements? + +html_path_elements = element (whitespace* ">" whitespace* element)* element = tag_names class_name* fresh? @@ -70,7 +79,7 @@ class_name = "." identifier fresh = ":fresh" -identifier = ~"[A-Z0-9]*"i +identifier = ~"[A-Z0-9]+"i whitespace = ~"\s"* diff --git a/tests/style_reader_tests.py b/tests/style_reader_tests.py index c4ac513..464cb4a 100644 --- a/tests/style_reader_tests.py +++ b/tests/style_reader_tests.py @@ -7,11 +7,19 @@ from mammoth.style_reader import read_html_path, read_document_matcher, read_sty @istest class ReadHtmlPathTests(object): @istest + def can_read_empty_path(self): + assert_equal( + html_paths.empty, + read_html_path("") + ) + + @istest def can_read_single_element(self): assert_equal( html_paths.path([html_paths.element(["p"])]), read_html_path("p") ) + @istest def can_read_choice_of_two_elements(self): |
