Search in sources :

Example 46 with CascadingStyleSheet

use of com.helger.css.decl.CascadingStyleSheet in project ph-css by phax.

the class CSSCompressor method getRewrittenCSS.

/**
 * Get the rewritten version of the passed CSS code. This is done by
 * interpreting the CSS and than writing it again with the passed settings.
 * This can e.g. be used to create a compressed version of a CSS.
 *
 * @param sOriginalCSS
 *        The original CSS code to be compressed.
 * @param aSettings
 *        The CSS writer settings to use. The version is used to read the
 *        original CSS.
 * @return If compression failed because the CSS is invalid or whatsoever, the
 *         original CSS is returned, else the rewritten version is returned.
 */
@Nonnull
public static String getRewrittenCSS(@Nonnull final String sOriginalCSS, @Nonnull final CSSWriterSettings aSettings) {
    ValueEnforcer.notNull(sOriginalCSS, "OriginalCSS");
    ValueEnforcer.notNull(aSettings, "Settings");
    final CascadingStyleSheet aCSS = CSSReader.readFromString(sOriginalCSS, aSettings.getVersion());
    if (aCSS != null) {
        try {
            return new CSSWriter(aSettings).getCSSAsString(aCSS);
        } catch (final Exception ex) {
            s_aLogger.warn("Failed to write optimized CSS!", ex);
        }
    }
    return sOriginalCSS;
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) Nonnull(javax.annotation.Nonnull)

Example 47 with CascadingStyleSheet

use of com.helger.css.decl.CascadingStyleSheet in project ph-css by phax.

the class ParserCSS30Test method test2.

@Test
public void test2() throws ParseException {
    final ParserCSS30TokenManager aTokenHdl = new ParserCSS30TokenManager(new CSSCharStream(new NonBlockingStringReader(CSS2)));
    aTokenHdl.setDebugStream(System.out);
    final ParserCSS30 aParser = new ParserCSS30(aTokenHdl);
    aParser.disable_tracing();
    final CSSNode aNode = aParser.styleSheet();
    assertNotNull(aNode);
    final CascadingStyleSheet aCSS = CSSHandler.readCascadingStyleSheetFromNode(ECSSVersion.CSS30, aNode, CSSReader.getDefaultInterpretErrorHandler());
    assertNotNull(aCSS);
    for (final ICSSTopLevelRule aTopLevelRule : aCSS.getAllFontFaceRules()) assertTrue(aCSS.removeRule(aTopLevelRule).isChanged());
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) ICSSTopLevelRule(com.helger.css.decl.ICSSTopLevelRule) NonBlockingStringReader(com.helger.commons.io.stream.NonBlockingStringReader) Test(org.junit.Test)

Example 48 with CascadingStyleSheet

use of com.helger.css.decl.CascadingStyleSheet in project ph-css by phax.

the class AbstractFuncTestCSSReader method testReadGood.

protected final void testReadGood(@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("Filename: " + sKey);
        final CollectingCSSParseErrorHandler aErrorHdl = new CollectingCSSParseErrorHandler();
        m_aReaderSettings.setCustomErrorHandler(aErrorHdl.and(new LoggingCSSParseErrorHandler()));
        final CascadingStyleSheet aCSS = CSSReader.readFromFile(aFile, m_aReaderSettings);
        assertNotNull(sKey, aCSS);
        // May have errors or not
        if (m_bDebug)
            m_aLogger.info("Parse errors: " + aErrorHdl.getAllParseErrors().toString());
        CommonsTestHelper.testDefaultSerialization(aCSS);
        // Write optimized version and compare it
        String sCSS = new CSSWriter(m_aWriterSettings.setOptimizedOutput(true)).getCSSAsString(aCSS);
        assertNotNull(sKey, sCSS);
        if (m_bDebug)
            m_aLogger.info("Created CSS: " + sCSS);
        final CascadingStyleSheet aCSSReRead = CSSReader.readFromStringReader(sCSS, m_aReaderSettings);
        assertNotNull("Failed to parse " + sKey + ":\n" + sCSS, aCSSReRead);
        assertEquals(sKey + "\n" + sCSS, aCSS, aCSSReRead);
        // Write non-optimized version and compare it
        sCSS = new CSSWriter(m_aWriterSettings.setOptimizedOutput(false)).getCSSAsString(aCSS);
        assertNotNull(sKey, sCSS);
        if (m_bDebug)
            m_aLogger.info("Read and re-created CSS: " + sCSS);
        assertEquals(sKey, aCSS, CSSReader.readFromStringReader(sCSS, m_aReaderSettings));
        // Write non-optimized and code-removed version and ensure it is not
        // null
        sCSS = new CSSWriter(m_aWriterSettings.setOptimizedOutput(false).setRemoveUnnecessaryCode(true)).getCSSAsString(aCSS);
        assertNotNull(sKey, sCSS);
        assertNotNull(sKey, CSSReader.readFromStringReader(sCSS, m_aReaderSettings));
        // Restore value :)
        m_aWriterSettings.setRemoveUnnecessaryCode(false);
    }
}
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)

Example 49 with CascadingStyleSheet

use of com.helger.css.decl.CascadingStyleSheet in project ph-css by phax.

the class CSSReader30SpecialFuncTest method testReadSpecialGood.

@Test
public void testReadSpecialGood() {
    final ECSSVersion eVersion = ECSSVersion.CSS30;
    final Charset aCharset = StandardCharsets.UTF_8;
    final File aFile = new File("src/test/resources/testfiles/css30/good/artificial/test-postcss-cssnext.css");
    final CascadingStyleSheet aCSS = CSSReader.readFromFile(aFile, aCharset, eVersion);
    assertNotNull(aCSS);
    final String sCSS = new CSSWriter(eVersion, false).getCSSAsString(aCSS);
    assertNotNull(sCSS);
    if (false)
        s_aLogger.info(sCSS);
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) ECSSVersion(com.helger.css.ECSSVersion) Charset(java.nio.charset.Charset) CSSWriter(com.helger.css.writer.CSSWriter) File(java.io.File) Test(org.junit.Test)

Example 50 with CascadingStyleSheet

use of com.helger.css.decl.CascadingStyleSheet in project ph-css by phax.

the class Issue24Test method testIssue.

@Test
public void testIssue() {
    final IReadableResource aRes = new ClassPathResource("testfiles/css30/bad_but_browsercompliant/issue24.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)

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