Skip to content

Commit 6d0a526

Browse files
committed
implements parsing of !important for CSS properties. #16
1 parent 90fac9b commit 6d0a526

File tree

7 files changed

+66
-4
lines changed

7 files changed

+66
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ void appendProperty( @Nonnull String name, @Nonnull Expression value ) {
732732
} else {
733733
value.appendTo( this );
734734
}
735-
if( important ) {
735+
if( important || value.isImportant() ) {
736736
output.append( " !important" );
737737
}
738738
semicolon();

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ abstract class Expression extends LessObject implements Formattable {
5858

5959
private String str;
6060

61+
private boolean important;
62+
6163
/**
6264
* Create a new instance.
6365
*
@@ -156,7 +158,7 @@ public void appendTo( CssFormatter formatter ) {
156158
* the CCS target
157159
* @return the value
158160
*/
159-
public String stringValue( CssFormatter formatter ) {
161+
String stringValue( CssFormatter formatter ) {
160162
try {
161163
formatter.addOutput();
162164
appendTo( formatter );
@@ -166,6 +168,21 @@ public String stringValue( CssFormatter formatter ) {
166168
}
167169
}
168170

171+
/**
172+
* If this expression is mark as important
173+
* @return true, if important
174+
*/
175+
boolean isImportant() {
176+
return important;
177+
}
178+
179+
/**
180+
* Enable the important flag.
181+
*/
182+
void setImportant() {
183+
important = true;
184+
}
185+
169186
/**
170187
* Get the value as a list
171188
*

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,31 @@ private Expression parseExpression( char leftOperator ) {
878878
case '`':
879879
left = concat( left, ' ', new JavaScriptExpression( reader, readQuote( ch ) ) );
880880
break;
881+
case '!':
882+
if( builder.length() > 0 ) {
883+
left = concat( left, ' ', buildExpression( trim( builder ) ) );
884+
}
885+
if( left != null ) {
886+
for( ;; ) {
887+
ch = read();
888+
switch( ch ) {
889+
case ';':
890+
case ')':
891+
case '}':
892+
str = trim( builder );
893+
if( str.equals( "important" ) ) {
894+
left.setImportant();
895+
back( ch );
896+
} else {
897+
left = concat( left, ' ', buildExpression( '!' + str ) );
898+
}
899+
return left;
900+
default:
901+
builder.append( ch );
902+
}
903+
}
904+
}
905+
//$FALL-THROUGH$
881906
default:
882907
boolean isWhite = Character.isWhitespace( ch );
883908
if( isWhite ) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,14 @@ public Operation listValue( CssFormatter formatter ) {
599599
return super.listValue( formatter );
600600
}
601601

602+
/**
603+
* {@inheritDoc}
604+
*/
605+
@Override
606+
boolean isImportant() {
607+
return super.isImportant() || operands.get( operands.size() -1 ) .isImportant();
608+
}
609+
602610
/**
603611
* {@inheritDoc}
604612
*/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#important {
2+
color1: #FFFFFF !important;
3+
color2: #FFFFFF !important;
4+
color3: #777777 !important;
5+
color4: #343434 !important;
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#important {
2+
color1: #FFFFFF!important;
3+
color2: #FFFFFF !important;
4+
color3: #777777 ! important;
5+
color4: #333 + 1 !important;
6+
}

test/com/inet/lib/less/samples/less_org_tests/less/css.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ p + h1 {
7979
}
8080
#important {
8181
color: red !important;
82-
width: 100%!important;
83-
height: 20px ! important;
82+
width: 100% !important;
83+
height: 20px !important;
8484
}
8585
@font-face {
8686
font-family: font-a;

0 commit comments

Comments
 (0)