private void runTest(String css, String golden, boolean allowSubstitutions)
throws Exception {
mq.getMessages().clear();
mc.relevantKeys = Collections.singleton(CssValidator.INVALID);
CssTree t = css(fromString(css), allowSubstitutions);
String msg;
{
StringBuilder msgBuf = new StringBuilder();
t.formatTree(mc, 0, msgBuf);
msg = msgBuf.toString();
}
CssSchema cssSchema = CssSchema.getDefaultCss21Schema(mq);
new CssValidator(cssSchema, HtmlSchema.getDefault(mq), mq)
.validateCss(AncestorChain.instance(t));
new CssRewriter(
new UriPolicy() {
public String rewriteUri(
ExternalReference ref, UriEffect effect, LoaderType loader,
Map<String, ?> hints) {
URI uri = ref.getUri();
if ("http".equals(uri.getScheme()) // Used by CajaTestCase
&& "example.org".equals(uri.getHost())
&& uri.getPath() != null
&& uri.getPath().startsWith("/")) {
try {
return new URI(null, null, "/foo" + uri.getPath(),
uri.getQuery(), uri.getFragment())
.toString();
} catch (URISyntaxException ex) {
ex.printStackTrace();
return null;
}
} else if ("whitelisted-host.com".equals(uri.getHost())) {
return uri.toString();
} else {
return null;
}
}
},
cssSchema, mq)
.rewrite(AncestorChain.instance(t));
{
StringBuilder msgBuf = new StringBuilder();
t.formatTree(mc, 0, msgBuf);
msg += "\n ->\n" + msgBuf.toString();
}
assertEquals(msg, golden, render(t));
}