1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.xml.internal.html.filter

File AttributeFilter.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

10
27
3
1
127
72
12
0.44
9
3
4

Classes

Class Line # Actions
AttributeFilter 59 27 0% 12 2
0.9595%
 

Contributing tests

This file is covered by 109 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.xml.internal.html.filter;
21   
22    import java.util.HashMap;
23    import java.util.Map;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28    import javax.xml.xpath.XPath;
29    import javax.xml.xpath.XPathConstants;
30    import javax.xml.xpath.XPathExpressionException;
31    import javax.xml.xpath.XPathFactory;
32   
33    import org.slf4j.Logger;
34    import org.w3c.dom.Attr;
35    import org.w3c.dom.Document;
36    import org.w3c.dom.Element;
37    import org.w3c.dom.NodeList;
38    import org.xwiki.component.annotation.Component;
39    import org.xwiki.xml.html.HTMLConstants;
40    import org.xwiki.xml.html.filter.AbstractHTMLFilter;
41   
42    /**
43    * Filters attributes that are used by many different elements. Writing transformations for these attributes is tedious
44    * because tag transformation don't support matching all tags (e.g. using a wildcard character like '*') and so we need
45    * to duplicate the transformation for each element.
46    * <p>
47    * The following changes are made by this filter:
48    * <ul>
49    * <li>{@code align="value"} is replaced with {@code style="text-align:value"}</li>
50    * <li>{@code valign="value"} is replaced with {@code style="vertical-align:value"}</li>
51    * </ul>
52    *
53    * @version $Id: 7fe37d70c695b3610b4b0457a447b5ba2a493a52 $
54    * @since 4.3M1
55    */
56    @Component
57    @Named("attribute")
58    @Singleton
 
59    public class AttributeFilter extends AbstractHTMLFilter
60    {
61    /**
62    * The map between HTML attribute names and the corresponding CSS property name.
63    */
64    private static final Map<String, String> ATTRIBUTE_TO_CSS_PROPERTY = new HashMap<String, String>();
65   
66    /**
67    * The 'vertical-align' CSS property.
68    */
69    private static final String VERTICAL_ALIGN = "vertical-align";
70   
71    /**
72    * The logger.
73    */
74    @Inject
75    private Logger logger;
76   
 
77  136 toggle {
78  136 ATTRIBUTE_TO_CSS_PROPERTY.put("align", "text-align");
79  136 ATTRIBUTE_TO_CSS_PROPERTY.put("valign", VERTICAL_ALIGN);
80  136 ATTRIBUTE_TO_CSS_PROPERTY.put("bgcolor", "background-color");
81    }
82   
 
83  4985 toggle @Override
84    public void filter(Document document, Map<String, String> cleaningParameters)
85    {
86  4985 StringBuilder xpathExpression = new StringBuilder();
87  4985 for (String attributeName : ATTRIBUTE_TO_CSS_PROPERTY.keySet()) {
88  14955 if (xpathExpression.length() > 0) {
89  9970 xpathExpression.append('|');
90    }
91  14955 xpathExpression.append("//@").append(attributeName);
92    }
93   
94  4985 NodeList attributes = null;
95  4985 XPath xpath = XPathFactory.newInstance().newXPath();
96  4985 try {
97  4985 attributes = (NodeList) xpath.evaluate(xpathExpression.toString(), document, XPathConstants.NODESET);
98    } catch (XPathExpressionException e) {
99    // Shouldn't happen.
100  0 this.logger.error("Failed to apply the HTML attribute cleaning filter.", e);
101  0 return;
102    }
103   
104  4993 for (int i = 0; i < attributes.getLength(); i++) {
105  8 filterAttribute((Attr) attributes.item(i));
106    }
107    }
108   
 
109  8 toggle private void filterAttribute(Attr attribute)
110    {
111  8 Element element = attribute.getOwnerElement();
112  8 String property = ATTRIBUTE_TO_CSS_PROPERTY.get(attribute.getName());
113  8 String value = attribute.getValue();
114  8 if (HTMLConstants.TAG_IMG.equals(element.getTagName())
115    && HTMLConstants.ATTRIBUTE_ALIGN.equals(attribute.getName())) {
116    // We need to transform the align attribute differently when it is used on an image element.
117  5 property = "left".equals(value) || "right".equals(value) ? "float" : VERTICAL_ALIGN;
118    }
119  8 StringBuilder style = new StringBuilder(element.getAttribute(ATTRIBUTE_STYLE).trim());
120  8 if (style.length() > 0 && style.charAt(style.length() - 1) != ';') {
121  2 style.append(';');
122    }
123  8 style.append(property).append(':').append(value);
124  8 element.setAttribute(ATTRIBUTE_STYLE, style.toString());
125  8 element.removeAttributeNode(attribute);
126    }
127    }