1 | package fr.sii.ogham.html.inliner.impl.regexp; | |
2 | ||
3 | import static fr.sii.ogham.html.inliner.impl.regexp.CssImageInlinerConstants.InlineModes.BASE64; | |
4 | import static java.text.MessageFormat.format; | |
5 | ||
6 | import java.util.ArrayList; | |
7 | import java.util.List; | |
8 | ||
9 | import fr.sii.ogham.core.util.Base64Utils; | |
10 | import fr.sii.ogham.html.inliner.ContentWithImages; | |
11 | import fr.sii.ogham.html.inliner.ImageInliner; | |
12 | import fr.sii.ogham.html.inliner.ImageResource; | |
13 | import fr.sii.ogham.html.inliner.impl.regexp.CssImageInlinerConstants.InlineModes; | |
14 | ||
15 | /** | |
16 | * Image inliner that reads the image and converts it into a base64 string. The | |
17 | * string is then included directly in the HTML content in place of the previous URL/path. | |
18 | * | |
19 | * <p> | |
20 | * The inlining using base64 is only applied if the attribute | |
21 | * {@link CssImageInlinerConstants#INLINE_MODE_PROPERTY} is set to | |
22 | * {@link InlineModes#BASE64}. | |
23 | * </p> | |
24 | * | |
25 | * @author Aurélien Baudet | |
26 | * | |
27 | */ | |
28 | public class RegexBase64BackgroundImageInliner implements ImageInliner { | |
29 | private static final String BASE64_URI = "data:{0};base64,{1}"; | |
30 | ||
31 | @Override | |
32 | public ContentWithImages inline(String htmlContent, List<ImageResource> images) { | |
33 | List<Encoded> encoded = new ArrayList<>(images.size()); | |
34 |
3
1. lambda$inline$0 : replaced return value with "" for fr/sii/ogham/html/inliner/impl/regexp/RegexBase64BackgroundImageInliner::lambda$inline$0 → NO_COVERAGE 2. lambda$inline$0 : replaced return value with "" for fr/sii/ogham/html/inliner/impl/regexp/RegexBase64BackgroundImageInliner::lambda$inline$0 → KILLED 3. lambda$inline$0 : replaced return value with "" for fr/sii/ogham/html/inliner/impl/regexp/RegexBase64BackgroundImageInliner::lambda$inline$0 → KILLED |
String inlined = CssImageInlineUtils.inline(htmlContent, images, BASE64, decl -> encodeImage(decl.getUrl().getUrl(), decl.getImage(), encoded)); |
35 |
3
1. inline : replaced return value with null for fr/sii/ogham/html/inliner/impl/regexp/RegexBase64BackgroundImageInliner::inline → NO_COVERAGE 2. inline : replaced return value with null for fr/sii/ogham/html/inliner/impl/regexp/RegexBase64BackgroundImageInliner::inline → KILLED 3. inline : replaced return value with null for fr/sii/ogham/html/inliner/impl/regexp/RegexBase64BackgroundImageInliner::inline → KILLED |
return new ContentWithImages(inlined, new ArrayList<>(0)); |
36 | } | |
37 | ||
38 | private String encodeImage(String imageUrl, ImageResource image, List<Encoded> encoded) { | |
39 | Encoded alreadyEncoded = getEncodedForUrl(imageUrl, encoded); | |
40 |
3
1. encodeImage : negated conditional → NO_COVERAGE 2. encodeImage : negated conditional → KILLED 3. encodeImage : negated conditional → KILLED |
if (alreadyEncoded != null) { |
41 |
2
1. encodeImage : replaced return value with "" for fr/sii/ogham/html/inliner/impl/regexp/RegexBase64BackgroundImageInliner::encodeImage → NO_COVERAGE 2. encodeImage : replaced return value with "" for fr/sii/ogham/html/inliner/impl/regexp/RegexBase64BackgroundImageInliner::encodeImage → KILLED |
return alreadyEncoded.getEncodedUrl(); |
42 | } | |
43 | String encodedUrl = format(BASE64_URI, image.getMimetype(), Base64Utils.encodeToString(image.getContent())); | |
44 | encoded.add(new Encoded(imageUrl, encodedUrl)); | |
45 |
3
1. encodeImage : replaced return value with "" for fr/sii/ogham/html/inliner/impl/regexp/RegexBase64BackgroundImageInliner::encodeImage → NO_COVERAGE 2. encodeImage : replaced return value with "" for fr/sii/ogham/html/inliner/impl/regexp/RegexBase64BackgroundImageInliner::encodeImage → KILLED 3. encodeImage : replaced return value with "" for fr/sii/ogham/html/inliner/impl/regexp/RegexBase64BackgroundImageInliner::encodeImage → KILLED |
return encodedUrl; |
46 | } | |
47 | ||
48 | private Encoded getEncodedForUrl(String imageUrl, List<Encoded> encoded) { | |
49 | // @formatter:off | |
50 |
2
1. getEncodedForUrl : replaced return value with null for fr/sii/ogham/html/inliner/impl/regexp/RegexBase64BackgroundImageInliner::getEncodedForUrl → SURVIVED 2. getEncodedForUrl : replaced return value with null for fr/sii/ogham/html/inliner/impl/regexp/RegexBase64BackgroundImageInliner::getEncodedForUrl → NO_COVERAGE |
return encoded.stream() |
51 |
4
1. lambda$getEncodedForUrl$1 : replaced boolean return with false for fr/sii/ogham/html/inliner/impl/regexp/RegexBase64BackgroundImageInliner::lambda$getEncodedForUrl$1 → SURVIVED 2. lambda$getEncodedForUrl$1 : replaced boolean return with false for fr/sii/ogham/html/inliner/impl/regexp/RegexBase64BackgroundImageInliner::lambda$getEncodedForUrl$1 → NO_COVERAGE 3. lambda$getEncodedForUrl$1 : replaced boolean return with true for fr/sii/ogham/html/inliner/impl/regexp/RegexBase64BackgroundImageInliner::lambda$getEncodedForUrl$1 → NO_COVERAGE 4. lambda$getEncodedForUrl$1 : replaced boolean return with true for fr/sii/ogham/html/inliner/impl/regexp/RegexBase64BackgroundImageInliner::lambda$getEncodedForUrl$1 → KILLED |
.filter(e -> imageUrl.equals(e.getMatchedUrl())) |
52 | .findFirst() | |
53 | .orElse(null); | |
54 | // @formatter:on | |
55 | } | |
56 | ||
57 | private static class Encoded { | |
58 | private final String matchedUrl; | |
59 | private final String encodedUrl; | |
60 | ||
61 | public Encoded(String matchedUrl, String encodedUrl) { | |
62 | super(); | |
63 | this.matchedUrl = matchedUrl; | |
64 | this.encodedUrl = encodedUrl; | |
65 | } | |
66 | ||
67 | public String getMatchedUrl() { | |
68 |
2
1. getMatchedUrl : replaced return value with "" for fr/sii/ogham/html/inliner/impl/regexp/RegexBase64BackgroundImageInliner$Encoded::getMatchedUrl → SURVIVED 2. getMatchedUrl : replaced return value with "" for fr/sii/ogham/html/inliner/impl/regexp/RegexBase64BackgroundImageInliner$Encoded::getMatchedUrl → NO_COVERAGE |
return matchedUrl; |
69 | } | |
70 | ||
71 | public String getEncodedUrl() { | |
72 |
2
1. getEncodedUrl : replaced return value with "" for fr/sii/ogham/html/inliner/impl/regexp/RegexBase64BackgroundImageInliner$Encoded::getEncodedUrl → NO_COVERAGE 2. getEncodedUrl : replaced return value with "" for fr/sii/ogham/html/inliner/impl/regexp/RegexBase64BackgroundImageInliner$Encoded::getEncodedUrl → KILLED |
return encodedUrl; |
73 | } | |
74 | } | |
75 | } | |
Mutations | ||
34 |
1.1 2.2 3.3 |
|
35 |
1.1 2.2 3.3 |
|
40 |
1.1 2.2 3.3 |
|
41 |
1.1 2.2 |
|
45 |
1.1 2.2 3.3 |
|
50 |
1.1 2.2 |
|
51 |
1.1 2.2 3.3 4.4 |
|
68 |
1.1 2.2 |
|
72 |
1.1 2.2 |