| 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 |
3
1. isInlineModeAllowed : negated conditional → NO_COVERAGE 2. isInlineModeAllowed : negated conditional → KILLED 3. isInlineModeAllowed : negated conditional → KILLED |
if (!img.attr(INLINED_ATTR).isEmpty()) { |
| 36 |
2
1. isInlineModeAllowed : replaced boolean return with true for fr/sii/ogham/html/inliner/impl/jsoup/ImageInlineUtils::isInlineModeAllowed → NO_COVERAGE 2. isInlineModeAllowed : replaced boolean return with true for fr/sii/ogham/html/inliner/impl/jsoup/ImageInlineUtils::isInlineModeAllowed → KILLED |
return false; |
| 37 | } | |
| 38 | // if inline mode defined but not the wanted mode => reject | |
| 39 |
6
1. isInlineModeAllowed : negated conditional → NO_COVERAGE 2. isInlineModeAllowed : negated conditional → NO_COVERAGE 3. isInlineModeAllowed : negated conditional → KILLED 4. isInlineModeAllowed : negated conditional → KILLED 5. isInlineModeAllowed : negated conditional → KILLED 6. isInlineModeAllowed : negated conditional → KILLED |
if (!img.attr(INLINE_MODE_ATTR).isEmpty() && !mode.is(img.attr(INLINE_MODE_ATTR))) { |
| 40 |
3
1. isInlineModeAllowed : replaced boolean return with true for fr/sii/ogham/html/inliner/impl/jsoup/ImageInlineUtils::isInlineModeAllowed → NO_COVERAGE 2. isInlineModeAllowed : replaced boolean return with true for fr/sii/ogham/html/inliner/impl/jsoup/ImageInlineUtils::isInlineModeAllowed → KILLED 3. isInlineModeAllowed : replaced boolean return with true for fr/sii/ogham/html/inliner/impl/jsoup/ImageInlineUtils::isInlineModeAllowed → KILLED |
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 |
3
1. isInlineModeAllowed : replaced boolean return with false for fr/sii/ogham/html/inliner/impl/jsoup/ImageInlineUtils::isInlineModeAllowed → NO_COVERAGE 2. isInlineModeAllowed : replaced boolean return with false for fr/sii/ogham/html/inliner/impl/jsoup/ImageInlineUtils::isInlineModeAllowed → KILLED 3. isInlineModeAllowed : replaced boolean return with false for fr/sii/ogham/html/inliner/impl/jsoup/ImageInlineUtils::isInlineModeAllowed → KILLED |
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 |
3
1. removeOghamAttributes : replaced return value with "" for fr/sii/ogham/html/inliner/impl/jsoup/ImageInlineUtils::removeOghamAttributes → NO_COVERAGE 2. removeOghamAttributes : replaced return value with "" for fr/sii/ogham/html/inliner/impl/jsoup/ImageInlineUtils::removeOghamAttributes → KILLED 3. removeOghamAttributes : replaced return value with "" for fr/sii/ogham/html/inliner/impl/jsoup/ImageInlineUtils::removeOghamAttributes → KILLED |
return doc.outerHtml(); |
| 66 | } | |
| 67 | ||
| 68 | private ImageInlineUtils() { | |
| 69 | super(); | |
| 70 | } | |
| 71 | } | |
Mutations | ||
| 35 |
1.1 2.2 3.3 |
|
| 36 |
1.1 2.2 |
|
| 39 |
1.1 2.2 3.3 4.4 5.5 6.6 |
|
| 40 |
1.1 2.2 3.3 |
|
| 44 |
1.1 2.2 3.3 |
|
| 65 |
1.1 2.2 3.3 |