Search in sources :

Example 6 with CSSReaderSettings

use of com.helger.css.reader.CSSReaderSettings in project ph-css by phax.

the class Issue38Test method testIssue.

@Test
public void testIssue() {
    final String css = "h1:lang(or) { line-height: 1.2em; }";
    final CSSReaderSettings aSettings = new CSSReaderSettings().setCSSVersion(ECSSVersion.LATEST).setBrowserCompliantMode(true);
    final CascadingStyleSheet cascadingStyleSheet = CSSReader.readFromStringStream(css, aSettings);
    final CSSWriter writer = new CSSWriter(new CSSWriterSettings(ECSSVersion.LATEST, true));
    assertEquals("h1:lang(or){line-height:1.2em}", writer.getCSSAsString(cascadingStyleSheet));
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) CSSWriter(com.helger.css.writer.CSSWriter) CSSReaderSettings(com.helger.css.reader.CSSReaderSettings) Test(org.junit.Test)

Example 7 with CSSReaderSettings

use of com.helger.css.reader.CSSReaderSettings in project ph-css by phax.

the class Issue22Test method testIssue.

@Test
public void testIssue() {
    // Multiple errors contained
    final IReadableResource aRes = new ClassPathResource("testfiles/css30/good/issue22.css");
    assertTrue(aRes.exists());
    final CascadingStyleSheet aCSS = CSSReader.readFromStream(aRes, new CSSReaderSettings().setFallbackCharset(StandardCharsets.UTF_8).setCustomErrorHandler(new LoggingCSSParseErrorHandler()));
    assertNotNull(aCSS);
    if (false)
        System.out.println(new CSSWriter(ECSSVersion.CSS30).getCSSAsString(aCSS));
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) IReadableResource(com.helger.commons.io.resource.IReadableResource) CSSWriter(com.helger.css.writer.CSSWriter) CSSReaderSettings(com.helger.css.reader.CSSReaderSettings) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler) Test(org.junit.Test)

Example 8 with CSSReaderSettings

use of com.helger.css.reader.CSSReaderSettings in project ph-css by phax.

the class Issue26Test method testIssue.

@Test
public void testIssue() {
    final IReadableResource aRes = new ClassPathResource("testfiles/css30/bad_but_browsercompliant/issue26.css");
    assertTrue(aRes.exists());
    final CascadingStyleSheet aCSS = CSSReader.readFromStream(aRes, new CSSReaderSettings().setFallbackCharset(StandardCharsets.UTF_8).setBrowserCompliantMode(true).setCustomErrorHandler(new LoggingCSSParseErrorHandler()));
    assertNotNull(aCSS);
    if (false)
        System.out.println(new CSSWriter().getCSSAsString(aCSS));
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) IReadableResource(com.helger.commons.io.resource.IReadableResource) CSSWriter(com.helger.css.writer.CSSWriter) CSSReaderSettings(com.helger.css.reader.CSSReaderSettings) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler) Test(org.junit.Test)

Example 9 with CSSReaderSettings

use of com.helger.css.reader.CSSReaderSettings 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)

Example 10 with CSSReaderSettings

use of com.helger.css.reader.CSSReaderSettings in project ph-css by phax.

the class CSSVisitor30FuncTest method testVisitContent30.

@Test
public void testVisitContent30() {
    for (final File aFile : new FileSystemRecursiveIterator(new File("src/test/resources/testfiles/css30/good")).withFilter(IFileFilter.filenameEndsWith(".css"))) {
        final String sKey = aFile.getAbsolutePath();
        if (true)
            s_aLogger.info(sKey);
        final CascadingStyleSheet aCSS = CSSReader.readFromFile(aFile, new CSSReaderSettings().setFallbackCharset(StandardCharsets.UTF_8).setCSSVersion(ECSSVersion.CSS30).setCustomErrorHandler(new LoggingCSSParseErrorHandler()).setBrowserCompliantMode(true));
        assertNotNull(sKey, aCSS);
        CSSVisitor.visitCSSUrl(aCSS, new MockUrlVisitor(sKey));
    }
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) FileSystemRecursiveIterator(com.helger.commons.io.file.FileSystemRecursiveIterator) CSSReaderSettings(com.helger.css.reader.CSSReaderSettings) File(java.io.File) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler) Test(org.junit.Test)

Aggregations

CascadingStyleSheet (com.helger.css.decl.CascadingStyleSheet)14 CSSReaderSettings (com.helger.css.reader.CSSReaderSettings)14 Test (org.junit.Test)12 CSSWriter (com.helger.css.writer.CSSWriter)11 LoggingCSSParseErrorHandler (com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler)9 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)6 IReadableResource (com.helger.commons.io.resource.IReadableResource)6 CSSWriterSettings (com.helger.css.writer.CSSWriterSettings)6 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)2 DoNothingCSSInterpretErrorHandler (com.helger.css.reader.errorhandler.DoNothingCSSInterpretErrorHandler)2 File (java.io.File)2 Charset (java.nio.charset.Charset)2 FileSystemRecursiveIterator (com.helger.commons.io.file.FileSystemRecursiveIterator)1 CSSDeclaration (com.helger.css.decl.CSSDeclaration)1 CSSExpressionMemberTermURI (com.helger.css.decl.CSSExpressionMemberTermURI)1 CSSImportRule (com.helger.css.decl.CSSImportRule)1 ICSSTopLevelRule (com.helger.css.decl.ICSSTopLevelRule)1 DefaultCSSUrlVisitor (com.helger.css.decl.visit.DefaultCSSUrlVisitor)1 ICSSParseExceptionCallback (com.helger.css.handler.ICSSParseExceptionCallback)1 LoggingCSSParseExceptionCallback (com.helger.css.handler.LoggingCSSParseExceptionCallback)1