private void translateUrls(AncestorChain<? extends CssTree> t) {
t.node.visitPreOrder(new ParseTreeNodeVisitor() {
public boolean visit(ParseTreeNode node) {
if (node instanceof CssTree.Term
&& CssPropertyPartType.URI == propertyPartType(node)) {
CssTree term = (CssTree.Term) node;
CssTree.CssLiteral content =
(CssTree.CssLiteral) term.children().get(0);
if (content instanceof CssTree.Substitution) {
return true; // Handled by later pass.
}
Name propertyPart = propertyPart(node);
String uriStr = content.getValue();
try {
URI baseUri = content.getFilePosition().source().getUri();
URI relUri = new URI(uriStr);
URI uri = baseUri.resolve(relUri);
// Rewrite the URI.
// TODO(mikesamuel): for content: and other URI types, use
// mime-type of text/*.
ExternalReference ref = new ExternalReference(
uri, baseUri, relUri, content.getFilePosition());
CssTree.UriLiteral replacement;
if (uriPolicy != null) {
String rewrittenUri = UriPolicyNanny.apply(
uriPolicy,
ref, UriEffect.SAME_DOCUMENT, LoaderType.SANDBOXED,
Collections.singletonMap(
UriPolicyHintKey.CSS_PROP.key, propertyPart));
replacement = new SafeUriLiteral(
content.getFilePosition(), URI.create(rewrittenUri));
} else {
replacement = new UnsafeUriLiteral(
content.getFilePosition(), uri);
}
replacement.getAttributes().putAll(content.getAttributes());
term.replaceChild(replacement, content);
} catch (URISyntaxException ex) {
// Should've been checked in removeUnsafeConstructs.
throw new SomethingWidgyHappenedError(ex);
}
}