Skip to content

Commit

Permalink
Unified quote handling; #72
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Jan 12, 2022
1 parent 9262ce1 commit ad3049e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
5 changes: 5 additions & 0 deletions ph-css/src/main/java/com/helger/css/CCSS.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public final class CCSS
/** The "!important" suffix for property values */
public static final String IMPORTANT_SUFFIX = " !important";

/** Single quote for Strings */
public static final char SINGLE_QUOTE = '\'';
/** Double quote for Strings */
public static final char DOUBLE_QUOTE = '"';

/**
* The maximum fraction digits to be considered when printing CSS decimal
* values.
Expand Down
3 changes: 2 additions & 1 deletion ph-css/src/main/java/com/helger/css/decl/CSSExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.helger.commons.state.EChange;
import com.helger.commons.string.StringHelper;
import com.helger.commons.string.ToStringGenerator;
import com.helger.css.CCSS;
import com.helger.css.CSSSourceLocation;
import com.helger.css.ICSSSourceLocationAware;
import com.helger.css.ICSSWriteable;
Expand Down Expand Up @@ -244,7 +245,7 @@ public static String getQuotedStringValue (@Nonnull final String sValue)
ValueEnforcer.notNull (sValue, "Value");
if (sValue.length () == 0)
return "\"\"";
return '"' + StringHelper.replaceAll (sValue, "\"", "\\\"") + '"';
return CCSS.DOUBLE_QUOTE + StringHelper.replaceAll (sValue, "\"", "\\\"") + CCSS.DOUBLE_QUOTE;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.helger.commons.hashcode.HashCodeGenerator;
import com.helger.commons.string.StringHelper;
import com.helger.commons.string.ToStringGenerator;
import com.helger.css.CCSS;
import com.helger.css.CSSSourceLocation;
import com.helger.css.ICSSSourceLocationAware;
import com.helger.css.ICSSWriterSettings;
Expand Down Expand Up @@ -68,7 +69,7 @@ public CSSExpressionMemberTermSimple (@Nonnull @Nonempty final String sValue)
}

@Nonnull
public CSSExpressionMemberTermSimple setValue (@Nonnull @Nonempty final String sValue)
public final CSSExpressionMemberTermSimple setValue (@Nonnull @Nonempty final String sValue)
{
ValueEnforcer.notEmpty (sValue, "Value");
m_sValue = sValue;
Expand Down Expand Up @@ -100,12 +101,12 @@ public String getOptimizedValue ()

/**
* @return <code>true</code> if this value is a string literal.
* Otherwise it is considered to be an identifier.
* <code>false</code> it is considered to be an identifier.
*/
public boolean isStringLiteral ()
{
return (StringHelper.startsWith(m_sValue, '\'') && StringHelper.endsWith(m_sValue, '\'')) ||
(StringHelper.startsWith(m_sValue, '\"') && StringHelper.endsWith(m_sValue, '\"'));
final char cFirst = m_sValue.charAt (0);
return (cFirst == CCSS.DOUBLE_QUOTE || cFirst == CCSS.SINGLE_QUOTE) && cFirst == StringHelper.getLastChar (m_sValue);
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.helger.commons.annotation.PresentForCodeCoverage;
import com.helger.commons.regex.RegExCache;
import com.helger.commons.string.StringHelper;
import com.helger.css.CCSS;
import com.helger.css.propertyvalue.CCSSValue;

/**
Expand Down Expand Up @@ -72,7 +73,7 @@ public static String extractStringValue (@Nullable final String sStr)
return sStr;

final char cFirst = sStr.charAt (0);
if ((cFirst == '"' || cFirst == '\'') && StringHelper.getLastChar (sStr) == cFirst)
if ((cFirst == CCSS.DOUBLE_QUOTE || cFirst == CCSS.SINGLE_QUOTE) && StringHelper.getLastChar (sStr) == cFirst)
{
// Remove quotes around the string
return _trimBy (sStr, 1, 1);
Expand Down
9 changes: 5 additions & 4 deletions ph-css/src/main/java/com/helger/css/utils/CSSURLHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.helger.commons.annotation.PresentForCodeCoverage;
import com.helger.commons.string.StringHelper;
import com.helger.commons.url.ISimpleURL;
import com.helger.css.CCSS;
import com.helger.css.parser.CSSParseHelper;
import com.helger.css.propertyvalue.CCSSValue;

Expand Down Expand Up @@ -152,7 +153,7 @@ public static boolean isCSSURLRequiringQuotes (@Nonnull final String sURL)
* @param sURL
* The URL to be escaped. May not be <code>null</code>.
* @param cQuoteChar
* The quote char that is used. Either '\'' or '"'
* The quote char that is used. Either single quote or double quote.
* @return The escaped string. Never <code>null</code>.
*/
@Nonnull
Expand Down Expand Up @@ -200,9 +201,9 @@ public static String getAsCSSURL (@Nonnull final String sURL, final boolean bFor
{
// Determine the best quote char to use - default to '\'' for backwards
// compatibility
final int nIndexSingleQuote = sURL.indexOf ('\'');
final int nIndexDoubleQuote = sURL.indexOf ('"');
final char cQuote = nIndexSingleQuote >= 0 && nIndexDoubleQuote < 0 ? '"' : '\'';
final int nIndexSingleQuote = sURL.indexOf (CCSS.SINGLE_QUOTE);
final int nIndexDoubleQuote = sURL.indexOf (CCSS.DOUBLE_QUOTE);
final char cQuote = nIndexSingleQuote >= 0 && nIndexDoubleQuote < 0 ? CCSS.DOUBLE_QUOTE : CCSS.SINGLE_QUOTE;
// Append the quoted and escaped URL
aSB.append (cQuote).append (getEscapedCSSURL (sURL, cQuote)).append (cQuote);
}
Expand Down

1 comment on commit ad3049e

@phax
Copy link
Owner Author

@phax phax commented on ad3049e Jan 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant #75

Please sign in to comment.