Class StyleToHeadHelper

java.lang.Object
sk.iway.iwcm.doc.showdoc.StyleToHeadHelper

public class StyleToHeadHelper extends Object
Helper class for extracting <style> tags and <link rel="stylesheet"> tags from HTML component output and collecting them for later insertion into the <head> section. This solves HTML validation issues where <style> and stylesheet <link> tags appear in the body instead of the head section, and ensures correct CSS cascade order. Usage: 1. Call extractAndCollectStyles() during component processing in WriteTag 2. Call getCollectedStyles() to insert into head
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Request attribute key for storing collected styles
    static final String
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    clearCollectedStyles(jakarta.servlet.http.HttpServletRequest request)
    Clears collected styles from the request.
    extractAndCollectStyles(StringBuilder htmlCode, jakarta.servlet.http.HttpServletRequest request)
    Extracts all <style> tags and <link rel="stylesheet"> tags from the given HTML code, removes them from the content, and stores them in the request for later insertion into the head section.
    static String
    getCollectedStyles(jakarta.servlet.http.HttpServletRequest request)
    Returns all collected styles as a single string ready for insertion into the head section.
    static boolean
    hasCollectedStyles(jakarta.servlet.http.HttpServletRequest request)
    Checks if there are any collected styles in the request.
    static String
    Insert style tags into the head section of HTML.
    static boolean
    isMoveStyleToHeadEnabled(jakarta.servlet.http.HttpServletRequest request)
    Returns true if move style to head feature is enabled for the current request, either via request attribute or global configuration.
    static void
    setMoveStyleToHeadEnabled(boolean enabled, jakarta.servlet.http.HttpServletRequest request)
    Sets the request attribute to enable or disable moving styles to head for the current request.
    static void
    setRequestAttribute(TemplateDetails temp, jakarta.servlet.http.HttpServletRequest request)
    Sets the request attribute to enable or disable moving styles to head based on the TemplateDetails configuration.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Method Details

    • extractAndCollectStyles

      public static StringBuilder extractAndCollectStyles(StringBuilder htmlCode, jakarta.servlet.http.HttpServletRequest request)
      Extracts all <style> tags and <link rel="stylesheet"> tags from the given HTML code, removes them from the content, and stores them in the request for later insertion into the head section. If showDocMoveStyleToHead config is disabled, returns the original content unchanged.
      Parameters:
      htmlCode - The HTML content to process
      request - The HTTP request to store collected styles
      Returns:
      StringBuilder with style and link tags removed (or original if disabled)
    • hasCollectedStyles

      public static boolean hasCollectedStyles(jakarta.servlet.http.HttpServletRequest request)
      Checks if there are any collected styles in the request.
      Parameters:
      request - The HTTP request
      Returns:
      true if styles have been collected, false otherwise
    • getCollectedStyles

      public static String getCollectedStyles(jakarta.servlet.http.HttpServletRequest request)
      Returns all collected styles as a single string ready for insertion into the head section. Styles are returned in the order they were first encountered (LinkedHashSet preserves order).
      Parameters:
      request - The HTTP request
      Returns:
      String containing all style tags, or empty string if none collected
    • clearCollectedStyles

      public static void clearCollectedStyles(jakarta.servlet.http.HttpServletRequest request)
      Clears collected styles from the request. Called after styles have been inserted into the head section.
      Parameters:
      request - The HTTP request
    • insertStylesIntoHead

      public static String insertStylesIntoHead(String html, String styles)
      Insert style tags into the head section of HTML.
      Parameters:
      html - The HTML content
      styles - The style tags to insert
      Returns:
      HTML with styles inserted before </head>
    • isMoveStyleToHeadEnabled

      public static boolean isMoveStyleToHeadEnabled(jakarta.servlet.http.HttpServletRequest request)
      Returns true if move style to head feature is enabled for the current request, either via request attribute or global configuration.
      Parameters:
      request - The HTTP request
      Returns:
      true if the feature is enabled, false otherwise
    • setRequestAttribute

      public static void setRequestAttribute(TemplateDetails temp, jakarta.servlet.http.HttpServletRequest request)
      Sets the request attribute to enable or disable moving styles to head based on the TemplateDetails configuration.
      Parameters:
      temp -
      request -
    • setMoveStyleToHeadEnabled

      public static void setMoveStyleToHeadEnabled(boolean enabled, jakarta.servlet.http.HttpServletRequest request)
      Sets the request attribute to enable or disable moving styles to head for the current request.
      Parameters:
      enabled -
      request -