imports;
/**
* Constructs a new LessSource.
@@ -63,7 +63,26 @@ public class LessSource {
* @throws IOException If the LESS source cannot be read.
*/
public LessSource(Resource resource) throws IOException {
- this(resource, Charset.defaultCharset());
+ this(resource, Charset.defaultCharset(), null);
+ }
+
+ /**
+ * Constructs a new LessSource.
+ *
+ * This will read the metadata and content of the LESS source, and will automatically resolve the imports.
+ *
+ *
+ * The resource is read using the default Charset of the platform
+ *
+ *
+ * @param resource The File reference to the LESS source to read.
+ * @param imports Lists of imported file, useful for de-duplicate imported with multi level import. if null,
+ * will create empty map
+ * @throws FileNotFoundException If the LESS source (or one of its imports) could not be found.
+ * @throws IOException If the LESS source cannot be read.
+ */
+ public LessSource(Resource resource, Map imports) throws IOException {
+ this(resource, Charset.defaultCharset(), imports);
}
/**
@@ -74,16 +93,24 @@ public LessSource(Resource resource) throws IOException {
*
* @param resource The File reference to the LESS resource to read.
* @param charset charset used to read the less resource.
+ * @param imports Lists of imported file, useful for de-duplicate imported with multi level import. if null,
+ * will create empty map
* @throws FileNotFoundException If the LESS resource (or one of its imports) could not be found.
* @throws IOException If the LESS resource cannot be read.
*/
- public LessSource(Resource resource, Charset charset) throws IOException {
+ public LessSource(Resource resource, Charset charset, Map imports) throws IOException {
if (resource == null) {
throw new IllegalArgumentException("Resource must not be null.");
}
if (!resource.exists()) {
throw new IOException("Resource " + resource + " not found.");
}
+ if (imports == null) {
+ this.imports = new LinkedHashMap();
+ } else {
+ this.imports = imports;
+ }
+
this.resource = resource;
this.content = this.normalizedContent = loadResource(resource, charset);
resolveImports();
@@ -198,8 +225,8 @@ private void resolveImports() throws IOException {
if (importType.equals("less")) {
logger.debug("Importing %s", importedResource);
- if( !imports.containsKey(importedResource) ) {
- LessSource importedLessSource = new LessSource(getImportedResource(importedResource));
+ if (!imports.containsKey(importedResource)) {
+ LessSource importedLessSource = new LessSource(getImportedResource(importedResource), imports);
imports.put(importedResource, importedLessSource);
normalizedContent = includeImportedContent(importedLessSource, importMatcher);
diff --git a/src/test/java/org/lesscss/LessSourceTest.java b/src/test/java/org/lesscss/LessSourceTest.java
index 8d18a4d..bb4bf55 100644
--- a/src/test/java/org/lesscss/LessSourceTest.java
+++ b/src/test/java/org/lesscss/LessSourceTest.java
@@ -143,7 +143,7 @@ public void testWithBadEncodingLessFile() throws Exception {
private String readLessSourceWithEncoding(String encoding) throws IOException, IllegalAccessException {
URL sourceUrl = getClass().getResource("/compatibility/utf8-content.less");
File sourceFile = new File(sourceUrl.getFile());
- LessSource lessSource = new LessSource(new FileResource(sourceFile), Charset.forName(encoding));
+ LessSource lessSource = new LessSource(new FileResource(sourceFile), Charset.forName(encoding), null);
return (String) FieldUtils.readField(lessSource, "content", true);
}
diff --git a/src/test/resources/import/css/import.css b/src/test/resources/import/css/import.css
index 1d74435..bcb1adc 100644
--- a/src/test/resources/import/css/import.css
+++ b/src/test/resources/import/css/import.css
@@ -1,14 +1,14 @@
-import1a {
- color: blue;
-}
import1b {
- color: blue;
+ color: black;
}
import4 {
- color: blue;
+ color: red;
}
@media screen {
import5 {
color: green;
}
}
+body:after {
+ color: blue;
+}
diff --git a/src/test/resources/import/less/import.less b/src/test/resources/import/less/import.less
index 334e6c1..e318b79 100644
--- a/src/test/resources/import/less/import.less
+++ b/src/test/resources/import/less/import.less
@@ -3,4 +3,8 @@
// @import "import3.less";
@import "import4";
-@media screen { @import "import5"; }
\ No newline at end of file
+@media screen { @import "import5"; }
+
+body {
+ .import1a()
+}
\ No newline at end of file
diff --git a/src/test/resources/import/less/import1/import1a.less b/src/test/resources/import/less/import1/import1a.less
index 4bcc928..2146b8f 100644
--- a/src/test/resources/import/less/import1/import1a.less
+++ b/src/test/resources/import/less/import1/import1a.less
@@ -1,3 +1,5 @@
-import1a {
- color: blue;
+.import1a() {
+ &:after {
+ color: blue;
+ }
}
\ No newline at end of file
diff --git a/src/test/resources/import/less/import1/import1b.less b/src/test/resources/import/less/import1/import1b.less
index efc84cd..5dadbe8 100644
--- a/src/test/resources/import/less/import1/import1b.less
+++ b/src/test/resources/import/less/import1/import1b.less
@@ -1,3 +1,3 @@
import1b {
- color: blue;
+ color: black;
}
\ No newline at end of file
diff --git a/src/test/resources/import/less/import4.less b/src/test/resources/import/less/import4.less
index 54c2a9d..6a064db 100644
--- a/src/test/resources/import/less/import4.less
+++ b/src/test/resources/import/less/import4.less
@@ -1,3 +1,4 @@
+@import "import1/import1a.less";
import4 {
- color: blue;
+ color: red;
}
\ No newline at end of file
diff --git a/src/test/resources/import/less/import_quotes.less b/src/test/resources/import/less/import_quotes.less
index abb27c3..196930d 100644
--- a/src/test/resources/import/less/import_quotes.less
+++ b/src/test/resources/import/less/import_quotes.less
@@ -3,4 +3,8 @@
// @import 'import3.less';
@import "import4";
-@media screen { @import 'import5'; }
\ No newline at end of file
+@media screen { @import 'import5'; }
+
+body {
+ .import1a()
+}
\ No newline at end of file