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

File ListFilter.java

 

Coverage histogram

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

Code metrics

8
16
3
1
122
48
7
0.44
5.33
3
2.33

Classes

Class Line # Actions
ListFilter 60 16 0% 7 0
1.0100%
 

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    * Transform non XHTML list into XHTML valid lists. Specifically, move <ul> and <ol> elements (and any other
35    * nodes that are not allowed) nested inside a <ul> or <ol> element inside the previous <li> element.
36    * <p>
37    * For example: <code><pre>
38    * &lt;ul&gt;
39    * &lt;li&gt;item1&lt;/li&gt;
40    * &lt;ul&gt;
41    * &lt;li&gt;item2&lt;/li&gt;
42    * &lt;/ul&gt;
43    * &lt;/ul&gt;
44    * </pre></code> becomes <code><pre>
45    * &lt;ul&gt;
46    * &lt;li&gt;item1
47    * &lt;ul&gt;
48    * &lt;li&gt;item2&lt;/li&gt;
49    * &lt;/ul&gt;
50    * &lt;/li&gt;
51    * &lt;/ul&gt;
52    * </pre></code>
53    *
54    * @version $Id: 06c98d776989bd3be1f2e65aaa7d02be5dd3f422 $
55    * @since 1.6M1
56    */
57    @Component
58    @Named("list")
59    @Singleton
 
60    public class ListFilter extends AbstractHTMLFilter
61    {
62    /**
63    * {@inheritDoc}
64    * <p>
65    * The {@link ListFilter} does not use any cleaningParameters passed in.
66    * </p>
67    */
 
68  4985 toggle @Override
69    public void filter(Document document, Map<String, String> cleaningParameters)
70    {
71    // Iterate all lists and fix them.
72  4985 for (Element list : filterDescendants(document.getDocumentElement(), new String[] { TAG_UL, TAG_OL })) {
73  326 filter(list);
74    }
75    }
76   
77    /**
78    * Transforms the given list in a valid XHTML list by moving the nodes that are not allowed inside &lt;ul&gt; and
79    * &lt;ol&gt; in &lt;li&gt; elements.
80    *
81    * @param list the list to be filtered
82    */
 
83  326 toggle private void filter(Element list)
84    {
85    // Iterate all the child nodes of the given list to see who's allowed and who's not allowed inside it.
86  326 Node child = list.getFirstChild();
87  326 Node previousListItem = null;
88  1461 while (child != null) {
89  1135 Node nextSibling = child.getNextSibling();
90  1135 if (isAllowedInsideList(child)) {
91    // Save a reference to the previous list item. Note that the previous list item is not necessarily the
92    // previous sibling.
93  1119 if (child.getNodeName().equalsIgnoreCase(TAG_LI)) {
94  770 previousListItem = child;
95    }
96    } else {
97  16 if (previousListItem == null) {
98    // Create a new list item to be able to move the invalid child.
99  6 previousListItem = list.getOwnerDocument().createElement(TAG_LI);
100  6 list.insertBefore(previousListItem, child);
101    // Hide the marker of the list item to make the list look the same after it is cleaned.
102  6 ((Element) previousListItem).setAttribute(ATTRIBUTE_STYLE, "list-style-type: none");
103    }
104    // Move the child node at the end of the previous list item because it is not allowed where it is now.
105  16 previousListItem.appendChild(child);
106    }
107  1135 child = nextSibling;
108    }
109    }
110   
111    /**
112    * Checks if a given node is allowed or not as a child of a &lt;ul&gt; or &lt;ol&gt; element.
113    *
114    * @param node the node to be checked
115    * @return {@code true} if the given node is allowed inside an ordered or unordered list, {@code false} otherwise
116    */
 
117  1135 toggle private boolean isAllowedInsideList(Node node)
118    {
119  1135 return (node.getNodeType() != Node.ELEMENT_NODE || node.getNodeName().equalsIgnoreCase(TAG_LI))
120    && (node.getNodeType() != Node.TEXT_NODE || node.getNodeValue().trim().length() == 0);
121    }
122    }