Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
#-*- coding:utf-8 -*-
#
# Copyright (C) 2008 - Olivier Lauzanne
#
# Distributed under the BSD license, see LICENSE.txt
from cssselect import xpath as cssselect_xpath
class PDFQueryTranslator(cssselect_xpath.GenericTranslator):
def xpath_in_bbox_function(self, xpath, fn):
if len(fn.arguments) > 1:
x0,y0,x1,y1 = [float(t.value) for t in fn.arguments]
else:
x0,y0,x1,y1 = map(float, fn.arguments[0].value.split(","))
# TODO: seems to be doing < rather than <= ???
xpath.add_condition("@x0 >= %s" % x0)
xpath.add_condition("@y0 >= %s" % y0)
xpath.add_condition("@x1 <= %s" % x1)
xpath.add_condition("@y1 <= %s" % y1)
return xpath
def xpath_overlaps_bbox_function(self, xpath, fn):
if len(fn.arguments) > 1:
x0,y0,x1,y1 = [float(t.value) for t in fn.arguments]
# Copyright (C) 2008 - Olivier Lauzanne
#
# Distributed under the BSD license, see LICENSE.txt
from __future__ import unicode_literals
from cssselect import xpath as cssselect_xpath
from cssselect.xpath import ExpressionError
XPathExprOrig = cssselect_xpath.XPathExpr
class XPathExpr(XPathExprOrig):
def __init__(self, path='', element='*', condition='', star_prefix=False):
self.path = path
self.element = element
self.condition = condition
self.post_condition = None
def add_post_condition(self, post_condition):
if self.post_condition:
self.post_condition = '%s and (%s)' % (self.post_condition,
post_condition)
else:
self.post_condition = post_condition
def __str__(self):
path = XPathExprOrig.__str__(self)
if self.post_condition:
path = '%s[%s]' % (path, self.post_condition)
return path
def join(self, combiner, other):
res = XPathExprOrig.join(self, combiner, other)
self.post_condition = other.post_condition
return res
# keep cssselect < 0.8 compat for now
class JQueryTranslator(cssselect_xpath.HTMLTranslator):
"""This class is used to implement the css pseudo classes
(:first, :last, ...) that are not defined in the css standard,
but are defined in the jquery API.
"""
xpathexpr_cls = XPathExpr
def xpath_first_pseudo(self, xpath):
"""Matches the first selected element::
>>> from pyquery import PyQuery
>>> d = PyQuery('<div><p class="first"></p><p></p></div>')
>>> d('p:first')
[]
..