Permalink
Browse files

more selector parsing

  • Loading branch information...
jtauber committed Jul 8, 2012
1 parent eea1c09 commit b12a1ab0b853c2183a0006befdb46ab3872657c7
Showing with 32 additions and 4 deletions.
  1. +3 −1 selectors.py
  2. +2 −2 selectors_lex.py
  3. +27 −1 selectors_parse.py
View
@@ -20,7 +20,9 @@ def __eq__(self, other):
if (
isinstance(other, ElementSelector) and
self.name == other.name and
- self.attr_selectors == other.attr_selectors
+ self.attr_selectors == other.attr_selectors and
+ self.ancestor == other.ancestor and
+ self.parent == other.parent
):
return True
else:
View
@@ -68,8 +68,8 @@ def t_STRING(t):
t_FUNCTION = ident + r"\("
t_NUMBER = num
t_HASH = r"\#" + name
-t_PLUS = w + r"\+"
-t_GREATER = w + r">"
+t_PLUS = w + r"\+" + w
+t_GREATER = w + r">" + w
t_COMMA = w + r","
# t_TILDE = w + r"~"
t_NOT = r":not\("
View
@@ -19,6 +19,27 @@ def p_selector(p):
p[0] = p[1]
+def p_selector_descendant(p):
+ """
+ selector : selector S simple_selector_sequence
+ """
+ p[0] = p[1].descendant(p[3])
+
+
+def p_selector_child(p):
+ """
+ selector : selector GREATER simple_selector_sequence
+ """
+ p[0] = p[1].child(p[3])
+
+
+def p_selector_followed_by(p):
+ """
+ selector : selector PLUS simple_selector_sequence
+ """
+ p[0] = p[1].followed_by(p[3])
+
+
def p_simple_selector_sequence1(p):
"""
simple_selector_sequence : simple_selector
@@ -120,7 +141,6 @@ def p_element_name(p):
assert parser.parse("h1[title]") == ElementSelector("h1").attr("title")
assert parser.parse("span[hello='Cleveland'][goodbye='Columbus']") == ElementSelector("span").attr("hello", "Cleveland").attr("goodbye", "Columbus")
-
assert parser.parse("a[rel='copyright']") == ElementSelector("a").attr("rel", "copyright")
assert parser.parse("a[rel~='copyright']") == ElementSelector("a").attr("rel", "copyright", "~=")
assert parser.parse("a[hreflang='en']") == ElementSelector("a").attr("hreflang", "en", "=")
@@ -131,3 +151,9 @@ def p_element_name(p):
assert parser.parse("a[href='.html']") == ElementSelector("a").attr("href", ".html")
assert parser.parse("p[title*='hello']") == ElementSelector("p").attr("title", "hello", "*=")
assert parser.parse("p[title='hello']") == ElementSelector("p").attr("title", "hello")
+
+assert parser.parse("h1 em") == ElementSelector("h1").descendant(ElementSelector("em"))
+assert parser.parse("span > em") == ElementSelector("span").child(ElementSelector("em"))
+assert parser.parse("div * p") == ElementSelector("div").descendant(ElementSelector()).descendant(ElementSelector("p"))
+assert parser.parse("div p *[href]") == ElementSelector("div").descendant(ElementSelector("p")).descendant(ElementSelector().attr("href"))
+assert parser.parse("math + p") == ElementSelector("math").followed_by(ElementSelector("p"))

0 comments on commit b12a1ab

Please sign in to comment.