Skip to content

Commit e2a461b

Browse files
committed
8351332: Line breaks in search tag descriptions corrupt JSON search index
Reviewed-by: hannesw, liach
1 parent c14bbea commit e2a461b

File tree

5 files changed

+97
-5
lines changed

5 files changed

+97
-5
lines changed

src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlDocletWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1890,7 +1890,7 @@ && equalsIgnoreCase(endElement.getName(), tagName)) {
18901890
}
18911891
// Generate index item
18921892
if (!headingContent.isEmpty() && configuration.indexBuilder != null) {
1893-
String tagText = headingContent.replaceAll("\\s+", " ");
1893+
String tagText = utils.normalizeWhitespace(headingContent);
18941894
IndexItem item = IndexItem.of(element, node, tagText,
18951895
getTagletWriterInstance(context).getHolderName(element),
18961896
"",

src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/taglets/IndexTaglet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ public Content getInlineTagOutput(Element element, DocTree tag, TagletWriter tag
5858
if (tagText.charAt(0) == '"' && tagText.charAt(tagText.length() - 1) == '"') {
5959
tagText = tagText.substring(1, tagText.length() - 1);
6060
}
61-
tagText = tagText.replaceAll("\\s+", " ");
61+
tagText = utils.normalizeWhitespace(tagText);
6262

6363
Content desc = tagletWriter.htmlWriter.commentTagsToContent(element, indexTree.getDescription(), context.within(indexTree));
64-
String descText = extractText(desc);
64+
String descText = utils.normalizeWhitespace(extractText(desc));
6565

6666
return tagletWriter.createAnchorAndSearchIndex(element, tagText, descText, tag);
6767
}

src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/taglets/SpecTaglet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private Content specTagToContent(Element holder, SpecTree specTree) {
123123
List<? extends DocTree> specTreeLabel = specTree.getTitle();
124124
Content label = htmlWriter.commentTagsToContent(holder, specTreeLabel, tagletWriter.context.isFirstSentence);
125125
return getExternalSpecContent(holder, specTree, specTreeURL,
126-
textOf(label).replaceAll("\\s+", " "), label);
126+
utils.normalizeWhitespace(textOf(label)), label);
127127
}
128128

129129
// this is here, for now, but might be a useful addition elsewhere,

src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/Utils.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1132,6 +1132,15 @@ public String replaceTabs(String text) {
11321132
return result.toString();
11331133
}
11341134

1135+
/**
1136+
* Replaces each group of one or more whitespace characters with a single canonical space
1137+
* @param s the string to be normalized
1138+
* @return normalized string
1139+
*/
1140+
public String normalizeWhitespace(String s) {
1141+
return s.replaceAll("\\s+", " ");
1142+
}
1143+
11351144
/**
11361145
* Returns a locale independent lower cased String. That is, it
11371146
* always uses US locale, this is a clone of the one in StringUtils.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8351332
27+
* @summary Line breaks in the description of `{@index}` tags may corrupt JSON search index
28+
* @library /tools/lib ../../lib
29+
* @modules jdk.javadoc/jdk.javadoc.internal.tool
30+
* @build toolbox.ToolBox javadoc.tester.*
31+
* @run main TestIndexLineBreaks
32+
*/
33+
34+
import java.io.IOException;
35+
import java.nio.file.Path;
36+
37+
import javadoc.tester.JavadocTester;
38+
import toolbox.ToolBox;
39+
40+
public class TestIndexLineBreaks extends JavadocTester {
41+
42+
public static void main(String... args) throws Exception {
43+
var tester = new TestIndexLineBreaks ();
44+
tester.runTests();
45+
}
46+
47+
ToolBox tb = new ToolBox();
48+
49+
@Test
50+
public void test() throws IOException {
51+
Path src = Path.of("src");
52+
tb.writeJavaFiles(src,
53+
"""
54+
package p;
55+
public interface I {
56+
/**
57+
*
58+
* The {@index "phrase1
59+
* phrase2" description1
60+
* description2 }
61+
*/
62+
int a();
63+
}
64+
""");
65+
66+
javadoc("-d",
67+
"out",
68+
"-sourcepath",
69+
src.toString(),
70+
"p");
71+
72+
checkExit(Exit.OK);
73+
74+
checkOutput("tag-search-index.js", true,
75+
"""
76+
{"l":"phrase1 phrase2","h":"p.I.a()","d":"description1 description2 ","u":"p/I.html#phrase1phrase2"},{"l":"Search Tags","h":"","k":"18","u":"search-tags.html"}""");
77+
78+
checkOutput("tag-search-index.js", false,
79+
"""
80+
{"l":"phrase1 phrase2","h":"p.I.a()","d":"description1
81+
description2 ","u":"p/I.html#phrase1phrase2"},{"l":"Search Tags","h":"","k":"18","u":"search-tags.html"}""");
82+
}
83+
}

0 commit comments

Comments
 (0)