1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.wysiwyg.server.internal.cleaner

File NestedAnchorsFilter.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

8
16
2
1
90
50
7
0.44
8
2
3.5

Classes

Class Line # Actions
NestedAnchorsFilter 46 16 0% 7 5
0.807692380.8%
 

Contributing tests

This file is covered by 3 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.wysiwyg.server.internal.cleaner;
21   
22    import java.util.Map;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27    import javax.xml.transform.TransformerException;
28   
29    import org.apache.xpath.XPathAPI;
30    import org.slf4j.Logger;
31    import org.w3c.dom.Document;
32    import org.w3c.dom.DocumentFragment;
33    import org.w3c.dom.Element;
34    import org.w3c.dom.NodeList;
35    import org.xwiki.component.annotation.Component;
36   
37    /**
38    * Removes nested anchors. Since anchors in anchors are not valid in XHTML and the parser does not accept them, we use
39    * this filter to remove all the anchors inside other anchors, preserving their content.
40    *
41    * @version $Id: cd3af1c7f199554d2b063de9f442740344068d6f $
42    */
43    @Component(roles = { HTMLFilter.class })
44    @Named("nestedAnchors")
45    @Singleton
 
46    public class NestedAnchorsFilter extends AbstractHTMLFilter
47    {
48    /**
49    * Logger.
50    */
51    @Inject
52    private Logger logger;
53   
 
54  13 toggle @Override
55    public void filter(Document document, Map<String, String> parameters)
56    {
57  13 try {
58    // all anchors descendants of other anchors
59  13 NodeList nestedAnchors = XPathAPI.selectNodeList(document, "//a//a");
60  17 for (int i = 0; i < nestedAnchors.getLength(); i++) {
61  4 Element nestedAnchor = (Element) nestedAnchors.item(i);
62  4 unwrap(nestedAnchor);
63    }
64    } catch (TransformerException e) {
65  0 logger.error("Exception while filtering nested anchors.", e);
66    }
67    }
68   
69    /**
70    * Removes an element from its parent, replacing it with all its children.
71    *
72    * @param element the element to unwrap
73    */
 
74  4 toggle private void unwrap(Element element)
75    {
76  4 Document ownerDocument = element.getOwnerDocument();
77  4 if (ownerDocument == null) {
78  0 return;
79    }
80  4 Element parent = (Element) element.getParentNode();
81  4 if (parent == null) {
82  0 return;
83    }
84  4 DocumentFragment children = element.getOwnerDocument().createDocumentFragment();
85  9 while (element.hasChildNodes()) {
86  5 children.appendChild(element.getChildNodes().item(0));
87    }
88  4 parent.replaceChild(children, element);
89    }
90    }