Search in sources :

Example 1 with CSSMediaRule

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

the class MediaQueryTools method parseToMediaQuery.

/**
 * Utility method to convert a media query string to a structured list of
 * {@link CSSMediaQuery} objects.
 *
 * @param sMediaQuery
 *        The media query string to parse. May be <code>null</code>.
 * @param eVersion
 *        The CSS version to use. May not be <code>null</code>.
 * @return <code>null</code> if the passed media query is <code>null</code> or
 *         empty or not parsable.
 */
@Nullable
public static ICommonsList<CSSMediaQuery> parseToMediaQuery(@Nullable final String sMediaQuery, @Nonnull final ECSSVersion eVersion) {
    if (StringHelper.hasNoText(sMediaQuery))
        return null;
    final String sCSS = "@media " + sMediaQuery + " {}";
    final CascadingStyleSheet aCSS = CSSReader.readFromString(sCSS, eVersion);
    if (aCSS == null)
        return null;
    final CSSMediaRule aMediaRule = aCSS.getAllMediaRules().get(0);
    return aMediaRule.getAllMediaQueries();
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSMediaRule(com.helger.css.decl.CSSMediaRule) Nullable(javax.annotation.Nullable)

Example 2 with CSSMediaRule

use of com.helger.css.decl.CSSMediaRule 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

CSSMediaRule (com.helger.css.decl.CSSMediaRule)2 CascadingStyleSheet (com.helger.css.decl.CascadingStyleSheet)2 Nullable (javax.annotation.Nullable)2 CSSImportRule (com.helger.css.decl.CSSImportRule)1 CSSMediaQuery (com.helger.css.decl.CSSMediaQuery)1 CSSNamespaceRule (com.helger.css.decl.CSSNamespaceRule)1 ICSSTopLevelRule (com.helger.css.decl.ICSSTopLevelRule)1