Skip to content

Commit 3d49192

Browse files
committed
Does not split a selector inside of quotes. fix #30
1 parent c91f8f8 commit 3d49192

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,7 @@ class Rule extends LessObject implements Formattable, FormattableContainer {
7070
Rule( LessObject obj, FormattableContainer parent, String selectors, @Nullable Operation params, Expression guard ) {
7171
super( obj );
7272
this.parent = parent;
73-
this.selectors = selectors.split( "," );
74-
for( int i = 0; i < this.selectors.length; i++ ) {
75-
this.selectors[i] = this.selectors[i].trim();
76-
}
73+
this.selectors = SelectorUtils.split( selectors );
7774
if( params == null ) {
7875
this.params = null;
7976
} else {

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727
package com.inet.lib.less;
2828

29+
import java.util.ArrayList;
2930
import java.util.List;
3031

3132
import javax.annotation.Nonnull;
@@ -217,4 +218,40 @@ static String replacePlaceHolder( CssFormatter formatter, String str, LessObject
217218
}
218219
return str;
219220
}
221+
222+
/**
223+
* Split a selectors in single selectors. This is like selectors.split("'") but ignored quoted parts.
224+
*
225+
* @param selectors
226+
* the selectors
227+
* @return the splitted selectors
228+
*/
229+
static String[] split( String selectors ) {
230+
ArrayList<String> result = null;
231+
int length = selectors.length();
232+
char quote = 0;
233+
int off = 0;
234+
for( int i = 0; i < length; i++ ) {
235+
char ch = selectors.charAt( i );
236+
switch( ch ) {
237+
case ',':
238+
if( result == null ) {
239+
result = new ArrayList<>();
240+
}
241+
result.add( selectors.substring( off, i ).trim() );
242+
off = i + 1;
243+
break;
244+
case '\'':
245+
case '\"':
246+
do {
247+
i++;
248+
} while( i < length && selectors.charAt( i ) != ch );
249+
}
250+
}
251+
if( result == null ) {
252+
return new String[] { selectors.trim() };
253+
}
254+
result.add( selectors.substring( off, length ).trim() );
255+
return result.toArray( new String[result.size()] );
256+
}
220257
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
div[comment="/*foo*/"] {
22
background: #ffff00;
33
}
4+
div[data-test='foo,bar'] {
5+
cursor: default;
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
div[comment="/*foo*/"] {
22
background: #ffff00;
33
}
4+
div[data-test='foo,bar'] {
5+
cursor: default;
6+
}

0 commit comments

Comments
 (0)