Skip to content

Commit

Permalink
eliminate code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Sep 9, 2024
1 parent 794c4e7 commit 79e3e8c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
4 changes: 2 additions & 2 deletions changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
<body>

<release version="1.4.0" date="not released">
<action type="update" dev="sseifert">
<action type="update" dev="sseifert" issue="5">
Do not include JS/CSS client libraries twice if the same library is requested multiple times within one request.
</action>
<action type="update" dev="sseifert">
<action type="update" dev="sseifert" issue="5">
Ensure request context path is added to client library URLs.
</action>
<action type="update" dev="sseifert">
Expand Down
15 changes: 3 additions & 12 deletions src/main/java/io/wcm/wcm/ui/clientlibs/components/CSSInclude.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,13 @@ private void activate() {
*/
private @NotNull String buildIncludeString(@NotNull List<String> libraryPaths, @NotNull Map<String, String> attrs,
@NotNull Map<String, String> customAttrs) {
RequestIncludedLibraries includedLibraries = new RequestIncludedLibraries(request);
StringBuilder markup = new StringBuilder();
for (String libraryPath : libraryPaths) {
// ignore libraries that are already included
if (includedLibraries.isInlucded(libraryPath)) {
continue;
}
return new RequestIncludedLibraries(request).buildMarkupIgnoringDuplicateLibraries(libraryPaths, libraryPath -> {
HtmlTagBuilder builder = new HtmlTagBuilder("link", false, xssApi);
builder.setAttrs(attrs);
builder.setAttrs(customAttrs);
builder.setAttr("href", request.getContextPath() + libraryPath);
markup.append(builder.build());
// mark library as included
includedLibraries.storeIncluded(libraryPath);
}
return markup.toString();
return builder;
});
}

/**
Expand Down
15 changes: 3 additions & 12 deletions src/main/java/io/wcm/wcm/ui/clientlibs/components/JSInclude.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,22 +147,13 @@ private void activate() {
*/
private @NotNull String buildIncludeString(@NotNull List<String> libraryPaths, @NotNull Map<String, String> attrs,
@NotNull Map<String, String> customAttrs) {
RequestIncludedLibraries includedLibraries = new RequestIncludedLibraries(request);
StringBuilder markup = new StringBuilder();
for (String libraryPath : libraryPaths) {
// ignore libraries that are already included
if (includedLibraries.isInlucded(libraryPath)) {
continue;
}
return new RequestIncludedLibraries(request).buildMarkupIgnoringDuplicateLibraries(libraryPaths, libraryPath -> {
HtmlTagBuilder builder = new HtmlTagBuilder("script", true, xssApi);
builder.setAttrs(attrs);
builder.setAttrs(customAttrs);
builder.setAttr("src", request.getContextPath() + libraryPath);
markup.append(builder.build());
// mark library as included
includedLibraries.storeIncluded(libraryPath);
}
return markup.toString();
return builder;
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
package io.wcm.wcm.ui.clientlibs.components;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;

import org.apache.sling.api.SlingHttpServletRequest;

Expand Down Expand Up @@ -71,4 +73,27 @@ void storeIncluded(@NotNull String libraryPath) {
getLibaryPathsSetFromRequest().add(libraryPath);
}

/**
* Builds the markup for all given HTML libraries that are not already included in the current request.
* @param libraryPaths Library paths
* @param htmlTagBuilderFactory Factory to create HTML tag builders
* @return Markup
*/
String buildMarkupIgnoringDuplicateLibraries(@NotNull List<String> libraryPaths,
@NotNull Function<String, HtmlTagBuilder> htmlTagBuilderFactory) {
RequestIncludedLibraries includedLibraries = new RequestIncludedLibraries(request);
StringBuilder markup = new StringBuilder();
for (String libraryPath : libraryPaths) {
// ignore libraries that are already included
if (includedLibraries.isInlucded(libraryPath)) {
continue;
}
// build markup for library
markup.append(htmlTagBuilderFactory.apply(libraryPath).build());
// mark library as included
includedLibraries.storeIncluded(libraryPath);
}
return markup.toString();
}

}

0 comments on commit 79e3e8c

Please sign in to comment.