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

File ListItemFilter.java

 

Coverage histogram

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

Code metrics

6
10
1
1
69
35
6
0.6
10
1
6

Classes

Class Line # Actions
ListItemFilter 42 10 0% 6 2
0.8823529588.2%
 

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.Map;
23   
24    import javax.inject.Named;
25    import javax.inject.Singleton;
26   
27    import org.w3c.dom.Document;
28    import org.w3c.dom.Element;
29    import org.w3c.dom.Node;
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.xml.html.filter.AbstractHTMLFilter;
32   
33    /**
34    * If <li> elements have no <ul> or <ol> parent then add a <ul> parent to make it valid.
35    *
36    * @version $Id: 0b947d597514dec251ed8ac85310e9b2d6d7425d $
37    * @since 3.2RC1
38    */
39    @Component
40    @Named("listitem")
41    @Singleton
 
42    public class ListItemFilter extends AbstractHTMLFilter
43    {
 
44  4985 toggle @Override
45    public void filter(Document document, Map<String, String> cleaningParameters)
46    {
47    // Iterate all lists and fix them.
48  4985 for (Element listItem : filterDescendants(document.getDocumentElement(), new String[] { TAG_LI })) {
49  770 Node parent = listItem.getParentNode();
50  770 if (parent != null && (!parent.getNodeName().equalsIgnoreCase(TAG_UL)
51    && !parent.getNodeName().equalsIgnoreCase(TAG_OL)))
52    {
53    // Add a UL parent
54  1 Element newUL = listItem.getOwnerDocument().createElement(TAG_UL);
55  1 parent.replaceChild(newUL, listItem);
56  1 newUL.appendChild(listItem);
57   
58    // If the parent is a <p> then remove it. HTMLCleaner will clean "<li></li>" by wrapping it in a
59    // paragraph: "<p><li></li></p>". Since this isn't valid XHTML we need to remove the <p>.
60  1 if (parent.getNodeName().equalsIgnoreCase(TAG_P)) {
61  1 Node parentOfParent = parent.getParentNode();
62  1 if (parentOfParent != null) {
63  1 parentOfParent.replaceChild(newUL, parent);
64    }
65    }
66    }
67    }
68    }
69    }