Skip to content

Commit b52bdf3

Browse files
committed
improve "if" function. fix #53
1 parent 99db32b commit b52bdf3

File tree

5 files changed

+67
-23
lines changed

5 files changed

+67
-23
lines changed

src/com/inet/lib/less/LessParser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,10 @@ private Expression concat( Expression left, char operator, Expression right ) {
11321132
Operation op;
11331133
if( left.getClass() == Operation.class && ((Operation)left).getOperator() == operator ) {
11341134
op = (Operation)left;
1135+
} else if( right != null && right.getClass() == Operation.class && ((Operation)right).getOperator() == operator ) {
1136+
op = (Operation)right;
1137+
op.addLeftOperand( left );
1138+
return op;
11351139
} else {
11361140
op = new Operation( reader, left, operator );
11371141
}

src/com/inet/lib/less/Operation.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* MIT License (MIT)
33
*
4-
* Copyright (c) 2014 - 2016 Volker Berlin
4+
* Copyright (c) 2014 - 2019 Volker Berlin
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -433,6 +433,23 @@ public boolean booleanValue(CssFormatter formatter) {
433433
return value;
434434
case '!':
435435
return !leftOp.booleanValue( formatter );
436+
case ' ':
437+
if( leftOp instanceof ValueExpression && "not".equals( leftOp.toString() ) ) {
438+
return !operands.get( 1 ).booleanValue( formatter );
439+
}
440+
if( operands.size() == 3 ) {
441+
Expression op = operands.get( 1 );
442+
if( op instanceof ValueExpression ) {
443+
Expression rightOp = operands.get( 2 );
444+
switch( op.toString() ) {
445+
case "and":
446+
return leftOp.booleanValue( formatter ) & rightOp.booleanValue( formatter );
447+
case "or":
448+
return leftOp.booleanValue( formatter ) | rightOp.booleanValue( formatter );
449+
}
450+
}
451+
}
452+
break;
436453
case '>':
437454
case '<':
438455
case '=':

src/com/inet/lib/less/ValueExpression.java

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -260,32 +260,41 @@ private void eval( CssFormatter formatter ) {
260260
str = str.toLowerCase();
261261
rgb = getRgbFromColorConst( str );
262262
if( rgb == -1 ) {
263-
if( str.equals( "transparent" ) ) {
264-
value = 0;
265-
type = RGBA;
266-
return;
267-
} else {
268-
ParsePosition pos = new ParsePosition( 0 );
269-
Number number = formatter.getFormat().parse( str, pos );
270-
if( number == null ) {
271-
if( str.startsWith( "+" ) ) { // DecimalFormat can not parse an option plus sign
272-
pos.setIndex( 1 );
273-
number = formatter.getFormat().parse( str, pos );
274-
if( number == null ) {
263+
switch( str ) {
264+
case "transparent":
265+
value = 0;
266+
type = RGBA;
267+
return;
268+
case "true":
269+
type = BOOLEAN;
270+
value = -1;
271+
return;
272+
case "false":
273+
type = BOOLEAN;
274+
value = 0;
275+
return;
276+
default:
277+
ParsePosition pos = new ParsePosition( 0 );
278+
Number number = formatter.getFormat().parse( str, pos );
279+
if( number == null ) {
280+
if( str.startsWith( "+" ) ) { // DecimalFormat can not parse an option plus sign
281+
pos.setIndex( 1 );
282+
number = formatter.getFormat().parse( str, pos );
283+
if( number == null ) {
284+
type = STRING;
285+
return;
286+
}
287+
} else {
275288
type = STRING;
276289
return;
277290
}
278-
} else {
279-
type = STRING;
280-
return;
281291
}
282-
}
283-
value = number.doubleValue();
284-
if( pos.getIndex() != str.length() ) {
285-
unit = str.substring( pos.getIndex() );
286-
}
287-
type = NUMBER;
288-
return;
292+
value = number.doubleValue();
293+
if( pos.getIndex() != str.length() ) {
294+
unit = str.substring( pos.getIndex() );
295+
}
296+
type = NUMBER;
297+
return;
289298
}
290299
}
291300
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.if {
2+
a: bar;
3+
b: foo;
4+
c: foo;
5+
d: blue;
6+
e: blue;
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.if {
2+
a: if(not (true), foo, bar);
3+
b: if((true) and (2 > 1), foo, bar);
4+
c: if((false) or (isstring("boo!")), foo, bar);
5+
d: if(2 > 1, blue, green);
6+
e: if((2 > 1), blue, green);
7+
}

0 commit comments

Comments
 (0)