Search in sources :

Example 11 with CSSWriter

use of com.helger.css.writer.CSSWriter 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 12 with CSSWriter

use of com.helger.css.writer.CSSWriter 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 13 with CSSWriter

use of com.helger.css.writer.CSSWriter 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 14 with CSSWriter

use of com.helger.css.writer.CSSWriter in project ph-css by phax.

the class CSSVisitorDeclarationListFuncTest method testModifyingCSSUrlVisitor.

@Test
public void testModifyingCSSUrlVisitor() {
    final CSSDeclarationList aCSS = CSSReaderDeclarationList.readFromString("background:url(a.gif);background:url(b.gif);", ECSSVersion.CSS30);
    assertNotNull(aCSS);
    // Append ".modified" to all URLs
    final MockModifyingCSSUrlVisitor aVisitor2 = new MockModifyingCSSUrlVisitor();
    CSSVisitor.visitAllDeclarationUrls(aCSS, aVisitor2);
    // Check the result
    assertEquals("background:url(a.gif.modified);background:url(b.gif.modified)", new CSSWriter(ECSSVersion.CSS30, true).getCSSAsString(aCSS));
    // Re-iterate to check twice
    CSSVisitor.visitAllDeclarationUrls(aCSS, new DefaultCSSUrlVisitor() {

        @Override
        public void onImport(@Nonnull final CSSImportRule aImportRule) {
            assertTrue(aImportRule.getLocationString().endsWith(".modified"));
        }

        @Override
        public void onUrlDeclaration(@Nullable final ICSSTopLevelRule aTopLevelRule, @Nonnull final CSSDeclaration aDeclaration, @Nonnull final CSSExpressionMemberTermURI aURITerm) {
            assertTrue(aURITerm.getURIString().endsWith(".modified"));
        }
    });
}
Also used : ICSSTopLevelRule(com.helger.css.decl.ICSSTopLevelRule) CSSExpressionMemberTermURI(com.helger.css.decl.CSSExpressionMemberTermURI) CSSImportRule(com.helger.css.decl.CSSImportRule) CSSDeclaration(com.helger.css.decl.CSSDeclaration) CSSDeclarationList(com.helger.css.decl.CSSDeclarationList) CSSWriter(com.helger.css.writer.CSSWriter) Test(org.junit.Test)

Example 15 with CSSWriter

use of com.helger.css.writer.CSSWriter in project ph-css by phax.

the class AbstractFuncTestCSSReader method testReadBadButRecoverable.

protected final void testReadBadButRecoverable(@Nonnull final String sBaseDir) {
    final File aBaseDir = new File(sBaseDir);
    if (!aBaseDir.exists())
        throw new IllegalArgumentException("BaseDir " + sBaseDir + " does not exist!");
    for (final File aFile : new FileSystemRecursiveIterator(aBaseDir).withFilter(IFileFilter.filenameEndsWith(".css"))) {
        final String sKey = aFile.getAbsolutePath();
        if (m_bDebug)
            m_aLogger.info(sKey);
        // Handle each error as a fatal error!
        final CollectingCSSParseErrorHandler aErrorHdl = new CollectingCSSParseErrorHandler();
        m_aReaderSettings.setCustomErrorHandler(aErrorHdl.and(new LoggingCSSParseErrorHandler()));
        final CascadingStyleSheet aCSS = CSSReader.readFromFile(aFile, m_aReaderSettings);
        assertNotNull(sKey, aCSS);
        assertTrue(sKey, aErrorHdl.hasParseErrors());
        assertTrue(sKey, aErrorHdl.getParseErrorCount() > 0);
        if (m_bDebug)
            m_aLogger.info(aErrorHdl.getAllParseErrors().toString());
        // Write optimized version and re-read it
        final String sCSS = new CSSWriter(m_aWriterSettings.setOptimizedOutput(true)).getCSSAsString(aCSS);
        assertNotNull(sKey, sCSS);
        if (m_bDebug)
            m_aLogger.info(sCSS);
        final CascadingStyleSheet aCSSReRead = CSSReader.readFromStringReader(sCSS, m_aReaderSettings);
        assertNotNull("Failed to parse:\n" + sCSS, aCSSReRead);
        assertEquals(sKey, aCSS, aCSSReRead);
    }
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CollectingCSSParseErrorHandler(com.helger.css.reader.errorhandler.CollectingCSSParseErrorHandler) FileSystemRecursiveIterator(com.helger.commons.io.file.FileSystemRecursiveIterator) CSSWriter(com.helger.css.writer.CSSWriter) File(java.io.File) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler)

Aggregations

CSSWriter (com.helger.css.writer.CSSWriter)28 CascadingStyleSheet (com.helger.css.decl.CascadingStyleSheet)25 Test (org.junit.Test)24 LoggingCSSParseErrorHandler (com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler)12 CSSReaderSettings (com.helger.css.reader.CSSReaderSettings)11 CSSWriterSettings (com.helger.css.writer.CSSWriterSettings)10 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)8 IReadableResource (com.helger.commons.io.resource.IReadableResource)8 File (java.io.File)8 Charset (java.nio.charset.Charset)7 ECSSVersion (com.helger.css.ECSSVersion)5 CSSDeclaration (com.helger.css.decl.CSSDeclaration)3 CSSExpressionMemberTermURI (com.helger.css.decl.CSSExpressionMemberTermURI)3 FileSystemRecursiveIterator (com.helger.commons.io.file.FileSystemRecursiveIterator)2 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)2 CSSExpressionMemberFunction (com.helger.css.decl.CSSExpressionMemberFunction)2 CSSExpressionMemberMath (com.helger.css.decl.CSSExpressionMemberMath)2 CSSExpressionMemberTermSimple (com.helger.css.decl.CSSExpressionMemberTermSimple)2 CSSStyleRule (com.helger.css.decl.CSSStyleRule)2 ICSSExpressionMember (com.helger.css.decl.ICSSExpressionMember)2