Class CssStringNode
- All Implemented Interfaces:
Locatable
TODO(user): refactor the encoding/decoding logic out of this class, thereby restoring the architectural integrity of the CssNode type hierarchy; all these classes should be value types with no elaborate behaviors.
This node represents both a CSS fragment and the abstract value
represented by that concrete syntax. For example, the following
three declarations mean precisely the same thing in CSS:
.warning { content-before: 'warning:'; }
.warning { content-before: "warning:"; }
.warning { content-before: 'war\06e ing:"; }
Some clients care about concrete CSS syntax. For high-fidelity
roundtrip CSS processing it is necessary to preserve the original
author's choice of quote character. On the other hand, some clients
care about abstract values. For purposes of machine translation or
typeface resolution, we are uninterested in the differences that
distinguish the cases shown above; in these applications we would
like to deal in terms of the simple Java String warning:.
Java's Character and String classes represent
values in the UTF-16 encoding; some codepoints are represented by
surrogate pairs of Character instances. CSS escape sequences are
designed without a particular encoding in mind; there are CSS
escape sequences that correspond to a single Unicode character
and multiple Java Character instances.
Java's Character repertoire is a strict subset of the
codepoints that can be represented in CSS. When decoding CSS
escape sequences, this class substitutes the Unicode replacement
character for characters that cannot be represented in Java
Strings, as permitted by
http://www.w3.org/TR/CSS2/syndata.html#characters
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumCSS syntax permits strings to be expressed either using single-quotes or double-quotes. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final UnaryOperator<String>Represents this node's value in CSS syntax that is also safe for inclusion in HTML attribute values and element contents.static final UnaryOperator<String>Replaces characters that have no literal representation in CSS with their escape codes. -
Constructor Summary
ConstructorsConstructorDescriptionCssStringNode(CssStringNode node) Copy constructor.CssStringNode(CssStringNode.Type type, SourceCodeLocation location) Constructor of a string node.CssStringNode(CssStringNode.Type type, String value) Constructor of a string node. -
Method Summary
Modifier and TypeMethodDescriptiondeepCopy()static StringEncodes a CSS term denotingraw.Retrieves the characters that should appear between the quotes when this node is written in concrete CSS syntax.getType()setConcreteValue(String concreteValue) Specifies the characters that should appear between the quotes when this node is written as CSS, and updates this node's value accordingly.voidEstablishes a value for this node by conservatively escapingvalueand delegating tosetConcreteValue(java.lang.String)to maintain consistency between theCssValueNode.valueand theconcreteValue.toString()Use for debugging only.Generates a CSS snippet representing this node.static StringDetermines the canonical Java String representation of a value encoded in CSS syntax.Methods inherited from class com.google.common.css.compiler.ast.CssValueNode
getIsDefault, getValue, setIsDefaultMethods inherited from class com.google.common.css.compiler.ast.CssNode
ancestors, appendComment, deepCopyNodes, equals, getComments, getParent, getShouldBeFlipped, getSourceCodeLocation, getVisitController, hasComment, hashCode, inFunArgs, setComments, setShouldBeFlipped, setSourceCodeLocation
-
Field Details
-
HTML_ESCAPER
Represents this node's value in CSS syntax that is also safe for inclusion in HTML attribute values and element contents. This is a good choice when you want defense in depth against client code that fails to escape things properly. -
SHORT_ESCAPER
Replaces characters that have no literal representation in CSS with their escape codes. This implementation compromises computational efficiency in order to produce the shortest possible output for each replaced character. This is a good choice for readability.
-
-
Constructor Details
-
CssStringNode
Constructor of a string node.- Parameters:
type- CSS provides multiple syntax alternatives for strings; which was used for this term?location- The location in source code corresponding to this node
-
CssStringNode
Constructor of a string node.- Parameters:
type- CSS provides multiple syntax alternatives for strings; which was used for this term?value- the Java String representation of this string (not its concrete CSS syntax)
-
CssStringNode
Copy constructor.
-
-
Method Details
-
getType
-
setConcreteValue
Specifies the characters that should appear between the quotes when this node is written as CSS, and updates this node's value accordingly.For example, the Java method invocation:
n.setConcreteValue("hi\\\"");could result in the CSS:p { content-after: "hi\""; }or perhapsp { content-after: 'hi\"'; }, depending on theCssStringNode.Typeofnand theCssTreein which it occurs, but it would never result inp { content-after: "hi\000022"; } -
getConcreteValue
Retrieves the characters that should appear between the quotes when this node is written in concrete CSS syntax. -
setValue
Establishes a value for this node by conservatively escapingvalueand delegating tosetConcreteValue(java.lang.String)to maintain consistency between theCssValueNode.valueand theconcreteValue.This function stores a normalized representation of the given
value; if you want to work in more exact terms, trysetConcreteValue(java.lang.String).For example, the Java snippet:
n.setValue("Senator's Response")could result in the CSS snippet:p { content-before: "Senator's Response"; }orp { content-before: 'Senator\'s Response'; }orp { content-before: 'Senator\27 s Response'; }, depending on theCssStringNode.Typeofnand theCssTreein which it occurs and the choice of theCssTreeVisitorthat renders the output.Note that the
valueparameter here will normally not begin or end with a quotation mark.- Overrides:
setValuein classCssValueNode
-
deepCopy
- Specified by:
deepCopyin classCssValueNode
-
toString
Description copied from class:CssValueNodeUse for debugging only.- Overrides:
toStringin classCssValueNode- See Also:
-
unescape
Determines the canonical Java String representation of a value encoded in CSS syntax.- Parameters:
escaped- whatever lies between the quotes (excluding the quotes themselves).
-
escape
public static String escape(CssStringNode.Type type, Function<? super String, String> discretionaryEscaper, String raw) Encodes a CSS term denotingraw. In general, there are multiple representations in CSS of the same value; we allow clients to influence this choice throughdiscretionaryEscaper.- See Also:
-
toString
Generates a CSS snippet representing this node. This may differ in semantically unimportant ways from the snippet from which this node was originally parsed.You might reasonably
n.setConcreteValue(n.toString(ESC))because that will not change what you get fromn.getValue(). But it is probably an error to writen.setValue(n.toString(ESC)); you can pump the string to unbounded length by putting the latter snippet in the body of a loop.- Returns:
- a
Stringcorresponding to this node's abstract value, but suitable for inclusion in CSS. - See Also:
-