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

File DefaultHTMLCleaner.java

 

Coverage histogram

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

Code metrics

0
9
2
1
85
41
2
0.22
4.5
2
1

Classes

Class Line # Actions
DefaultHTMLCleaner 44 9 0% 2 0
1.0100%
 

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.io.StringReader;
23    import java.util.ArrayList;
24    import java.util.Collections;
25    import java.util.Comparator;
26    import java.util.List;
27   
28    import javax.inject.Inject;
29    import javax.inject.Singleton;
30   
31    import org.w3c.dom.Document;
32    import org.xwiki.component.annotation.Component;
33    import org.xwiki.gwt.wysiwyg.client.cleaner.HTMLCleaner;
34    import org.xwiki.xml.html.HTMLCleanerConfiguration;
35    import org.xwiki.xml.html.HTMLUtils;
36   
37    /**
38    * Default HTML cleaner for the WYSIWYG editor's output.
39    *
40    * @version $Id: b439fb2704a6e09c40d5aef0354ed5e76febeb35 $
41    */
42    @Component
43    @Singleton
 
44    public class DefaultHTMLCleaner implements HTMLCleaner
45    {
46    /**
47    * The component used to clean the HTML.
48    */
49    @Inject
50    private org.xwiki.xml.html.HTMLCleaner cleaner;
51   
52    /**
53    * The list of WYSIWYG editor specific HTML cleaning filters.
54    */
55    @Inject
56    private List<HTMLFilter> specificFilters;
57   
 
58  13 toggle @Override
59    public String clean(String dirtyHTML)
60    {
61    // Sort the list of specific filters based on their priority.
62  13 Collections.sort(specificFilters, new Comparator<HTMLFilter>()
63    {
 
64  52 toggle @Override
65    public int compare(HTMLFilter alice, HTMLFilter bob)
66    {
67  52 return alice.getPriority() - bob.getPriority();
68    }
69    });
70   
71    // We have to remove or replace the HTML elements that were added by the WYSIWYG editor only for internal
72    // reasons, before any cleaning filter is applied. Otherwise cleaning filters might transform these
73    // WYSIWYG-specific HTML elements making their removal difficult. We cannot transform the WYSIWYG output on the
74    // client side because the editor is a widget that can be used independently inside or outside an HTML form and
75    // thus it doesn't know when its current value is submitted.
76  13 HTMLCleanerConfiguration config = cleaner.getDefaultConfiguration();
77  13 List<org.xwiki.xml.html.filter.HTMLFilter> filters = new ArrayList<org.xwiki.xml.html.filter.HTMLFilter>();
78  13 filters.addAll(specificFilters);
79  13 filters.addAll(config.getFilters());
80  13 config.setFilters(filters);
81   
82  13 Document document = cleaner.clean(new StringReader(dirtyHTML), config);
83  13 return HTMLUtils.toString(document);
84    }
85    }