JavaScriptScanner.java revision 3896:8e4dbcb99277
1/* 2 * Copyright (c) 2016, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25package jdk.javadoc.internal.doclets.toolkit.util; 26 27 28import java.util.List; 29import java.util.Locale; 30import java.util.function.Consumer; 31 32import com.sun.source.doctree.AttributeTree; 33import com.sun.source.doctree.DocCommentTree; 34import com.sun.source.doctree.DocTree; 35import com.sun.source.doctree.DocTree.Kind; 36import com.sun.source.doctree.StartElementTree; 37import com.sun.source.util.DocTreePath; 38import com.sun.source.util.DocTreePathScanner; 39import com.sun.source.util.TreePath; 40import com.sun.tools.javac.util.DefinedBy; 41import com.sun.tools.javac.util.DefinedBy.Api; 42 43/** 44 * A DocTree scanner to detect use of JavaScript in a doc comment tree. 45 */ 46public class JavaScriptScanner extends DocTreePathScanner<Void, Consumer<DocTreePath>> { 47 48 public Void scan(DocCommentTree tree, TreePath p, Consumer<DocTreePath> f) { 49 return scan(new DocTreePath(p, tree), f); 50 } 51 52 @Override @DefinedBy(Api.COMPILER_TREE) 53 public Void visitStartElement(StartElementTree tree, Consumer<DocTreePath> f) { 54 String name = tree.getName().toString(); 55 if (name.equalsIgnoreCase("script")) 56 f.accept(getCurrentPath()); 57 return super.visitStartElement(tree, f); 58 } 59 60 @Override @DefinedBy(Api.COMPILER_TREE) 61 public Void visitAttribute(AttributeTree tree, Consumer<DocTreePath> f) { 62 String name = tree.getName().toString().toLowerCase(Locale.ENGLISH); 63 switch (name) { 64 // See https://www.w3.org/TR/html-markup/global-attributes.html#common.attrs.event-handler 65 case "onabort": case "onblur": case "oncanplay": case "oncanplaythrough": 66 case "onchange": case "onclick": case "oncontextmenu": case "ondblclick": 67 case "ondrag": case "ondragend": case "ondragenter": case "ondragleave": 68 case "ondragover": case "ondragstart": case "ondrop": case "ondurationchange": 69 case "onemptied": case "onended": case "onerror": case "onfocus": case "oninput": 70 case "oninvalid": case "onkeydown": case "onkeypress": case "onkeyup": 71 case "onload": case "onloadeddata": case "onloadedmetadata": case "onloadstart": 72 case "onmousedown": case "onmousemove": case "onmouseout": case "onmouseover": 73 case "onmouseup": case "onmousewheel": case "onpause": case "onplay": 74 case "onplaying": case "onprogress": case "onratechange": case "onreadystatechange": 75 case "onreset": case "onscroll": case "onseeked": case "onseeking": 76 case "onselect": case "onshow": case "onstalled": case "onsubmit": case "onsuspend": 77 case "ontimeupdate": case "onvolumechange": case "onwaiting": 78 79 // See https://www.w3.org/TR/html4/sgml/dtd.html 80 // Most of the attributes that take a %Script are also defined as event handlers 81 // in HTML 5. The one exception is onunload. 82 // case "onchange": case "onclick": case "ondblclick": case "onfocus": 83 // case "onkeydown": case "onkeypress": case "onkeyup": case "onload": 84 // case "onmousedown": case "onmousemove": case "onmouseout": case "onmouseover": 85 // case "onmouseup": case "onreset": case "onselect": case "onsubmit": 86 case "onunload": 87 f.accept(getCurrentPath()); 88 break; 89 90 // See https://www.w3.org/TR/html4/sgml/dtd.html 91 // https://www.w3.org/TR/html5/ 92 // These are all the attributes that take a %URI or a valid URL potentially surrounded 93 // by spaces 94 case "action": case "cite": case "classid": case "codebase": case "data": 95 case "datasrc": case "for": case "href": case "longdesc": case "profile": 96 case "src": case "usemap": 97 List<? extends DocTree> value = tree.getValue(); 98 if (!value.isEmpty() && value.get(0).getKind() == Kind.TEXT) { 99 String v = value.get(0).toString().trim().toLowerCase(Locale.ENGLISH); 100 if (v.startsWith("javascript:")) { 101 f.accept(getCurrentPath()); 102 } 103 } 104 break; 105 } 106 return super.visitAttribute(tree, f); 107 } 108 109 /** 110 * Used to indicate a fault when parsing, typically used in 111 * lambda methods. 112 */ 113 public static class Fault extends RuntimeException { 114 private static final long serialVersionUID = 0L; 115 } 116} 117