ImageInlineUtils.java

1
package fr.sii.ogham.html.inliner.impl.jsoup;
2
3
import static fr.sii.ogham.html.inliner.ImageInlinerConstants.INLINED_ATTR;
4
import static fr.sii.ogham.html.inliner.ImageInlinerConstants.INLINE_MODE_ATTR;
5
6
import org.jsoup.Jsoup;
7
import org.jsoup.nodes.Document;
8
import org.jsoup.nodes.Element;
9
import org.jsoup.nodes.Node;
10
import org.jsoup.select.Elements;
11
12
import fr.sii.ogham.html.inliner.ImageInlinerConstants;
13
import fr.sii.ogham.html.inliner.ImageInlinerConstants.InlineMode;
14
15
/**
16
 * Helper class that factorizes code for classes that are using Jsoup inliners.
17
 * 
18
 * @author Aurélien Baudet
19
 *
20
 */
21
public final class ImageInlineUtils {
22
	/**
23
	 * Checks if inlining mode is allowed on the provided element.
24
	 * 
25
	 * @param img
26
	 *            the image element to check if the actual inlining mode is
27
	 *            allowed
28
	 * @param mode
29
	 *            the actual mode
30
	 * @return true if this mode is allowed, false otherwise
31
	 */
32
	@SuppressWarnings("squid:S1126")
33
	public static boolean isInlineModeAllowed(Node img, InlineMode mode) {
34
		// if already inlined => reject (do not inline twice)
35 1 1. isInlineModeAllowed : negated conditional → RUN_ERROR
		if (!img.attr(INLINED_ATTR).isEmpty()) {
36 1 1. isInlineModeAllowed : replaced boolean return with true for fr/sii/ogham/html/inliner/impl/jsoup/ImageInlineUtils::isInlineModeAllowed → RUN_ERROR
			return false;
37
		}
38
		// if inline mode defined but not the wanted mode => reject
39 2 1. isInlineModeAllowed : negated conditional → RUN_ERROR
2. isInlineModeAllowed : negated conditional → RUN_ERROR
		if (!img.attr(INLINE_MODE_ATTR).isEmpty() && !mode.is(img.attr(INLINE_MODE_ATTR))) {
40 1 1. isInlineModeAllowed : replaced boolean return with true for fr/sii/ogham/html/inliner/impl/jsoup/ImageInlineUtils::isInlineModeAllowed → RUN_ERROR
			return false;
41
		}
42
		// if inline mode defined and matches the wanted mode => allow
43
		// if no inline mode defined => allow (any mode allowed)
44 1 1. isInlineModeAllowed : replaced boolean return with false for fr/sii/ogham/html/inliner/impl/jsoup/ImageInlineUtils::isInlineModeAllowed → RUN_ERROR
		return true;
45
	}
46
47
	/**
48
	 * Remove attributes that are used only by Ogham:
49
	 * <ul>
50
	 * <li>{@link ImageInlinerConstants#INLINE_MODE_ATTR}</li>
51
	 * <li>{@link ImageInlinerConstants#INLINED_ATTR}</li>
52
	 * </ul>
53
	 * 
54
	 * @param html
55
	 *            the html to clean
56
	 * @return the cleaned html
57
	 */
58
	public static String removeOghamAttributes(String html) {
59
		Document doc = Jsoup.parse(html);
60
		Elements imgs = doc.select("img");
61
		for (Element img : imgs) {
62
			img.removeAttr(INLINE_MODE_ATTR);
63
			img.removeAttr(INLINED_ATTR);
64
		}
65 1 1. removeOghamAttributes : replaced return value with "" for fr/sii/ogham/html/inliner/impl/jsoup/ImageInlineUtils::removeOghamAttributes → RUN_ERROR
		return doc.outerHtml();
66
	}
67
68
	private ImageInlineUtils() {
69
		super();
70
	}
71
}

Mutations

35

1.1
Location : isInlineModeAllowed
Killed by :
negated conditional → RUN_ERROR

36

1.1
Location : isInlineModeAllowed
Killed by :
replaced boolean return with true for fr/sii/ogham/html/inliner/impl/jsoup/ImageInlineUtils::isInlineModeAllowed → RUN_ERROR

39

1.1
Location : isInlineModeAllowed
Killed by :
negated conditional → RUN_ERROR

2.2
Location : isInlineModeAllowed
Killed by :
negated conditional → RUN_ERROR

40

1.1
Location : isInlineModeAllowed
Killed by :
replaced boolean return with true for fr/sii/ogham/html/inliner/impl/jsoup/ImageInlineUtils::isInlineModeAllowed → RUN_ERROR

44

1.1
Location : isInlineModeAllowed
Killed by :
replaced boolean return with false for fr/sii/ogham/html/inliner/impl/jsoup/ImageInlineUtils::isInlineModeAllowed → RUN_ERROR

65

1.1
Location : removeOghamAttributes
Killed by :
replaced return value with "" for fr/sii/ogham/html/inliner/impl/jsoup/ImageInlineUtils::removeOghamAttributes → RUN_ERROR

Active mutators

Tests examined


Report generated by PIT 1.13.1