|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.google.caja.lang.css.CssPropertyPatterns
public class CssPropertyPatterns
Operates on CSS property signatures to come up with a simple regular expression that validates values where possible.
This class produces a javascript file like
var css = { "background-image": /url("[^\"\\\(\)]+")\s+/i, clear: /(?:none|left|right|both|inherit)\s+/i, color: /(?:blue|red|green|fuschia)\s+/i };
The css
map does not contain every property in the given
CssSchema
since some cannot be matched efficiently by values.
See comments on [ a || b ]*
constructs inline below.
Nor does it contain every option matched by the server side CSS processor. Specifically, it does not currently match
rect(...)
and rgb(...)
unreserved-word
symbol used for font-families.
Font families need to be quoted, e.g. "Helvetica"
.
The server side rewriter takes adjacent unreserved words and quotes
them, but the goal of this pattern is to allow client-side validation,
not rewriting.
background-image
does not match "http://foo/myimage.com"
.
url(...)
syntax is allowed, but only containing double-quoted
strings. E.g. url(foo.gif)
is not allowed though
url("foo.gif")
is.
The patterns in the example above all end in \s+
. This simplifies
a lot of the patterns since a signature like foo*
strictly translates
to the regular expression /(foo(\s+foo)*)?/i
. Even if we repeated
subexpressions, we would run into problems with the signature
a [b | c? d]? e
which could translates to a regular expression but
only with non-local handling of whitespace around sub-expressions that can
be empty.
Instead, we require every literal to be followed by one or more spaces. We can then match against CSS padded with spaces, as in
var isValid = css[cssPropertyName].test(cssText + ' ');
This class examines a schema and builds a list of all allowed CSS properties.
It then tries to convert each property's signature to a regular expression
pattern.
It may fail for some patterns, especially the aggregate ones like
background
which combine
background-image
, background-style
, etc.
Next it optimizes the patterns it found. This includes flattening
concatenation and union operators, and moving the \s+
out of unions.
Optimizing /((blue\s+|red\s+|green\s+)|inherit\s+)/
might yield the
simpler expression /(blue|red|green|inherit)\s+/
.
Once it has a mapping of property names to regular expressions it builds a
constant pool by hashing on regular expression text.
This allows properties with identical patterns such as border-top
and
border-bottom
to share an instance.
Finally it builds a javascript parse tree that assigns the css
namespace to an object whose keys are CSS property names, and whose
values are regular expressions.
Some of the regular expressions do match URLs. If valid css text contains the string 'uri' case-insensitively, then a client may need to extract and rewrite URLs. Since all strings are double quoted, this should be doable without lexing CSS.
Nested Class Summary | |
---|---|
static class |
CssPropertyPatterns.Builder
|
private static class |
CssPropertyPatterns.JSREBuilder
|
Field Summary | |
---|---|
private static java.util.Map<java.lang.String,JSRE> |
BUILTINS
|
private static Name |
COLOR
|
static java.util.Set<Name> |
HISTORY_INSENSITIVE_STYLE_WHITELIST
Set of properties accessible on computed style of an anchor (<A>) element or some element nested within an anchor. |
private static JSRE |
OPT_SPACES
|
private Bag<java.lang.String> |
refsUsed
|
private CssSchema |
schema
|
private static JSRE |
SPACES
|
private static Name |
STANDARD_COLOR
|
Constructor Summary | |
---|---|
CssPropertyPatterns(CssSchema schema)
|
Method Summary | |
---|---|
private JSRE |
builtinToPattern(Name name)
|
private CssPropertyPatterns.JSREBuilder |
callToPattern(boolean identBefore,
CssPropertySignature.CallSignature sig)
|
java.util.regex.Pattern |
cssPropertyToJavaRegex(CssPropertySignature sig)
|
private JSRE |
cssPropertyToJSRE(CssPropertySignature sig)
|
java.lang.String |
cssPropertyToPattern(CssPropertySignature sig)
Generates a regular expression for the given signature if a simple regular expression exists. |
private CssPropertyPatterns.JSREBuilder |
exclusiveToPattern(boolean identBefore,
CssPropertySignature sig)
|
static void |
generatePatterns(CssSchema schema,
java.lang.Appendable out)
|
private static boolean |
isIdentChar(char ch)
|
private static CssPropertyPatterns.JSREBuilder |
litToPattern(boolean identBefore,
CssPropertySignature.LiteralSignature lit)
|
static void |
main(java.lang.String[] args)
|
private static Expression |
makeRegexp(java.util.Map<java.lang.String,java.lang.Integer> commonSubstrings,
java.lang.String regex)
|
private static void |
makeRegexpOnto(java.util.List<Pair<java.lang.String,Expression>> strs,
java.lang.String pattern,
int index,
java.util.List<Expression> parts)
|
(package private) static java.lang.String |
propertyNameToDom2Property(Name cssPropertyName)
Converts a css property name to a javascript identifier, e.g. |
private CssPropertyPatterns.JSREBuilder |
refToPattern(boolean identBefore,
CssPropertySignature.PropertyRefSignature sig)
|
private CssPropertyPatterns.JSREBuilder |
repToPattern(boolean identBefore,
CssPropertySignature.RepeatedSignature sig)
|
private CssPropertyPatterns.JSREBuilder |
seriesToPattern(boolean identBefore,
CssPropertySignature.SeriesSignature sig)
|
private CssPropertyPatterns.JSREBuilder |
setToPattern(boolean identBefore,
CssPropertySignature sig)
|
private CssPropertyPatterns.JSREBuilder |
sigToPattern(boolean identBefore,
CssPropertySignature sig)
|
private CssPropertyPatterns.JSREBuilder |
symbolToPattern(boolean identBefore,
CssPropertySignature.SymbolSignature sig)
|
private static JSRE |
withoutSpacesOrZero(JSRE p)
Spaces and zero tend to get moved/merged frequently during regex optimization so don't consider them when doing common substring matching. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private final CssSchema schema
public static java.util.Set<Name> HISTORY_INSENSITIVE_STYLE_WHITELIST
private static final JSRE SPACES
private static final JSRE OPT_SPACES
private final Bag<java.lang.String> refsUsed
private static final Name COLOR
private static final Name STANDARD_COLOR
private static final java.util.Map<java.lang.String,JSRE> BUILTINS
Constructor Detail |
---|
public CssPropertyPatterns(CssSchema schema)
Method Detail |
---|
public java.lang.String cssPropertyToPattern(CssPropertySignature sig)
public java.util.regex.Pattern cssPropertyToJavaRegex(CssPropertySignature sig)
private JSRE cssPropertyToJSRE(CssPropertySignature sig)
private CssPropertyPatterns.JSREBuilder sigToPattern(boolean identBefore, CssPropertySignature sig)
private static CssPropertyPatterns.JSREBuilder litToPattern(boolean identBefore, CssPropertySignature.LiteralSignature lit)
private static boolean isIdentChar(char ch)
private CssPropertyPatterns.JSREBuilder repToPattern(boolean identBefore, CssPropertySignature.RepeatedSignature sig)
private CssPropertyPatterns.JSREBuilder refToPattern(boolean identBefore, CssPropertySignature.PropertyRefSignature sig)
private CssPropertyPatterns.JSREBuilder seriesToPattern(boolean identBefore, CssPropertySignature.SeriesSignature sig)
private CssPropertyPatterns.JSREBuilder symbolToPattern(boolean identBefore, CssPropertySignature.SymbolSignature sig)
private CssPropertyPatterns.JSREBuilder setToPattern(boolean identBefore, CssPropertySignature sig)
private CssPropertyPatterns.JSREBuilder callToPattern(boolean identBefore, CssPropertySignature.CallSignature sig)
private CssPropertyPatterns.JSREBuilder exclusiveToPattern(boolean identBefore, CssPropertySignature sig)
private JSRE builtinToPattern(Name name)
public static void generatePatterns(CssSchema schema, java.lang.Appendable out) throws java.io.IOException
java.io.IOException
private static Expression makeRegexp(java.util.Map<java.lang.String,java.lang.Integer> commonSubstrings, java.lang.String regex)
private static void makeRegexpOnto(java.util.List<Pair<java.lang.String,Expression>> strs, java.lang.String pattern, int index, java.util.List<Expression> parts)
private static JSRE withoutSpacesOrZero(JSRE p)
static java.lang.String propertyNameToDom2Property(Name cssPropertyName)
background-color
=> backgroundColor
.
public static void main(java.lang.String[] args) throws java.io.IOException
java.io.IOException
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |