Search in sources :

Example 21 with CSSWriter

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

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

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

Example 24 with CSSWriter

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

the class Issue4Test method testIssue4.

@Test
public void testIssue4() {
    final IReadableResource aRes = new ClassPathResource("testfiles/css30/good/issue4.css");
    assertTrue(aRes.exists());
    final CascadingStyleSheet aCSS = CSSReader.readFromStream(aRes, StandardCharsets.UTF_8, ECSSVersion.CSS30, 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) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler) Test(org.junit.Test)

Example 25 with CSSWriter

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

the class ECSSColorTest method testAll.

@Test
public void testAll() {
    final CSSWriter aWriter = new CSSWriter(ECSSVersion.CSS30);
    for (final ECSSColor eColor : ECSSColor.values()) {
        assertTrue(StringHelper.hasText(eColor.getName()));
        assertTrue(CSSColorHelper.isColorValue(eColor.getName()));
        final String sHex = eColor.getAsHexColorValue();
        assertTrue(sHex, CSSColorHelper.isHexColorValue(sHex));
        final String sRGB = eColor.getAsRGBColorValue();
        assertTrue(sRGB, CSSColorHelper.isRGBColorValue(sRGB));
        assertNotNull(eColor.getAsRGB());
        assertEquals(sRGB, aWriter.getCSSAsString(eColor.getAsRGB()));
        final String sRGBA = eColor.getAsRGBAColorValue(1f);
        assertTrue(sRGBA, CSSColorHelper.isRGBAColorValue(sRGBA));
        assertNotNull(eColor.getAsRGBA(1));
        assertEquals(sRGBA, aWriter.getCSSAsString(eColor.getAsRGBA(1)));
        final String sHSL = eColor.getAsHSLColorValue();
        assertTrue(sHSL, CSSColorHelper.isHSLColorValue(sHSL));
        assertNotNull(eColor.getAsHSL());
        assertEquals(sHSL, aWriter.getCSSAsString(eColor.getAsHSL()));
        final String sHSLA = eColor.getAsHSLAColorValue(1f);
        assertTrue(sHSLA, CSSColorHelper.isHSLAColorValue(sHSLA));
        assertNotNull(eColor.getAsHSLA(1));
        assertEquals(sHSLA, aWriter.getCSSAsString(eColor.getAsHSLA(1)));
        assertSame(eColor, ECSSColor.getFromNameCaseInsensitiveOrNull(eColor.getName()));
        assertTrue(ECSSColor.isDefaultColorName(eColor.getName()));
    }
}
Also used : CSSWriter(com.helger.css.writer.CSSWriter) Test(org.junit.Test)

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