use of com.helger.css.decl.ICSSTopLevelRule 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;
}
use of com.helger.css.decl.ICSSTopLevelRule in project ph-css by phax.
the class ParserCSS30Test method test2.
@Test
public void test2() throws ParseException {
final ParserCSS30TokenManager aTokenHdl = new ParserCSS30TokenManager(new CSSCharStream(new NonBlockingStringReader(CSS2)));
aTokenHdl.setDebugStream(System.out);
final ParserCSS30 aParser = new ParserCSS30(aTokenHdl);
aParser.disable_tracing();
final CSSNode aNode = aParser.styleSheet();
assertNotNull(aNode);
final CascadingStyleSheet aCSS = CSSHandler.readCascadingStyleSheetFromNode(ECSSVersion.CSS30, aNode, CSSReader.getDefaultInterpretErrorHandler());
assertNotNull(aCSS);
for (final ICSSTopLevelRule aTopLevelRule : aCSS.getAllFontFaceRules()) assertTrue(aCSS.removeRule(aTopLevelRule).isChanged());
}
use of com.helger.css.decl.ICSSTopLevelRule 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);
}
}
use of com.helger.css.decl.ICSSTopLevelRule 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();
}
}
Aggregations