Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XCOMMONS-2349: Improve performance of AttributeFilter in HTMLCleaner #159

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,16 @@
*/
package org.xwiki.xml.internal.html.filter;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.slf4j.Logger;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xwiki.component.annotation.Component;
import org.xwiki.xml.html.HTMLConstants;
Expand Down Expand Up @@ -61,48 +56,40 @@ public class AttributeFilter extends AbstractHTMLFilter
/**
* The map between HTML attribute names and the corresponding CSS property name.
*/
private static final Map<String, String> ATTRIBUTE_TO_CSS_PROPERTY = new HashMap<>();
private static final Map<String, String> ATTRIBUTE_TO_CSS_PROPERTY = new LinkedHashMap<>();

/**
* The 'vertical-align' CSS property.
*/
private static final String VERTICAL_ALIGN = "vertical-align";

/**
* The logger.
*/
@Inject
private Logger logger;

{
static {
ATTRIBUTE_TO_CSS_PROPERTY.put("align", "text-align");
ATTRIBUTE_TO_CSS_PROPERTY.put("valign", VERTICAL_ALIGN);
ATTRIBUTE_TO_CSS_PROPERTY.put("bgcolor", "background-color");
ATTRIBUTE_TO_CSS_PROPERTY.put("valign", VERTICAL_ALIGN);
}

@Override
public void filter(Document document, Map<String, String> cleaningParameters)
{
StringBuilder xpathExpression = new StringBuilder();
for (String attributeName : ATTRIBUTE_TO_CSS_PROPERTY.keySet()) {
if (xpathExpression.length() > 0) {
xpathExpression.append('|');
NodeList nodeList = document.getElementsByTagName("*");
for (int i = 0, len = nodeList.getLength(); i < len; i++) {
Node node = nodeList.item(i);
if (node instanceof Element) {
filterElement((Element) node);
}
xpathExpression.append("//@").append(attributeName);
}

NodeList attributes = null;
XPath xpath = XPathFactory.newInstance().newXPath();
try {
attributes = (NodeList) xpath.evaluate(xpathExpression.toString(), document, XPathConstants.NODESET);
} catch (XPathExpressionException e) {
// Shouldn't happen.
this.logger.error("Failed to apply the HTML attribute cleaning filter.", e);
return;
}
}

for (int i = 0; i < attributes.getLength(); i++) {
filterAttribute((Attr) attributes.item(i));
private void filterElement(Element element)
{
if (element.hasAttributes()) {
for (String attrName : ATTRIBUTE_TO_CSS_PROPERTY.keySet()) {
Attr attribute = element.getAttributeNode(attrName);
if (attribute != null) {
filterAttribute(attribute);
}
}
}
}

Expand Down