Skip to content

Commit 5cad0b4

Browse files
nizarbenallaliach
authored andcommitted
8322708: Global HTML attributes are not allowed
Reviewed-by: jjg
1 parent 6420846 commit 5cad0b4

File tree

4 files changed

+260
-24
lines changed

4 files changed

+260
-24
lines changed

src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclint/Checker.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,9 @@ public Void visitAttribute(AttributeTree tree, Void ignore) {
690690
}
691691
// for now, doclint allows all attribute names beginning with "on" as event handler names,
692692
// without checking the validity or applicability of the name
693-
if (!name.toString().startsWith("on")) {
693+
// custom "data-*" attributes are also accepted
694+
var attrName = name.toString();
695+
if (!attrName.startsWith("on") && !attrName.startsWith("data-")) {
694696
AttrKind k = currTag.getAttrKind(name);
695697
switch (k) {
696698
case OK -> { }

src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclint/HtmlTag.java

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2024, 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
@@ -434,57 +434,76 @@ public enum Flag {
434434

435435
public enum Attr {
436436
ABBR,
437+
ACCESSKEY(true),
437438
ALIGN,
438439
ALINK,
439440
ALT,
440-
ARIA_ACTIVEDESCENDANT,
441-
ARIA_CONTROLS,
442-
ARIA_DESCRIBEDBY,
443-
ARIA_EXPANDED,
444-
ARIA_LABEL,
445-
ARIA_LABELLEDBY,
446-
ARIA_LEVEL,
447-
ARIA_MULTISELECTABLE,
448-
ARIA_OWNS,
449-
ARIA_POSINSET,
450-
ARIA_SETSIZE,
451-
ARIA_READONLY,
452-
ARIA_REQUIRED,
453-
ARIA_SELECTED,
454-
ARIA_SORT,
441+
ARIA_ACTIVEDESCENDANT(true),
442+
ARIA_CONTROLS(true),
443+
ARIA_DESCRIBEDBY(true),
444+
ARIA_EXPANDED(true),
445+
ARIA_LABEL(true),
446+
ARIA_LABELLEDBY(true),
447+
ARIA_LEVEL(true),
448+
ARIA_MULTISELECTABLE(true),
449+
ARIA_OWNS(true),
450+
ARIA_POSINSET(true),
451+
ARIA_READONLY(true),
452+
ARIA_REQUIRED(true),
453+
ARIA_SELECTED(true),
454+
ARIA_SETSIZE(true),
455+
ARIA_SORT(true),
456+
AUTOCAPITALIZE(true),
457+
AUTOFOCUS(true),
455458
AXIS,
456459
BACKGROUND,
457460
BGCOLOR,
458461
BORDER,
459-
CELLSPACING,
460462
CELLPADDING,
463+
CELLSPACING,
461464
CHAR,
462465
CHAROFF,
463466
CHARSET,
464467
CITE,
468+
CLASS(true),
465469
CLEAR,
466-
CLASS,
467470
COLOR,
468471
COLSPAN,
469472
COMPACT,
473+
CONTENTEDITABLE(true),
470474
COORDS,
471475
CROSSORIGIN,
472476
DATETIME,
477+
DIR(true),
478+
DRAGGABLE(true),
479+
ENTERKEYHINT(true),
473480
FACE,
474481
FRAME,
475482
FRAMEBORDER,
476483
HEADERS,
477484
HEIGHT,
485+
HIDDEN(true),
478486
HREF,
479487
HSPACE,
480-
ID,
488+
ID(true),
489+
INERT(true),
490+
INPUTMODE(true),
491+
IS(true),
492+
ITEMID(true),
493+
ITEMPROP(true),
494+
ITEMREF(true),
495+
ITEMSCOPE(true),
496+
ITEMTYPE(true),
497+
LANG(true),
481498
LINK,
482499
LONGDESC,
483500
MARGINHEIGHT,
484501
MARGINWIDTH,
485502
NAME,
503+
NONCE(true),
486504
NOSHADE,
487505
NOWRAP,
506+
POPOVER(true),
488507
PROFILE,
489508
REV,
490509
REVERSED,
@@ -497,24 +516,39 @@ public enum Attr {
497516
SHAPE,
498517
SIZE,
499518
SPACE,
519+
SPELLCHECK(true),
500520
SRC,
501521
START,
502-
STYLE,
522+
STYLE(true),
503523
SUMMARY,
524+
TABINDEX(true),
504525
TARGET,
505526
TEXT,
527+
TITLE(true),
528+
TRANSLATE(true),
506529
TYPE,
507530
VALIGN,
508531
VALUE,
509532
VERSION,
510533
VLINK,
511534
VSPACE,
512-
WIDTH;
535+
WIDTH,
536+
WRITINGSUGGESTIONS(true);
513537

514538
private final String name;
539+
private final boolean isGlobal;
515540

516541
Attr() {
542+
this(false);
543+
}
544+
545+
Attr(boolean flag) {
517546
name = StringUtils.toLowerCase(name().replace("_", "-"));
547+
isGlobal = flag;
548+
}
549+
550+
public boolean isGlobal() {
551+
return isGlobal;
518552
}
519553

520554
public String getText() {
@@ -632,8 +666,13 @@ public Attr getAttr(Name attrName) {
632666
}
633667

634668
public AttrKind getAttrKind(Name attrName) {
635-
AttrKind k = attrs.get(getAttr(attrName)); // null-safe
636-
return (k == null) ? AttrKind.INVALID : k;
669+
Attr attr = getAttr(attrName);
670+
if (attr == null) {
671+
return AttrKind.INVALID;
672+
}
673+
return attr.isGlobal() ?
674+
AttrKind.OK :
675+
attrs.getOrDefault(attr, AttrKind.INVALID);
637676
}
638677

639678
private static AttrMap attrs(AttrKind k, Attr... attrs) {
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (c) 2024, 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 8322708
27+
* @summary Test to make sure global tags work properly
28+
* @library /tools/lib ../../lib
29+
* @modules jdk.javadoc/jdk.javadoc.internal.tool
30+
* @build toolbox.ToolBox javadoc.tester.*
31+
* @run main TestGlobalHtml
32+
*/
33+
34+
import javadoc.tester.JavadocTester;
35+
import toolbox.ToolBox;
36+
37+
import java.nio.file.Path;
38+
39+
public class TestGlobalHtml extends JavadocTester {
40+
ToolBox tb = new ToolBox();
41+
42+
public static void main(String... args) throws Exception {
43+
var tester = new TestGlobalHtml();
44+
tester.runTests();
45+
}
46+
47+
@Test
48+
public void testGlobalTags() {
49+
javadoc("--allow-script-in-comments",
50+
"-d",
51+
"out-global",
52+
"-sourcepath",
53+
testSrc,
54+
"pkg1");
55+
checkExit(Exit.OK);
56+
}
57+
58+
@Test
59+
public void testNegative(Path base) throws Exception {
60+
Path src = base.resolve("src");
61+
tb.writeJavaFiles(src,
62+
"""
63+
package p;
64+
/**
65+
* class comment
66+
* <a href="https://openjdk.org/">Hyperlink to the OpenJDK website</a>
67+
*/
68+
public class C {
69+
/**
70+
* <form>
71+
* <label for="methodname">Method name:</label><br>
72+
* <input type="text" id="methodname" name="methodname"><br>
73+
* <label for="paramname">Method Parameter:</label><br>
74+
* <input type="text" id="paramname" name="paramname">
75+
* </form>
76+
*/
77+
public C() {
78+
}
79+
}
80+
""");
81+
82+
javadoc("--allow-script-in-comments",
83+
"-d",
84+
"out-negative",
85+
"-sourcepath",
86+
src.toString(),
87+
"p");
88+
checkExit(Exit.ERROR);
89+
}
90+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright (c) 2024, 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+
package pkg1;
25+
26+
27+
/**
28+
* <div inert>
29+
* <p> This content is inert and not interactable.</p>
30+
* <a href="https://openjdk.org/" title="OpenJDK's Website" tabindex="0">
31+
* Visit OpenJDK's Website!
32+
* </a>
33+
* </div>
34+
*
35+
* <div>
36+
* <p autocapitalize="on">This content is interactable.</p>
37+
* <a href="https://openjdk.org/" title="OpenJDK's Website" tabindex="0">
38+
* Visit OpenJDK's Website!
39+
* </a>
40+
* </div>
41+
*
42+
*
43+
* <div dir="ltr" lang="en">
44+
* <p itemprop="description">This is used in a jtreg test to check that global HTML tags are allowed</p>
45+
* <ul spellcheck="true">
46+
* <li>Class C</li>
47+
* <li>Has a default constructor</li>
48+
* </ul>
49+
* </div>
50+
*
51+
* <p contenteditable="true" inputmode="text">Here is a description of the class and methods:</p>
52+
*
53+
* <ol draggable="true" tabindex="0">
54+
* <li><p accesskey="1" data-element-type="constructor" title="Class Details">Has a default constructor</p></li>
55+
* <li><p accesskey="2" data-element-type="toString" title="Methods Summary">Overrides toString method</p></li>
56+
* <li><p accesskey="3" data-element-type="other" title="Usage Example">Is used for testing</p></li>
57+
* </ol>
58+
*
59+
* <div itemscope>
60+
* <p itemprop="name">C1</p>
61+
* <p itemprop="description">C1</p>
62+
* </div>
63+
*/
64+
public class C1 {
65+
66+
/**
67+
* <p lang="en" accesskey="D" autocapitalize="on" draggable="true" spellcheck="false">
68+
* Default constructor for the {@code C1} class. (this content is draggable!) </p>
69+
* <div lang="en" contenteditable="true">
70+
* <p itemprop="creator">Author: try editing this content!</p>
71+
* <p title="Creation Date">Created on: June 14 2024</p>
72+
* </div>
73+
*/
74+
public C1() {
75+
}
76+
77+
/**
78+
* A method in C1
79+
*
80+
* <p lang="en" inputmode="numeric">simple method.</p>
81+
*
82+
* <div itemprop="method" itemscope>
83+
* <p itemprop="name">method m</p>
84+
* <p itemprop="description">the method m does nothing</p>
85+
* </div>
86+
*/
87+
public void m() {
88+
}
89+
90+
/**
91+
* A toString Override.
92+
*
93+
* <p dir="ltr" spellcheck="true">returns a String Object.</p>
94+
*
95+
* <div itemprop="method" itemscope>
96+
* <p itemprop="name">toString</p>
97+
* </div>
98+
*
99+
* @return a string.
100+
*/
101+
@Override
102+
public String toString() {
103+
return "C1";
104+
}
105+
}

0 commit comments

Comments
 (0)