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

File BodyFilter.java

 

Coverage histogram

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

Code metrics

22
32
3
1
137
77
15
0.47
10.67
3
5

Classes

Class Line # Actions
BodyFilter 47 32 0% 15 1
0.9824561598.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.Arrays;
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.w3c.dom.Node;
32    import org.w3c.dom.Text;
33    import org.xwiki.component.annotation.Component;
34    import org.xwiki.xml.html.HTMLConstants;
35    import org.xwiki.xml.html.filter.AbstractHTMLFilter;
36   
37    /**
38    * Wraps direct children of the Body tag with paragraphs. For example {@code a <table>...</table> b <p>c</p> d} is
39    * transformed into {@code <p>a </p><table>...</table><p> b </p><p>c</p><p> d</p>}.
40    *
41    * @version $Id: ead3901673d621cf4afc5bb6a0c1af6d50ae20d0 $
42    * @since 1.8RC3
43    */
44    @Component
45    @Named("body")
46    @Singleton
 
47    public class BodyFilter extends AbstractHTMLFilter
48    {
49    /**
50    * List of valid children elements of the BODY element in XHTML.
51    */
52    private static final List<String> ALLOWED_BODY_TAGS = Arrays.asList(HTMLConstants.TAG_ADDRESS,
53    HTMLConstants.TAG_BLOCKQUOTE, HTMLConstants.TAG_DEL, HTMLConstants.TAG_DIV, HTMLConstants.TAG_FIELDSET,
54    HTMLConstants.TAG_FORM, HTMLConstants.TAG_HR, HTMLConstants.TAG_INS, HTMLConstants.TAG_NOSCRIPT,
55    HTMLConstants.TAG_P, HTMLConstants.TAG_PRE, HTMLConstants.TAG_SCRIPT, HTMLConstants.TAG_TABLE,
56    HTMLConstants.TAG_H1, HTMLConstants.TAG_H2, HTMLConstants.TAG_H3, HTMLConstants.TAG_H4, HTMLConstants.TAG_H5,
57    HTMLConstants.TAG_H6, HTMLConstants.TAG_DL, HTMLConstants.TAG_OL, HTMLConstants.TAG_UL);
58   
 
59  4985 toggle @Override
60    public void filter(Document document, Map<String, String> cleaningParameters)
61    {
62  4985 Node body = document.getElementsByTagName(HTMLConstants.TAG_BODY).item(0);
63  4985 Node currentNode = body.getFirstChild();
64  4985 Node markerNode = null;
65  4985 boolean containsOnlySpaces = true;
66  10762 while (currentNode != null) {
67    // Note: We ignore comment nodes since there's no need to wrap them.
68  5777 if (currentNode.getNodeType() != Node.COMMENT_NODE) {
69  5759 if (!ALLOWED_BODY_TAGS.contains(currentNode.getNodeName())) {
70   
71    // Ensure that we don't wrap elements that contain only spaces or newlines.
72  4664 containsOnlySpaces = containsOnlySpaces(currentNode);
73   
74  4664 if (markerNode == null) {
75  4625 markerNode = currentNode;
76    } else {
77    // Do nothing, just go to the next node.
78    }
79  1095 } else if (markerNode != null) {
80    // surround all the nodes starting with the marker node with a paragraph unless there are only
81    // whitespaces or newlines.
82  412 if (!containsOnlySpaces) {
83  6 surroundWithParagraph(document, body, markerNode, currentNode);
84    }
85  412 markerNode = null;
86    }
87    }
88  5777 currentNode = currentNode.getNextSibling();
89    }
90   
91    // If the marker is still set it means we need to wrap all elements between the marker till
92    // the end of the body siblings with a paragraph.
93  4985 if (markerNode != null && !containsOnlySpaces) {
94  4026 surroundWithParagraph(document, body, markerNode, null);
95    }
96    }
97   
98    /**
99    * @param currentNode the current node to check
100    * @return false if the current node contains something other than whitespaces or newlines, true otherwise
101    */
 
102  4664 toggle private boolean containsOnlySpaces(Node currentNode)
103    {
104  4664 boolean result = true;
105  4664 if (currentNode.getNodeType() == Node.TEXT_NODE) {
106  639 Text textNode = (Text) currentNode;
107  639 if (textNode.getNodeValue().trim().length() > 0) {
108  26 result = false;
109    }
110  4025 } else if (currentNode.getNodeType() != Node.COMMENT_NODE) {
111  4025 result = false;
112    }
113  4664 return result;
114    }
115   
116    /**
117    * Surround passed nodes with a paragraph element.
118    *
119    * @param document the document to use to create the new paragraph element
120    * @param body the body under which to wrap non valid elements with paragraphs
121    * @param beginNode the first node where to start the wrapping
122    * @param endNode the last node where to stop the wrapping. If null then the wrapping is done till the last element
123    * inside the body element
124    */
 
125  4032 toggle private void surroundWithParagraph(Document document, Node body, Node beginNode, Node endNode)
126    {
127    // surround all the nodes starting with the marker node with a paragraph.
128  4032 Element paragraph = document.createElement(TAG_P);
129  4032 body.insertBefore(paragraph, beginNode);
130  4032 Node child = beginNode;
131  8094 while (child != endNode) {
132  4062 Node nextChild = child.getNextSibling();
133  4062 paragraph.appendChild(body.removeChild(child));
134  4062 child = nextChild;
135    }
136    }
137    }