Search in sources :

Example 1 with CSSNamespaceRule

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

Example 2 with CSSNamespaceRule

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

the class CSSWriter method writeCSS.

/**
 * Write the CSS content to the passed writer. No specific charset is used.
 *
 * @param aCSS
 *        The CSS to write. May not be <code>null</code>.
 * @param aWriter
 *        The write to write the text to. May not be <code>null</code>. Is
 *        automatically closed after the writing!
 * @throws IOException
 *         In case writing fails.
 * @throws IllegalStateException
 *         In case some elements cannot be written in the version supplied in
 *         the constructor.
 * @see #getCSSAsString(CascadingStyleSheet)
 */
public void writeCSS(@Nonnull final CascadingStyleSheet aCSS, @Nonnull @WillClose final Writer aWriter) throws IOException {
    ValueEnforcer.notNull(aCSS, "CSS");
    ValueEnforcer.notNull(aWriter, "Writer");
    try {
        final boolean bOptimizedOutput = m_aSettings.isOptimizedOutput();
        final String sNewLineString = m_aSettings.getNewLineString();
        // Write file header
        if (m_bWriteHeaderText && StringHelper.hasText(m_sHeaderText)) {
            aWriter.write("/*");
            aWriter.write(sNewLineString);
            for (final String sLine : StringHelper.getExploded("\n", m_sHeaderText)) {
                aWriter.write(" * " + sLine);
                aWriter.write(sNewLineString);
            }
            aWriter.write(" */");
            aWriter.write(sNewLineString);
        }
        // Charset? Must be the first element before the import
        if (StringHelper.hasText(m_sContentCharset)) {
            aWriter.write("@charset \"" + m_sContentCharset + "\";");
            if (!bOptimizedOutput)
                aWriter.write(sNewLineString);
        }
        // Import rules
        int nRulesEmitted = 0;
        final ICommonsList<CSSImportRule> aImportRules = aCSS.getAllImportRules();
        if (aImportRules.isNotEmpty())
            for (final CSSImportRule aImportRule : aImportRules) {
                aWriter.write(aImportRule.getAsCSSString(m_aSettings));
                ++nRulesEmitted;
            }
        // Namespace rules
        final ICommonsList<CSSNamespaceRule> aNamespaceRules = aCSS.getAllNamespaceRules();
        if (aNamespaceRules.isNotEmpty())
            for (final CSSNamespaceRule aNamespaceRule : aNamespaceRules) {
                aWriter.write(aNamespaceRule.getAsCSSString(m_aSettings));
                ++nRulesEmitted;
            }
        // Main CSS rules
        for (final ICSSTopLevelRule aRule : aCSS.getAllRules()) {
            final String sRuleCSS = aRule.getAsCSSString(m_aSettings);
            if (StringHelper.hasText(sRuleCSS)) {
                if (!bOptimizedOutput && nRulesEmitted > 0)
                    aWriter.write(sNewLineString);
                aWriter.write(sRuleCSS);
                ++nRulesEmitted;
            }
        }
        // Write file footer
        if (m_bWriteFooterText && StringHelper.hasText(m_sFooterText)) {
            aWriter.write("/*");
            aWriter.write(sNewLineString);
            for (final String sLine : StringHelper.getExploded('\n', m_sFooterText)) {
                aWriter.write(" * " + sLine);
                aWriter.write(sNewLineString);
            }
            aWriter.write(" */");
            aWriter.write(sNewLineString);
        }
    } finally {
        StreamHelper.close(aWriter);
    }
}
Also used : ICSSTopLevelRule(com.helger.css.decl.ICSSTopLevelRule) CSSImportRule(com.helger.css.decl.CSSImportRule) CSSNamespaceRule(com.helger.css.decl.CSSNamespaceRule)

Example 3 with CSSNamespaceRule

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

the class CSSVisitor method visitCSS.

/**
 * Visit all CSS elements in the order of their declaration. import rules come
 * first, namespace rules come next and all other top-level rules in the order
 * of their declaration.
 *
 * @param aCSS
 *        The CSS to visit. May not be <code>null</code>.
 * @param aVisitor
 *        The callback to be invoked for each element found. May not be
 *        <code>null</code>.
 */
public static void visitCSS(@Nonnull final CascadingStyleSheet aCSS, @Nonnull final ICSSVisitor aVisitor) {
    ValueEnforcer.notNull(aCSS, "CSS");
    ValueEnforcer.notNull(aVisitor, "Visitor");
    aVisitor.begin();
    try {
        // for all imports
        for (final CSSImportRule aImportRule : aCSS.getAllImportRules()) visitImportRule(aImportRule, aVisitor);
        // for all namespaces
        for (final CSSNamespaceRule aNamespaceRule : aCSS.getAllNamespaceRules()) visitNamespaceRule(aNamespaceRule, aVisitor);
        // for all other top level rules
        for (final ICSSTopLevelRule aTopLevelRule : aCSS.getAllRules()) visitTopLevelRule(aTopLevelRule, aVisitor);
    } finally {
        aVisitor.end();
    }
}
Also used : ICSSTopLevelRule(com.helger.css.decl.ICSSTopLevelRule) CSSImportRule(com.helger.css.decl.CSSImportRule) CSSNamespaceRule(com.helger.css.decl.CSSNamespaceRule)

Aggregations

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