Skip to content

Commit

Permalink
Does not split a selector inside of quotes. fix #30
Browse files Browse the repository at this point in the history
  • Loading branch information
Horcrux7 committed Nov 19, 2016
1 parent c91f8f8 commit 3d49192
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/com/inet/lib/less/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ class Rule extends LessObject implements Formattable, FormattableContainer {
Rule( LessObject obj, FormattableContainer parent, String selectors, @Nullable Operation params, Expression guard ) {
super( obj );
this.parent = parent;
this.selectors = selectors.split( "," );
for( int i = 0; i < this.selectors.length; i++ ) {
this.selectors[i] = this.selectors[i].trim();
}
this.selectors = SelectorUtils.split( selectors );
if( params == null ) {
this.params = null;
} else {
Expand Down
37 changes: 37 additions & 0 deletions src/com/inet/lib/less/SelectorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/
package com.inet.lib.less;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -217,4 +218,40 @@ static String replacePlaceHolder( CssFormatter formatter, String str, LessObject
}
return str;
}

/**
* Split a selectors in single selectors. This is like selectors.split("'") but ignored quoted parts.
*
* @param selectors
* the selectors
* @return the splitted selectors
*/
static String[] split( String selectors ) {
ArrayList<String> result = null;
int length = selectors.length();
char quote = 0;
int off = 0;
for( int i = 0; i < length; i++ ) {
char ch = selectors.charAt( i );
switch( ch ) {
case ',':
if( result == null ) {
result = new ArrayList<>();
}
result.add( selectors.substring( off, i ).trim() );
off = i + 1;
break;
case '\'':
case '\"':
do {
i++;
} while( i < length && selectors.charAt( i ) != ch );
}
}
if( result == null ) {
return new String[] { selectors.trim() };
}
result.add( selectors.substring( off, length ).trim() );
return result.toArray( new String[result.size()] );
}
}
3 changes: 3 additions & 0 deletions test/com/inet/lib/less/samples/general/selectorWithQuotes.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
div[comment="/*foo*/"] {
background: #ffff00;
}
div[data-test='foo,bar'] {
cursor: default;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
div[comment="/*foo*/"] {
background: #ffff00;
}
div[data-test='foo,bar'] {
cursor: default;
}

0 comments on commit 3d49192

Please sign in to comment.