Skip to content

Commit 3a18f48

Browse files
committed
Implements the import keywords "once" and "multiple". #28
1 parent c179d9b commit 3a18f48

File tree

4 files changed

+52
-10
lines changed

4 files changed

+52
-10
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class LessLookAheadReader extends LessObject implements Closeable {
4040

4141
private final StringBuilder cache = new StringBuilder();
4242

43-
private final boolean isReference;
43+
private final boolean isReference, isMultiple;
4444

4545
private int cachePos;
4646

@@ -49,11 +49,13 @@ class LessLookAheadReader extends LessObject implements Closeable {
4949
* @param reader the underlying reader
5050
* @param fileName the filename of the less file or null if a String is parsed.
5151
* @param isReference true, if the less file is imported as reference
52+
* @param isMultiple true, if the less file is imported with keyword "multiple"
5253
*/
53-
LessLookAheadReader( Reader reader, String fileName, boolean isReference ) {
54+
LessLookAheadReader( Reader reader, String fileName, boolean isReference, boolean isMultiple ) {
5455
super( fileName );
5556
this.reader = reader.markSupported() ? reader : new BufferedReader( reader );
5657
this.isReference = isReference;
58+
this.isMultiple = isMultiple;
5759
line = 1;
5860
column = 0;
5961
}
@@ -328,4 +330,13 @@ public void close() throws IOException {
328330
boolean isReference() {
329331
return isReference;
330332
}
333+
334+
335+
/**
336+
* If the less file of this reader was import with "multiple" keyword.
337+
* @return true, if multiple
338+
*/
339+
boolean isMultiple() {
340+
return isMultiple;
341+
}
331342
}

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.ArrayDeque;
3333
import java.util.ArrayList;
3434
import java.util.HashMap;
35+
import java.util.HashSet;
3536
import java.util.List;
3637
import java.util.StringTokenizer;
3738

@@ -65,6 +66,8 @@ class LessParser implements FormattableContainer {
6566

6667
private ArrayDeque<Rule> ruleStack = new ArrayDeque<>();
6768

69+
private HashSet<URL> imports = new HashSet<>();
70+
6871
private List<LazyImport> lazyImports;
6972

7073
/**
@@ -101,7 +104,7 @@ void parse( URL baseURL, Reader input, ReaderFactory readerFactory ) throws Mal
101104
this.baseURL = baseURL;
102105
this.readerFactory = readerFactory;
103106
this.relativeURL = new URL( "file", null, "" );
104-
this.reader = new LessLookAheadReader( input, null, false );
107+
this.reader = new LessLookAheadReader( input, null, false, false );
105108
parse( this );
106109
}
107110

@@ -430,6 +433,7 @@ private void importFile( FormattableContainer currentRule, final String name ) {
430433
boolean isReference = reader.isReference();
431434
boolean isCss = false;
432435
boolean isLess = false;
436+
boolean isMultiple = reader.isMultiple();
433437
if( filename.startsWith( "(" ) ) {
434438
int endIdx = filename.indexOf( ')', 1);
435439
if( endIdx > 0 ) {
@@ -439,11 +443,15 @@ private void importFile( FormattableContainer currentRule, final String name ) {
439443
String keywordStr = tokenizer.nextToken().trim();
440444
switch( keywordStr ) {
441445
case "inline":
442-
case "once":
443-
case "multiple":
444446
case "optional":
445447
System.err.println( "not implemented @import keyword: " + keywordStr ); //TODO
446448
break;
449+
case "once":
450+
isMultiple = false;
451+
break;
452+
case "multiple":
453+
isMultiple = true;
454+
break;
447455
case "less":
448456
isLess = true;
449457
isCss = false;
@@ -542,12 +550,14 @@ private void importFile( FormattableContainer currentRule, final String name ) {
542550
baseURL = baseURL == null ? new URL( filename ) : new URL( baseURL, filename );
543551
}
544552
relativeURL = new URL( relativeURL, filename );
545-
if( isReference != reader.isReference() ) {
546-
add( new ReferenceInfo( isReference ) );
553+
if( imports.add( baseURL ) || isMultiple ) {
554+
if( isReference != reader.isReference() ) {
555+
add( new ReferenceInfo( isReference ) );
556+
}
557+
reader = new LessLookAheadReader( readerFactory.create( baseURL ), filename, isReference, isMultiple );
558+
parse( currentRule );
559+
reader.close();
547560
}
548-
reader = new LessLookAheadReader( readerFactory.create( baseURL ), filename, isReference );
549-
parse( currentRule );
550-
reader.close();
551561
} catch( LessException ex ) {
552562
throw ex;
553563
} catch( Exception ex ) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#import {
2+
color: red;
3+
}
4+
body {
5+
width: 100%;
6+
}
7+
.test-f {
8+
height: 10px;
9+
}
10+
body {
11+
width: 100%;
12+
}
13+
.test-f {
14+
height: 10px;
15+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@import "import/import-once-test-c";
2+
@import "import/import-once-test-c";
3+
@import "import/import-once-test-c.less";
4+
@import "import/deeper/import-once-test-a";
5+
@import (multiple) "import/import-test-f.less";
6+
@import (multiple) "import/import-test-f.less";

0 commit comments

Comments
 (0)