Package sk.iway.iwcm.doc.showdoc
Class StyleToHeadHelper
java.lang.Object
sk.iway.iwcm.doc.showdoc.StyleToHeadHelper
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 -
Method Summary
Modifier and TypeMethodDescriptionstatic voidclearCollectedStyles(jakarta.servlet.http.HttpServletRequest request) Clears collected styles from the request.static StringBuilderextractAndCollectStyles(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 StringgetCollectedStyles(jakarta.servlet.http.HttpServletRequest request) Returns all collected styles as a single string ready for insertion into the head section.static booleanhasCollectedStyles(jakarta.servlet.http.HttpServletRequest request) Checks if there are any collected styles in the request.static StringinsertStylesIntoHead(String html, String styles) Insert style tags into the head section of HTML.static booleanisMoveStyleToHeadEnabled(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 voidsetMoveStyleToHeadEnabled(boolean enabled, jakarta.servlet.http.HttpServletRequest request) Sets the request attribute to enable or disable moving styles to head for the current request.static voidsetRequestAttribute(TemplateDetails temp, jakarta.servlet.http.HttpServletRequest request) Sets the request attribute to enable or disable moving styles to head based on the TemplateDetails configuration.
-
Field Details
-
MOVE_STYLE_TO_HEAD_KEY
- See Also:
-
COLLECTED_STYLES_KEY
Request attribute key for storing collected styles- See Also:
-
-
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 processrequest- 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
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
Insert style tags into the head section of HTML.- Parameters:
html- The HTML contentstyles- 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-
-