This is an automated email from the ASF dual-hosted git repository.
jamesyong pushed a commit to branch trunk in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git The following commit(s) were added to refs/heads/trunk by this push: new 33deb94 Improved: Allow definition of external style sheet in multi-block html template (OFBIZ-11819) 33deb94 is described below commit 33deb945018894613bdc928e0d164529695005ab Author: James Yong <[hidden email]> AuthorDate: Fri Jun 12 14:12:45 2020 +0800 Improved: Allow definition of external style sheet in multi-block html template (OFBIZ-11819) External style sheet in html template will be extracted to layoutSettings.styleSheets when multi-block=true. --- .../org/apache/ofbiz/widget/model/HtmlWidget.java | 18 +++++-- .../ofbiz/widget/model/ModelScreenWidget.java | 2 +- .../widget/model/MultiBlockHtmlTemplateUtil.java | 62 +++++++++++++++------- 3 files changed, 57 insertions(+), 25 deletions(-) diff --git a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/HtmlWidget.java b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/HtmlWidget.java index e03fe38..902cf8e 100644 --- a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/HtmlWidget.java +++ b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/HtmlWidget.java @@ -186,8 +186,8 @@ public class HtmlWidget extends ModelScreenWidget { } } - public static void renderHtmlTemplateMultiBlock(Appendable writer, FlexibleStringExpander locationExdr, - Map<String, Object> context) throws IOException { + public static void renderHtmlTemplateWithMultiBlock(Appendable writer, FlexibleStringExpander locationExdr, + Map<String, Object> context) throws IOException { String location = locationExdr.expandString(context); StringWriter stringWriter = new StringWriter(); @@ -242,6 +242,16 @@ public class HtmlWidget extends ModelScreenWidget { MultiBlockHtmlTemplateUtil.addScriptLinkForFoot(request, url); } } + Elements csslinkElements = doc.select("link"); + if (csslinkElements != null && csslinkElements.size() > 0) { + for (org.jsoup.nodes.Element link : csslinkElements) { + String src = link.attr("href"); + if (UtilValidate.isNotEmpty(src)) { + // remove external style sheet in the template that will be added to the html header + link.remove(); + } + } + } // the 'template' block String body = doc.body().html(); @@ -273,7 +283,7 @@ public class HtmlWidget extends ModelScreenWidget { urls.add(origLoc); } else { try { - urls = MultiBlockHtmlTemplateUtil.getHtmlLinksFromHtmlTemplate(origLoc); + urls = MultiBlockHtmlTemplateUtil.extractHtmlLinksFromRawHtmlTemplate(origLoc); } catch (IOException e) { String errMsg = "Error getting html imports from template at location [" + origLoc + "]: " + e.toString(); Debug.logError(e, errMsg, MODULE); @@ -295,7 +305,7 @@ public class HtmlWidget extends ModelScreenWidget { public void renderWidgetString(Appendable writer, Map<String, Object> context, ScreenStringRenderer screenStringRenderer) throws IOException { if (isMultiBlock()) { - renderHtmlTemplateMultiBlock(writer, this.locationExdr, context); + renderHtmlTemplateWithMultiBlock(writer, this.locationExdr, context); } else { renderHtmlTemplate(writer, this.locationExdr, context); } diff --git a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelScreenWidget.java b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelScreenWidget.java index e0914be..7867197 100644 --- a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelScreenWidget.java +++ b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelScreenWidget.java @@ -288,7 +288,7 @@ public abstract class ModelScreenWidget extends ModelWidget { AbstractModelAction.runSubActions(this.actions, context); try { - MultiBlockHtmlTemplateUtil.addLinksToLayoutSettings(context); + MultiBlockHtmlTemplateUtil.addLinksToLayoutSettingsWhenConditionsAreRight(context); // section by definition do not themselves do anything, so this method will generally do nothing, but we'll call it anyway screenStringRenderer.renderSectionBegin(writer, context, this); diff --git a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/MultiBlockHtmlTemplateUtil.java b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/MultiBlockHtmlTemplateUtil.java index 3181573..12dce33 100644 --- a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/MultiBlockHtmlTemplateUtil.java +++ b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/MultiBlockHtmlTemplateUtil.java @@ -135,11 +135,11 @@ public final class MultiBlockHtmlTemplateUtil { } /** - * Get html import scr location from html template + * Get locations for external css link and external script from raw html template * @param fileLocation Location to html template. Expression is not allowed. * @return */ - public static Set<String> getHtmlLinksFromHtmlTemplate(String fileLocation) throws IOException { + public static Set<String> extractHtmlLinksFromRawHtmlTemplate(String fileLocation) throws IOException { Set<String> imports = new LinkedHashSet<>(); String template = FileUtil.readString("UTF-8", FileUtil.getFile(fileLocation)); Document doc = Jsoup.parseBodyFragment(template); @@ -150,11 +150,20 @@ public final class MultiBlockHtmlTemplateUtil { if (UtilValidate.isNotEmpty(src)) { String dataImport = script.attr("data-import"); if ("head".equals(dataImport)) { - imports.add(src); + imports.add("script:" + src); } } } } + Elements csslinkElements = doc.select("link"); + if (csslinkElements != null && csslinkElements.size() > 0) { + for (org.jsoup.nodes.Element link : csslinkElements) { + String src = link.attr("href"); + if (UtilValidate.isNotEmpty(src)) { + imports.add("link:" + src); + } + } + } return imports; } @@ -184,7 +193,7 @@ public final class MultiBlockHtmlTemplateUtil { * @param context * @throws IOException */ - public static void addLinksToLayoutSettings(final Map<String, Object> context) throws IOException { + public static void addLinksToLayoutSettingsWhenConditionsAreRight(final Map<String, Object> context) throws IOException { HttpServletRequest request = (HttpServletRequest) context.get("request"); if (request == null) { return; @@ -194,8 +203,12 @@ public final class MultiBlockHtmlTemplateUtil { if (UtilValidate.isEmpty(layoutSettings)) { return; } - List<String> layoutSettingsJsList = UtilGenerics.cast(layoutSettings.get("javaScripts")); - if (UtilValidate.isEmpty(layoutSettingsJsList)) { + List<String> layoutSettingsJavaScripts = UtilGenerics.cast(layoutSettings.get("javaScripts")); + if (UtilValidate.isEmpty(layoutSettingsJavaScripts)) { + return; + } + List<String> layoutSettingsStyleSheets = UtilGenerics.cast(layoutSettings.get("styleSheets")); + if (UtilValidate.isEmpty(layoutSettingsStyleSheets)) { return; } // ensure initTheme.groovy has run. @@ -228,7 +241,7 @@ public final class MultiBlockHtmlTemplateUtil { if (url.contains("${")) { String expandUrl = FlexibleStringExpander.expandString(url, context); if (UtilValidate.isNotEmpty(expandUrl)) { - htmlLinks.addAll(getHtmlLinksFromHtmlTemplate(expandUrl)); + htmlLinks.addAll(extractHtmlLinksFromRawHtmlTemplate(expandUrl)); } else { retryTemplateLocationExpressions.add(url); } @@ -241,12 +254,7 @@ public final class MultiBlockHtmlTemplateUtil { } } if (UtilValidate.isNotEmpty(htmlLinks)) { - // check url is not already in layoutSettings.javaScripts - for (String url : htmlLinks) { - if (!layoutSettingsJsList.contains(url)) { - layoutSettingsJsList.add(url); - } - } + addLinksToLayoutSettings(htmlLinks, layoutSettingsJavaScripts, layoutSettingsStyleSheets); } if (UtilValidate.isEmpty(retryScreenLocHashNameExpressions) && UtilValidate.isEmpty(retryTemplateLocationExpressions)) { request.setAttribute(HTML_LINKS_FOR_HEAD, true); @@ -289,19 +297,14 @@ public final class MultiBlockHtmlTemplateUtil { // we know url contains "${", so we expand the url String expandUrl = FlexibleStringExpander.expandString(url, context); if (UtilValidate.isNotEmpty(expandUrl)) { - htmlLinks.addAll(getHtmlLinksFromHtmlTemplate(expandUrl)); + htmlLinks.addAll(extractHtmlLinksFromRawHtmlTemplate(expandUrl)); it.remove(); } } } if (UtilValidate.isNotEmpty(htmlLinks)) { - // check url is not already in layoutSettings.javaScripts - for (String url : htmlLinks) { - if (!layoutSettingsJsList.contains(url)) { - layoutSettingsJsList.add(url); - } - } + addLinksToLayoutSettings(htmlLinks, layoutSettingsJavaScripts, layoutSettingsStyleSheets); } if (UtilValidate.isEmpty(retryScreenLocHashNameExpressions) && UtilValidate.isEmpty(retryTemplateLocationExpressions)) { request.setAttribute(HTML_LINKS_FOR_HEAD, true); @@ -312,7 +315,26 @@ public final class MultiBlockHtmlTemplateUtil { request.setAttribute(HTML_LINKS_FOR_HEAD, retry2); } } + } + private static void addLinksToLayoutSettings(Set<String> htmlLinks, + List<String> layoutSettingsJavaScripts, + List<String> layoutSettingsStyleSheets) { + for (String link : htmlLinks) { + if (link.startsWith("script:")) { + String url = link.substring(7); + // check url is not already in layoutSettings.javaScripts + if (!layoutSettingsJavaScripts.contains(url)) { + layoutSettingsJavaScripts.add(url); + } + } else if (link.startsWith("link:")) { + String url = link.substring(5); + // check url is not already in layoutSettings.styleSheets + if (!layoutSettingsStyleSheets.contains(url)) { + layoutSettingsStyleSheets.add(url); + } + } + } } /** |
Free forum by Nabble | Edit this page |