[ADD] hasclass() xpath function
Server-side, view extension is done via xpath. This includes "template" views full of HTML. HTML elements often have a bunch of classes, sometimes even semantic (!). XPath is generally great, but specifically lousy at dealing with space-separated values: in standard XPath 1.0 to know if an element has a class 'foo' the predicate is: contains(concat(' ', normalize-space(@class), ' '), ' foo ') and this has to be fully duplicated if there's a second class involved. Things are slightly better with EXSLT/XPath 2.0 and tokenize, but still not great: tokenize(@class, '\s+') = 'foo' and the equality check is very weird when unaware of XPath's evaluation rules. ``hasclass`` makes this much simpler to deal with: to get any ``foo`` node with the class ``bar`` is as simple as: //foo[hasclass('bar') and it can take multiple class, as with e.g. jquery it will return elements with all specified classes. Beware though, the predicate function will be called once for each element to check, since it's implemented in pure python and not profiled elements should be filtered as much as possible before this point.
Loading
Please register or sign in to comment