Skip to content

Commit

Permalink
PBR-495 Avoid self closing <script /> tags
Browse files Browse the repository at this point in the history
 Feature activated in web.xml with org.jboss.portletbridge.PREVENT_SELF_CLOSING_SCRIPT_TAG context-param set to true
  • Loading branch information
kenfinnigan committed Feb 27, 2013
1 parent 0c04400 commit 4ada928
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
Expand Up @@ -274,7 +274,7 @@ public interface BridgeConfig {
* Sets the <code>Class</code> that the bridge uses to wrap the response when rendering a <code>JSP</code> to implement the
* Faces implementation specific support for handling interleaved response writing.
*
* @param wbrClass <code>Class</code> that implements the <code>BridgeWritebehindResponse</code> interface and is a proper
* @param renderResponseWrapper <code>Class</code> that implements the <code>BridgeWritebehindResponse</code> interface and is a proper
* portlet render response wrapper.
*/
void setWriteBehindRenderResponseWrapper(Class<? extends BridgeWriteBehindResponse> renderResponseWrapper);
Expand All @@ -292,7 +292,7 @@ public interface BridgeConfig {
* Sets the <code>Class</code> that the bridge uses to wrap the response when rendering a <code>JSP</code> resource to
* implement the Faces implementation specific support for handling interleaved response writing.
*
* @param wbrClass <code>Class</code> that implements the <code>BridgeWritebehindResponse</code> interface and is a proper
* @param resourceResponseWrapper <code>Class</code> that implements the <code>BridgeWritebehindResponse</code> interface and is a proper
* portlet resource response wrapper.
*/
void setWriteBehindResourceResponseWrapper(Class<? extends BridgeWriteBehindResponse> resourceResponseWrapper);
Expand Down Expand Up @@ -354,4 +354,19 @@ public interface BridgeConfig {
*/
String getDefaultRenderKitId();

/**
* Sets whether or not the bridge should prevent script tags from being rendered as self-closing in the page HEAD.
*
* @param preventSelfClosingScriptTag <code>Boolean.TRUE</code> indicates the script tag will not be self-closing.
* <code>Boolean.FALSE</code> indicates they are self-closing.
*/
void setPreventSelfClosingScriptTag(boolean preventSelfClosingScriptTag);

/**
* Gets whether or not the bridge should prevent script tags from being rendered as self-closing in the page HEAD.
* If not previously set, it returns <code>false</code>.
*
* @return <code>true</code> the script tag will not be self-closing. <code>false</code> indicates they are self-closing.
*/
boolean doPreventSelfClosingScriptTag();
}
Expand Up @@ -33,4 +33,6 @@ public interface PortletBridgeConstants {
String AJAX_PARAM = "_pbrAjax";

String WSRP_REQUEST_PARAM = "org.gatein.invocation.fromWSRP";

String PREVENT_SELF_CLOSING_SCRIPT_TAG_PARAM = "org.jboss.portletbridge.PREVENT_SELF_CLOSING_SCRIPT_TAG";
}
Expand Up @@ -194,6 +194,10 @@ private BridgeConfig getBridgeConfig(PortletConfig portletConfig) {
throw new BridgeException("No JSF view id's defined in portlet.xml for " + portletConfig.getPortletName());
}

// PBR-495 - Parameter to determine whether HeadRenderer forces non self-closing script tags
String preventSelfClosing = portletContext.getInitParameter(PortletBridgeConstants.PREVENT_SELF_CLOSING_SCRIPT_TAG_PARAM);
bridgeConfig.setPreventSelfClosingScriptTag(Boolean.parseBoolean(preventSelfClosing) ? true : false);

return bridgeConfig;
}

Expand Down
Expand Up @@ -67,6 +67,7 @@ public class BridgeConfigImpl implements BridgeConfig {
private String viewIdParameterName = Bridge.FACES_VIEW_ID_PARAMETER;
private String viewIdResourceParameterName = VIEWID_RESOURCE_PARAMETER_NAME;
private Map<String, Object> attributes;
private boolean preventSelfClosingScriptTag = false;

public BridgeConfigImpl() {
}
Expand Down Expand Up @@ -370,4 +371,19 @@ public String getDefaultRenderKitId() {
Bridge.BRIDGE_PACKAGE_PREFIX + portletConfig.getPortletName() + "." + Bridge.DEFAULT_RENDERKIT_ID);
}

/**
* @see org.jboss.portletbridge.bridge.config.BridgeConfig#doPreventSelfClosingScriptTag()
*/
@Override
public boolean doPreventSelfClosingScriptTag() {
return preventSelfClosingScriptTag;
}

/**
* @see org.jboss.portletbridge.bridge.config.BridgeConfig#setPreventSelfClosingScriptTag(boolean)
*/
@Override
public void setPreventSelfClosingScriptTag(boolean preventSelfClosingScriptTag) {
this.preventSelfClosingScriptTag = preventSelfClosingScriptTag;
}
}
Expand Up @@ -21,6 +21,7 @@
*/
package org.jboss.portletbridge.renderkit.portlet;

import org.jboss.portletbridge.bridge.context.BridgeContext;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
Expand All @@ -45,11 +46,13 @@ public class PortletHeadResponseWriter extends ResponseWriterWrapper {
PortletResponse response;

private Stack<Element> elements;
private boolean preventSelfClosing = false;

public PortletHeadResponseWriter(ResponseWriter parent, PortletResponse portletResponse) {
this.wrapped = parent;
this.response = portletResponse;
this.elements = new Stack<Element>();
this.preventSelfClosing = BridgeContext.getCurrentInstance().getBridgeConfig().doPreventSelfClosingScriptTag();
}

/**
Expand All @@ -73,14 +76,14 @@ public void endElement(String name) throws IOException {
} else {
Element elem = elements.pop();

if (("script".equals(name) || "style".equals(name)) && !elem.hasAttribute("src")) {
if (("script".equalsIgnoreCase(name) || "style".equalsIgnoreCase(name)) && !elem.hasAttribute("src")) {
Text text1;
Text text2 = null;
CDATASection cdata;
String content = elem.getTextContent();
Document owner = elem.getOwnerDocument();

if ("script".equals(name)) {
if ("script".equalsIgnoreCase(name)) {
text1 = owner.createTextNode("\n//");
cdata = owner.createCDATASection("\n" + content + "\n//");
} else {
Expand All @@ -95,6 +98,10 @@ public void endElement(String name) throws IOException {
if (null != text2) {
elem.appendChild(text2);
}
} else if (preventSelfClosing) {
if (("script".equalsIgnoreCase(name)) && (null == elem.getTextContent() || elem.getTextContent().length() == 0)) {
elem.appendChild(elem.getOwnerDocument().createComment(" "));
}
}
response.addProperty(MimeResponse.MARKUP_HEAD_ELEMENT, elem);
}
Expand Down

0 comments on commit 4ada928

Please sign in to comment.