Skip to content

Commit

Permalink
add function range #47
Browse files Browse the repository at this point in the history
  • Loading branch information
Horcrux7 committed Feb 3, 2019
1 parent 2f303fe commit f180eac
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/com/inet/lib/less/FunctionExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ public void appendTo( CssFormatter formatter ) {
return;
}
break;
case "range":
range( formatter ).appendTo( formatter );
return;
}
if( type == UNKNOWN ) {
eval( formatter );
Expand Down Expand Up @@ -451,6 +454,9 @@ private void eval( CssFormatter formatter ) {
case "extract":
extract( formatter );
return;
case "range":
type = LIST;
return;
case "alpha":
type = NUMBER;
switch( get( 0 ).getDataType( formatter ) ) {
Expand Down Expand Up @@ -1028,9 +1034,11 @@ private double getDouble( int idx, double defaultValue, CssFormatter formatter )
*/
@Override
public Operation listValue( CssFormatter formatter ) {
switch( super.toString().toLowerCase() ) {
switch( super.toString() ) {
case "extract":
return extract( formatter ).listValue( formatter );
case "range":
return range( formatter );
}
return super.listValue( formatter );
}
Expand Down Expand Up @@ -1084,4 +1092,36 @@ private Expression extract( CssFormatter formatter ) {
return ex;

}

/**
* Function range. Generate a list spanning a range of values
*
* @param formatter
* current CSS output
* @return The operation representing a list of values
*/
private Operation range( CssFormatter formatter ) {
type = LIST;
double start = get( 0 ).doubleValue( formatter );
double end;
if( parameters.size() >= 2 ) {
end = get( 1 ).doubleValue( formatter );
} else {
end = start;
start = 1;
}
double step;
if( parameters.size() >= 3 ) {
step = get( 2 ).doubleValue( formatter );
} else {
step = 1;
}
String unit = get( 0 ).unit( formatter );
Operation op = new Operation( this, ' ' );
while( start <= end ) {
op.addOperand( new ValueExpression( this, start + unit ) );
start += step;
}
return op;
}
}
16 changes: 16 additions & 0 deletions test/com/inet/lib/less/samples/less_org_doc/list/range.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
simple {
value: 1 2 3 4;
length: 4;
}
full {
value: 10px 20px 30px;
length: 3;
}
empty {
value: ;
length: 0;
}
floating {
value: 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3 3.2;
length: 10;
}
20 changes: 20 additions & 0 deletions test/com/inet/lib/less/samples/less_org_doc/list/range.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
simple {
@list: range(4);
value: @list;
length: length( @list );
}
full {
@list: range(10px, 30px, 10);
value: @list;
length: length( @list );
}
empty {
@list: range(10, 5);
value: @list;
length: length( @list );
}
floating {
@list: range(1.4, 3.4, 0.2);
value: @list;
length: length( @list );
}

0 comments on commit f180eac

Please sign in to comment.