use of com.helger.css.decl.CascadingStyleSheet in project ph-css by phax.
the class Issue3Test method testErrorInMediaRule1.
@Test
public void testErrorInMediaRule1() {
// Parse error in "unexpected:;"
final String sTest = "@media print { div { color:red; unexpected:; align:top; } } span {color:blue;}";
final CascadingStyleSheet aCSS = _parse(sTest, true);
assertNotNull(aCSS);
if (false)
_print(aCSS);
assertEquals(1, aCSS.getMediaRuleCount());
assertEquals(2, ((CSSStyleRule) aCSS.getMediaRuleAtIndex(0).getRuleAtIndex(0)).getDeclarationCount());
assertEquals(1, aCSS.getStyleRuleCount());
}
use of com.helger.css.decl.CascadingStyleSheet in project ph-css by phax.
the class Issue3Test method testErrorInStyleDeclarationBlock2a.
@Test
public void testErrorInStyleDeclarationBlock2a() {
// Parse error at ".class" - nesting error which is not closed afterwards
final String sTest = "body1 {background:red;}\n" + "body2 {background:blue;.class{color:green}\n" + " body3 {background:green;}\n" + "body4{background:orange;}";
final CascadingStyleSheet aCSS = _parse(sTest, true);
assertNotNull(aCSS);
if (true)
_print(aCSS);
assertEquals(1, aCSS.getStyleRuleCount());
assertEquals(1, aCSS.getStyleRuleAtIndex(0).getDeclarationCount());
}
use of com.helger.css.decl.CascadingStyleSheet in project ph-css by phax.
the class Issue3Test method testErrorInStyleDeclarationBlock2.
@Test
public void testErrorInStyleDeclarationBlock2() {
// Parse error at ".class" - nesting error which is afterwards closed
final String sTest = "body {background:red;}" + "body {background:blue;.class{color:green}" + " body {background:green;}" + "}" + "body{background:orange;}";
final CascadingStyleSheet aCSS = _parse(sTest, true);
assertNotNull(aCSS);
if (true)
_print(aCSS);
assertEquals(3, aCSS.getStyleRuleCount());
assertEquals(1, aCSS.getStyleRuleAtIndex(0).getDeclarationCount());
assertEquals(1, aCSS.getStyleRuleAtIndex(1).getDeclarationCount());
assertEquals(1, aCSS.getStyleRuleAtIndex(2).getDeclarationCount());
}
use of com.helger.css.decl.CascadingStyleSheet in project ph-css by phax.
the class CSSWriterFuncTest method _testMe.
private void _testMe(@Nonnull final File aFile, @Nonnull final ECSSVersion eVersion) {
if (false)
s_aLogger.info(aFile.getAbsolutePath());
// read and interpret
final CascadingStyleSheet aCSS = CSSReader.readFromFile(aFile, StandardCharsets.UTF_8, eVersion);
assertNotNull(aFile.getAbsolutePath(), aCSS);
// Both normal and optimized!
for (int i = 0; i < 2; ++i) {
// write to buffer
final String sCSS = new CSSWriter(eVersion, i == 1).getCSSAsString(aCSS);
if (false)
System.out.println("--" + i + "--\n" + sCSS);
// read again from buffer
assertEquals(aFile.getAbsolutePath() + (i == 0 ? " unoptimized" : " optimized"), aCSS, CSSReader.readFromString(sCSS, eVersion));
}
}
use of com.helger.css.decl.CascadingStyleSheet in project ph-css by phax.
the class CSSCompressMojo method _compressCSSFile.
private void _compressCSSFile(@Nonnull final File aChild) {
// Compress the file only if the compressed file is older than the original
// file. Note: lastModified on a non-existing file returns 0L
final File aCompressed = new File(FilenameHelper.getWithoutExtension(aChild.getAbsolutePath()) + targetFileExtension);
if (aCompressed.lastModified() < aChild.lastModified() || forceCompress) {
if (verbose)
getLog().info("Start compressing CSS file " + _getRelativePath(aChild));
else
getLog().debug("Start compressing CSS file " + _getRelativePath(aChild));
final ICSSParseExceptionCallback aExHdl = (@Nonnull final ParseException ex) -> getLog().error("Failed to parse CSS file " + _getRelativePath(aChild), ex);
final Charset aFallbackCharset = CharsetHelper.getCharsetFromName(sourceEncoding);
final CSSReaderSettings aSettings = new CSSReaderSettings().setCSSVersion(ECSSVersion.CSS30).setFallbackCharset(aFallbackCharset).setCustomExceptionHandler(aExHdl).setBrowserCompliantMode(browserCompliantMode);
final CascadingStyleSheet aCSS = CSSReader.readFromFile(aChild, aSettings);
if (aCSS != null) {
// We read it!
final FileSystemResource aDestFile = new FileSystemResource(aCompressed);
try {
final CSSWriterSettings aWriterSettings = new CSSWriterSettings(ECSSVersion.CSS30);
aWriterSettings.setOptimizedOutput(true);
aWriterSettings.setRemoveUnnecessaryCode(removeUnnecessaryCode);
aWriterSettings.setNewLineMode(newLineMode);
aWriterSettings.setQuoteURLs(quoteURLs);
aWriterSettings.setWriteNamespaceRules(writeNamespaceRules);
aWriterSettings.setWriteFontFaceRules(writeFontFaceRules);
aWriterSettings.setWriteKeyframesRules(writeKeyframesRules);
aWriterSettings.setWriteMediaRules(writeMediaRules);
aWriterSettings.setWritePageRules(writePageRules);
aWriterSettings.setWriteViewportRules(writeViewportRules);
aWriterSettings.setWriteSupportsRules(writeSupportsRules);
aWriterSettings.setWriteUnknownRules(writeUnknownRules);
final Charset aTargetCharset = CharsetHelper.getCharsetFromName(targetEncoding);
new CSSWriter(aWriterSettings).writeCSS(aCSS, aDestFile.getWriter(aTargetCharset, EAppend.TRUNCATE));
} catch (final IOException ex) {
getLog().error("Failed to write compressed CSS file '" + aCompressed.toString() + "' to disk", ex);
}
}
} else {
if (verbose)
getLog().info("Ignoring already compressed CSS file " + _getRelativePath(aChild));
else
getLog().debug("Ignoring already compressed CSS file " + _getRelativePath(aChild));
}
}
Aggregations