Skip to content

Ampersand in title causes parser to fail #79

Description

@jeffcjohnson

The parser will allow an ampersand in the title tag but fails in some instances. The following will cause it to treat all HTML that follows as plain text:

String htmlCode = "<html><head><title>&X</title></head</html>";

System.out.println(page.getTitleText());
prints &x</title></head</html> instead of &x.

The full code to reproduce:

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.StringWebResponse;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HTMLParser;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class HtmlUnitTest {
	public static void main(String[] args) {
		String htmlCode = "<html><head><title>&x</title></head</html>";
		try (WebClient webClient = new WebClient(BrowserVersion.BEST_SUPPORTED)) {
			StringWebResponse response = new StringWebResponse(htmlCode, new URL("http://http://htmlunit.sourceforge.net//test.html"));
			HtmlPage page = HTMLParser.parseHtml(response, webClient.getCurrentWindow());
			System.out.println(page.getTitleText());
			// work with the html page
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions