CSSModuleBuilder extends TextModuleBuilder implements  IExtensionInitializer, IExtensionSingleton, IShutdownListener, IConfigListener { @Override public void configLoaded(IConfig conf, long sequence) { final String sourceMethod = "configLoaded"; SignalUtil.lock( configUpdatingRWL.writeLock(), sourceClass, sourceMethod); try { Object obj = conf.getProperty(SIZETHRESHOLD_CONFIGPARAM, null); imageSizeThreshold = TypeUtil.asInt(obj, SIZETHRESHOLD_DEFAULT_VALUE); obj = conf.getProperty(INLINEIMPORTS_CONFIGPARAM, null); inlineImports = TypeUtil.asBoolean(obj, INLINEIMPORTS_DEFAULT_VALUE); obj = conf.getProperty(INCLUDEAMDPATHS_CONFIGPARAM, null); includeAMDPaths = TypeUtil.asBoolean(obj, INCLUDEAMDPATHS_DEFAULT_VALUE); Collection<String> types = new ArrayList<String>(s_inlineableImageTypes); Object oImageTypes = conf.getProperty(IMAGETYPES_CONFIGPARAM, null); if (oImageTypes != IConfig.NOT_FOUND && oImageTypes != null) { if (oImageTypes instanceof String) { String[] aTypes = oImageTypes.toString().split(","); for (String type : aTypes) { types.add(type); } } else { @SuppressWarnings("unchecked") Map<String, String> map = (Map<String, String>)conf.getProperty(IMAGETYPES_CONFIGPARAM, Map.class); inlineableImageTypeMap.putAll(map); } } inlineableImageTypes = types; Collection<Pattern> list = Collections.emptyList(); Object oIncludeList = conf.getProperty(INCLUDELIST_CONFIGPARAM, null); if (oIncludeList != IConfig.NOT_FOUND && oIncludeList != null) { list = new ArrayList<Pattern>(); for (String s : oIncludeList.toString().split(",")) { list.add(toRegexp(s)); } } inlinedImageIncludeList = list; list = Collections.emptyList(); Object oExcludeList = conf.getProperty(EXCLUDELIST_CONFIGPARAM, null); if (oExcludeList != IConfig.NOT_FOUND && oExcludeList != null) { list = new ArrayList<Pattern>(); for (String s : oExcludeList.toString().split(",")) { list.add(toRegexp(s)); } } inlinedImageExcludeList = list; initPostcss(conf); } finally { configUpdatingRWL.writeLock().unlock(); } } CSSModuleBuilder(); protected  CSSModuleBuilder(IAggregator aggregator, ExecutorService es, int scopePoolSize); @Override List<ICacheKeyGenerator> getCacheKeyGenerators(IAggregator aggregator); @Override boolean handles(String mid, IResource resource); @Override void shutdown(IAggregator aggregator); @Override void initialize(IAggregator aggregator,
			IAggregatorExtension extension, IExtensionRegistrar registrar); @Override void configLoaded(IConfig conf, long sequence); String dequote(String in); static final String INLINEIMPORTS_CONFIGPARAM; static final String INCLUDELIST_CONFIGPARAM; static final String EXCLUDELIST_CONFIGPARAM; static final String IMAGETYPES_CONFIGPARAM; static final String SIZETHRESHOLD_CONFIGPARAM; static final String INCLUDEAMDPATHS_CONFIGPARAM; static final String POSTCSSPLUGINS_CONFIGPARAM; static final String SCOPEPOOLSIZE_CONFIGPARAM; static final String POSTCSS_CONFIGPARAM; static final boolean INLINEIMPORTS_DEFAULT_VALUE; static final boolean INCLUDEAMDPATHS_DEFAULT_VALUE; static final int SIZETHRESHOLD_DEFAULT_VALUE; static final String INLINEIMPORTS_REQPARAM_NAME; static final String INLINEIMAGES_REQPARAM_NAME; static final String PREAMBLE; static final String BLANK; static final String UTF8_CHARSET; static final String POSTCSS_RES_NAME; static final String MINIFYER_RES_NAME; static final String POSTCSS; static final String PROCESS; static final String PLUGINS; static final String POSTCSS_INSTANCE; static final String MODULE; static final String EXPORTS; static final String AMD_DEFINE_SHIM_JS; static final String MINIFIER_INITIALIZATION_VAR; static final String MINIFIER_INITIALIZATION_JS; static final Map<String, Boolean> POSTCSS_OPTIONS; static final int INITIALIZER_THREAD_POOL_SIZE; static final int DEFAULT_SCOPE_POOL_SIZE; static final int SCOPE_POOL_TIMEOUT_SECONDS; public int imageSizeThreshold; public boolean inlineImports; public boolean includeAMDPaths; public Collection<Pattern> inlinedImageExcludeList; }
@Test public void testMinify() throws Exception { IConfig config = new ConfigImpl(mockAggregator, tmpdir.toURI(), "{inlineCSSImports: false}"); configRef.set(config); builder.configLoaded(config, seq++); String css, output; URI resuri = testdir.toURI(); css = "\r\n.foo\t { \tcolor:black; \r\nfont-weight : bold;\r\n }\r\n\n\t.bar { font-size:small } \r\n"; output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".foo{color:black;font-weight:bold}.bar{font-size:small}", output); css = "\r\n\r\n.imported {\r\n\tcolor : black;\r\n}"; CopyUtil.copy(css, new FileWriter(new File(testdir, "imported.css"))); css = "\n\r@import \"imported.css\""; output = buildCss(new StringResource(css, resuri)); Assert.assertEquals("@import \"imported.css\"", output); css = "\n\r@import \"name with spaces.css\" ;\r\n@import url( name_with_no_spaces.css );\r\n@import url( \" name with spaces.css\" );\r\n"; output = buildCss(new StringResource(css, resuri)); Assert.assertEquals("@import \"name with spaces.css\";@import url(name_with_no_spaces.css);@import url(\" name with spaces.css\");", output); css = "\n\r@import 'name with spaces.css';\r\n @import url( ' name with spaces.css' )\r\n"; output = buildCss(new StringResource(css, resuri)); Assert.assertEquals("@import 'name with spaces.css';@import url(' name with spaces.css')", output); css = "@import \"funny name with url(...) inside.css\"; .foo { color : black }"; output = buildCss(new StringResource(css, resuri)); Assert.assertEquals("@import \"funny name with url(...) inside.css\";.foo{color:black}", output); css = "@import \"funny 'name' with url(\\\"...\\\") and escaped quotes inside.css\"; .foo { color : black }"; output = buildCss(new StringResource(css, resuri)); Assert.assertEquals("@import \"funny 'name' with url(\\\"...\\\") and escaped quotes inside.css\";.foo{color:black}", output); css = "@import url( 'funny name with \" single double quote.css' ); .foo { color : black }"; output = buildCss(new StringResource(css, resuri)); Assert.assertEquals("@import url('funny name with \" single double quote.css');.foo{color:black}", output); css = "@import 'funny name \"with double\" quote.css'; .foo { color : black }"; output = buildCss(new StringResource(css, resuri)); Assert.assertEquals("@import 'funny name \"with double\" quote.css';.foo{color:black}", output); css = ".foo:after{content:\"\\a\";white-space:pre}"; output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(css, output); }
@Test public void testImport() throws Exception { String css, output; URI resuri = testdir.toURI(); css = "\r\n\r\n.imported {\r\n\tcolor : black;\r\n}"; CopyUtil.copy(css, new FileWriter(new File(testdir, "imported.css"))); configScript.put(CSSModuleBuilder.INLINEIMPORTS_REQPARAM_NAME, configScript, Boolean.TRUE); IConfig config = new ConfigImpl(mockAggregator, tmpdir.toURI(), configScript); builder.configLoaded(config, seq++); css = "\n\r@import \"imported.css\""; output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".imported{color:black}", output); css = "\n\r@import \"./imported.css\""; output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".imported{color:black}", output); css = "\n\r@import url(\"././imported.css\")"; output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".imported{color:black}", output); css = "\n\r@import url('foo/../imported.css')"; output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".imported{color:black}", output); css = "\n\r@import url( \"./foo/bar/.././../imported.css\" )"; output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".imported{color:black}", output); css = "\n\r@import \"foo/bar/../../imported.css\""; output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".imported{color:black}", output); css = "\n\r@import (less) \"imported.css\""; output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".imported{color:black}", output); css = "\n\r@import \"foo/imported.css\""; boolean exceptionCaught = false; try { output = buildCss(new StringResource(css, resuri)); } catch (NotFoundException e) { exceptionCaught = true; } Assert.assertTrue("Expected FileNotFoundException", exceptionCaught); css = "\r\n\r\n.background-image: url( \"images/img.jpg\" );"; CopyUtil.copy(css, new FileWriter(new File(testdir, "imported.css"))); output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".background-image:url(\"images/img.jpg\");", output); File subdir = new File(testdir, "subdir"); File imported = new File(subdir, "imported.css"); subdir.mkdir(); String importedCss = "\r\n\r\n.background-image: url( \"images/img.jpg\" );"; CopyUtil.copy(importedCss, new FileWriter(imported)); css = "\n\r@import \"subdir/imported.css\""; output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".background-image:url(\"subdir/images/img.jpg\");", output); importedCss = "\r\n\r\n.background-image: url('./images/./img.jpg' );"; CopyUtil.copy(importedCss, new FileWriter(imported)); output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".background-image:url('subdir/images/img.jpg');", output); importedCss = "\r\n\r\n.background-image: url( \"./images/foo/../img.jpg\" );"; CopyUtil.copy(importedCss, new FileWriter(imported)); output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".background-image:url(\"subdir/images/img.jpg\");", output); importedCss = "\r\n\r\n.background-image: url(./images/foo/bar/../../img.jpg);"; CopyUtil.copy(importedCss, new FileWriter(imported)); output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".background-image:url(subdir/images/img.jpg);", output); importedCss = "\r\n\r\n.background-image: url( images/../images/img.jpg );"; CopyUtil.copy(importedCss, new FileWriter(imported)); output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".background-image:url(subdir/images/img.jpg);", output); importedCss = "\r\n\r\n.background-image: url( '/images/img.jpg' );"; CopyUtil.copy(importedCss, new FileWriter(imported)); output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".background-image:url('/images/img.jpg');", output); importedCss = "\r\n\r\n.background-image: url( 'http: CopyUtil.copy(importedCss, new FileWriter(imported)); output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".background-image:url('http: importedCss = "\r\n\r\n.background-image: url( '#images/img.jpg' );"; CopyUtil.copy(importedCss, new FileWriter(imported)); output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".background-image:url('#images/img.jpg');", output); requestParams.put(CSSModuleBuilder.INLINEIMPORTS_REQPARAM_NAME, new String[]{"false"}); output = buildCss(new StringResource(css, resuri)); Assert.assertEquals("@import \"subdir/imported.css\"", output); requestParams.put(CSSModuleBuilder.INLINEIMPORTS_REQPARAM_NAME, new String[]{"true"}); output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".background-image:url('#images/img.jpg');", output); requestParams.put(CSSModuleBuilder.INLINEIMPORTS_REQPARAM_NAME, new String[]{"0"}); output = buildCss(new StringResource(css, resuri)); Assert.assertEquals("@import \"subdir/imported.css\"", output); requestParams.put(CSSModuleBuilder.INLINEIMPORTS_REQPARAM_NAME, new String[]{"1"}); output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".background-image:url('#images/img.jpg');", output); requestAttributes.put(IHttpTransport.SHOWFILENAMES_REQATTRNAME, Boolean.TRUE); output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".background-image:url('#images/img.jpg');", output); mockAggregator.getOptions().setOption(IOptions.DEVELOPMENT_MODE, "true"); output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".background-image:url('#images/img.jpg');", output); requestAttributes.remove(IHttpTransport.SHOWFILENAMES_REQATTRNAME); output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".background-image:url('#images/img.jpg');", output); }
@Test public void testInlineUrls() throws Exception { String css, output; URI resuri = testdir.toURI(); byte[] bytes = Base64.decodeBase64(base64PngData.getBytes()); File images = new File(testdir, "images"); images.mkdir(); File image = new File(images, "testImage.png"); OutputStream out = new FileOutputStream(image); ByteArrayInputStream in = new ByteArrayInputStream(bytes); CopyUtil.copy(in, out); css = ".foo {background-image:url(images/testImage.png)}"; output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".foo{background-image:url(images/testImage.png)}", output); configScript.put(CSSModuleBuilder.INLINEIMPORTS_CONFIGPARAM, configScript, "true"); IConfig config = new ConfigImpl(mockAggregator, tmpdir.toURI(), configScript); builder.configLoaded(config, seq++); output = buildCss(new StringResource(css, resuri)); long size = image.length(); configScript.put(CSSModuleBuilder.SIZETHRESHOLD_CONFIGPARAM, configScript, Long.toString(size)); config = new ConfigImpl(mockAggregator, tmpdir.toURI(), configScript); builder.configLoaded(config, seq++); output = buildCss(new StringResource(css, resuri)); Assert.assertTrue(output.matches("\\.foo\\{background-image:url\\('data:image\\/png;base64\\,[^']*'\\)\\}")); css = ".foo {background-image:url('images/testImage.png')}"; output = buildCss(new StringResource(css, resuri)); Assert.assertTrue(output.matches("\\.foo\\{background-image:url\\('data:image\\/png;base64\\,[^']*'\\)\\}")); css = ".foo {background-image:url(\"images/testImage.png\")}"; output = buildCss(new StringResource(css, resuri)); Assert.assertTrue(output.matches("\\.foo\\{background-image:url\\('data:image\\/png;base64\\,[^']*'\\)\\}")); css = ".foo {background-image:url( 'images/testImage.png' )}"; output = buildCss(new StringResource(css, resuri)); Assert.assertTrue(output.matches("\\.foo\\{background-image:url\\('data:image\\/png;base64\\,[^']*'\\)\\}")); String dataUrl = "data:image/svg+xml;utf8, <svg version=\"1.1\" xmlns=\"http: css = ".foo {background-image:url( '" + dataUrl + "' )}"; output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".foo{background-image:url('" + dataUrl + "')}", output); css = ".foo {background-image:url(images/testImage.png)}"; output = buildCss(new StringResource(css, resuri)); size--; configScript.put(CSSModuleBuilder.SIZETHRESHOLD_CONFIGPARAM, configScript, Long.toString(size)); config = new ConfigImpl(mockAggregator, tmpdir.toURI(), configScript); builder.configLoaded(config, seq++); output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".foo{background-image:url(images/testImage.png)}", output); size++; configScript.put(CSSModuleBuilder.SIZETHRESHOLD_CONFIGPARAM, configScript, Long.toString(size)); config = new ConfigImpl(mockAggregator, tmpdir.toURI(), configScript); builder.configLoaded(config, seq++); output = buildCss(new StringResource(css, resuri)); Assert.assertTrue(output.matches("\\.foo\\{background-image:url\\('data:image\\/png;base64\\,[^']*'\\)\\}")); requestParams.put(CSSModuleBuilder.INLINEIMAGES_REQPARAM_NAME, new String[]{"false"}); config = new ConfigImpl(mockAggregator, tmpdir.toURI(), configScript); builder.configLoaded(config, seq++); output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".foo{background-image:url(images/testImage.png)}", output); requestParams.put(CSSModuleBuilder.INLINEIMAGES_REQPARAM_NAME, new String[]{"true"}); config = new ConfigImpl(mockAggregator, tmpdir.toURI(), configScript); builder.configLoaded(config, seq++); output = buildCss(new StringResource(css, resuri)); Assert.assertTrue(output.matches("\\.foo\\{background-image:url\\('data:image\\/png;base64\\,[^']*'\\)\\}")); configScript.put(CSSModuleBuilder.SIZETHRESHOLD_CONFIGPARAM, configScript, Long.toString(0)); configScript.put(CSSModuleBuilder.INCLUDELIST_CONFIGPARAM, configScript, "testImage.png"); config = new ConfigImpl(mockAggregator, tmpdir.toURI(), configScript); builder.configLoaded(config, seq++); output = buildCss(new StringResource(css, resuri)); Assert.assertTrue(output.matches("\\.foo\\{background-image:url\\('data:image\\/png;base64\\,[^']*'\\)\\}")); configScript.put(CSSModuleBuilder.INCLUDELIST_CONFIGPARAM, configScript, "*.png"); config = new ConfigImpl(mockAggregator, tmpdir.toURI(), configScript); builder.configLoaded(config, seq++); output = buildCss(new StringResource(css, resuri)); Assert.assertTrue(output.matches("\\.foo\\{background-image:url\\('data:image\\/png;base64\\,[^']*'\\)\\}")); configScript.put(CSSModuleBuilder.INCLUDELIST_CONFIGPARAM, configScript, "testImage*"); config = new ConfigImpl(mockAggregator, tmpdir.toURI(), configScript); builder.configLoaded(config, seq++); output = buildCss(new StringResource(css, resuri)); Assert.assertTrue(output.matches("\\.foo\\{background-image:url\\('data:image\\/png;base64\\,[^']*'\\)\\}")); configScript.put(CSSModuleBuilder.INCLUDELIST_CONFIGPARAM, configScript, "*"); config = new ConfigImpl(mockAggregator, tmpdir.toURI(), configScript); builder.configLoaded(config, seq++); output = buildCss(new StringResource(css, resuri)); Assert.assertTrue(output.matches("\\.foo\\{background-image:url\\('data:image\\/png;base64\\,[^']*'\\)\\}")); configScript.put(CSSModuleBuilder.INCLUDELIST_CONFIGPARAM, configScript, "*Image.pn?"); config = new ConfigImpl(mockAggregator, tmpdir.toURI(), configScript); builder.configLoaded(config, seq++); output = buildCss(new StringResource(css, resuri)); Assert.assertTrue(output.matches("\\.foo\\{background-image:url\\('data:image\\/png;base64\\,[^']*'\\)\\}")); configScript.put(CSSModuleBuilder.INCLUDELIST_CONFIGPARAM, configScript, "test*.png"); config = new ConfigImpl(mockAggregator, tmpdir.toURI(), configScript); builder.configLoaded(config, seq++); output = buildCss(new StringResource(css, resuri)); Assert.assertTrue(output.matches("\\.foo\\{background-image:url\\('data:image\\/png;base64\\,[^']*'\\)\\}")); configScript.put(CSSModuleBuilder.INCLUDELIST_CONFIGPARAM, configScript, "te?tIma???png"); config = new ConfigImpl(mockAggregator, tmpdir.toURI(), configScript); builder.configLoaded(config, seq++); output = buildCss(new StringResource(css, resuri)); Assert.assertTrue(output.matches("\\.foo\\{background-image:url\\('data:image\\/png;base64\\,[^']*'\\)\\}")); configScript.put(CSSModuleBuilder.INCLUDELIST_CONFIGPARAM, configScript, "testImage.*png"); config = new ConfigImpl(mockAggregator, tmpdir.toURI(), configScript); builder.configLoaded(config, seq++); output = buildCss(new StringResource(css, resuri)); Assert.assertTrue(output.matches("\\.foo\\{background-image:url\\('data:image\\/png;base64\\,[^']*'\\)\\}")); configScript.put(CSSModuleBuilder.INCLUDELIST_CONFIGPARAM, configScript, "???Image.png"); config = new ConfigImpl(mockAggregator, tmpdir.toURI(), configScript); builder.configLoaded(config, seq++); output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".foo{background-image:url(images/testImage.png)}", output); configScript.put(CSSModuleBuilder.INCLUDELIST_CONFIGPARAM, configScript, "test*.??"); config = new ConfigImpl(mockAggregator, tmpdir.toURI(), configScript); builder.configLoaded(config, seq++); output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".foo{background-image:url(images/testImage.png)}", output); configScript.put(CSSModuleBuilder.INCLUDELIST_CONFIGPARAM, configScript, "*"); configScript.put(CSSModuleBuilder.EXCLUDELIST_CONFIGPARAM, configScript, "*.png"); config = new ConfigImpl(mockAggregator, tmpdir.toURI(), configScript); builder.configLoaded(config, seq++); output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".foo{background-image:url(images/testImage.png)}", output); configScript.put(CSSModuleBuilder.INCLUDELIST_CONFIGPARAM, configScript, "testImage.png"); configScript.put(CSSModuleBuilder.EXCLUDELIST_CONFIGPARAM, configScript, "testImage.png"); config = new ConfigImpl(mockAggregator, tmpdir.toURI(), configScript); builder.configLoaded(config, seq++); output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".foo{background-image:url(images/testImage.png)}", output); CopyUtil.copy("hello world!\r\n", new FileWriter(new File(testdir, "hello.txt"))); css = ".foo {background-image:url(hello.txt)}"; configScript.put(CSSModuleBuilder.SIZETHRESHOLD_CONFIGPARAM, configScript, Long.toString(size)); config = new ConfigImpl(mockAggregator, tmpdir.toURI(), configScript); builder.configLoaded(config, seq++); output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".foo{background-image:url(hello.txt)}", output); configScript.put(CSSModuleBuilder.IMAGETYPES_CONFIGPARAM, configScript, "text/plain"); config = new ConfigImpl(mockAggregator, tmpdir.toURI(), configScript); builder.configLoaded(config, seq++); output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".foo{background-image:url(hello.txt)}", output); configScript.delete(CSSModuleBuilder.INCLUDELIST_CONFIGPARAM); config = new ConfigImpl(mockAggregator, tmpdir.toURI(), configScript); builder.configLoaded(config, seq++); output = buildCss(new StringResource(css, resuri)); Assert.assertTrue(output.matches("\\.foo\\{background-image:url\\('data:text\\/plain;base64\\,[^']*'\\)\\}")); configScript.put(CSSModuleBuilder.EXCLUDELIST_CONFIGPARAM, configScript, "*.txt"); config = new ConfigImpl(mockAggregator, tmpdir.toURI(), configScript); builder.configLoaded(config, seq++); output = buildCss(new StringResource(css, resuri)); Assert.assertEquals(".foo{background-image:url(hello.txt)}", output); CopyUtil.copy("hello world!\r\n", new FileWriter(new File(testdir, "hello.foo"))); css = ".foo {background-image:url(hello.foo)}"; configScript.put(CSSModuleBuilder.IMAGETYPES_CONFIGPARAM, configScript, "content/unknown"); config = new ConfigImpl(mockAggregator, tmpdir.toURI(), configScript); builder.configLoaded(config, seq++); output = buildCss(new StringResource(css, resuri)); Assert.assertTrue(output.matches("\\.foo\\{background-image:url\\('data:content\\/unknown;base64\\,[^']*'\\)\\}")); CopyUtil.copy("hello world!\r\n", new FileWriter(new File(testdir, "hello.svg"))); css = ".hello {background-image:url(hello.svg)}"; String cfg = "{" + CSSModuleBuilder.IMAGETYPES_CONFIGPARAM + ":{svg:'image/svg+xml'}," + CSSModuleBuilder.SIZETHRESHOLD_CONFIGPARAM + ":1000}"; config = new ConfigImpl(mockAggregator, tmpdir.toURI(), cfg); builder.configLoaded(config, seq++); output = buildCss(new StringResource(css, resuri)); Assert.assertTrue(output.matches("\\.hello\\{background-image:url\\('data:image\\/svg\\+xml;base64\\,[^']*'\\)\\}")); }
@Test public void testWebpackModuleImports() throws Exception { String css, output; File subdir = new File(testdir, "randomDir"); File imported = new File(subdir, "randomFile.css"); subdir.mkdir(); String importedCss = "\r\n\r\n.imported {\r\n\tcolor : black;\r\n}"; CopyUtil.copy(importedCss, new FileWriter(imported)); IConfig config = new ConfigImpl(mockAggregator, tmpdir.toURI(), "{paths: {AMDDir:'" + subdir.toURI() + "'}}"); configRef.set(config); builder.configLoaded(config, seq++); css = "\n\r@import '~AMDDir/randomFile.css'"; File importingFile = new File(subdir, "importingFile.css"); CopyUtil.copy(css, new FileWriter(importingFile)); output = buildCss(new FileResource(importingFile.toURI())); Assert.assertEquals(".imported{color:black}", output); }
@Test public void testNonRelativeImports() throws Exception { String css, output; File subdir = new File(testdir, "randomDir"); File imported = new File(subdir, "randomFile.css"); subdir.mkdir(); String importedCss = "\r\n\r\n.imported {\r\n\tcolor : black;\r\n}"; CopyUtil.copy(importedCss, new FileWriter(imported)); IConfig config = new ConfigImpl(mockAggregator, tmpdir.toURI(), "{paths: {AMDDir:'" + subdir.toURI() + "'}, cssEnableAMDIncludePaths: false}"); builder.configLoaded(config, seq++); css = "\n\r@import 'AMDDir/randomFile.css'"; File importingFile = new File(subdir, "importingFile.css"); CopyUtil.copy(css, new FileWriter(importingFile)); boolean exceptionCaught = false; try { output = buildCss(new FileResource(importingFile.toURI())); } catch (NotFoundException e) { exceptionCaught = true; } Assert.assertTrue("Expected FileNotFoundException", exceptionCaught); config = new ConfigImpl(mockAggregator, tmpdir.toURI(), "{paths: {AMDDir:'" + subdir.toURI() + "'}, cssEnableAMDIncludePaths: true}"); configRef.set(config); builder.configLoaded(config, seq++); css = "\n\r@import 'AMDDir/randomFile.css'"; importingFile = new File(subdir, "importingFile.css"); CopyUtil.copy(css, new FileWriter(importingFile)); output = buildCss(new FileResource(importingFile.toURI())); Assert.assertEquals(".imported{color:black}", output); }