Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
/ jdk21 Public archive

Commit

Permalink
8309471: Limit key characters in static index pages
Browse files Browse the repository at this point in the history
Reviewed-by: jjg
Backport-of: 21f6d83358458c2e5a6cc1bc19a6a33b3262f990
  • Loading branch information
hns committed Jun 26, 2023
1 parent 63d419e commit 8748b46
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,14 @@ private boolean shouldIndex(Element element) {
}

private static Character keyCharacter(String s) {
return s.isEmpty() ? '*' : Character.toUpperCase(s.charAt(0));
// Use first valid java identifier start character as key,
// or '*' for strings that do not contain one.
for (int i = 0; i < s.length(); i++) {
if (Character.isJavaIdentifierStart(s.charAt(i))) {
return Character.toUpperCase(s.charAt(i));
}
}
return '*';
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/*
* @test
* @bug 8289332 8286470
* @bug 8289332 8286470 8309471
* @summary Auto-generate ids for user-defined headings
* @library /tools/lib ../../lib
* @modules jdk.javadoc/jdk.javadoc.internal.tool
Expand Down Expand Up @@ -59,9 +59,9 @@ public void testAutoHeaderId(Path base) throws Exception {
/**
* First sentence.
*
* <h2>First Header</h2>
* <h2>1.0 First Header</h2>
*
* <h3 id="fixed-id-1">Header with ID</h3>
* <h3 id="fixed-id-1">1.1 Header with ID</h3>
*
* <h4><a id="fixed-id-2">Embedded A-Tag with ID</a></h4>
*
Expand All @@ -73,13 +73,13 @@ public void testAutoHeaderId(Path base) throws Exception {
*
* <h4>Duplicate Text</h4>
*
* <h2>Extra (#*!. chars</h2>
* <h2>2.0 Extra (#*!. chars</h2>
*
* <h3 style="color: red;" class="some-class">Other attributes</h3>
*
* <h4></h4>
*
* <h2> Multi-line
* <h2> 3.0 Multi-line
* heading with extra
* whitespace</h2>
*
Expand All @@ -94,13 +94,18 @@ public class C {
javadoc("-d", base.resolve("api").toString(),
"-sourcepath", src.toString(),
"--no-platform-links", "p");
checkIds();
checkSearchIndex();
checkHtmlIndex();
}

private void checkIds() {
checkOutput("p/C.html", true,
"""
<h2 id="first-header-heading">First Header</h2>
<h2 id="1-0-first-header-heading">1.0 First Header</h2>
""",
"""
<h3 id="fixed-id-1">Header with ID</h3>
<h3 id="fixed-id-1">1.1 Header with ID</h3>
""",
"""
<h4><a id="fixed-id-2">Embedded A-Tag with ID</a></h4>
Expand All @@ -118,7 +123,7 @@ public class C {
<h4 id="duplicate-text-heading1">Duplicate Text</h4>
""",
"""
<h2 id="extra-chars-heading">Extra (#*!. chars</h2>
<h2 id="2-0-extra-chars-heading">2.0 Extra (#*!. chars</h2>
""",
"""
<h3 id="other-attributes-heading" style="color: red;" class="some-class">Other attributes</h3>
Expand All @@ -127,9 +132,12 @@ public class C {
<h4 id="-heading"></h4>
""",
"""
<h2 id="multi-line-heading-with-extra-whitespace-heading"> Multi-line
<h2 id="3-0-multi-line-heading-with-extra-whitespace-heading"> 3.0 Multi-line
heading with extra
whitespace</h2>""");
}

private void checkSearchIndex() {
checkOutput("tag-search-index.js", true,
"""
{"l":"Duplicate Text","h":"class p.C","d":"Section","u":"p/C.html#duplicate-text-heading"}""",
Expand All @@ -142,15 +150,87 @@ public class C {
"""
{"l":"Embedded Link Tag","h":"class p.C","d":"Section","u":"p/C.html#embedded-link-tag-heading"}""",
"""
{"l":"Extra (#*!. chars","h":"class p.C","d":"Section","u":"p/C.html#extra-chars-heading"}""",
{"l":"2.0 Extra (#*!. chars","h":"class p.C","d":"Section","u":"p/C.html#2-0-extra-chars-heading"}""",
"""
{"l":"First Header","h":"class p.C","d":"Section","u":"p/C.html#first-header-heading"}""",
{"l":"1.0 First Header","h":"class p.C","d":"Section","u":"p/C.html#1-0-first-header-heading"}""",
"""
{"l":"Header with ID","h":"class p.C","d":"Section","u":"p/C.html#fixed-id-1"}""",
{"l":"1.1 Header with ID","h":"class p.C","d":"Section","u":"p/C.html#fixed-id-1"}""",
"""
{"l":"Multi-line heading with extra whitespace","h":"class p.C","d":"Section","u":"p/C.html\
#multi-line-heading-with-extra-whitespace-heading"}""",
{"l":"3.0 Multi-line heading with extra whitespace","h":"class p.C","d":"Section","u":"p/C.html\
#3-0-multi-line-heading-with-extra-whitespace-heading"}""",
"""
{"l":"Other attributes","h":"class p.C","d":"Section","u":"p/C.html#other-attributes-heading"}""");
}

private void checkHtmlIndex() {
// Make sure section links are not included in static index pages
checkOutput("index-all.html", true,
"""
<a href="#I:C">C</a>&nbsp;<a href="#I:D">D</a>&nbsp;<a href="#I:E">E</a>&nbsp;<a href="#I\
:F">F</a>&nbsp;<a href="#I:H">H</a>&nbsp;<a href="#I:M">M</a>&nbsp;<a href="#I:O">O</a>&n\
bsp;<a href="#I:P">P</a>&nbsp;<br><a href="allclasses-index.html">All&nbsp;Classes&nbsp;a\
nd&nbsp;Interfaces</a><span class="vertical-separator">|</span><a href="allpackages-index\
.html">All&nbsp;Packages</a>
<h2 class="title" id="I:C">C</h2>
<dl class="index">
<dt><a href="p/C.html" class="type-name-link" title="class in p">C</a> - Class in <a href\
="p/package-summary.html">p</a></dt>
<dd>
<div class="block">First sentence.</div>
</dd>
</dl>
<h2 class="title" id="I:D">D</h2>
<dl class="index">
<dt><a href="p/C.html#duplicate-text-heading" class="search-tag-link">Duplicate Text</a> \
- Search tag in class p.C</dt>
<dd>Section</dd>
<dt><a href="p/C.html#duplicate-text-heading1" class="search-tag-link">Duplicate Text</a>\
- Search tag in class p.C</dt>
<dd>Section</dd>
</dl>
<h2 class="title" id="I:E">E</h2>
<dl class="index">
<dt><a href="p/C.html#2-0-extra-chars-heading" class="search-tag-link">2.0 Extra (#*!. ch\
ars</a> - Search tag in class p.C</dt>
<dd>Section</dd>
<dt><a href="p/C.html#fixed-id-2" class="search-tag-link">Embedded A-Tag with ID</a> - Se\
arch tag in class p.C</dt>
<dd>Section</dd>
<dt><a href="p/C.html#embedded-code-tag-heading" class="search-tag-link">Embedded Code Ta\
g</a> - Search tag in class p.C</dt>
<dd>Section</dd>
<dt><a href="p/C.html#embedded-link-tag-heading" class="search-tag-link">Embedded Link Ta\
g</a> - Search tag in class p.C</dt>
<dd>Section</dd>
</dl>
<h2 class="title" id="I:F">F</h2>
<dl class="index">
<dt><a href="p/C.html#1-0-first-header-heading" class="search-tag-link">1.0 First Header<\
/a> - Search tag in class p.C</dt>
<dd>Section</dd>
</dl>
<h2 class="title" id="I:H">H</h2>
<dl class="index">
<dt><a href="p/C.html#fixed-id-1" class="search-tag-link">1.1 Header with ID</a> - Search\
tag in class p.C</dt>
<dd>Section</dd>
</dl>
<h2 class="title" id="I:M">M</h2>
<dl class="index">
<dt><a href="p/C.html#3-0-multi-line-heading-with-extra-whitespace-heading" class="search\
-tag-link">3.0 Multi-line heading with extra whitespace</a> - Search tag in class p.C</dt>
<dd>Section</dd>
</dl>
<h2 class="title" id="I:O">O</h2>
<dl class="index">
<dt><a href="p/C.html#other-attributes-heading" class="search-tag-link">Other attributes<\
/a> - Search tag in class p.C</dt>
<dd>Section</dd>
</dl>
<h2 class="title" id="I:P">P</h2>
<dl class="index">
<dt><a href="p/package-summary.html">p</a> - package p</dt>
<dd>&nbsp;</dd>
</dl>""");
}
}

1 comment on commit 8748b46

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.