8204330: Javadoc IllegalArgumentException: HTML special chars in constant value

Reviewed-by: ksrini
This commit is contained in:
Jonathan Gibbons 2018-06-18 13:52:30 -07:00
parent b75d70e381
commit 4d47f2e0a6
3 changed files with 40 additions and 17 deletions

View File

@ -176,8 +176,6 @@ public class HtmlDocletWriter {
HtmlTree fixedNavDiv = new HtmlTree(HtmlTag.DIV);
final static Pattern IMPROPER_HTML_CHARS = Pattern.compile(".*[&<>].*");
/**
* The window title of this file.
*/
@ -910,15 +908,7 @@ public class HtmlDocletWriter {
*/
public Content getDocLink(LinkInfoImpl.Kind context, TypeElement typeElement, Element element,
CharSequence label, boolean strong, boolean isProperty) {
return getDocLink(context, typeElement, element, new StringContent(check(label)), strong, isProperty);
}
CharSequence check(CharSequence s) {
Matcher m = IMPROPER_HTML_CHARS.matcher(s);
if (m.matches()) {
throw new IllegalArgumentException(s.toString());
}
return s;
return getDocLink(context, typeElement, element, new StringContent(label), strong, isProperty);
}
public Content getDocLink(LinkInfoImpl.Kind context, TypeElement typeElement, Element element,

View File

@ -406,9 +406,9 @@ public class TagletWriterImpl extends TagletWriter {
* {@inheritDoc}
*/
public Content valueTagOutput(VariableElement field, String constantVal, boolean includeLink) {
return includeLink ?
htmlWriter.getDocLink(LinkInfoImpl.Kind.VALUE_TAG, field,
constantVal, false) : new RawHtml(constantVal);
return includeLink
? htmlWriter.getDocLink(LinkInfoImpl.Kind.VALUE_TAG, field, constantVal, false)
: new StringContent(constantVal);
}
/**

View File

@ -23,7 +23,7 @@
/*
* @test
* @bug 4764045 8004825 8026567 8191030
* @bug 4764045 8004825 8026567 8191030 8204330
* @summary This test ensures that the value tag works in all
* use cases, the tests are explained below.
* @author jamieh
@ -33,6 +33,12 @@
* @run main TestValueTag
*/
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
public class TestValueTag extends JavadocTester {
public static void main(String... args) throws Exception {
@ -110,7 +116,7 @@ public class TestValueTag extends JavadocTester {
checkForException();
}
@Test()
@Test
void test2() {
javadoc("-Xdoclint:none",
"-d", "out2",
@ -133,7 +139,7 @@ public class TestValueTag extends JavadocTester {
checkForException();
}
@Test()
@Test
void test3() {
javadoc("-d", "out3",
"-sourcepath", testSrc,
@ -146,10 +152,37 @@ public class TestValueTag extends JavadocTester {
"The value2 is <a href=\"#CONSTANT\">\"constant\"</a>.",
"The value3 is <a href=\"../pkg2/Class3.html#TEST_12_PASSES\">"
+ "\"Test 12 passes\"</a>.");
checkForException();
}
@Test
void test4() throws IOException {
Path base = Paths.get("test4");
Path src = base.resolve("src");
Files.createDirectories(src.resolve("p"));
Files.write(src.resolve("p").resolve("C.java"), List.of(
"package p;",
"/** This class defines specialChars: {@value C#specialChars}. */",
"public class C {",
" /** The value is {@value}. */",
" public static final String specialChars = \"abc < def & ghi > jkl\";",
"}"));
javadoc("-d", base.resolve("out").toString(),
"-sourcepath", src.toString(),
"p");
checkExit(Exit.OK);
checkOutput("p/C.html", false,
"The value is \"abc < def & ghi > jkl\".");
checkOutput("p/C.html", true,
"The value is \"abc &lt; def &amp; ghi &gt; jkl\".");
checkForException();
}
void checkForException() {
checkOutput(Output.STDERR, false, "DocletAbortException");
checkOutput(Output.STDERR, false, "IllegalArgumentException");
}
}