@@ -56,7 +56,7 @@ def __repr__(self):
56
56
57
57
def add_condition (self , condition ):
58
58
if self .condition :
59
- self .condition = '%s and (%s)' % (self .condition , condition )
59
+ self .condition = '(%s) and (%s)' % (self .condition , condition )
60
60
else :
61
61
self .condition = condition
62
62
return self
@@ -457,19 +457,19 @@ def xpath_nth_child_function(self, xpath, function, last=False,
457
457
if a == 0 :
458
458
return xpath .add_condition ('%s = %s' % (siblings_count , b_min_1 ))
459
459
460
- expr = []
460
+ expressions = []
461
461
462
462
if a > 0 :
463
463
# siblings count, an+b-1, is always >= 0,
464
464
# so if a>0, and (b-1)<=0, an "n" exists to satisfy this,
465
465
# therefore, the predicate is only interesting if (b-1)>0
466
466
if b_min_1 > 0 :
467
- expr .append ('%s >= %s' % (siblings_count , b_min_1 ))
467
+ expressions .append ('%s >= %s' % (siblings_count , b_min_1 ))
468
468
else :
469
469
# if a<0, and (b-1)<0, no "n" satisfies this,
470
470
# this is tested above as an early exist condition
471
471
# otherwise,
472
- expr .append ('%s <= %s' % (siblings_count , b_min_1 ))
472
+ expressions .append ('%s <= %s' % (siblings_count , b_min_1 ))
473
473
474
474
# operations modulo 1 or -1 are simpler, one only needs to verify:
475
475
#
@@ -495,9 +495,14 @@ def xpath_nth_child_function(self, xpath, function, last=False,
495
495
b_neg = '+%s' % b_neg
496
496
left = '(%s %s)' % (left , b_neg )
497
497
498
- expr .append ('%s mod %s = 0' % (left , a ))
498
+ expressions .append ('%s mod %s = 0' % (left , a ))
499
499
500
- xpath .add_condition (' and ' .join (expr ))
500
+ if len (expressions ) > 1 :
501
+ template = '(%s)'
502
+ else :
503
+ template = '%s'
504
+ xpath .add_condition (' and ' .join (template % expression
505
+ for expression in expressions ))
501
506
return xpath
502
507
503
508
def xpath_nth_last_child_function (self , xpath , function ):
0 commit comments