Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Versioned Clientlibs: Support javascript clientlibs in link tags. #3508

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com)

## Unreleased ([details][unreleased changes details])

- #3507 - Rewrite javascript clientlibs when used in link tags for preloading.

### Changed

- #3494 - Remove offline instrumentation with Jacoco
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,32 @@ private SaxElementUtils() {
public static final String STYLESHEET_REL="stylesheet";

public static boolean isCss(final String elementName, final Attributes attrs) {
if (!StringUtils.equals(elementName, "link")) {
return false;
}
final String type = attrs.getValue("", "type");
final String href = attrs.getValue("", "href");
final String rel = attrs.getValue("","rel");

return StringUtils.equals("link", elementName)
&& (StringUtils.equals(type, CSS_TYPE) || StringUtils.equals(rel,STYLESHEET_REL))
return (StringUtils.equals(type, CSS_TYPE) || StringUtils.equals(rel,STYLESHEET_REL))
&& StringUtils.startsWith(href, "/")
&& !StringUtils.startsWith(href, "//")
&& StringUtils.endsWith(href, LibraryType.CSS.extension);
}

public static boolean isJavaScript(final String elementName, final Attributes attrs) {
if (!StringUtils.equalsAny(elementName, "script", "link")) {
return false;
}
final String type = attrs.getValue("", "type");
final String src = attrs.getValue("", "src");
final String src;
if (StringUtils.equals(elementName, "script")) {
src = attrs.getValue("", "src");
} else {
src = attrs.getValue("", "href");
}

return StringUtils.equals("script", elementName)
&& (type == null || StringUtils.equals(type, JS_TYPE) || StringUtils.equals(type, JS_MODULE_TYPE))
return (type == null || StringUtils.equals(type, JS_TYPE) || StringUtils.equals(type, JS_MODULE_TYPE))
&& StringUtils.startsWith(src, "/")
&& !StringUtils.startsWith(src, "//")
&& StringUtils.endsWith(src, LibraryType.JS.extension);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ public final class VersionedClientlibsTransformerFactory extends AbstractGuavaCa
boolValue = DEFAULT_ENFORCE_MD5)
private static final String PROP_ENFORCE_MD5 = "enforce.md5";

private static final String ATTR_JS_PATH = "src";
private static final String ATTR_CSS_PATH = "href";
private static final String ATTR_SRC = "src";
private static final String ATTR_HREF = "href";

private static final String MIN_SELECTOR = "min";
private static final String MIN_SELECTOR_SEGMENT = "." + MIN_SELECTOR;
Expand Down Expand Up @@ -197,12 +197,13 @@ public Transformer createTransformer() {

private Attributes versionClientLibs(final String elementName, final Attributes attrs, final SlingHttpServletRequest request) {
if (SaxElementUtils.isCss(elementName, attrs)) {
return this.rebuildAttributes(new AttributesImpl(attrs), attrs.getIndex("", ATTR_CSS_PATH),
attrs.getValue("", ATTR_CSS_PATH), LibraryType.CSS, request);
return this.rebuildAttributes(new AttributesImpl(attrs), attrs.getIndex("", ATTR_HREF),
attrs.getValue("", ATTR_HREF), LibraryType.CSS, request);

} else if (SaxElementUtils.isJavaScript(elementName, attrs)) {
return this.rebuildAttributes(new AttributesImpl(attrs), attrs.getIndex("", ATTR_JS_PATH),
attrs.getValue("", ATTR_JS_PATH), LibraryType.JS, request);
String attributeName = StringUtils.equals(elementName, "script") ? ATTR_SRC : ATTR_HREF;
return this.rebuildAttributes(new AttributesImpl(attrs), attrs.getIndex("", attributeName),
attrs.getValue("", attributeName), LibraryType.JS, request);

} else {
return attrs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ public void testIsJavascript() throws Exception {
makeAtts(
"src", "/js.js")));

assertTrue("JS in link Happy Path",
SaxElementUtils.isJavaScript("link",
makeAtts(
"href", "/js.js",
"type", SaxElementUtils.JS_TYPE)));

assertFalse("JS - not a link",
SaxElementUtils.isJavaScript("notscript",
makeAtts(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,25 @@ public void testJavaScriptClientLibrary() throws Exception {
assertEquals(PATH + "."+ FAKE_STREAM_CHECKSUM +".js", attributesCaptor.getValue().getValue(0));
}

@Test
public void testJavaScriptClientLibraryInLink() throws Exception {

when(htmlLibraryManager.getLibrary(eq(LibraryType.JS), eq(PATH))).thenReturn(htmlLibrary);

final AttributesImpl in = new AttributesImpl();
in.addAttribute("", "href", "", "CDATA", PATH + ".js");
in.addAttribute("", "type", "", "CDATA", "text/javascript");

transformer.startElement(null, "link", null, in);

ArgumentCaptor<Attributes> attributesCaptor = ArgumentCaptor.forClass(Attributes.class);

verify(handler, only()).startElement(isNull(), eq("link"), isNull(),
attributesCaptor.capture());

assertEquals(PATH + "."+ FAKE_STREAM_CHECKSUM +".js", attributesCaptor.getValue().getValue(0));
}

@Test
public void testJavaScriptClientLibraryWithDot() throws Exception {
final String path = PATH + ".foo";
Expand Down
Loading