Revision: 4169
Author: jasvir
Date: Sun Jul 11 10:41:54 2010
Log: NPE when the first line of an HTML file being parsed is a comment
http://codereview.appspot.com/1770042
When a comment is added, CajaTreeBuilder assumes the document already has a
parent node. This is not
true if the comment appears as the first line.
Changed processComment to add use a document fragment as a parent node when
processing a comment
[email protected]
http://code.google.com/p/google-caja/source/detail?r=4169
Modified:
/trunk/src/com/google/caja/parser/html/CajaTreeBuilder.java
/trunk/tests/com/google/caja/parser/html/DomParserTest.java
=======================================
--- /trunk/src/com/google/caja/parser/html/CajaTreeBuilder.java Tue Jun 1
15:11:38 2010
+++ /trunk/src/com/google/caja/parser/html/CajaTreeBuilder.java Sun Jul 11
10:41:54 2010
@@ -144,7 +144,12 @@
@Override
protected void appendCommentToDocument(char[] buf, int start, int
length) {
- appendComment(doc.getDocumentElement(), buf, start, length);
+ Node el = doc.getDocumentElement();
+ if (null == el) {
+ el = doc.createDocumentFragment();
+ doc.appendChild(el);
+ }
+ appendComment(el, buf, start, length);
}
@Override
=======================================
--- /trunk/tests/com/google/caja/parser/html/DomParserTest.java Tue Jun 1
16:47:13 2010
+++ /trunk/tests/com/google/caja/parser/html/DomParserTest.java Sun Jul 11
10:41:54 2010
@@ -231,6 +231,14 @@
assertEquals(config, DOM2_HTML_RENDERED_GOLDEN,
Nodes.render(document));
}
}
+
+ public final void testParseFirstLineComment() throws Exception {
+ String DOM_WITH_COMMENT = "<!-- This is a comment -->\n" + DOM2_HTML;
+ TokenQueue<HtmlTokenType> tq = tokenizeTestInput(
+ DOM_WITH_COMMENT, false, true);
+ Element el = new DomParser(tq, false, mq).parseDocument();
+ assertEquals(DOM2_HTML_RENDERED_GOLDEN, Nodes.render(el));
+ }
public final void testParseXmlFragmentManyWays() throws Exception {
for (DomParser p : allPossibleConfigurations(DOM3_XML, false)) {