Skip to content

Commit d7b4139

Browse files
committed
Add @import keyword "inline" and media filter.
1 parent 3a18f48 commit d7b4139

File tree

5 files changed

+86
-5
lines changed

5 files changed

+86
-5
lines changed

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.HashMap;
3535
import java.util.HashSet;
3636
import java.util.List;
37+
import java.util.Scanner;
3738
import java.util.StringTokenizer;
3839

3940
import javax.annotation.Nonnull;
@@ -434,6 +435,8 @@ private void importFile( FormattableContainer currentRule, final String name ) {
434435
boolean isCss = false;
435436
boolean isLess = false;
436437
boolean isMultiple = reader.isMultiple();
438+
boolean isInline = false;
439+
boolean isOptional = false;
437440
if( filename.startsWith( "(" ) ) {
438441
int endIdx = filename.indexOf( ')', 1);
439442
if( endIdx > 0 ) {
@@ -443,7 +446,10 @@ private void importFile( FormattableContainer currentRule, final String name ) {
443446
String keywordStr = tokenizer.nextToken().trim();
444447
switch( keywordStr ) {
445448
case "inline":
449+
isInline = true;
450+
break;
446451
case "optional":
452+
isOptional = true;
447453
System.err.println( "not implemented @import keyword: " + keywordStr ); //TODO
448454
break;
449455
case "once":
@@ -514,6 +520,9 @@ private void importFile( FormattableContainer currentRule, final String name ) {
514520
if( i < filename.length() - 1 ) {
515521
//additional content after url(...)
516522
media = filename.substring( i + 1 ).trim();
523+
Rule rule = new Rule( reader, currentRule, "@media " + media, null, null );
524+
currentRule.add( rule );
525+
currentRule = rule;
517526
}
518527
filename = trim( builder );
519528

@@ -533,13 +542,13 @@ private void importFile( FormattableContainer currentRule, final String name ) {
533542
lazyImports.add( lazy );
534543
return;
535544
}
536-
if( !isLess && (isCss || filename.endsWith( "css" )) ) {
545+
if( !isLess && !isInline && (isCss || filename.endsWith( "css" )) ) {
537546
// filenames ends with "css" will not be inline else a CSS @import directive is written
538547
currentRule.add( new CssAtRule( reader, "@import " + name + ';') );
539548
return;
540549
}
541550
baseURL = baseURL == null ? new URL( filename ) : new URL( baseURL, filename );
542-
if( !isLess && baseURL.getPath().endsWith( "css" ) ) {
551+
if( !isLess && !isInline && baseURL.getPath().endsWith( "css" ) ) {
543552
// URL path ends with "css" will not be inline else a CSS @import directive is written
544553
currentRule.add( new CssAtRule( reader, "@import " + name + ';') );
545554
return;
@@ -554,9 +563,17 @@ private void importFile( FormattableContainer currentRule, final String name ) {
554563
if( isReference != reader.isReference() ) {
555564
add( new ReferenceInfo( isReference ) );
556565
}
557-
reader = new LessLookAheadReader( readerFactory.create( baseURL ), filename, isReference, isMultiple );
558-
parse( currentRule );
559-
reader.close();
566+
Reader importReader = readerFactory.create( baseURL );
567+
if( isInline ) {
568+
Scanner scanner = new Scanner(importReader).useDelimiter( "\\A" );
569+
if( scanner.hasNext() ) {
570+
currentRule.add( new CssAtRule( reader, scanner.next() ) );
571+
}
572+
} else {
573+
reader = new LessLookAheadReader( importReader, filename, isReference, isMultiple );
574+
parse( currentRule );
575+
reader.close();
576+
}
560577
}
561578
} catch( LessException ex ) {
562579
throw ex;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@media (min-width:600px) {
2+
#css { color: yellow; }
3+
4+
}
5+
this isn't very valid CSS.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@import (inline) url("import/import-test-d.css") (min-width:600px);
2+
@import (inline, css) url("import/invalid-css.less");
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
@import url(http://fonts.googleapis.com/css?family=Open+Sans);
2+
@import url(/absolute/something.css) screen and (color) and (max-width: 600px);
3+
@import url("//ha.com/file.css") (min-width:100px);
4+
#import-test {
5+
height: 10px;
6+
color: red;
7+
width: 10px;
8+
height: 30%;
9+
}
10+
@media screen and (max-width: 600px) {
11+
body {
12+
width: 100%;
13+
}
14+
}
15+
#import {
16+
color: red;
17+
}
18+
.mixin {
19+
height: 10px;
20+
color: red;
21+
}
22+
@media screen and (max-width: 601px) {
23+
#css {
24+
color: yellow;
25+
}
26+
}
27+
@media screen and (max-width: 602px) {
28+
body {
29+
width: 100%;
30+
}
31+
}
32+
@media screen and (max-width: 603px) {
33+
#css {
34+
color: yellow;
35+
}
36+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@import url(http://fonts.googleapis.com/css?family=Open+Sans);
2+
3+
@import url(/absolute/something.css) screen and (color) and (max-width: 600px);
4+
5+
@var: 100px;
6+
@import url("//ha.com/file.css") (min-width:@var);
7+
8+
#import-test {
9+
.mixin;
10+
width: 10px;
11+
height: (@a + 10%);
12+
}
13+
@import "import/import-test-e" screen and (max-width: 600px);
14+
15+
@import url("import/import-test-a.less");
16+
17+
@import (less, multiple) "import/import-test-d.css" screen and (max-width: 601px);
18+
19+
@import (multiple) "import/import-test-e" screen and (max-width: 602px);
20+
21+
@import (less, multiple) url("import/import-test-d.css") screen and (max-width: 603px);

0 commit comments

Comments
 (0)