aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-12-12 22:22:16 +0000
committerMichael Williamson <mike@zwobble.org>2013-12-12 22:22:16 +0000
commit2791c74b8c1afc1014e380111b7e7f4d4ea09539 (patch)
tree966d0718caa3522eb2477d1f64558ff7085d19a6
parenteab369b5721b933c080516fd1dcdfa67e6e2dfcd (diff)
Read :fresh on HTML path elements
-rw-r--r--mammoth/html_paths.py8
-rw-r--r--mammoth/style_reader.py13
-rw-r--r--tests/style_reader_tests.py10
3 files changed, 24 insertions, 7 deletions
diff --git a/mammoth/html_paths.py b/mammoth/html_paths.py
index 0f2184d..887288b 100644
--- a/mammoth/html_paths.py
+++ b/mammoth/html_paths.py
@@ -5,11 +5,13 @@ def path(elements):
return HtmlPath(elements)
-def element(names, class_names=None):
+def element(names, class_names=None, fresh=None):
if class_names is None:
class_names = []
- return HtmlPathElement(names, class_names)
+ if fresh is None:
+ fresh = False
+ return HtmlPathElement(names, class_names, fresh)
HtmlPath = collections.namedtuple("HtmlPath", ["elements"])
-HtmlPathElement = collections.namedtuple("HtmlPathElement", ["names", "class_names"])
+HtmlPathElement = collections.namedtuple("HtmlPathElement", ["names", "class_names", "fresh"])
diff --git a/mammoth/style_reader.py b/mammoth/style_reader.py
index 4418dbb..2cdc261 100644
--- a/mammoth/style_reader.py
+++ b/mammoth/style_reader.py
@@ -20,7 +20,8 @@ def _read_path_node(path_node):
def _read_element_node(node):
tag_names = _read_tag_names_node(node.children[0])
class_names = _read_class_names_node(node.children[1])
- return html_paths.element(tag_names, class_names=class_names)
+ fresh = _read_fresh_node(node.children[2])
+ return html_paths.element(tag_names, class_names=class_names, fresh=fresh)
def _read_tag_names_node(node):
@@ -41,6 +42,10 @@ def _read_class_name_node(node):
return node.children[1].text
+def _read_fresh_node(node):
+ return len(node.children) > 0
+
+
def _repeated_children_with_separator(node, has_whitespace):
yield node.children[0]
@@ -58,11 +63,13 @@ _grammar = Grammar(r"""
path = element whitespace* (">" whitespace* element)*
-element = tag_names class_name*
+element = tag_names class_name* fresh?
+
+tag_names = identifier ("|" identifier)*
class_name = "." identifier
-tag_names = identifier ("|" identifier)*
+fresh = ":fresh"
identifier = ~"[A-Z0-9]*"i
diff --git a/tests/style_reader_tests.py b/tests/style_reader_tests.py
index a25d7ce..80c3124 100644
--- a/tests/style_reader_tests.py
+++ b/tests/style_reader_tests.py
@@ -46,8 +46,16 @@ class ReadHtmlPathTests(object):
@istest
- def can_read__multiple_classes_on_element(self):
+ def can_read_multiple_classes_on_element(self):
assert_equal(
html_paths.path([html_paths.element(["p"], class_names=["tip", "help"])]),
read_html_path("p.tip.help")
)
+
+
+ @istest
+ def can_read_when_element_must_be_fresh(self):
+ assert_equal(
+ html_paths.path([html_paths.element(["p"], fresh=True)]),
+ read_html_path("p:fresh")
+ )