Skip to content

Commit

Permalink
#2295 - [ClientLibraries] Add option to inject the onload attribute i…
Browse files Browse the repository at this point in the history
…nto the stylesheet link tag (#2692)

* Add option to inject the onload attribute into the stylesheet link tag
* Inject existing onload attribute into the stylesheet link tag
* fixed ITs

---------

Co-authored-by: korben-ne-dallas <alexvedenov@gmail.com>
Co-authored-by: Levente Sántha <levente@adobe.com>
  • Loading branch information
3 people committed Apr 30, 2024
1 parent d62e2dc commit ec57de2
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
Expand Up @@ -207,6 +207,7 @@ private String getHtmlWithInjectedAttributes(String html) {
jsAttributes.append(getHtmlAttr(OPTION_ONLOAD, onload));
StringBuilder cssAttributes = new StringBuilder();
cssAttributes.append(getHtmlAttr(OPTION_MEDIA, media));
cssAttributes.append(getHtmlAttr(OPTION_ONLOAD, onload));
String updatedHtml = StringUtils.replace(html,"<script ", "<script " + jsAttributes.toString());
return StringUtils.replace(updatedHtml,"<link ", "<link " + cssAttributes.toString());
}
Expand Down
Expand Up @@ -125,9 +125,9 @@ void setUp() {
cssIncludes.put(CAROUSEL_CATEGORY, "<link rel=\"stylesheet\" href=\"" + CAROUSEL_CLIENTLIB_PATH + ".css\" type=\"text/css\">");

cssIncludesWithAttributes = new HashMap<>();
cssIncludesWithAttributes.put(TEASER_CATEGORY, "<link media=\"print\" rel=\"stylesheet\" href=\"" + TEASER_CLIENTLIB_PATH + ".css\" type=\"text/css\">");
cssIncludesWithAttributes.put(ACCORDION_CATEGORY, "<link media=\"print\" rel=\"stylesheet\" href=\"" + ACCORDION_CLIENTLIB_PATH + ".css\" type=\"text/css\">");
cssIncludesWithAttributes.put(CAROUSEL_CATEGORY, "<link media=\"print\" rel=\"stylesheet\" href=\"" + CAROUSEL_CLIENTLIB_PATH + ".css\" type=\"text/css\">");
cssIncludesWithAttributes.put(TEASER_CATEGORY, "<link media=\"print\" onload=\"myFunction()\" rel=\"stylesheet\" href=\"" + TEASER_CLIENTLIB_PATH + ".css\" type=\"text/css\">");
cssIncludesWithAttributes.put(ACCORDION_CATEGORY, "<link media=\"print\" onload=\"myFunction()\" rel=\"stylesheet\" href=\"" + ACCORDION_CLIENTLIB_PATH + ".css\" type=\"text/css\">");
cssIncludesWithAttributes.put(CAROUSEL_CATEGORY, "<link media=\"print\" onload=\"myFunction()\" rel=\"stylesheet\" href=\"" + CAROUSEL_CLIENTLIB_PATH + ".css\" type=\"text/css\">");

jsInlines = new HashMap<>();
jsInlines.put(TEASER_CATEGORY, "console.log('teaser clientlib js');");
Expand Down
Expand Up @@ -43,6 +43,7 @@ public class ClientlibsIncludeIT {
private String testPage = "/content/core-components/clientlibs-include-page";
private final static String REGEX_SCRIPT_ELEMENT = "<script async crossorigin=\"anonymous\" onload=\"console.log..\".*src=\".*/etc.clientlibs/core/wcm/tests/components/clientlibs-include/clientlibs/site.*.js\"></script>";
private final static String REGEX_LINK_ELEMENT = "<link media=\"print\" rel=\"stylesheet\" href=\".*/etc.clientlibs/core/wcm/tests/components/clientlibs-include/clientlibs/site.*.css\" type=\"text/css\">";
private final static String REGEX_LINK_ONLOAD_ELEMENT = "<link media=\"print\" onload=\"console.log\\(\\)\" rel=\"stylesheet\" href=\".*/etc.clientlibs/core/wcm/tests/components/clientlibs-include/clientlibs/site.*.css\" type=\"text/css\">";

@BeforeClass
public static void beforeClass() {
Expand All @@ -67,7 +68,7 @@ public void testCssInclude() throws ClientException {
public void testAllInclude() throws ClientException {
String content = adminAuthor.doGet(testPage + ".includeall.html", 200).getContent();
GraniteAssert.assertRegExFind("Incorrect script and/or script attributes", content, REGEX_SCRIPT_ELEMENT);
GraniteAssert.assertRegExFind("Incorrect link and/or link attributes", content, REGEX_LINK_ELEMENT);
GraniteAssert.assertRegExFind("Incorrect link and/or link attributes", content, REGEX_LINK_ONLOAD_ELEMENT);
}

}

0 comments on commit ec57de2

Please sign in to comment.