Reviewers: ihab.awad,
Please review this at http://codereview.appspot.com/90067
Affected files:
M src/com/google/caja/plugin/templates/SafeHtmlMaker.java
M tests/com/google/caja/plugin/templates/TemplateCompilerTest.java
Index: tests/com/google/caja/plugin/templates/TemplateCompilerTest.java
===================================================================
--- tests/com/google/caja/plugin/templates/TemplateCompilerTest.java
(revision 3549)
+++ tests/com/google/caja/plugin/templates/TemplateCompilerTest.java
(working copy)
@@ -298,6 +298,46 @@
);
}
+ public void test1050Finish() throws Exception {
+ // bug 1050, sometimes finish() is misplaced
+ assertSafeHtml(
+ htmlFragment(fromString(
+ ""
+ + "<div id=a></div>"
+ + "<div id=b></div>"
+ + "<script>1</script>")),
+ htmlFragment(fromString(
+ ""
+ + "<div id='id_1___'></div>"
+ + "<div id='id_2___'></div>")),
+ js(fromString(
+ ""
+ + "{"
+ + " var el___;"
+ + " var emitter___ = IMPORTS___.htmlEmitter___;"
+ + " el___ = emitter___.byId('id_1___');"
+ + " emitter___.setAttr(el___, 'id',"
+ + " 'a-' + IMPORTS___.getIdClass___());"
+ + " el___ = emitter___.byId('id_2___');"
+ + " emitter___.setAttr(el___, 'id',"
+ + " 'b-' + IMPORTS___.getIdClass___());"
+ + " el___ = emitter___.finish();"
+ + "}"
+ + "try {"
+ + " {"
+ + " 1;"
+ + " }"
+ + "} catch (ex___) {"
+ + " ___.getNewModuleHandler().handleUncaughtException(ex___,"
+ + " onerror, 'test1050Finish', '1');"
+ + "}"
+ + "{"
+ + " emitter___.signalLoaded();"
+ + "}"
+ ))
+ );
+ }
+
private void assertSafeHtml(
DocumentFragment input, DocumentFragment htmlGolden, Block jsGolden)
throws ParseException {
Index: src/com/google/caja/plugin/templates/SafeHtmlMaker.java
===================================================================
--- src/com/google/caja/plugin/templates/SafeHtmlMaker.java (revision 3549)
+++ src/com/google/caja/plugin/templates/SafeHtmlMaker.java (working copy)
@@ -241,17 +241,17 @@
fleshOutScriptBlock(((ScriptBone) bone).script);
} else {
NodeBone nb = (NodeBone) bone;
- boolean splitDom = i + 1 < n && bones.get(i + 1) instanceof
ScriptBone;
- if (splitDom && i + 1 == firstDeferredScriptIndex) {
- splitDom = false;
- finish();
- }
+ boolean splitDom = i + 1 < n && bones.get(i + 1) instanceof
ScriptBone
+ && i + 1 != firstDeferredScriptIndex;
if (splitDom) { start(); }
if (nb.node instanceof Text) {
fleshOutText((Text) nb.safeNode, splitDom);
} else {
fleshOutElement((Element) nb.node, (Element) nb.safeNode,
splitDom);
}
+ if (i + 1 == firstDeferredScriptIndex) {
+ finish();
+ }
}
}
finish();