Search in sources :

Example 56 with CascadingStyleSheet

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

the class MediaQueryToolsTest method testGetWrapped.

@Test
public void testGetWrapped() {
    // Read and arbitrary CSS
    final CascadingStyleSheet aBaseCSS = CSSReader.readFromString("p { color:red;}", s_eVersion);
    assertNotNull(aBaseCSS);
    // Create structured media queries
    final List<CSSMediaQuery> aMQs = MediaQueryTools.parseToMediaQuery("screen", s_eVersion);
    assertNotNull(aMQs);
    // Wrap the source CSS with the specified media queries
    final CascadingStyleSheet aWrappedCSS = MediaQueryTools.getWrappedInMediaQuery(aBaseCSS, aMQs, false);
    assertNotNull(aWrappedCSS);
    assertEquals("@media screen{p{color:red}}", new CSSWriter(s_eVersion, true).getCSSAsString(aWrappedCSS));
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSMediaQuery(com.helger.css.decl.CSSMediaQuery) CSSWriter(com.helger.css.writer.CSSWriter) Test(org.junit.Test)

Example 57 with CascadingStyleSheet

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

the class Issue8Test method testIssue8.

@Test
public void testIssue8() {
    final IReadableResource aRes = new ClassPathResource("testfiles/css30/good/issue8.css");
    assertTrue(aRes.exists());
    final CascadingStyleSheet aCSS = CSSReader.readFromStream(aRes, StandardCharsets.UTF_8, ECSSVersion.CSS30, new LoggingCSSParseErrorHandler());
    assertNotNull(aCSS);
    assertEquals(1, aCSS.getStyleRuleCount());
    final CSSStyleRule aStyleRule = aCSS.getStyleRuleAtIndex(0);
    assertNotNull(aStyleRule);
    assertEquals(4, aStyleRule.getDeclarationCount());
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSStyleRule(com.helger.css.decl.CSSStyleRule) IReadableResource(com.helger.commons.io.resource.IReadableResource) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler) Test(org.junit.Test)

Example 58 with CascadingStyleSheet

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

the class IssueGC18Test method testIssue18.

@Test
public void testIssue18() {
    final IReadableResource aRes = new ClassPathResource("testfiles/css30/good/issue-gc-18.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 59 with CascadingStyleSheet

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

the class CSSWriterFuncTest method testRead30Write21.

@Test
public void testRead30Write21() throws IOException {
    for (final File aFile : new FileSystemRecursiveIterator(new File("src/test/resources/testfiles/css30/good/artificial")).withFilter(IFileFilter.filenameEndsWith(".css"))) {
        final String sKey = aFile.getAbsolutePath();
        try {
            // read and interpret CSS 3.0
            final CascadingStyleSheet aCSS = CSSReader.readFromFile(aFile, StandardCharsets.UTF_8, ECSSVersion.CSS30);
            assertNotNull(sKey, aCSS);
            // write to CSS 2.1
            final NonBlockingStringWriter aSW = new NonBlockingStringWriter();
            new CSSWriter(ECSSVersion.CSS21).writeCSS(aCSS, aSW);
            // This should throw an error
            fail(sKey + " should have thrown an exception but got: " + aSW.getAsString());
        } catch (final IllegalStateException ex) {
        }
    }
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) FileSystemRecursiveIterator(com.helger.commons.io.file.FileSystemRecursiveIterator) File(java.io.File) NonBlockingStringWriter(com.helger.commons.io.stream.NonBlockingStringWriter) Test(org.junit.Test)

Example 60 with CascadingStyleSheet

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

the class CSSWriterFuncTest method testCompressCSS_Size.

@Test
public void testCompressCSS_Size() {
    final CascadingStyleSheet aCSS = CSSReader.readFromStream(new ClassPathResource("/testfiles/css21/good/phloc/test/content.css"), StandardCharsets.UTF_8, ECSSVersion.CSS30);
    assertNotNull(aCSS);
    // Only whitespace optimization
    final CSSWriterSettings aSettings = new CSSWriterSettings(ECSSVersion.CSS21, true);
    String sContent = new CSSWriter(aSettings).getCSSAsString(aCSS);
    assertEquals(2846, sContent.length());
    // Also remove empty declarations
    aSettings.setRemoveUnnecessaryCode(true);
    sContent = new CSSWriter(aSettings).getCSSAsString(aCSS);
    assertEquals(2839, sContent.length());
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) 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