diff --git a/xwiki-commons-core/xwiki-commons-xml/src/main/java/org/xwiki/xml/internal/html/filter/AttributeFilter.java b/xwiki-commons-core/xwiki-commons-xml/src/main/java/org/xwiki/xml/internal/html/filter/AttributeFilter.java index f2eed9a7cd..1acc78d7b2 100644 --- a/xwiki-commons-core/xwiki-commons-xml/src/main/java/org/xwiki/xml/internal/html/filter/AttributeFilter.java +++ b/xwiki-commons-core/xwiki-commons-xml/src/main/java/org/xwiki/xml/internal/html/filter/AttributeFilter.java @@ -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; @@ -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 ATTRIBUTE_TO_CSS_PROPERTY = new HashMap<>(); + private static final Map 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 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); + } + } } }