[ofbiz-framework] branch trunk updated: Improved: multi-block attribute for html-template tag (OFBIZ-11686)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[ofbiz-framework] branch trunk updated: Improved: multi-block attribute for html-template tag (OFBIZ-11686)

James Yong-2
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 cf083f2  Improved: multi-block attribute for html-template tag (OFBIZ-11686)
cf083f2 is described below

commit cf083f2cbee5985a26f357afdaa1d4988459879f
Author: James Yong <[hidden email]>
AuthorDate: Mon Aug 10 09:23:50 2020 +0800

    Improved: multi-block attribute for html-template tag (OFBIZ-11686)
   
    Improved documentation. No code change.
---
 .../java/org/apache/ofbiz/widget/model/HtmlWidget.java  | 17 +++++++++++++++--
 .../ofbiz/widget/model/MultiBlockHtmlTemplateUtil.java  | 16 ++++++++++++++++
 2 files changed, 31 insertions(+), 2 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 10a6ca1..a3ec19f 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
@@ -187,6 +187,19 @@ public class HtmlWidget extends ModelScreenWidget {
         }
     }
 
+    /**
+     * Render html template when multi-block=true.
+     * We use stack to store the string writer because a freemarker template may also render a sub screen widget
+     * by using ${screens.render(<link to the screen>)}. So before rendering the sub screen widget, ScreenRenderer
+     * class will check for the existence of the stack and retrieve the correct string writer.
+     * The following tags are removed from the final rendering:
+     * 1. External and inline javascript tags
+     * 2. Css link tags
+     * @param writer
+     * @param locationExdr
+     * @param context
+     * @throws IOException
+     */
     public static void renderHtmlTemplateWithMultiBlock(Appendable writer, FlexibleStringExpander locationExdr,
                                                         Map<String, Object> context) throws IOException {
         String location = locationExdr.expandString(context);
@@ -197,7 +210,6 @@ public class HtmlWidget extends ModelScreenWidget {
             stringWriterStack = new Stack<>();
         }
         stringWriterStack.push(stringWriter);
-        // we use stack because a freemarker template may render a sub screen widget
         context.put(MultiBlockHtmlTemplateUtil.MULTI_BLOCK_WRITER, stringWriterStack);
         renderHtmlTemplate(stringWriter, locationExdr, context);
         stringWriterStack.pop();
@@ -210,7 +222,7 @@ public class HtmlWidget extends ModelScreenWidget {
 
         Document doc = Jsoup.parseBodyFragment(data);
 
-        // extract scripts
+        // extract js script tags
         Elements scriptElements = doc.select("script");
         if (scriptElements != null && scriptElements.size() > 0) {
             StringBuilder scripts = new StringBuilder();
@@ -253,6 +265,7 @@ public class HtmlWidget extends ModelScreenWidget {
                 MultiBlockHtmlTemplateUtil.addScriptLinkForFoot(request, url);
             }
         }
+        // extract css link tags
         Elements csslinkElements = doc.select("link");
         if (csslinkElements != null && csslinkElements.size() > 0) {
             for (org.jsoup.nodes.Element link : csslinkElements) {
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 702ca83..2a6f6e5 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
@@ -40,6 +40,18 @@ import org.jsoup.Jsoup;
 import org.jsoup.nodes.Document;
 import org.jsoup.select.Elements;
 
+/**
+ * Utility to support different handling of code blocks in an html template:
+ * 1. external script tags with data-import="head" are removed from the rendered template and merged with
+ *    layoutSetting.javaScripts. This helps to keep page-specific external script tags to the html-template that needs it.
+ *    In future when the javascript library allows, we can use import module functionality of the browser instead of
+ *    special handling at the server side.
+ * 2. link tags are removed from the rendered template and merged with layoutSetting.styleSheets.
+ *    This helps to keep page-specific link tags to the html-template that needs it.
+ * 3. Inline javascript tags are turned into external javascript tags for better compliance of Content Security Policy.
+ *    These external javascript tags are placed at the bottom of the html page. The scripts are retrieved via the getJs
+ *    request handler.
+ */
 public final class MultiBlockHtmlTemplateUtil {
 
     private static final String MODULE = MultiBlockHtmlTemplateUtil.class.getName();
@@ -438,6 +450,10 @@ public final class MultiBlockHtmlTemplateUtil {
         return "";
     }
 
+    /**
+     * cleanup the script cache when user session is invalidated.
+     * @param session
+     */
     public static void cleanupScriptCache(HttpSession session) {
         scriptCache.remove(session.getId());
     }