Search in sources :

Example 21 with CascadingStyleSheet

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());
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) Test(org.junit.Test)

Example 22 with CascadingStyleSheet

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());
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) Test(org.junit.Test)

Example 23 with CascadingStyleSheet

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());
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) Test(org.junit.Test)

Example 24 with CascadingStyleSheet

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));
    }
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet)

Example 25 with CascadingStyleSheet

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));
    }
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) Charset(java.nio.charset.Charset) ParseException(com.helger.css.parser.ParseException) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) CSSWriter(com.helger.css.writer.CSSWriter) IOException(java.io.IOException) CSSReaderSettings(com.helger.css.reader.CSSReaderSettings) File(java.io.File) ICSSParseExceptionCallback(com.helger.css.handler.ICSSParseExceptionCallback)

Aggregations

CascadingStyleSheet (com.helger.css.decl.CascadingStyleSheet)64 Test (org.junit.Test)50 CSSWriter (com.helger.css.writer.CSSWriter)25 LoggingCSSParseErrorHandler (com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler)19 CSSReaderSettings (com.helger.css.reader.CSSReaderSettings)14 File (java.io.File)14 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)13 IReadableResource (com.helger.commons.io.resource.IReadableResource)12 CSSWriterSettings (com.helger.css.writer.CSSWriterSettings)10 Charset (java.nio.charset.Charset)10 ECSSVersion (com.helger.css.ECSSVersion)8 FileSystemRecursiveIterator (com.helger.commons.io.file.FileSystemRecursiveIterator)6 CSSDeclaration (com.helger.css.decl.CSSDeclaration)5 CSSExpressionMemberTermURI (com.helger.css.decl.CSSExpressionMemberTermURI)5 ICSSTopLevelRule (com.helger.css.decl.ICSSTopLevelRule)5 CSSImportRule (com.helger.css.decl.CSSImportRule)4 CSSExpressionMemberFunction (com.helger.css.decl.CSSExpressionMemberFunction)3 CSSStyleRule (com.helger.css.decl.CSSStyleRule)3 DefaultCSSUrlVisitor (com.helger.css.decl.visit.DefaultCSSUrlVisitor)3 CollectingCSSParseErrorHandler (com.helger.css.reader.errorhandler.CollectingCSSParseErrorHandler)3