Skip to content

Commit

Permalink
fix bug with html parser class cast exception on empty content
Browse files Browse the repository at this point in the history
  • Loading branch information
nuviosoftware committed Jan 27, 2018
1 parent 251761c commit 51255a1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
38 changes: 38 additions & 0 deletions pdf-html/src/test/java/com/lowagie/text/html/HtmlParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.lowagie.text.html;

import static org.junit.Assert.assertNotNull;

import java.io.StringReader;

import org.junit.Test;

import com.lowagie.text.Document;
import com.lowagie.text.TextElementArray;

public class HtmlParserTest {

/**
* Bug fix scenario: a table with a space throws a {@link TextElementArray}
* class cast Exception. A table without spaces is parsed correctly.
*/
@Test
public void testParse_tableWithNoSpaces() {
Document doc1 = new Document();
doc1.open();
HtmlParser.parse(doc1, new StringReader("<table><tr><td>test</td></tr></table>")); // succeeds
assertNotNull(doc1);
}

/**
* Bug fix scenario: a table with a space throws a {@link TextElementArray}
* class cast Exception.
*/
@Test
public void testParse_tableWithSpaces() {
Document doc1 = new Document();
doc1.open();
HtmlParser.parse(doc1, new StringReader("<table> <tr><td>test</td></tr></table>")); // fails
assertNotNull(doc1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ public void startElement(String uri, String lname, String name,
handleStartingTags(name, attributes);
}

private boolean isNotBlank(String text) {
return text != null && !text.trim().isEmpty();
}

/**
* This method deals with the starting tags.
*
Expand All @@ -223,7 +227,7 @@ public void handleStartingTags(String name, Properties attributes) {
}

// maybe there is some meaningful data that wasn't between tags
if (currentChunk != null) {
if (currentChunk != null && isNotBlank(currentChunk.getContent())) {
TextElementArray current;
try {
current = (TextElementArray) stack.pop();
Expand Down

0 comments on commit 51255a1

Please sign in to comment.