[Lxml-checkins] r44684 - lxml/branch/html/src/lxml/html

ianb at codespeak.net ianb at codespeak.net
Tue Jul 3 02:10:56 CEST 2007


Author: ianb
Date: Tue Jul  3 02:10:56 2007
New Revision: 44684

Modified:
   lxml/branch/html/src/lxml/html/css.py
Log:
Add some fast translation for id, class, and plain element name matches

Modified: lxml/branch/html/src/lxml/html/css.py
==============================================================================
--- lxml/branch/html/src/lxml/html/css.py	(original)
+++ lxml/branch/html/src/lxml/html/css.py	Tue Jul  3 02:10:56 2007
@@ -474,8 +474,23 @@
 ##############################
 ## XPathExpr objects:
 
+_el_re = re.compile(r'^\w+\s*$')
+_id_re = re.compile(r'^(\w*)#(\w+)\s*$')
+_class_re = re.compile(r'^(\w*)\.(\w+)\s*$')
+
 def css_to_xpath(css_expr, prefix='descendant-or-self::'):
     if isinstance(css_expr, basestring):
+        match = _el_re.search(css_expr)
+        if match is not None:
+            return '%s%s' % (prefix, match.group(0).strip())
+        match = _id_re.search(css_expr)
+        if match is not None:
+            return "%s%s[@id = '%s']" % (
+                prefix, match.group(1) or '*', match.group(2))
+        match = _class_re.search(css_expr)
+        if match is not None:
+            return "%s%s[contains(concat(' ', normalize-space(@class), ' '), ' %s ')]" % (
+                prefix, match.group(1) or '*', match.group(2))
         css_expr = parse(css_expr)
     expr = css_expr.xpath()
     assert expr is not None, (


More information about the lxml-checkins mailing list