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

File FontFilter.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

12
34
2
1
102
63
9
0.26
17
2
4.5

Classes

Class Line # Actions
FontFilter 43 34 0% 9 5
0.895833389.6%
 

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.List;
24    import java.util.Map;
25   
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28   
29    import org.w3c.dom.Document;
30    import org.w3c.dom.Element;
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.xml.html.filter.AbstractHTMLFilter;
33   
34    /**
35    * Replaces invalid <font> tags with equivalent <span> tags using inline css rules.
36    *
37    * @version $Id: 172a44aaed0eca121a9d7ea0c20c81b12a78238d $
38    * @since 1.8RC2
39    */
40    @Component
41    @Named("font")
42    @Singleton
 
43    public class FontFilter extends AbstractHTMLFilter
44    {
45    /**
46    * A map holding the translation from 'size' attribute of html font tag to 'font-size' css property.
47    */
48    private static final Map<String, String> FONT_SIZE_MAP;
49   
 
50  34 toggle static {
51  34 FONT_SIZE_MAP = new HashMap<String, String>();
52  34 FONT_SIZE_MAP.put("1", "0.6em");
53  34 FONT_SIZE_MAP.put("2", "0.8em");
54  34 FONT_SIZE_MAP.put("3", "1.0em");
55  34 FONT_SIZE_MAP.put("4", "1.2em");
56  34 FONT_SIZE_MAP.put("5", "1.4em");
57  34 FONT_SIZE_MAP.put("6", "1.6em");
58  34 FONT_SIZE_MAP.put("7", "1.8em");
59  34 FONT_SIZE_MAP.put("-3", "0.4em");
60  34 FONT_SIZE_MAP.put("-2", FONT_SIZE_MAP.get("1"));
61  34 FONT_SIZE_MAP.put("-1", FONT_SIZE_MAP.get("2"));
62  34 FONT_SIZE_MAP.put("+1", FONT_SIZE_MAP.get("4"));
63  34 FONT_SIZE_MAP.put("+2", FONT_SIZE_MAP.get("5"));
64  34 FONT_SIZE_MAP.put("+3", FONT_SIZE_MAP.get("6"));
65    }
66   
67    /**
68    * {@inheritDoc}
69    *
70    * <p>The {@link FontFilter} does not use any cleaningParameters passed in.</p>
71    */
 
72  4985 toggle @Override
73    public void filter(Document document, Map<String, String> cleaningParameters)
74    {
75  4985 List<Element> fontTags = filterDescendants(document.getDocumentElement(), new String[] { TAG_FONT });
76  4985 for (Element fontTag : fontTags) {
77  2 Element span = document.createElement(TAG_SPAN);
78  2 moveChildren(fontTag, span);
79  2 StringBuffer buffer = new StringBuffer();
80  2 if (fontTag.hasAttribute(ATTRIBUTE_FONTCOLOR)) {
81  1 buffer.append(String.format("color:%s;", fontTag.getAttribute(ATTRIBUTE_FONTCOLOR)));
82    }
83  2 if (fontTag.hasAttribute(ATTRIBUTE_FONTFACE)) {
84  1 buffer.append(String.format("font-family:%s;", fontTag.getAttribute(ATTRIBUTE_FONTFACE)));
85    }
86  2 if (fontTag.hasAttribute(ATTRIBUTE_FONTSIZE)) {
87  2 String fontSize = fontTag.getAttribute(ATTRIBUTE_FONTSIZE);
88  2 String fontSizeCss = FONT_SIZE_MAP.get(fontSize);
89  2 fontSizeCss = (fontSizeCss != null) ? fontSizeCss : fontSize;
90  2 buffer.append(String.format("font-size:%s;", fontSizeCss));
91    }
92  2 if (fontTag.hasAttribute(ATTRIBUTE_STYLE) && fontTag.getAttribute(ATTRIBUTE_STYLE).trim().length() == 0) {
93  0 buffer.append(fontTag.getAttribute(ATTRIBUTE_STYLE));
94    }
95  2 if (buffer.length() > 0) {
96  2 span.setAttribute(ATTRIBUTE_STYLE, buffer.toString());
97    }
98  2 fontTag.getParentNode().insertBefore(span, fontTag);
99  2 fontTag.getParentNode().removeChild(fontTag);
100    }
101    }
102    }