[google-caja] r3933 committed - Thread baseUri through with HTML jobs....

0 views
Skip to first unread message

googl...@googlecode.com

unread,
Dec 29, 2009, 12:33:57 PM12/29/09
to caja....@gmail.com
Revision: 3933
Author: mikes...@gmail.com
Date: Tue Dec 29 09:33:01 2009
Log: Thread baseUri through with HTML jobs.
http://codereview.appspot.com/181068

Associate a base URI with HTML and CSS jobs so we can properly resolve URIs.

This changes the way Jobs are constructed to carry around a base URI,
attaches that info to embedded content, and threads it into the
ResolveURIStage and InlineCSSImportsStage.

R=jas...@gmail.com

http://code.google.com/p/google-caja/source/detail?r=3933

Modified:
/trunk/src/com/google/caja/opensocial/DefaultGadgetRewriter.java
/trunk/src/com/google/caja/plugin/BuildServiceImplementation.java
/trunk/src/com/google/caja/plugin/Job.java
/trunk/src/com/google/caja/plugin/Jobs.java
/trunk/src/com/google/caja/plugin/PluginCompiler.java
/trunk/src/com/google/caja/plugin/PluginCompilerMain.java
/trunk/src/com/google/caja/plugin/stages/CompileHtmlStage.java
/trunk/src/com/google/caja/plugin/stages/ConsolidateCodeStage.java
/trunk/src/com/google/caja/plugin/stages/DebuggingSymbolsStage.java
/trunk/src/com/google/caja/plugin/stages/EmbeddedContent.java
/trunk/src/com/google/caja/plugin/stages/HtmlEmbeddedContentFinder.java
/trunk/src/com/google/caja/plugin/stages/InlineCssImportsStage.java
/trunk/src/com/google/caja/plugin/stages/LegacyNamespaceFixupStage.java
/trunk/src/com/google/caja/plugin/stages/OpenTemplateStage.java
/trunk/src/com/google/caja/plugin/stages/ResolveUriStage.java
/trunk/src/com/google/caja/plugin/stages/RewriteCssStage.java
/trunk/src/com/google/caja/plugin/stages/RewriteHtmlStage.java
/trunk/src/com/google/caja/plugin/stages/SanitizeHtmlStage.java
/trunk/src/com/google/caja/plugin/stages/ValidateCssStage.java
/trunk/src/com/google/caja/plugin/stages/ValidateJavascriptStage.java
/trunk/src/com/google/caja/plugin/templates/TemplateCompiler.java
/trunk/src/com/google/caja/service/HtmlHandler.java
/trunk/tests/com/google/caja/demos/benchmarks/BenchmarkRunner.java
/trunk/tests/com/google/caja/demos/benchmarks/BenchmarkSize.java
/trunk/tests/com/google/caja/plugin/HtmlCompiledPluginTest.java
/trunk/tests/com/google/caja/plugin/HtmlEmitterTest.java
/trunk/tests/com/google/caja/plugin/stages/CompileHtmlStageTest.java
/trunk/tests/com/google/caja/plugin/stages/DebuggingSymbolsStageTest.java
/trunk/tests/com/google/caja/plugin/stages/InlineCssImportsStageTest.java

/trunk/tests/com/google/caja/plugin/stages/LegacyNamespaceFixupStageTest.java
/trunk/tests/com/google/caja/plugin/stages/OpenTemplateStageTest.java
/trunk/tests/com/google/caja/plugin/stages/PipelineStageTestCase.java
/trunk/tests/com/google/caja/plugin/stages/ResolveUriStageTest.java
/trunk/tests/com/google/caja/plugin/stages/RewriteHtmlStageTest.java
/trunk/tests/com/google/caja/plugin/templates/TemplateCompilerTest.java

=======================================
--- /trunk/src/com/google/caja/opensocial/DefaultGadgetRewriter.java Fri
Nov 20 16:58:55 2009
+++ /trunk/src/com/google/caja/opensocial/DefaultGadgetRewriter.java Tue
Dec 29 09:33:01 2009
@@ -229,7 +229,7 @@

PluginCompiler compiler = createPluginCompiler(meta, mq);

- compiler.addInput(AncestorChain.instance(new Dom(content)));
+ compiler.addInput(AncestorChain.instance(new Dom(content)), baseUri);

if (!compiler.run()) {
throw new GadgetRewriteException("Gadget has compile errors");
=======================================
--- /trunk/src/com/google/caja/plugin/BuildServiceImplementation.java Wed
Dec 16 14:43:16 2009
+++ /trunk/src/com/google/caja/plugin/BuildServiceImplementation.java Tue
Dec 29 09:33:01 2009
@@ -160,12 +160,13 @@
// Parse inputs
for (File f : inputs) {
try {
+ URI fileUri = f.getCanonicalFile().toURI();
AncestorChain<?> parsedInput = parseInput(
- new InputSource(f.getCanonicalFile().toURI()), mq);
+ new InputSource(fileUri), mq);
if (parsedInput == null) {
passed = false;
} else {
- compiler.addInput(parsedInput);
+ compiler.addInput(parsedInput, fileUri);
}
} catch (IOException ex) {
logger.println("Failed to read " + f);
=======================================
--- /trunk/src/com/google/caja/plugin/Job.java Thu Dec 10 17:39:38 2009
+++ /trunk/src/com/google/caja/plugin/Job.java Tue Dec 29 09:33:01 2009
@@ -22,42 +22,77 @@
import com.google.caja.parser.js.Expression;
import com.google.caja.parser.js.Statement;
import com.google.caja.parser.js.UncajoledModule;
+import com.google.caja.util.ContentType;
+
+import java.net.URI;

/**
* A parse tree that is awaiting rewriting, compiling, or rendering.
*/
public final class Job {
- public static enum JobType {
- CSS,
- JAVASCRIPT,
- HTML
- ;
- }
-
private final AncestorChain<?> root;
- private final Job.JobType type;
-
- public Job(AncestorChain<?> root) {
- assert root != null;
- this.root = root;
+ private final ContentType type;
+ private final URI baseUri;
+
+ public static Job job(AncestorChain<?> root, URI baseUri) {
+ ContentType type;
ParseTreeNode rootNode = root.node;
if (rootNode instanceof Statement
|| rootNode instanceof Expression
|| rootNode instanceof UncajoledModule
|| rootNode instanceof CajoledModule) {
- this.type = Job.JobType.JAVASCRIPT;
+ type = ContentType.JS;
} else if (rootNode instanceof Dom) {
- this.type = Job.JobType.HTML;
+ type = ContentType.HTML;
+ assert baseUri != null;
} else if (rootNode instanceof CssTree.StyleSheet) {
- this.type = Job.JobType.CSS;
+ type = ContentType.CSS;
+ assert baseUri != null;
} else {
throw new SomethingWidgyHappenedError("Unknown input type " +
rootNode);
}
+ return new Job(root, type, baseUri);
+ }
+
+ public static Job jsJob(AncestorChain<? extends Statement> root) {
+ return new Job(root, ContentType.JS, null);
+ }
+
+ public static Job exprJob(AncestorChain<? extends Expression> root) {
+ return new Job(root, ContentType.JS, null);
+ }
+
+ public static Job moduleJob(AncestorChain<? extends UncajoledModule>
root) {
+ return new Job(root, ContentType.JS, null);
+ }
+
+ public static Job cajoledJob(AncestorChain<? extends CajoledModule>
root) {
+ return new Job(root, ContentType.JS, null);
+ }
+
+ public static Job domJob(AncestorChain<? extends Dom> root, URI baseUri)
{
+ assert baseUri != null;
+ return new Job(root, ContentType.HTML, baseUri);
+ }
+
+ public static Job cssJob(
+ AncestorChain<? extends CssTree.StyleSheet> root, URI baseUri) {
+ assert baseUri != null;
+ return new Job(root, ContentType.CSS, baseUri);
+ }
+
+ private Job(AncestorChain<?> root, ContentType type, URI baseUri) {
+ assert root != null;
+ this.root = root;
+ this.type = type;
+ this.baseUri = baseUri;
}

public AncestorChain<?> getRoot() { return root; }

- public Job.JobType getType() { return type; }
+ public ContentType getType() { return type; }
+
+ public URI getBaseUri() { return baseUri; }

@Override
public String toString() {
=======================================
--- /trunk/src/com/google/caja/plugin/Jobs.java Tue Feb 19 17:55:01 2008
+++ /trunk/src/com/google/caja/plugin/Jobs.java Tue Dec 29 09:33:01 2009
@@ -18,6 +18,8 @@
import com.google.caja.reporting.MessageContext;
import com.google.caja.reporting.MessageLevel;
import com.google.caja.reporting.MessageQueue;
+import com.google.caja.util.ContentType;
+
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
@@ -43,21 +45,21 @@
this.mq = mq;
this.meta = meta;
}
-
+
public void setMessageContext(MessageContext newMc) { this.mc = newMc; }
-
+
public MessageContext getMessageContext() { return mc; }

public MessageQueue getMessageQueue() { return mq; }

public PluginMeta getPluginMeta() { return meta; }
-
+
/** May be mutated in place. */
public List<Job> getJobs() { return jobs; }

- public List<Job> getJobsByType(Job.JobType type, Job.JobType... others) {
+ public List<Job> getJobsByType(ContentType type, ContentType... others) {
List<Job> matches = new ArrayList<Job>();
- EnumSet<Job.JobType> types = EnumSet.of(type, others);
+ EnumSet<ContentType> types = EnumSet.of(type, others);
for (Job job : jobs) {
if (types.contains(job.getType())) { matches.add(job); }
}
=======================================
--- /trunk/src/com/google/caja/plugin/PluginCompiler.java Wed Dec 23
23:20:46 2009
+++ /trunk/src/com/google/caja/plugin/PluginCompiler.java Tue Dec 29
09:33:01 2009
@@ -39,9 +39,11 @@
import com.google.caja.reporting.MessageQueue;
import com.google.caja.reporting.MessageType;
import com.google.caja.reporting.BuildInfo;
+import com.google.caja.util.ContentType;
import com.google.caja.util.Criterion;
import com.google.caja.util.Pipeline;

+import java.net.URI;
import java.util.ArrayList;
import java.util.List;

@@ -95,11 +97,16 @@
this.compilationPipeline = null;
}

- public void addInput(AncestorChain<?> input) {
- jobs.getJobs().add(new Job(input));
+ public void addInput(AncestorChain<?> input, URI baseUri) {
+ jobs.getJobs().add(Job.job(input, baseUri));
jobs.getMessageContext().addInputSource(
input.node.getFilePosition().source());
}
+
+ @Deprecated
+ public void addInput(AncestorChain<?> input) {
+ addInput(input, input.node.getFilePosition().source().getUri());
+ }

/**
* The list of parse trees that comprise the plugin after run has been
called.
@@ -156,7 +163,7 @@
public Node getStaticHtml() {
Job soleHtmlJob = getConsolidatedOutput(new Criterion<Job>() {
public boolean accept(Job job) {
- return job.getType() == Job.JobType.HTML;
+ return job.getType() == ContentType.HTML;
}
});
return soleHtmlJob != null
@@ -171,7 +178,7 @@
public CajoledModule getJavascript() {
Job soleJsJob = getConsolidatedOutput(new Criterion<Job>() {
public boolean accept(Job job) {
- return job.getType() == Job.JobType.JAVASCRIPT;
+ return job.getType() == ContentType.JS;
}
});
return soleJsJob != null ? (CajoledModule) soleJsJob.getRoot().node :
null;
=======================================
--- /trunk/src/com/google/caja/plugin/PluginCompilerMain.java Mon Dec 21
16:25:19 2009
+++ /trunk/src/com/google/caja/plugin/PluginCompilerMain.java Tue Dec 29
09:33:01 2009
@@ -152,7 +152,7 @@
try {
ParseTreeNode parseTree = parseInput(input);
if (null != parseTree) {
- pluginc.addInput(AncestorChain.instance(parseTree));
+ pluginc.addInput(AncestorChain.instance(parseTree), input);
}
} catch (ParseException ex) {
ex.toMessageQueue(mq);
=======================================
--- /trunk/src/com/google/caja/plugin/stages/CompileHtmlStage.java Mon Nov
2 13:56:19 2009
+++ /trunk/src/com/google/caja/plugin/stages/CompileHtmlStage.java Tue Dec
29 09:33:01 2009
@@ -33,12 +33,14 @@
import com.google.caja.plugin.templates.TemplateSanitizer;
import com.google.caja.reporting.MessageQueue;
import com.google.caja.reporting.RenderContext;
+import com.google.caja.util.Lists;
import com.google.caja.util.Pair;
import com.google.caja.util.Pipeline;
+import com.google.caja.lexer.InputSource;
import com.google.caja.lexer.TokenConsumer;
import com.google.caja.render.Concatenator;

-import java.util.ArrayList;
+import java.net.URI;
import java.util.Iterator;
import java.util.List;

@@ -61,8 +63,8 @@
}

public boolean apply(Jobs jobs) {
- List<Node> ihtmlRoots = new ArrayList<Node>();
- List<CssTree.StyleSheet> stylesheets = new
ArrayList<CssTree.StyleSheet>();
+ List<Pair<Node, URI>> ihtmlRoots = Lists.newArrayList();
+ List<CssTree.StyleSheet> stylesheets = Lists.newArrayList();

for (Iterator<Job> jobIt = jobs.getJobs().iterator();
jobIt.hasNext();) {
Job job = jobIt.next();
@@ -73,7 +75,8 @@
// system and we set up expectations on the part of our users to
// maintain this behavior, regardless of whatever complexity that
// might entail.
- ihtmlRoots.add(job.getRoot().cast(Dom.class).node.getValue());
+ ihtmlRoots.add(Pair.pair(
+ job.getRoot().cast(Dom.class).node.getValue(),
job.getBaseUri()));
jobIt.remove();
break;
case CSS:
@@ -88,19 +91,26 @@
MessageQueue mq = jobs.getMessageQueue();

TemplateSanitizer ts = new TemplateSanitizer(htmlSchema, mq);
- for (Node ihtmlRoot : ihtmlRoots) { ts.sanitize(ihtmlRoot); }
+ for (Pair<Node, URI> ihtmlRoot : ihtmlRoots) {
ts.sanitize(ihtmlRoot.a); }
TemplateCompiler tc = new TemplateCompiler(
ihtmlRoots, stylesheets, cssSchema, htmlSchema,
jobs.getPluginMeta(), jobs.getMessageContext(), mq);
Pair<Node, List<Block>> htmlAndJs = tc.getSafeHtml(
DomParser.makeDocument(null, null));

- jobs.getJobs().add(new Job(AncestorChain.instance(
- jobs.getPluginMeta().isOnlyJsEmitted()
- ? makeEmitStaticStmt(htmlAndJs.a) : new Dom(htmlAndJs.a))));
+ Job outJob;
+ if (jobs.getPluginMeta().isOnlyJsEmitted()) {
+ outJob = Job.jsJob(
+ AncestorChain.instance(makeEmitStaticStmt(htmlAndJs.a)));
+ } else {
+ outJob = Job.domJob(
+ AncestorChain.instance(new Dom(htmlAndJs.a)),
+ InputSource.UNKNOWN.getUri());
+ }
+ jobs.getJobs().add(outJob);

for (Block bl : htmlAndJs.b) {
- jobs.getJobs().add(new Job(AncestorChain.instance(bl)));
+ jobs.getJobs().add(Job.jsJob(AncestorChain.instance(bl)));
}
}

=======================================
--- /trunk/src/com/google/caja/plugin/stages/ConsolidateCodeStage.java Thu
Sep 10 15:16:44 2009
+++ /trunk/src/com/google/caja/plugin/stages/ConsolidateCodeStage.java Tue
Dec 29 09:33:01 2009
@@ -22,6 +22,7 @@
import com.google.caja.parser.js.UncajoledModule;
import com.google.caja.plugin.Job;
import com.google.caja.plugin.Jobs;
+import com.google.caja.util.ContentType;
import com.google.caja.util.Pipeline;

import java.util.Collections;
@@ -44,7 +45,7 @@
ListIterator<Job> it = jobs.getJobs().listIterator();
while (it.hasNext()) {
Job job = it.next();
- if (Job.JobType.JAVASCRIPT != job.getType()) { continue; }
+ if (ContentType.JS != job.getType()) { continue; }

Statement stmt = (Statement) job.getRoot().node;
if (stmt instanceof Block) {
@@ -66,7 +67,7 @@
// Now initFunctionBody contains all the top level statements.

UncajoledModule envelope = new UncajoledModule(initFunctionBody);
- jobs.getJobs().add(new Job(AncestorChain.instance(envelope)));
+ jobs.getJobs().add(Job.moduleJob(AncestorChain.instance(envelope)));

return jobs.hasNoFatalErrors();
}
=======================================
--- /trunk/src/com/google/caja/plugin/stages/DebuggingSymbolsStage.java Mon
Aug 10 14:00:55 2009
+++ /trunk/src/com/google/caja/plugin/stages/DebuggingSymbolsStage.java Tue
Dec 29 09:33:01 2009
@@ -26,6 +26,7 @@
import com.google.caja.plugin.Jobs;
import com.google.caja.plugin.PluginMessageType;
import com.google.caja.reporting.MessageQueue;
+import com.google.caja.util.ContentType;
import com.google.caja.util.Pipeline;

import java.util.LinkedHashMap;
@@ -73,7 +74,7 @@
for (ListIterator<Job> it = jobs.getJobs().listIterator();
it.hasNext();) {
Job job = it.next();
- if (job.getType() != Job.JobType.JAVASCRIPT
+ if (job.getType() != ContentType.JS
// May occur if the cajita rewriter does not run due to errors.
|| !(job.getRoot().node instanceof CajoledModule)) {
continue;
@@ -87,13 +88,14 @@
}

DebuggingSymbols symbols = new DebuggingSymbols();
- CajoledModule js =
addSymbols(job.getRoot().cast(CajoledModule.class), symbols, mq);
+ CajoledModule js = addSymbols(
+ job.getRoot().cast(CajoledModule.class), symbols, mq);
if (!symbols.isEmpty()) {
if (DEBUG) {
System.err.println("\n\nPost\n===\n" + js.toStringDeep()
+ "\n\n");
}
- it.set(
- new Job(AncestorChain.instance(attachSymbols(symbols, js,
mq))));
+ it.set(Job.cajoledJob(AncestorChain.instance(
+ attachSymbols(symbols, js, mq))));
}
}
}
=======================================
--- /trunk/src/com/google/caja/plugin/stages/EmbeddedContent.java Wed Dec
23 23:20:46 2009
+++ /trunk/src/com/google/caja/plugin/stages/EmbeddedContent.java Tue Dec
29 09:33:01 2009
@@ -29,6 +29,7 @@
import com.google.caja.util.ContentType;
import com.google.caja.util.Function;

+import java.net.URI;
import java.util.Collections;

import org.w3c.dom.Node;
@@ -61,6 +62,10 @@
this.type = type;
}

+ public URI getBaseUri() {
+ return contentLocation != null
+ ? contentLocation.getUri() : finder.getBaseUri();
+ }
public FilePosition getPosition() { return pos; }
/**
* The message queue associated with the HtmlEmbeddedContentFinder that
=======================================
--- /trunk/src/com/google/caja/plugin/stages/HtmlEmbeddedContentFinder.java
Wed Dec 23 23:20:46 2009
+++ /trunk/src/com/google/caja/plugin/stages/HtmlEmbeddedContentFinder.java
Tue Dec 29 09:33:01 2009
@@ -76,7 +76,7 @@
public HtmlEmbeddedContentFinder(
HtmlSchema schema, URI baseUri, MessageQueue mq, MessageContext mc) {
assert schema != null && mq != null
- && (baseUri == null || (baseUri.isAbsolute()
&& !baseUri.isOpaque()));
+ && baseUri.isAbsolute() && !baseUri.isOpaque();
this.schema = schema;
this.baseUri = baseUri;
this.mq = mq;
@@ -88,6 +88,8 @@
findEmbeddedContent(node, out);
return out;
}
+
+ public URI getBaseUri() { return baseUri; }

private static final ElKey LINK = ElKey.forHtmlElement("link");
private static final ElKey SCRIPT = ElKey.forHtmlElement("script");
=======================================
--- /trunk/src/com/google/caja/plugin/stages/InlineCssImportsStage.java Mon
Feb 23 20:15:55 2009
+++ /trunk/src/com/google/caja/plugin/stages/InlineCssImportsStage.java Tue
Dec 29 09:33:01 2009
@@ -21,6 +21,7 @@
import com.google.caja.lexer.InputSource;
import com.google.caja.lexer.ParseException;
import com.google.caja.lexer.TokenQueue;
+import com.google.caja.lexer.escaping.UriUtil;
import com.google.caja.parser.MutableParseTreeNode;
import com.google.caja.parser.css.CssParser;
import com.google.caja.parser.css.CssTree;
@@ -31,11 +32,11 @@
import com.google.caja.reporting.MessageLevel;
import com.google.caja.reporting.MessagePart;
import com.google.caja.reporting.MessageQueue;
+import com.google.caja.util.ContentType;
import com.google.caja.util.Name;
import com.google.caja.util.Pipeline;

import java.net.URI;
-import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
@@ -83,8 +84,9 @@
*/
public class InlineCssImportsStage implements Pipeline.Stage<Jobs> {
public boolean apply(Jobs jobs) {
- for (Job job : jobs.getJobsByType(Job.JobType.CSS)) {
+ for (Job job : jobs.getJobsByType(ContentType.CSS)) {
inlineImports(job.getRoot().cast(CssTree.StyleSheet.class).node,
+ job.getBaseUri(),
MAXIMUM_IMPORT_DEPTH,
jobs.getPluginMeta().getPluginEnvironment(),
jobs.getMessageQueue());
@@ -97,7 +99,7 @@

/** Inline imports at the beginning of ss. */
private static void inlineImports(
- CssTree.StyleSheet ss, int depth, PluginEnvironment env,
+ CssTree.StyleSheet ss, URI baseUri, int depth, PluginEnvironment env,
MessageQueue mq) {
MutableParseTreeNode.Mutation mut = ss.createMutation();
for (CssTree t : ss.children()) {
@@ -111,7 +113,7 @@
return;
}
try {
- inlineImport(importNode, depth, env, mq, mut);
+ inlineImport(importNode, baseUri, depth, env, mq, mut);
} catch (ParseException ex) {
ex.toMessageQueue(mq);
}
@@ -128,18 +130,17 @@
* content of the URI.
*/
private static void inlineImport(
- CssTree.Import importNode, int depth, PluginEnvironment env,
+ CssTree.Import importNode, URI baseUri, int depth, PluginEnvironment
env,
MessageQueue mq, MutableParseTreeNode.Mutation mut)
throws ParseException {
CssTree.UriLiteral uriNode = importNode.getUri();
// Compute the URI to import
- ExternalReference importUrl;
- try {
- URI uri = new URI(uriNode.getValue());
- importUrl = new ExternalReference(
- uriNode.getFilePosition().source().getUri().resolve(uri),
- uriNode.getFilePosition());
- } catch (URISyntaxException ex) {
+ ExternalReference importUrl = null;
+ URI absUri = UriUtil.resolve(baseUri, uriNode.getValue());
+ if (absUri != null) {
+ importUrl = new ExternalReference(absUri, uriNode.getFilePosition());
+ }
+ if (importUrl == null) {
mq.addMessage(
PluginMessageType.MALFORMED_URL,
uriNode.getFilePosition(),
@@ -156,7 +157,7 @@
return;
}
CssTree.StyleSheet importedSs = parseCss(cp, mq);
- inlineImports(importedSs, depth - 1, env, mq);
+ inlineImports(importedSs, importUrl.getUri(), depth - 1, env, mq);

// Create a set of blocks to import by taking the union of media types
on
// the import block and the media blocks in the style-sheet.
=======================================
--- /trunk/src/com/google/caja/plugin/stages/LegacyNamespaceFixupStage.java
Fri Dec 18 20:56:18 2009
+++ /trunk/src/com/google/caja/plugin/stages/LegacyNamespaceFixupStage.java
Tue Dec 29 09:33:01 2009
@@ -20,10 +20,10 @@
import com.google.caja.plugin.Job;
import com.google.caja.plugin.Jobs;
import com.google.caja.plugin.PluginMessageType;
-import com.google.caja.plugin.Job.JobType;
import com.google.caja.reporting.MessageLevel;
import com.google.caja.reporting.MessagePart;
import com.google.caja.reporting.MessageQueue;
+import com.google.caja.util.ContentType;
import com.google.caja.util.Lists;
import com.google.caja.util.Pipeline;

@@ -44,7 +44,7 @@

public boolean apply(Jobs jobs) {
Fixer f = new Fixer(jobs.getMessageQueue());
- for (Job job : jobs.getJobsByType(JobType.HTML)) { f.fix(job); }
+ for (Job job : jobs.getJobsByType(ContentType.HTML)) { f.fix(job); }
return jobs.hasNoFatalErrors();
}

=======================================
--- /trunk/src/com/google/caja/plugin/stages/OpenTemplateStage.java Thu Dec
10 17:39:38 2009
+++ /trunk/src/com/google/caja/plugin/stages/OpenTemplateStage.java Tue Dec
29 09:33:01 2009
@@ -37,6 +37,7 @@
import com.google.caja.plugin.Job;
import com.google.caja.plugin.Jobs;
import com.google.caja.reporting.MessageQueue;
+import com.google.caja.util.ContentType;
import com.google.caja.util.Pair;
import com.google.caja.util.Pipeline;

@@ -55,7 +56,7 @@
*/
public final class OpenTemplateStage implements Pipeline.Stage<Jobs> {
public boolean apply(Jobs jobs) {
- for (Job job : jobs.getJobsByType(Job.JobType.JAVASCRIPT)) {
+ for (Job job : jobs.getJobsByType(ContentType.JS)) {
optimizeOpenTemplate(job.getRoot(), jobs);
}
return jobs.hasNoFatalErrors();
=======================================
--- /trunk/src/com/google/caja/plugin/stages/ResolveUriStage.java Wed Dec
23 23:20:46 2009
+++ /trunk/src/com/google/caja/plugin/stages/ResolveUriStage.java Tue Dec
29 09:33:01 2009
@@ -17,18 +17,20 @@
import com.google.caja.lang.html.HTML;
import com.google.caja.lang.html.HtmlSchema;
import com.google.caja.lexer.FilePosition;
-import com.google.caja.lexer.InputSource;
import com.google.caja.lexer.escaping.UriUtil;
+import com.google.caja.parser.AncestorChain;
import com.google.caja.parser.html.AttribKey;
import com.google.caja.parser.html.ElKey;
import com.google.caja.parser.html.Nodes;
import com.google.caja.plugin.Dom;
import com.google.caja.plugin.Job;
import com.google.caja.plugin.Jobs;
+import com.google.caja.util.ContentType;
import com.google.caja.util.Pipeline;

import java.net.URI;
import java.net.URISyntaxException;
+import java.util.ListIterator;

import org.w3c.dom.Attr;
import org.w3c.dom.Element;
@@ -51,16 +53,22 @@
this.schema = schema;
}

- private URI baseUri(Node root, FilePosition pos) {
- URI uri = baseUriForDoc(root);
- if (uri == null) {
- // TODO(mikesamuel): this is problematic for DOM nodes parsed without
- // proper debugging info.
- if (!InputSource.UNKNOWN.equals(pos.source())) {
- uri = pos.source().getUri();
+ private static boolean isBaseUri(URI uri) {
+ return uri != null && uri.isAbsolute() && !uri.isOpaque();
+ }
+
+ private URI baseUri(Node root, URI uri, FilePosition pos) {
+ URI baseUri = baseUriForDoc(root);
+ if (!isBaseUri(baseUri)) {
+ baseUri = uri;
+ if (!isBaseUri(baseUri)) {
+ // TODO(mikesamuel): this is problematic for DOM nodes parsed
without
+ // proper debugging info.
+ baseUri = pos.source().getUri();
+ if (!isBaseUri(baseUri)) { return null; }
}
}
- return (uri != null && uri.isAbsolute() && !uri.isOpaque()) ? uri :
null;
+ return baseUri;
}

private URI baseUriForDoc(Node root) {
@@ -92,19 +100,24 @@
String value = a.getValue();
try {
URI uri = new URI(value);
- return uri.isAbsolute() && !uri.isOpaque() ? uri : null;
+ return isBaseUri(uri) ? uri : null;
} catch (URISyntaxException ex) {
return null;
}
}

public boolean apply(Jobs jobs) {
- for (Job job : jobs.getJobsByType(Job.JobType.HTML)) {
+ ListIterator<Job> it = jobs.getJobs().listIterator();
+ while (it.hasNext()) {
+ Job job = it.next();
+ if (job.getType() != ContentType.HTML) { continue; }
+ AncestorChain<Dom> root = job.getRoot().cast(Dom.class);
Dom dom = job.getRoot().cast(Dom.class).node;
- Node root = dom.getValue();
- URI baseUri = baseUri(root, dom.getFilePosition());
+ Node node = dom.getValue();
+ URI baseUri = baseUri(node, job.getBaseUri(), dom.getFilePosition());
if (baseUri != null) {
- resolveRelativeUrls(root, baseUri);
+ resolveRelativeUrls(node, baseUri);
+ it.set(Job.domJob(root, baseUri));
}
}
return true;
=======================================
--- /trunk/src/com/google/caja/plugin/stages/RewriteCssStage.java Mon Jun
1 16:50:26 2009
+++ /trunk/src/com/google/caja/plugin/stages/RewriteCssStage.java Tue Dec
29 09:33:01 2009
@@ -18,6 +18,7 @@
import com.google.caja.plugin.CssRuleRewriter;
import com.google.caja.plugin.Job;
import com.google.caja.plugin.Jobs;
+import com.google.caja.util.ContentType;
import com.google.caja.util.Pipeline;

import java.util.ListIterator;
@@ -33,7 +34,7 @@
public boolean apply(Jobs jobs) {
for (ListIterator<Job> it = jobs.getJobs().listIterator();
it.hasNext();) {
Job job = it.next();
- if (job.getType() != Job.JobType.CSS) { continue; }
+ if (job.getType() != ContentType.CSS) { continue; }

new CssRuleRewriter(jobs.getPluginMeta()).rewriteCss(
job.getRoot().cast(CssTree.StyleSheet.class).node);
=======================================
--- /trunk/src/com/google/caja/plugin/stages/RewriteHtmlStage.java Wed Dec
23 23:20:46 2009
+++ /trunk/src/com/google/caja/plugin/stages/RewriteHtmlStage.java Tue Dec
29 09:33:01 2009
@@ -36,6 +36,7 @@
import com.google.caja.reporting.MessageContext;
import com.google.caja.reporting.MessagePart;
import com.google.caja.reporting.MessageQueue;
+import com.google.caja.util.ContentType;
import com.google.caja.util.Lists;
import com.google.caja.util.Name;
import com.google.caja.util.Pipeline;
@@ -79,10 +80,10 @@
public boolean apply(Jobs jobs) {
MessageQueue mq = jobs.getMessageQueue();
MessageContext mc = jobs.getMessageContext();
- for (Job job : jobs.getJobsByType(Job.JobType.HTML)) {
+ for (Job job : jobs.getJobsByType(ContentType.HTML)) {
Node root = ((Dom) job.getRoot().node).getValue();
HtmlEmbeddedContentFinder finder = new HtmlEmbeddedContentFinder(
- htmlSchema, null, mq, mc);
+ htmlSchema, job.getBaseUri(), mq, mc);
for (EmbeddedContent content : finder.findEmbeddedContent(root)) {
Node src = content.getSource();
if (content.getSource() instanceof Element) {
@@ -234,7 +235,8 @@
}
}

- jobs.getJobs().add(new Job(AncestorChain.instance(stylesheet)));
+ jobs.getJobs().add(Job.cssJob(
+ AncestorChain.instance(stylesheet), c.getBaseUri()));
}

/**
=======================================
--- /trunk/src/com/google/caja/plugin/stages/SanitizeHtmlStage.java Mon
Jun 1 16:50:26 2009
+++ /trunk/src/com/google/caja/plugin/stages/SanitizeHtmlStage.java Tue Dec
29 09:33:01 2009
@@ -19,6 +19,7 @@
import com.google.caja.plugin.Job;
import com.google.caja.plugin.Jobs;
import com.google.caja.plugin.templates.TemplateSanitizer;
+import com.google.caja.util.ContentType;
import com.google.caja.util.Pipeline;

/**
@@ -42,7 +43,7 @@
htmlSchema, jobs.getMessageQueue());

boolean valid = true;
- for (Job job : jobs.getJobsByType(Job.JobType.HTML)) {
+ for (Job job : jobs.getJobsByType(ContentType.HTML)) {
if (!s.sanitize(job.getRoot().cast(Dom.class).node.getValue())) {
valid = false;
// Keep going so that we can display error messages for all inputs.
=======================================
--- /trunk/src/com/google/caja/plugin/stages/ValidateCssStage.java Wed Dec
16 14:43:16 2009
+++ /trunk/src/com/google/caja/plugin/stages/ValidateCssStage.java Tue Dec
29 09:33:01 2009
@@ -23,6 +23,7 @@
import com.google.caja.plugin.Job;
import com.google.caja.plugin.Jobs;
import com.google.caja.reporting.MessageLevel;
+import com.google.caja.util.ContentType;
import com.google.caja.util.Pipeline;

/**
@@ -58,7 +59,7 @@

v.withInvalidNodeMessageLevel(MessageLevel.WARNING);
rw.withInvalidNodeMessageLevel(MessageLevel.WARNING);
- for (Job job : jobs.getJobsByType(Job.JobType.CSS)) {
+ for (Job job : jobs.getJobsByType(ContentType.CSS)) {
validate(v, rw, job.getRoot().cast(CssTree.class));
}

=======================================
--- /trunk/src/com/google/caja/plugin/stages/ValidateJavascriptStage.java
Fri Nov 13 13:37:15 2009
+++ /trunk/src/com/google/caja/plugin/stages/ValidateJavascriptStage.java
Tue Dec 29 09:33:01 2009
@@ -22,6 +22,7 @@
import com.google.caja.plugin.ExpressionSanitizerCaja;
import com.google.caja.plugin.Job;
import com.google.caja.plugin.Jobs;
+import com.google.caja.util.ContentType;
import com.google.caja.util.Pipeline;
import com.google.caja.reporting.BuildInfo;

@@ -42,7 +43,7 @@
public boolean apply(Jobs jobs) {
for (ListIterator<Job> it = jobs.getJobs().listIterator();
it.hasNext();) {
Job job = it.next();
- if (job.getType() != Job.JobType.JAVASCRIPT) { continue; }
+ if (job.getType() != ContentType.JS) { continue; }
// Pass in the rootmost scope that has non-synthetic children, so
that
// the Caja rules correctly identify global function declarations.
AncestorChain<?> nonSyntheticScopeRoot
@@ -53,7 +54,7 @@
buildInfo, jobs.getMessageQueue())
.sanitize(nonSyntheticScopeRoot);
if (nonSyntheticScopeRoot.parent == null) {
- it.set(new Job(AncestorChain.instance(validated)));
+ it.set(Job.job(AncestorChain.instance(validated), null));
} else {
((MutableParseTreeNode) nonSyntheticScopeRoot.parent.node)
.replaceChild(validated, nonSyntheticScopeRoot.node);
=======================================
--- /trunk/src/com/google/caja/plugin/templates/TemplateCompiler.java Wed
Dec 23 23:20:46 2009
+++ /trunk/src/com/google/caja/plugin/templates/TemplateCompiler.java Tue
Dec 29 09:33:01 2009
@@ -35,6 +35,7 @@
import com.google.caja.util.Maps;
import com.google.caja.util.Pair;

+import java.net.URI;
import java.util.List;
import java.util.Map;

@@ -58,7 +59,7 @@
* @author mikes...@gmail.com
*/
public class TemplateCompiler {
- private final List<Node> ihtmlRoots;
+ private final List<Pair<Node, URI>> ihtmlRoots;
private final List<CssTree.StyleSheet> safeStylesheets;
private final HtmlSchema htmlSchema;
private final PluginMeta meta;
@@ -92,7 +93,8 @@
= Maps.newIdentityHashMap();

/**
- * @param ihtmlRoots roots of trees to process.
+ * @param ihtmlRoots roots of trees to process and the baseURI used to
resolve
+ * URIs in those nodes.
* @param safeStylesheets CSS style-sheets that have had unsafe
* constructs removed and had rules rewritten.
* @param meta specifies how URLs and other attributes are rewritten.
@@ -101,7 +103,7 @@
* @param mq receives messages about invalid attribute values.
*/
public TemplateCompiler(
- List<? extends Node> ihtmlRoots,
+ List<Pair<Node, URI>> ihtmlRoots,
List<? extends CssTree.StyleSheet> safeStylesheets,
CssSchema cssSchema, HtmlSchema htmlSchema,
PluginMeta meta, MessageContext mc, MessageQueue mq) {
@@ -121,14 +123,14 @@
*/
private void inspect() {
if (!mq.hasMessageAtLevel(MessageLevel.FATAL_ERROR)) {
- for (Node ihtmlRoot : ihtmlRoots) {
+ for (Pair<Node, URI> ihtmlRoot : ihtmlRoots) {
HtmlEmbeddedContentFinder finder = new HtmlEmbeddedContentFinder(
- htmlSchema, null, mq, mc);
- for (EmbeddedContent c : finder.findEmbeddedContent(ihtmlRoot)) {
+ htmlSchema, ihtmlRoot.b, mq, mc);
+ for (EmbeddedContent c : finder.findEmbeddedContent(ihtmlRoot.a)) {
Node src = c.getSource();
if (src instanceof Attr) { embeddedContent.put((Attr) src, c); }
}
- inspect(ihtmlRoot, ElKey.forHtmlElement("div"));
+ inspect(ihtmlRoot.a, ElKey.forHtmlElement("div"));
}
}
}
@@ -271,8 +273,10 @@
inspect();

// Emit safe HTML with JS which attaches dynamic attributes.
+ List<Node> roots = Lists.newArrayList();
+ for (Pair<Node, URI> root : ihtmlRoots) { roots.add(root.a); }
SafeHtmlMaker htmlMaker = new SafeHtmlMaker(
- meta, mc, doc, scriptsPerNode, ihtmlRoots,
aRewriter.getHandlers());
+ meta, mc, doc, scriptsPerNode, roots, aRewriter.getHandlers());
Pair<Node, List<Block>> htmlAndJs = htmlMaker.make();
Node html = htmlAndJs.a;
List<Block> js = htmlAndJs.b;
=======================================
--- /trunk/src/com/google/caja/service/HtmlHandler.java Thu Dec 10 15:47:30
2009
+++ /trunk/src/com/google/caja/service/HtmlHandler.java Tue Dec 29 09:33:01
2009
@@ -184,7 +184,7 @@

PluginCompiler compiler = new PluginCompiler(buildInfo, meta, mq);

- compiler.addInput(AncestorChain.instance(html));
+ compiler.addInput(AncestorChain.instance(html), inputUri);
if (okToContinue) {
okToContinue &= compiler.run();
}
=======================================
--- /trunk/tests/com/google/caja/demos/benchmarks/BenchmarkRunner.java Fri
Nov 13 13:37:15 2009
+++ /trunk/tests/com/google/caja/demos/benchmarks/BenchmarkRunner.java Tue
Dec 29 09:33:01 2009
@@ -138,7 +138,7 @@
fromString(plain(fromResource(filename)));
pc.addInput(AncestorChain.instance(valija
? BenchmarkUtils.addUseCajitaDirective(js(src))
- : js(src)));
+ : js(src)), is.getUri());
if (!pc.run()) {
return -1;
}
=======================================
--- /trunk/tests/com/google/caja/demos/benchmarks/BenchmarkSize.java Fri
Nov 13 13:37:15 2009
+++ /trunk/tests/com/google/caja/demos/benchmarks/BenchmarkSize.java Tue
Dec 29 09:33:01 2009
@@ -176,7 +176,7 @@
MessageQueue mq = TestUtil.createTestMessageQueue(this.mc);
if (!valija) { js = BenchmarkUtils.addUseCajitaDirective(js); }
PluginCompiler pc = new PluginCompiler(new TestBuildInfo(), meta, mq);
- pc.addInput(AncestorChain.instance(js));
+ pc.addInput(AncestorChain.instance(js), null);
if (pc.run()) {
result = pc.getJavascript();
if (valija) {
=======================================
--- /trunk/tests/com/google/caja/plugin/HtmlCompiledPluginTest.java Fri Dec
18 20:56:18 2009
+++ /trunk/tests/com/google/caja/plugin/HtmlCompiledPluginTest.java Tue Dec
29 09:33:01 2009
@@ -228,7 +228,7 @@
PluginCompiler compiler = new PluginCompiler(new TestBuildInfo(),
meta, mq);
compiler.setMessageContext(mc);
Dom html = new Dom(htmlFragment(fromString("<script>{</script>")));
- compiler.addInput(AncestorChain.instance(html));
+ compiler.addInput(AncestorChain.instance(html), is.getUri());

boolean passed = compiler.run();
assertFalse(passed);
@@ -254,7 +254,8 @@
});
PluginCompiler compiler = new PluginCompiler(new TestBuildInfo(),
meta, mq);
compiler.setMessageContext(mc);
- compiler.addInput(AncestorChain.instance(html));
+ compiler.addInput(
+ AncestorChain.instance(html),
html.getFilePosition().source().getUri());

boolean failed = !compiler.run();

=======================================
--- /trunk/tests/com/google/caja/plugin/HtmlEmitterTest.java Wed Dec 9
15:23:04 2009
+++ /trunk/tests/com/google/caja/plugin/HtmlEmitterTest.java Tue Dec 29
09:33:01 2009
@@ -68,7 +68,8 @@
}
});
TemplateCompiler tc = new TemplateCompiler(
- Collections.singletonList(htmlWithExtractedScripts(input)),
+ Collections.singletonList(
+ Pair.pair(htmlWithExtractedScripts(input), is.getUri())),
Collections.<CssTree.StyleSheet>emptyList(),
CssSchema.getDefaultCss21Schema(mq), HtmlSchema.getDefault(mq),
meta, mc, mq);
=======================================
--- /trunk/tests/com/google/caja/plugin/stages/CompileHtmlStageTest.java
Tue Oct 27 15:38:50 2009
+++ /trunk/tests/com/google/caja/plugin/stages/CompileHtmlStageTest.java
Tue Dec 29 09:33:01 2009
@@ -15,7 +15,7 @@
package com.google.caja.plugin.stages;

import com.google.caja.plugin.Jobs;
-import com.google.caja.plugin.Job;
+import com.google.caja.util.ContentType;
import com.google.caja.lang.css.CssSchema;
import com.google.caja.lang.html.HtmlSchema;

@@ -26,19 +26,19 @@
public final void testEmitHtmlAsJsStaticOnly() throws Exception {
meta.setOnlyJsEmitted(true);
assertPipeline(
- job("<p>Hello world</p>", Job.JobType.HTML),
+ job("<p>Hello world</p>", ContentType.HTML),
job("IMPORTS___.htmlEmitter___.emitStatic('<p>Hello world</p>')",
- Job.JobType.JAVASCRIPT));
+ ContentType.JS));
}

public final void testEmitHtmlAsJsAttributes() throws Exception {
meta.setOnlyJsEmitted(true);
assertPipeline(
- job("<p id=\"foo\">Hello world</p>", Job.JobType.HTML),
+ job("<p id=\"foo\">Hello world</p>", ContentType.HTML),
job(""
+ "IMPORTS___.htmlEmitter___.emitStatic('<p
id=\\\"id_1___\\\">"
+ "Hello world</p>')",
- Job.JobType.JAVASCRIPT),
+ ContentType.JS),
job(""
+ "{ /* Start translated code */\n"
+ " throw 'Translated code must never be executed';\n"
@@ -52,9 +52,9 @@
+ " emitter___.signalLoaded();\n"
+ " } /* End translated code */\n"
+ "}",
- Job.JobType.JAVASCRIPT));
- }
-
+ ContentType.JS));
+ }
+
@Override
protected boolean runPipeline(Jobs jobs) throws Exception {
mq.getMessages().clear();
=======================================
---
/trunk/tests/com/google/caja/plugin/stages/DebuggingSymbolsStageTest.java
Fri Nov 13 13:37:15 2009
+++
/trunk/tests/com/google/caja/plugin/stages/DebuggingSymbolsStageTest.java
Tue Dec 29 09:33:01 2009
@@ -316,7 +316,7 @@
meta.setDebugMode(true);

Jobs jobs = new Jobs(mc, mq, meta);
- jobs.getJobs().add(new
Job(AncestorChain.instance(uncajoledModuleBody)));
+
jobs.getJobs().add(Job.jsJob(AncestorChain.instance(uncajoledModuleBody)));

Pipeline<Jobs> pipeline = new Pipeline<Jobs>();
pipeline.getStages().add(new ConsolidateCodeStage());
=======================================
---
/trunk/tests/com/google/caja/plugin/stages/InlineCssImportsStageTest.java
Thu Jul 23 09:16:30 2009
+++
/trunk/tests/com/google/caja/plugin/stages/InlineCssImportsStageTest.java
Tue Dec 29 09:33:01 2009
@@ -15,24 +15,24 @@
package com.google.caja.plugin.stages;

import com.google.caja.lexer.ParseException;
-import com.google.caja.plugin.Job;
import com.google.caja.plugin.Jobs;
import com.google.caja.plugin.PluginMessageType;
import com.google.caja.reporting.MessageLevel;
import com.google.caja.reporting.MessageType;
+import com.google.caja.util.ContentType;

public class InlineCssImportsStageTest extends PipelineStageTestCase {
public final void testNoImports() throws Exception {
assertPipeline(
- job("p { color: purple }", Job.JobType.CSS),
- job("p {\n color: purple\n}", Job.JobType.CSS));
+ job("p { color: purple }", ContentType.CSS),
+ job("p {\n color: purple\n}", ContentType.CSS));
}

public final void testOneImport() throws Exception {
addUrlToPluginEnvironment("foo.css", "p { color: purple }");
assertPipeline(
- job("@import 'foo.css';", Job.JobType.CSS),
- job("p {\n color: purple\n}", Job.JobType.CSS));
+ job("@import 'foo.css';", ContentType.CSS),
+ job("p {\n color: purple\n}", ContentType.CSS));
}

public final void testImportResolvedRelativeToImporter() throws
Exception {
@@ -40,8 +40,8 @@
addUrlToPluginEnvironment("baz.css", "p { color: #f88 }");
addUrlToPluginEnvironment("foo/baz.css", "p { color: purple }");
assertPipeline(
- job("@import 'foo/bar.css';", Job.JobType.CSS),
- job("p {\n color: purple\n}", Job.JobType.CSS));
+ job("@import 'foo/bar.css';", ContentType.CSS),
+ job("p {\n color: purple\n}", ContentType.CSS));
}

public final void testMultipleImport() throws Exception {
@@ -49,10 +49,10 @@
addUrlToPluginEnvironment("bar.css", "b { color: blue }");
assertPipeline(
job("@import 'foo.css'; @import 'bar.css'; i { color: #ff8 }",
- Job.JobType.CSS),
+ ContentType.CSS),
job("p {\n color: purple\n}\n"
+ "b {\n color: blue\n}\n"
- + "i {\n color: #ff8\n}", Job.JobType.CSS));
+ + "i {\n color: #ff8\n}", ContentType.CSS));
}

public final void testMediaTypesUnioned() throws Exception {
@@ -79,7 +79,7 @@
+ "@import 'all2.css' screen, print;\n" // Cross with
explicit all
+ "@import 'all1.css' all;\n" // All x implicit all
+ "@import 'all2.css' all;", // All x explicit all
- Job.JobType.CSS),
+ ContentType.CSS),
job("@media screen {\n s {\n content: 'screen'\n }\n}\n"
+ "@media print {\n p {\n content: 'print'\n }\n}\n"
+ "@media print {\n b {\n content: 'multi'\n }\n}\n"
@@ -87,13 +87,13 @@
+ "@media screen, print {\n a {\n content: 'all2'\n
}\n}\n"
+ "a {\n content: 'all1'\n}\n"
+ "@media all {\n a {\n content: 'all2'\n }\n}",
- Job.JobType.CSS));
+ ContentType.CSS));
}

public final void testUnresolveableImport() throws Exception {
assertPipelineFails(
- job("@import 'bogus.css';", Job.JobType.CSS),
- job("@import url('bogus.css');", Job.JobType.CSS));
+ job("@import 'bogus.css';", ContentType.CSS),
+ job("@import url('bogus.css');", ContentType.CSS));
assertMessage(PluginMessageType.FAILED_TO_LOAD_EXTERNAL_URL,
MessageLevel.ERROR);
}

@@ -102,16 +102,16 @@
addUrlToPluginEnvironment("foo.css", "@import 'foo.css';");
}
assertPipelineFails(
- job("@import 'foo.css';", Job.JobType.CSS),
- job("@import url('foo.css');", Job.JobType.CSS));
+ job("@import 'foo.css';", ContentType.CSS),
+ job("@import url('foo.css');", ContentType.CSS));
assertMessage(PluginMessageType.CYCLIC_INCLUDE, MessageLevel.ERROR);
}

public final void testMalformedUrl() throws Exception {
try {
assertPipelineFails(
- job("@import ':::';", Job.JobType.CSS),
- job("@import url(':::');", Job.JobType.CSS));
+ job("@import ':::';", ContentType.CSS),
+ job("@import url(':::');", ContentType.CSS));
} catch (ParseException ex) {
ex.toMessageQueue(mq);
}
=======================================
---
/trunk/tests/com/google/caja/plugin/stages/LegacyNamespaceFixupStageTest.java
Fri Dec 18 20:56:18 2009
+++
/trunk/tests/com/google/caja/plugin/stages/LegacyNamespaceFixupStageTest.java
Tue Dec 29 09:33:01 2009
@@ -14,6 +14,7 @@

package com.google.caja.plugin.stages;

+import com.google.caja.lexer.InputSource;
import com.google.caja.parser.AncestorChain;
import com.google.caja.parser.ParseTreeNode;
import com.google.caja.parser.html.DomParser;
@@ -136,7 +137,8 @@

Job job() {
assertEquals(stack, Lists.newArrayList(root));
- return new Job(AncestorChain.instance(new Dom(root)));
+ return Job.domJob(
+ AncestorChain.instance(new Dom(root)),
InputSource.UNKNOWN.getUri());
}
NamespaceUnawareBuilder open(String qname) {
Element el = doc.createElement(qname);
=======================================
--- /trunk/tests/com/google/caja/plugin/stages/OpenTemplateStageTest.java
Thu Jul 23 09:16:30 2009
+++ /trunk/tests/com/google/caja/plugin/stages/OpenTemplateStageTest.java
Tue Dec 29 09:33:01 2009
@@ -99,10 +99,10 @@
pipeline.getStages().add(new ValidateCssStage(cssSchema, htmlSchema));
pipeline.getStages().add(new ConsolidateCodeStage());

- ParseTreeNode node = js(fromString(input));
+ Block node = js(fromString(input));
PluginMeta meta = new PluginMeta();
Jobs jobs = new Jobs(mc, mq, meta);
- jobs.getJobs().add(new Job(AncestorChain.instance(node)));
+ jobs.getJobs().add(Job.jsJob(AncestorChain.instance(node)));

assertTrue(pipeline.apply(jobs));
assertEquals(
=======================================
--- /trunk/tests/com/google/caja/plugin/stages/PipelineStageTestCase.java
Thu Dec 10 17:39:38 2009
+++ /trunk/tests/com/google/caja/plugin/stages/PipelineStageTestCase.java
Tue Dec 29 09:33:01 2009
@@ -29,6 +29,7 @@
import com.google.caja.plugin.PluginMeta;
import com.google.caja.reporting.RenderContext;
import com.google.caja.util.CajaTestCase;
+import com.google.caja.util.ContentType;
import com.google.caja.util.MoreAsserts;
import com.google.caja.util.Pair;

@@ -107,17 +108,20 @@
switch (inputJob.type) {
case HTML:
outputJobs.getJobs().add(
- new Job(AncestorChain.instance(
- new Dom(htmlFragment(fromString(inputJob.content, is))))));
+ Job.domJob(
+ AncestorChain.instance(
+ new Dom(htmlFragment(fromString(inputJob.content,
is)))),
+ is.getUri()));
break;
case CSS:
outputJobs.getJobs().add(
- new Job(AncestorChain.instance(
- css(fromString(inputJob.content, is)))));
+ Job.cssJob(
+ AncestorChain.instance(css(fromString(inputJob.content,
is))),
+ is.getUri()));
break;
- case JAVASCRIPT:
+ case JS:
outputJobs.getJobs().add(
- new Job(AncestorChain.instance(
+ Job.jsJob(AncestorChain.instance(
js(fromString(inputJob.content, is)))));
break;
default:
@@ -139,15 +143,15 @@
protected abstract boolean runPipeline(Jobs jobs) throws Exception;

/** Create a stub job object. */
- protected static JobStub job(String content, Job.JobType type) {
+ protected static JobStub job(String content, ContentType type) {
return new JobStub(content, type);
}

protected static final class JobStub {
final String content;
- final Job.JobType type;
-
- JobStub(String content, Job.JobType type) {
+ final ContentType type;
+
+ JobStub(String content, ContentType type) {
if (content == null || type == null) { throw new
NullPointerException(); }
this.content = content;
this.type = type;
=======================================
--- /trunk/tests/com/google/caja/plugin/stages/ResolveUriStageTest.java Wed
Dec 23 23:20:46 2009
+++ /trunk/tests/com/google/caja/plugin/stages/ResolveUriStageTest.java Tue
Dec 29 09:33:01 2009
@@ -15,79 +15,79 @@
package com.google.caja.plugin.stages;

import com.google.caja.lang.html.HtmlSchema;
-import com.google.caja.plugin.Job;
import com.google.caja.plugin.Jobs;
+import com.google.caja.util.ContentType;

public class ResolveUriStageTest extends PipelineStageTestCase {

public final void testEmptyDoc() throws Exception {
assertPipeline(
- job("", Job.JobType.HTML),
- job("", Job.JobType.HTML));
+ job("", ContentType.HTML),
+ job("", ContentType.HTML));
}

public final void testLink() throws Exception {
assertPipeline(
- job("<a href=foo.html>foo</a>", Job.JobType.HTML),
- job("<a href=\"test:/foo.html\">foo</a>", Job.JobType.HTML));
+ job("<a href=foo.html>foo</a>", ContentType.HTML),
+ job("<a href=\"test:/foo.html\">foo</a>", ContentType.HTML));
}

public final void testAnchorOnly() throws Exception {
assertPipeline(
- job("<a href=#bar>foo</a>", Job.JobType.HTML),
- job("<a href=\"#bar\">foo</a>", Job.JobType.HTML));
+ job("<a href=#bar>foo</a>", ContentType.HTML),
+ job("<a href=\"#bar\">foo</a>", ContentType.HTML));
}

public final void testLinkWithAnchor() throws Exception {
assertPipeline(
- job("<a href=foo.html#bar>foo</a>", Job.JobType.HTML),
+ job("<a href=foo.html#bar>foo</a>", ContentType.HTML),
job("<a href=\"test:/foo.html#bar\">foo</a>",
- Job.JobType.HTML));
+ ContentType.HTML));
}

public final void testLinkWithBase() throws Exception {
assertPipeline(
job("<base href=http://example.org/bar/baz/foo.html>"
+ "<a href=../boo.html>foo</a>",
- Job.JobType.HTML),
+ ContentType.HTML),
job("<base href=\"http://example.org/bar/baz/foo.html\" />"
+ "<a href=\"http://example.org/bar/boo.html\">foo</a>",
- Job.JobType.HTML));
+ ContentType.HTML));
}

public final void testUnresolvableUrl() throws Exception {
assertPipeline(
job("<base href=http://example.org/bar/baz/foo.html>"
+ "<a href=../../../../boo.html>foo</a>",
- Job.JobType.HTML),
+ ContentType.HTML),
job("<base href=\"http://example.org/bar/baz/foo.html\" />"
+ "<a href=\"../../../../boo.html\">foo</a>",
- Job.JobType.HTML));
+ ContentType.HTML));
}

public final void testMalformedUrl() throws Exception {
assertPipeline(
job("<a href='foo bar'>foo</a>",
- Job.JobType.HTML),
+ ContentType.HTML),
job("<a href=\"test:/foo%20bar\">foo</a>",
- Job.JobType.HTML));
+ ContentType.HTML));
}

public final void testOpaqueUrl() throws Exception {
assertPipeline(
job("<a href=mailto:b...@example.com>foo</a>",
- Job.JobType.HTML),
+ ContentType.HTML),
job("<a href=\"mailto:bob%40example.com\">foo</a>",
- Job.JobType.HTML));
+ ContentType.HTML));
}

public final void testJavascriptUrl() throws Exception {
assertPipeline(
job("<a href='javascript:foo() + bar([1, 2, 3]) * 4'>foo</a>",
- Job.JobType.HTML),
+ ContentType.HTML),
job("<a href=\"javascript:"

+ "foo%28%29%20+%20bar%28%5B1,%202,%203%5D%29%20%2A%204\">foo</a>",
- Job.JobType.HTML));
+ ContentType.HTML));
}

@Override
=======================================
--- /trunk/tests/com/google/caja/plugin/stages/RewriteHtmlStageTest.java
Wed Dec 23 23:20:46 2009
+++ /trunk/tests/com/google/caja/plugin/stages/RewriteHtmlStageTest.java
Tue Dec 29 09:33:01 2009
@@ -24,11 +24,11 @@
import com.google.caja.plugin.Job;
import com.google.caja.plugin.Jobs;
import com.google.caja.plugin.PluginMessageType;
-import com.google.caja.plugin.Job.JobType;
import com.google.caja.reporting.MessageLevel;
import com.google.caja.reporting.MessagePart;
import com.google.caja.reporting.MessageType;
-import java.util.ArrayList;
+import com.google.caja.util.ContentType;
+import com.google.caja.util.Lists;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
@@ -40,34 +40,34 @@
public final class RewriteHtmlStageTest extends PipelineStageTestCase {
public final void testScriptExtraction() throws Exception {
assertPipeline(
- job("foo<script>extracted();</script>baz", Job.JobType.HTML),
+ job("foo<script>extracted();</script>baz", ContentType.HTML),
// The "jobnum" attribute was added by the extractScript method
below.
- job("foo<span jobnum=\"1\"></span>baz", Job.JobType.HTML),
- job("{ extracted(); }", Job.JobType.JAVASCRIPT)
+ job("foo<span jobnum=\"1\"></span>baz", ContentType.HTML),
+ job("{ extracted(); }", ContentType.JS)
);
assertNoErrors();

assertPipeline(
job("foo<script type=text/vbscript>deleted()</script>baz",
- Job.JobType.HTML),
- job("foobaz", Job.JobType.HTML)
+ ContentType.HTML),
+ job("foobaz", ContentType.HTML)
);
assertMessage(
PluginMessageType.UNRECOGNIZED_CONTENT_TYPE, MessageLevel.WARNING);

assertPipeline(
job("foo<script type=\"text/javascript\">var x = 1;</script>baz",
- Job.JobType.HTML),
- job("foo<span jobnum=\"1\"></span>baz", Job.JobType.HTML),
- job("{\n var x = 1;\n}", Job.JobType.JAVASCRIPT)
+ ContentType.HTML),
+ job("foo<span jobnum=\"1\"></span>baz", ContentType.HTML),
+ job("{\n var x = 1;\n}", ContentType.JS)
);
assertNoErrors();

assertPipeline(
job("foo<script type=\"text/javascript\""
+ ">useXml(<xml>foo</xml>);</script>baz",
- Job.JobType.HTML),
- job("foobaz", Job.JobType.HTML)
+ ContentType.HTML),
+ job("foobaz", ContentType.HTML)
);
assertMessage(
MessageType.UNEXPECTED_TOKEN,
@@ -77,54 +77,54 @@

assertPipeline(
job("foo<script type=text/javascript>extracted();</script>baz",
- Job.JobType.HTML),
- job("foo<span jobnum=\"1\"></span>baz", Job.JobType.HTML),
- job("{ extracted(); }", Job.JobType.JAVASCRIPT)
+ ContentType.HTML),
+ job("foo<span jobnum=\"1\"></span>baz", ContentType.HTML),
+ job("{ extracted(); }", ContentType.JS)
);
assertNoErrors();

assertPipeline(
- job("foo<script></script>baz", Job.JobType.HTML),
- job("foobaz", Job.JobType.HTML)
+ job("foo<script></script>baz", ContentType.HTML),
+ job("foobaz", ContentType.HTML)
);
assertNoErrors();
}

public final void testStyleExtraction() throws Exception {
assertPipeline(
- job("Foo<style>p { color: blue }</style><p>Bar", Job.JobType.HTML),
- job("Foo<p>Bar</p>", Job.JobType.HTML),
- job("p {\n color: blue\n}", Job.JobType.CSS));
+ job("Foo<style>p { color: blue }</style><p>Bar", ContentType.HTML),
+ job("Foo<p>Bar</p>", ContentType.HTML),
+ job("p {\n color: blue\n}", ContentType.CSS));
assertNoErrors();

assertPipeline(
job("Foo<link rel=stylesheet
href=content:p+%7Bcolor%3A+blue%7D><p>Bar",
- Job.JobType.HTML),
- job("Foo<p>Bar</p>", Job.JobType.HTML),
- job("p {\n color: blue\n}", Job.JobType.CSS));
+ ContentType.HTML),
+ job("Foo<p>Bar</p>", ContentType.HTML),
+ job("p {\n color: blue\n}", ContentType.CSS));
assertNoErrors();

assertPipeline(
- job("Foo<style></style><p>Bar", Job.JobType.HTML),
- job("Foo<p>Bar</p>", Job.JobType.HTML));
+ job("Foo<style></style><p>Bar", ContentType.HTML),
+ job("Foo<p>Bar</p>", ContentType.HTML));
assertNoErrors();
}

public final void testOnLoadHandlers() throws Exception {
assertPipeline(
- job("<body onload=init();>Foo</body>", Job.JobType.HTML),
+ job("<body onload=init();>Foo</body>", ContentType.HTML),
job("<html><head></head>"
+ "<body>Foo<span jobnum=\"1\"></span></body></html>",
- Job.JobType.HTML),
- job("{ init(); }", Job.JobType.JAVASCRIPT));
+ ContentType.HTML),
+ job("{ init(); }", ContentType.JS));
assertNoErrors();
}

public final void testImportedStyles() throws Exception {
assertPipeline(
- job("<style>@import 'styles.css';</style>", Job.JobType.HTML),
- job("", Job.JobType.HTML),
- job("@import url('styles.css');", Job.JobType.CSS)
+ job("<style>@import 'styles.css';</style>", ContentType.HTML),
+ job("", ContentType.HTML),
+ job("@import url('styles.css');", ContentType.CSS)
);
assertNoErrors();
}
@@ -132,31 +132,31 @@
public final void testTypeAndMediaAttributes() throws Exception {
assertPipeline(
job("<link rel=stylesheet media=screen href=content:p+%7B%7D>",
- Job.JobType.HTML),
- job("", Job.JobType.HTML),
- job("@media screen {\n p {\n }\n}", Job.JobType.CSS));
+ ContentType.HTML),
+ job("", ContentType.HTML),
+ job("@media screen {\n p {\n }\n}", ContentType.CSS));
assertNoErrors();

assertPipeline(
job("<link rel=stylesheet type=text/css href=content:p+%7B%7D>",
- Job.JobType.HTML),
- job("", Job.JobType.HTML),
- job("p {\n}", Job.JobType.CSS));
+ ContentType.HTML),
+ job("", ContentType.HTML),
+ job("p {\n}", ContentType.CSS));
assertNoErrors();

assertPipeline(
job("<link rel=stylesheet media=all href=content:p+%7B%7D>",
- Job.JobType.HTML),
- job("", Job.JobType.HTML),
- job("p {\n}", Job.JobType.CSS));
+ ContentType.HTML),
+ job("", ContentType.HTML),
+ job("p {\n}", ContentType.CSS));
assertNoErrors();

assertPipeline(
job("<link rel=stylesheet media=braille,tty type=text/css"
+ " href=content:p+%7B%7D>",
- Job.JobType.HTML),
- job("", Job.JobType.HTML),
- job("@media braille, tty {\n p {\n }\n}", Job.JobType.CSS));
+ ContentType.HTML),
+ job("", ContentType.HTML),
+ job("@media braille, tty {\n p {\n }\n}", ContentType.CSS));
assertNoErrors();
}

@@ -167,14 +167,14 @@
+ "<script src=content:c(); defer=defer></script>"
+ "<script src=content:d(); defer=no></script>"
+ "<br>",
- Job.JobType.HTML),
+ ContentType.HTML),
job("<span jobnum=\"1\"></span><span jobnum=\"2\"></span><br />"
+ "<span jobnum=\"3\"></span><span jobnum=\"4\"></span>",
- Job.JobType.HTML),
- job("{ a(); }", Job.JobType.JAVASCRIPT),
- job("{ d(); }", Job.JobType.JAVASCRIPT),
- job("{ b(); }", Job.JobType.JAVASCRIPT),
- job("{ c(); }", Job.JobType.JAVASCRIPT));
+ ContentType.HTML),
+ job("{ a(); }", ContentType.JS),
+ job("{ d(); }", ContentType.JS),
+ job("{ b(); }", ContentType.JS),
+ job("{ c(); }", ContentType.JS));
assertNoErrors();
}

@@ -183,14 +183,14 @@
job("<script src=content:onerror=panic;></script>"
+ "<script src=\"http://bogus.com/bogus.js#'!\"></script>"
+ "<script src=content:foo()></script>",
- Job.JobType.HTML),
+ ContentType.HTML),
job("<span jobnum=\"1\"></span><span jobnum=\"2\"></span>"
+ "<span jobnum=\"3\"></span>",
- Job.JobType.HTML),
- job("{\n onerror = panic;\n}", Job.JobType.JAVASCRIPT),
+ ContentType.HTML),
+ job("{\n onerror = panic;\n}", ContentType.JS),
job("{\n throw new Error('Failed to load bogus.js#%27%21');\n}",
- Job.JobType.JAVASCRIPT),
- job("{ foo(); }", Job.JobType.JAVASCRIPT));
+ ContentType.JS),
+ job("{ foo(); }", ContentType.JS));
assertNoErrors();
}

@@ -201,7 +201,7 @@
boolean result = new ResolveUriStage(schema).apply(jobs)
&& new RewriteHtmlStage(schema).apply(jobs);
// Dump the extracted script bits on the queue.
- for (Job job : new ArrayList<Job>(jobs.getJobsByType(JobType.HTML))) {
+ for (Job job :
Lists.newArrayList(jobs.getJobsByType(ContentType.HTML))) {
Dom dom = job.getRoot().cast(Dom.class).node;
extractScripts(dom.getValue(), jobs);
}
@@ -217,7 +217,7 @@
int jobNum = jobs.getJobs().size();
el.setAttributeNS(
Namespaces.HTML_NAMESPACE_URI, "jobnum", "" + jobNum);
- jobs.getJobs().add(new Job(AncestorChain.instance(extracted)));
+ jobs.getJobs().add(Job.jsJob(AncestorChain.instance(extracted)));
}
for (Node c = el.getFirstChild(); c != null; c =
c.getNextSibling()) {
extractScripts(c, jobs);
=======================================
--- /trunk/tests/com/google/caja/plugin/templates/TemplateCompilerTest.java
Wed Dec 23 23:20:46 2009
+++ /trunk/tests/com/google/caja/plugin/templates/TemplateCompilerTest.java
Tue Dec 29 09:33:01 2009
@@ -40,10 +40,10 @@
import com.google.caja.reporting.MessagePart;
import com.google.caja.reporting.MessageType;
import com.google.caja.util.CajaTestCase;
+import com.google.caja.util.Lists;
import com.google.caja.util.Pair;

import java.net.URI;
-import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -677,10 +677,11 @@
private void assertSafeHtml(
List<DocumentFragment> inputs, DocumentFragment htmlGolden,
Block jsGolden, boolean checkErrors) throws ParseException {
- List<Node> html = new ArrayList<Node>();
- List<CssTree.StyleSheet> css = new ArrayList<CssTree.StyleSheet>();
+ List<Pair<Node, URI>> html = Lists.newArrayList();
+ List<CssTree.StyleSheet> css = Lists.newArrayList();
for (DocumentFragment input : inputs) {
- extractScriptsAndStyles(input, html, css);
+ extractScriptsAndStyles(
+ input, Nodes.getFilePositionFor(input).source().getUri(), html,
css);
}

TemplateCompiler tc = new TemplateCompiler(
@@ -702,10 +703,11 @@
}

private void extractScriptsAndStyles(
- Node n, List<Node> htmlOut, List<CssTree.StyleSheet> cssOut)
+ Node n, URI baseUri, List<Pair<Node, URI>> htmlOut,
+ List<CssTree.StyleSheet> cssOut)
throws ParseException {
n = extractScripts(n);
- htmlOut.add(n);
+ htmlOut.add(Pair.pair(n, baseUri));
extractStyles(n, cssOut);
}

Reply all
Reply to author
Forward
0 new messages