#!/usr/bin/python # # selectorp.g by Mark Paschal # Parse a CSS3-ish selector. # __author__ = "Mark Paschal " __copyright__ = "Copyright 2003 Mark Paschal" __version__ = "1.0" __license__ = """MIT Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ class Attr(object): def __init__(self, attr, comp=None, value=None): self.attr = attr self.comp = comp self.value = value def matches(self, attrs): if not attrs.has_key(self.attr): return 0 if None == self.comp: return 1 # just the above has_key check if "~=" == self.comp: return self.value in attrs[self.attr].split(" ") if "|=" == self.comp: return self.value in attrs[self.attr].split("-") if "^=" == self.comp: return attrs[self.attr].startswith(self.value) if "$=" == self.comp: return attrs[self.attr].endswith(self.value) if "*=" == self.comp: return -1 <> attrs[self.attr].find(self.value) return self.value == attrs[self.attr] class Selector(object): def __init__(self): self.attrs = [] self.tag = None def addAttr(self, attr, comparator, value): self.attrs.append(Attr(attr, comparator, value)) def matches(self, tag): # This tag, right? if self.tag and tag.tag <> self.tag: return 0 # Have all the attributes? for a in self.attrs: if not a.matches(tag): return 0 return 1 %% parser SelectorParser: ignore: "\s+" token HASH: "#\w+" token IDENT: "\w+" token COMPAR: "[~|*^$]?=" token STRING: "([\"']).*?\\1" rule id: "#" IDENT {{ return IDENT }} rule klass: "\." IDENT {{ return IDENT }} rule pseudo: ":" IDENT {{ return IDENT }} rule attrib: "\[" IDENT {{ attr = IDENT }} {{ COMPAR = attrval = None }} [ COMPAR ( IDENT {{ attrval = IDENT }} | STRING {{ attrval = STRING[1:-1] }} ) ] "\]" {{ return (attr, COMPAR, attrval) }} rule element_name: IDENT {{ return IDENT }} | "\*" {{ return None }} rule subselector<>:( id {{ sel.addAttr("id", "=", id) }} | klass {{ sel.addAttr("class", "|=", klass) }} | attrib {{ apply(sel.addAttr, attrib) }} | pseudo {{ # no pseudos, sorry! }} )+ rule simple_selector: {{ sel = Selector() }} element_name {{ sel.tag = element_name }} [subselector<>] {{ return sel }} rule combinator: ( "\+" {{ return '+' }} | ">" {{ return '>' }} ) rule selector: simple_selector {{ sels = [simple_selector] }} ( [ combinator {{ # haha, no combinators either }} ] simple_selector {{ sels.append(simple_selector) }} )* "$" {{ return sels }}