Class ParseHelper

java.lang.Object
io.sf.carte.doc.style.css.parser.ParseHelper

public class ParseHelper extends Object
Methods that are useful for CSS parsing.

Note: this is an implementation helper, most methods are not intended for usage outside of this library.

  • Field Details

  • Constructor Details

    • ParseHelper

      public ParseHelper()
  • Method Details

    • unescapeStringValue

      public static String unescapeStringValue(String value)
    • unescapeStringValue

      public static String unescapeStringValue(String value, boolean unescapeControl, boolean replaceNull) throws DOMNullCharacterException
      Changes ISO 10646 escaped character values to the actual characters, when appropriate.
      Parameters:
      value - the string value to unescape.
      unescapeControl - if false, control characters are not unescaped.
      replaceNull - if false, if NULL is found at the end of a string, it will trigger an exception instead of being replaced by the replacement character.
      Returns:
      the unescaped string.
      Throws:
      DOMNullCharacterException - if a null-character browser hack was found and replaceNull is false.
    • escape

      public static String escape(String text)
      Escape a string according to CSS syntax, preserving escaped hex characters and surrogates.
      Parameters:
      text - the text to escape.
      Returns:
      the escaped string.
    • escape

      public static String escape(String text, boolean preserveHexEscapes, boolean escapeSurrogates)
      Escape a string according to CSS syntax.
      Parameters:
      text - the text to escape.
      preserveHexEscapes - if true, escaped hex characters shall be preserved (instead of escaping its backslash).
      escapeSurrogates - if true, surrogate characters shall be escaped, otherwise preserved.
      Returns:
      the escaped string.
    • safeEscape

      public static String safeEscape(String text)
      Escape the given string according to CSS syntax.

      For convenience, if the string contains an escape sequence escaping a private or unassigned character, the corresponding backslash is not escaped.

      For additional safety, when hex-escaping a code point it will put a whitespace separator if the next character is an ASCII one, even if that character is not a valid hexadecimal digit.

      Parameters:
      text - the text to escape.
      Returns:
      the escaped text.
    • safeEscape

      public static String safeEscape(String text, boolean escapeSurrogates)
      Escape the given string according to CSS syntax.

      For convenience, if the string contains an escape sequence escaping a private or unassigned character, the corresponding backslash is not escaped.

      For additional safety, when hex-escaping a code point it will put a whitespace separator if the next character is an ASCII one, even if that character is not a valid hexadecimal digit.

      Parameters:
      text - the text to escape.
      escapeSurrogates - true if surrogates have to be escaped.
      Returns:
      the escaped text.
    • escapeString

      public static String escapeString(String text, char quoteChar)
    • escapeString

      public static String escapeString(String text, char quoteChar, boolean endOfString)
    • quote

      public static String quote(String text, char quote)
      Quote a string according to the supplied quote character.
      Parameters:
      text - the string to quote.
      quote - the character to be used as quote.
      Returns:
      the quoted string.
    • escapeCssChars

      public static CharSequence escapeCssChars(CharSequence strval)
      Escapes characters that have a special meaning for CSS, excluding whitespace (0x20) and backslash (x5c).

      Note: this is an implementation helper method, not intended for usage outside of this library. May be removed at any time without previous notice.

      Parameters:
      strval - the sequence to escape.
      Returns:
      the escaped string.
    • escapeCssCharsAndFirstChar

      public static CharSequence escapeCssCharsAndFirstChar(CharSequence strval)
      Escapes characters that have a special meaning for CSS, excluding whitespace (0x20) and backslash (x5c).

      If the first character is a number, also escapes it.

      Note: this is an implementation helper method, not intended for usage outside of this library. May be removed at any time without previous notice.

      Parameters:
      strval - the sequence to escape.
      Returns:
      the escaped string.
    • escapeAllBackslash

      public static CharSequence escapeAllBackslash(CharSequence strval)
      Escapes all the backslash characters found in the given string.
      Parameters:
      strval - the string to be escaped.
      Returns:
      the escaped string.
    • escapeBackslash

      public static CharSequence escapeBackslash(CharSequence strval)
      Escapes the backslash characters found in the given string, except those that escape hex-encoded codepoints.

      If you have to call it together with escapeCssCharsAndFirstChar(CharSequence), make sure to execute this method first.

      Parameters:
      strval - the character sequence to be escaped.
      Returns:
      the escaped string.
    • escapeControl

      public static String escapeControl(CharSequence strval)
      Escapes control characters.
      Parameters:
      strval - the string to escape.
      Returns:
      the escaped string.
    • unitFromString

      public static short unitFromString(String unit)
      Retrieves the CSS unit associated to the given unit string.
      Parameters:
      unit - the unit string (must be interned).
      Returns:
      the associated CSS unit, or CSS_OTHER if the unit is not known.
    • parseIdent

      public static String parseIdent(String ident) throws DOMException
      Parse an escaped CSS identifier.
      Parameters:
      ident - the identifier to parse.
      Returns:
      the parsed identifier.
      Throws:
      DOMException - DOMException.DOMSTRING_SIZE_ERR if the identifier is the empty string.
      DOMException.INVALID_CHARACTER_ERR if the identifier contains invalid characters.
      DOMException.SYNTAX_ERR if the identifier does not follow the syntax (e.g. only contains a hyphen-minus, or starts with a hyphen-minus and a digit).
    • equalSelectorList

      public static boolean equalSelectorList(SelectorList list1, SelectorList list2)
    • startsWithIgnoreCase

      public static boolean startsWithIgnoreCase(CharSequence seq, String lcString)
      Tests if the given CharSequence starts with the specified lower case string, but ignoring the case of the first argument.
      Parameters:
      seq - the non-null sequence to check.
      lcString - the non-null lower case prefix that should be matched.
      Returns:
      true if the character sequence represented by the second argument is a prefix of the character sequence in the first argument;
    • equalsIgnoreCase

      public static boolean equalsIgnoreCase(CharSequence seq, CharSequence lcString)
      Tests if the two given character sequences are the same, ignoring the case considerations except for the fact that the second argument must be lower case.
      Parameters:
      seq - the non-null sequence to compare to the second argument.
      lcString - the non-null lower case sequence to compare to the first argument.
      Returns:
      true if the two arguments represent an equivalent CharSequence ignoring case.
    • createPseudoElementCondition

      public static PseudoCondition createPseudoElementCondition(String pseudoElement)
      Create a single pseudo-element condition, like ::first-letter.
      Parameters:
      pseudoElement - the pseudo-element.
      Returns:
      the pseudo-element condition.
    • isTransformFunction

      public static boolean isTransformFunction(String functionName)
      For internal use by the library, may be removed in the future.

      Determine if the name is a transform function.

      Parameters:
      functionName - the function name.
      Returns:
      true if the name is a transform function.
    • matchAttrType

      public static boolean matchAttrType(String dataType, CSSValueSyntax.Category cat)
      For internal use by the library, may be removed in the future.
      Parameters:
      dataType - the attr data type.
      cat - the grammar data type category to check.
      Returns:
      true if the data type matches the syntax category.