Search in sources :

Example 41 with CascadingStyleSheet

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

the class Issue3Test method testErrorInStyleDeclarationBlock1.

@Test
public void testErrorInStyleDeclarationBlock1() {
    // Parse error in "unexpected:;"
    final String sTest = "body { background:red; unexpected:; background:blue; } span {color:blue;}";
    // Expected output:
    // body { background:red; background:blue; } span {color:blue;}
    final CascadingStyleSheet aCSS = _parse(sTest, true);
    assertNotNull(aCSS);
    if (true)
        _print(aCSS);
    assertEquals(2, aCSS.getStyleRuleCount());
    // both backgrounds are present
    assertEquals(2, aCSS.getStyleRuleAtIndex(0).getDeclarationCount());
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) Test(org.junit.Test)

Example 42 with CascadingStyleSheet

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

the class Issue3Test method testErrorInKeyframeRule1.

@Test
public void testErrorInKeyframeRule1() {
    // Parse error in "unexpected::;"
    final String sTest = "@keyframes identifier { unexpected::; } span {color:blue;}";
    final CascadingStyleSheet aCSS = _parse(sTest, true);
    assertNotNull(aCSS);
    if (false)
        _print(aCSS);
    assertEquals(1, aCSS.getKeyframesRuleCount());
    assertEquals(0, aCSS.getKeyframesRuleAtIndex(0).getBlockCount());
    assertEquals(1, aCSS.getStyleRuleCount());
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) Test(org.junit.Test)

Example 43 with CascadingStyleSheet

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

the class Issue3Test method testErrorInKeyframeRule2.

@Test
public void testErrorInKeyframeRule2() {
    // Parse error in "unexpected::;"
    final String sTest = "@keyframes identifier { 0% { unexpected::; } 30% { top: 50px; }   } span {color:blue;}";
    final CascadingStyleSheet aCSS = _parse(sTest, true);
    assertNotNull(aCSS);
    if (true)
        _print(aCSS);
    assertEquals(1, aCSS.getKeyframesRuleCount());
    assertEquals(2, aCSS.getKeyframesRuleAtIndex(0).getBlockCount());
    assertEquals(1, aCSS.getStyleRuleCount());
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) Test(org.junit.Test)

Example 44 with CascadingStyleSheet

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

the class Issue3Test method testErrorInKeyframeRule3.

@Test
public void testErrorInKeyframeRule3() {
    final String sTest = "body {background:red;}\n" + "@keyframes identifier { .class{color:red;.class{color:green} } \n" + "/* No  matching closing bracket: the block is not closed and all the following rules are ignored. */\n" + "/* Add the \"}\" before the following rule to close the block and enable the rule */\n" + "body {background:green;}";
    final CascadingStyleSheet aCSS = _parse(sTest, true);
    assertNotNull(aCSS);
    if (true)
        _print(aCSS);
    assertEquals(1, aCSS.getRuleCount());
    assertEquals(0, aCSS.getKeyframesRuleCount());
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) Test(org.junit.Test)

Example 45 with CascadingStyleSheet

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

the class MediaQueryTools method getWrappedInMediaQuery.

/**
 * Get the CSS wrapped in the specified media query. Note: all existing rule
 * objects are reused, so modifying them also modifies the original CSS!
 *
 * @param aCSS
 *        The CSS to be wrapped. May not be <code>null</code>.
 * @param aMediaQueries
 *        The media queries to use. May neither be <code>null</code> nor empty
 *        nor may it contain <code>null</code> elements.
 * @param bAllowNestedMediaQueries
 *        if <code>true</code> nested media queries are allowed,
 *        <code>false</code> if they are prohibited.
 * @return <code>null</code> if out CSS cannot be wrapped, the newly created
 *         {@link CascadingStyleSheet} object otherwise.
 */
@Nullable
public static CascadingStyleSheet getWrappedInMediaQuery(@Nonnull final CascadingStyleSheet aCSS, @Nonnull @Nonempty final Iterable<? extends CSSMediaQuery> aMediaQueries, final boolean bAllowNestedMediaQueries) {
    ValueEnforcer.notNull(aCSS, "CSS");
    ValueEnforcer.notEmpty(aMediaQueries, "MediaQueries");
    if (!canWrapInMediaQuery(aCSS, bAllowNestedMediaQueries))
        return null;
    final CascadingStyleSheet ret = new CascadingStyleSheet();
    // Copy all import rules
    for (final CSSImportRule aImportRule : aCSS.getAllImportRules()) {
        if (aImportRule.hasMediaQueries()) {
            // import rule already has a media query - do not alter
            ret.addImportRule(aImportRule);
        } else {
            // Create a new rule and add the passed media queries
            final CSSImportRule aNewImportRule = new CSSImportRule(aImportRule.getLocation());
            for (final CSSMediaQuery aMediaQuery : aMediaQueries) aNewImportRule.addMediaQuery(aMediaQuery);
            ret.addImportRule(aNewImportRule);
        }
    }
    // Copy all namespace rules
    for (final CSSNamespaceRule aNamespaceRule : aCSS.getAllNamespaceRules()) ret.addNamespaceRule(aNamespaceRule);
    // Create a single top-level media rule ...
    // into this media rule
    final CSSMediaRule aNewMediaRule = new CSSMediaRule();
    for (final CSSMediaQuery aMediaQuery : aMediaQueries) aNewMediaRule.addMediaQuery(aMediaQuery);
    // ... and add the existing top-level rules into this media rule
    for (final ICSSTopLevelRule aRule : aCSS.getAllRules()) aNewMediaRule.addRule(aRule);
    // Finally add the resulting media rule into the new CSS
    ret.addRule(aNewMediaRule);
    return ret;
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSMediaQuery(com.helger.css.decl.CSSMediaQuery) ICSSTopLevelRule(com.helger.css.decl.ICSSTopLevelRule) CSSImportRule(com.helger.css.decl.CSSImportRule) CSSMediaRule(com.helger.css.decl.CSSMediaRule) CSSNamespaceRule(com.helger.css.decl.CSSNamespaceRule) Nullable(javax.annotation.Nullable)

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