Here you can find the source of removeJavascripts(Node node)
Parameter | Description |
---|---|
node | Node in text tree. |
private static Node removeJavascripts(Node node)
//package com.java2s; //License from project: Apache License import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import java.util.*; public class Main { /**/*from www.j a va 2s . c om*/ * List of event handler attributes that could contain javascript code. */ private static final List<String> eventAttributeList = Arrays.asList( "onclick", "onmousedown", "onmouseup", "onmouseover", "onmouseout", "onchange", "onsubmit", "onreset"); /** * Recursive method that traverses text node and removes any markup that could possibly contain javascript code. * @param node Node in text tree. * @return Node in text tree. */ private static Node removeJavascripts(Node node) { if (node == null) return node; //check for <script> tags if ("script".equalsIgnoreCase(node.getNodeName())) { node.getParentNode().removeChild(node); } //check for event handlers in attributes (onclick, etc.) NamedNodeMap attributes = node.getAttributes(); if (attributes != null) { for (int attrIdx = 0; attrIdx < attributes.getLength(); attrIdx++) { Node attr = attributes.item(attrIdx); for (String embedJsEvent : eventAttributeList) { if (embedJsEvent.equalsIgnoreCase(attr.getNodeName())) { attributes.removeNamedItem(attr.getNodeName()); } } } } if (node.hasChildNodes()) { NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { removeJavascripts(children.item(i)); } } return node; } }