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

File UniqueIdFilter.java

 

Coverage histogram

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

Code metrics

6
11
1
1
64
35
4
0.36
11
1
4

Classes

Class Line # Actions
UniqueIdFilter 43 11 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

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.HashMap;
23    import java.util.Map;
24   
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27   
28    import org.w3c.dom.Document;
29    import org.w3c.dom.Element;
30    import org.w3c.dom.NodeList;
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.xml.html.filter.AbstractHTMLFilter;
33   
34    /**
35    * Makes sure HTML element IDs are unique. When a duplicate ID is found it is suffixed with a number to make it unique.
36    *
37    * @version $Id: 49777df46cab8c4cf13f08a673f01af6d093c859 $
38    * @since 5.2M1
39    */
40    @Component
41    @Named("uniqueId")
42    @Singleton
 
43    public class UniqueIdFilter extends AbstractHTMLFilter
44    {
 
45  1 toggle @Override
46    public void filter(Document document, Map<String, String> cleaningParameters)
47    {
48  1 Map<String, Integer> idCount = new HashMap<String, Integer>();
49  1 NodeList elements = document.getElementsByTagName("*");
50  7 for (int i = 0; i < elements.getLength(); i++) {
51  6 Element element = (Element) elements.item(i);
52  6 String id = element.getAttribute(ATTRIBUTE_ID);
53  6 if (!"".equals(id)) {
54  3 Integer count = idCount.get(id);
55  3 if (count == null) {
56  2 count = 0;
57    } else {
58  1 element.setAttribute(ATTRIBUTE_ID, id + count++);
59    }
60  3 idCount.put(id, count);
61    }
62    }
63    }
64    }