Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ target/
zz

*.iml
.idea
9 changes: 9 additions & 0 deletions ph-css/src/main/java/com/helger/css/ICSSWriteable.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public interface ICSSWriteable
* Get the contents of this object as a serialized CSS string for writing to
* an output using the default writer settings.
*
* <p>The general contract is that this method writes only the content of this object, but not any surrounding context.
* In particular, this method does not add any leading or trailing space or newlines.
*
* @return The content of this object as CSS string. Never <code>null</code>.
* @see #getAsCSSString(ICSSWriterSettings, int)
* @since 6.0.0
Expand All @@ -46,6 +49,9 @@ default String getAsCSSString ()
* Get the contents of this object as a serialized CSS string for writing to
* an output.
*
* <p>The general contract is that this method writes only the content of this object, but not any surrounding context.
* In particular, this method does not add any leading or trailing space or newlines.
*
* @param aSettings
* The settings to be used to format the output. May not be
* <code>null</code>.
Expand All @@ -63,6 +69,9 @@ default String getAsCSSString (@NonNull final ICSSWriterSettings aSettings)
* Get the contents of this object as a serialized CSS string for writing to
* an output.
*
* <p>The general contract is that this method writes only the content of this object, but not any surrounding context.
* In particular, this method does not add any leading or trailing space or newlines.
*
* @param aSettings
* The settings to be used to format the output. May not be
* <code>null</code>.
Expand Down
43 changes: 35 additions & 8 deletions ph-css/src/main/java/com/helger/css/ICSSWriterSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@
*/
package com.helger.css;

import com.helger.css.decl.CSSFontFaceRule;
import com.helger.css.decl.CSSKeyframesRule;
import com.helger.css.decl.CSSLayerRule;
import com.helger.css.decl.CSSMediaRule;
import com.helger.css.decl.CSSNamespaceRule;
import com.helger.css.decl.CSSNestedDeclarations;
import com.helger.css.decl.CSSPageRule;
import com.helger.css.decl.CSSPropertyRule;
import com.helger.css.decl.CSSSupportsRule;
import com.helger.css.decl.CSSUnknownRule;
import com.helger.css.decl.CSSViewportRule;
import org.jspecify.annotations.NonNull;

import com.helger.annotation.Nonempty;
Expand Down Expand Up @@ -73,42 +84,58 @@ public interface ICSSWriterSettings
boolean isQuoteURLs ();

/**
* @return <code>true</code> if @namespace rules should be written, <code>false</code> if not
* @return <code>true</code> if {@link CSSNamespaceRule @namespace rules} should be written, <code>false</code> if not
*/
boolean isWriteNamespaceRules ();

/**
* @return <code>true</code> if @font-face rules should be written, <code>false</code> if not
* @return <code>true</code> if {@link CSSNestedDeclarations nested declarations} should be written,
* <code>false</code> if not
*/
boolean isWriteNestedDeclarations();

/**
* @return <code>true</code> if {@link CSSFontFaceRule @font-face rules} should be written, <code>false</code> if not
*/
boolean isWriteFontFaceRules ();

/**
* @return <code>true</code> if @keyframes rules should be written, <code>false</code> if not
* @return <code>true</code> if {@link CSSKeyframesRule @keyframes rules} should be written, <code>false</code> if not
*/
boolean isWriteKeyframesRules ();

/**
* @return <code>true</code> if @media rules should be written, <code>false</code> if not
* @return <code>true</code> if {@link CSSLayerRule @layer rules} should be written, <code>false</code> if not
*/
boolean isWriteLayerRules ();

/**
* @return <code>true</code> if {@link CSSMediaRule @media rules} should be written, <code>false</code> if not
*/
boolean isWriteMediaRules ();

/**
* @return <code>true</code> if @page rules should be written, <code>false</code> if not
* @return <code>true</code> if {@link CSSPageRule @page rules} should be written, <code>false</code> if not
*/
boolean isWritePageRules ();

/**
* @return <code>true</code> if @viewport rules should be written, <code>false</code> if not
* @return <code>true</code> if {@link CSSPropertyRule @property rules} should be written, <code>false</code> if not
*/
boolean isWritePropertyRules ();

/**
* @return <code>true</code> if {@link CSSViewportRule @viewport rules} should be written, <code>false</code> if not
*/
boolean isWriteViewportRules ();

/**
* @return <code>true</code> if @supports rules should be written, <code>false</code> if not
* @return <code>true</code> if {@link CSSSupportsRule @supports rules} should be written, <code>false</code> if not
*/
boolean isWriteSupportsRules ();

/**
* @return <code>true</code> if unknown @ rules should be written, <code>false</code> if not
* @return <code>true</code> if {@link CSSUnknownRule unknown @ rules} should be written, <code>false</code> if not
*/
boolean isWriteUnknownRules ();
}
104 changes: 104 additions & 0 deletions ph-css/src/main/java/com/helger/css/decl/AbstractHasTopLevelRules.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,110 @@ public ICommonsList <ICSSTopLevelRule> getAllRules (@NonNull final Predicate <?
return m_aRules.getAll (aFilter);
}

/**
* Check if at least one of the top-level rules is a layer rule (implementing
* {@link CSSLayerRule}).
*
* @return <code>true</code> if at least one layer rule is contained, <code>false</code>
* otherwise.
* @since 8.2.0
*/
public boolean hasLayerRules ()
{
return m_aRules.containsAny (CSSLayerRule.class::isInstance);
}

/**
* Get the number of top-level rules that are layer rules (implementing {@link CSSLayerRule}).
*
* @return The number of contained layer rules. Always &ge; 0.
* @since 8.2.0
*/
@Nonnegative
public int getLayerRuleCount ()
{
return m_aRules.getCount (CSSLayerRule.class::isInstance);
}

/**
* Get the layer rule at the specified index.
*
* @param nIndex
* The index to be resolved. Should be &ge; 0 and &lt; {@link #getStyleRuleCount()}.
* @return The layer rule at the given index, or <code>null</code> if an invalid index was specified.
* @since 8.2.0
*/
@Nullable
public CSSLayerRule getLayerRuleAtIndex (@Nonnegative final int nIndex)
{
return m_aRules.getAtIndexMapped (CSSLayerRule.class::isInstance, nIndex, CSSLayerRule.class::cast);
}

/**
* Get a list of all top-level rules that are layer rules (implementing {@link CSSLayerRule}).
*
* @return A copy of all contained layer rules. Never <code>null</code>.
* @since 8.2.0
*/
@NonNull
@ReturnsMutableCopy
public ICommonsList <CSSLayerRule> getAllLayerRules ()
{
return m_aRules.getAllMapped (CSSLayerRule.class::isInstance, CSSLayerRule.class::cast);
}

/**
* Check if at least one of the top-level rules is a property rule (implementing
* {@link CSSPropertyRule}).
*
* @return <code>true</code> if at least one property rule is contained, <code>false</code>
* otherwise.
* @since 8.2.0
*/
public boolean hasPropertyRules ()
{
return m_aRules.containsAny (CSSPropertyRule.class::isInstance);
}

/**
* Get the number of top-level rules that are property rules (implementing {@link CSSPropertyRule}).
*
* @return The number of contained property rules. Always &ge; 0.
* @since 8.2.0
*/
@Nonnegative
public int getPropertyRuleCount ()
{
return m_aRules.getCount (CSSPropertyRule.class::isInstance);
}

/**
* Get the property rule at the specified index.
*
* @param nIndex
* The index to be resolved. Should be &ge; 0 and &lt; {@link #getStyleRuleCount()}.
* @return The property rule at the given index, or <code>null</code> if an invalid index was specified.
* @since 8.2.0
*/
@Nullable
public CSSPropertyRule getPropertyRuleAtIndex (@Nonnegative final int nIndex)
{
return m_aRules.getAtIndexMapped (CSSPropertyRule.class::isInstance, nIndex, CSSPropertyRule.class::cast);
}

/**
* Get a list of all top-level rules that are property rules (implementing {@link CSSPropertyRule}).
*
* @return A copy of all contained property rules. Never <code>null</code>.
* @since 8.2.0
*/
@NonNull
@ReturnsMutableCopy
public ICommonsList <CSSPropertyRule> getAllPropertyRules ()
{
return m_aRules.getAllMapped (CSSPropertyRule.class::isInstance, CSSPropertyRule.class::cast);
}

/**
* Check if at least one of the top-level rules is a style rule (implementing
* {@link CSSStyleRule}).
Expand Down
8 changes: 4 additions & 4 deletions ph-css/src/main/java/com/helger/css/decl/CSSDeclaration.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import com.helger.css.property.ECSSProperty;

/**
* Represents a single element in a CSS style rule. (eg. <code>color:red;</code>
* Represents a single element in a CSS style rule. (e.g. <code>color:red;</code>
* or <code>background:uri(a.gif) !important;</code>)<br>
* Instances of this class are mutable since 3.7.4.
*
Expand Down Expand Up @@ -99,15 +99,15 @@ public final String getProperty ()
@NonNull
private static String _unifyProperty (@NonNull final String sProperty)
{
// CSS variables are case sensitive (see issue 63)
// CSS variables are case-sensitive (see issue 63)
if (sProperty.startsWith ("--"))
return sProperty;
return sProperty.toLowerCase (Locale.ROOT);
}

/**
* Check if this declaration has the specified property. The comparison is
* case insensitive!
* case-insensitive!
*
* @param sProperty
* The property to check. May not be <code>null</code>.
Expand All @@ -123,7 +123,7 @@ public final boolean hasProperty (@NonNull final String sProperty)

/**
* Check if this declaration has the specified property. The comparison is
* case insensitive!
* case-insensitive!
*
* @param eProperty
* The property to check. May not be <code>null</code>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ public class CSSDeclarationContainer extends CSSDeclarationList
public CSSDeclarationContainer ()
{}

@NonNull
@Nonempty
public String getDeclarationsAsCSSString (@NonNull final ICSSWriterSettings aSettings, @Nonnegative final int nIndentLevel)
{
return super.getAsCSSString (aSettings, nIndentLevel);
}

@Override
@NonNull
@Nonempty
Expand All @@ -55,16 +62,18 @@ public String getAsCSSString (@NonNull final ICSSWriterSettings aSettings, @Nonn
{
// A single declaration
aSB.append (bOptimizedOutput ? "{" : " { ");
aSB.append (super.getAsCSSString (aSettings, nIndentLevel));
aSB.append (super.getAsCSSString (aSettings, nIndentLevel + 1));
aSB.append (bOptimizedOutput ? "}" : " }");
}
else
{
// More than one declaration
aSB.append (bOptimizedOutput ? "{" : " {" + aSettings.getNewLineString ());
aSB.append (super.getAsCSSString (aSettings, nIndentLevel));
if (!bOptimizedOutput)
aSB.append (aSettings.getIndent (nIndentLevel));
aSB.append (aSettings.getIndent (nIndentLevel + 1));
aSB.append (super.getAsCSSString (aSettings, nIndentLevel + 1));
if (!bOptimizedOutput)
aSB.append(aSettings.getNewLineString()).append (aSettings.getIndent (nIndentLevel));
aSB.append ('}');
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,22 @@ public CSSExpressionMemberLineNames addMember (@NonNull @Nonempty final String s
}

@NonNull
public CSSExpressionMemberLineNames addMember (@Nonnegative final int nIndex, @NonNull @Nonempty final String aMember)
public CSSExpressionMemberLineNames addMember (@Nonnegative final int nIndex, @NonNull @Nonempty final String sMember)
{
ValueEnforcer.isGE0 (nIndex, "Index");
ValueEnforcer.notNull (aMember, "Member");
ValueEnforcer.notNull (sMember, "Member");

if (nIndex >= getMemberCount ())
m_aMembers.add (aMember);
m_aMembers.add (sMember);
else
m_aMembers.add (nIndex, aMember);
m_aMembers.add (nIndex, sMember);
return this;
}

@NonNull
public EChange removeMember (@NonNull final String aMember)
public EChange removeMember (@NonNull final String sMember)
{
return m_aMembers.removeObject (aMember);
return m_aMembers.removeObject (sMember);
}

@NonNull
Expand Down
22 changes: 10 additions & 12 deletions ph-css/src/main/java/com/helger/css/decl/CSSFontFaceRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@
import com.helger.css.ICSSWriterSettings;

/**
* Represents a single <code>@font-face</code> rule.<br>
* Example:<br>
* <code>@font-face {
font-family: 'icons';
src: url(path/to/font.woff) format('woff');
unicode-range: U+E000-E005;
}</code>
* Represents a single <code>@font-face</code> rule.
*
* <p>Example:
*
* <pre>@font-face {
font-family: 'icons';
src: url(path/to/font.woff) format('woff');
unicode-range: U+E000-E005;
}</pre>
*
* @author Philip Helger
*/
Expand Down Expand Up @@ -166,11 +168,7 @@ public String getAsCSSString (@NonNull final ICSSWriterSettings aSettings, @Nonn
if (aSettings.isRemoveUnnecessaryCode () && !hasDeclarations ())
return "";

final StringBuilder aSB = new StringBuilder (m_sDeclaration);
aSB.append (m_aDeclarations.getAsCSSString (aSettings, nIndentLevel));
if (!aSettings.isOptimizedOutput ())
aSB.append (aSettings.getNewLineString ());
return aSB.toString ();
return m_sDeclaration + m_aDeclarations.getAsCSSString(aSettings, nIndentLevel);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public String getAsCSSString (@NonNull final ICSSWriterSettings aSettings, @Nonn
aSB.append (aMediaQuery.getAsCSSString (aSettings, nIndentLevel));
}
}
return aSB.append (';').append (aSettings.getNewLineString ()).toString ();
return aSB.append (';').toString ();
}

@Nullable
Expand Down
Loading