Skip to content

Commit

Permalink
Merge pull request #5186 from eclipse-ee4j/1760_add_back_id_attribute…
Browse files Browse the repository at this point in the history
…_to_head_and_body

Add back id attribute to VDL of h:head and h:body
  • Loading branch information
BalusC authored Jan 30, 2023
2 parents 3aed0bd + d76bb38 commit 3c5a76b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ public void decode(FacesContext context, UIComponent component) {
public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
ResponseWriter writer = context.getResponseWriter();
writer.startElement("body", component);
writeIdAttributeIfNecessary(context, writer, component);
if (RenderKitUtils.isOutputHtml5Doctype(context)) {
writeIdAttributeIfNecessary(context, writer, component);
}
String styleClass = (String) component.getAttributes().get("styleClass");
if (styleClass != null && styleClass.length() != 0) {
writer.writeAttribute("class", styleClass, "styleClass");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import jakarta.faces.component.UIViewRoot;
import jakarta.faces.context.FacesContext;
import jakarta.faces.context.ResponseWriter;
import jakarta.faces.render.Renderer;

/**
* /**
Expand All @@ -35,7 +34,7 @@
* resources that should be output before the <code>head</code> tag is closed.
* </p>
*/
public class HeadRenderer extends Renderer {
public class HeadRenderer extends HtmlBasicRenderer {

private static final Attribute[] HEAD_ATTRIBUTES = AttributeManager.getAttributes(AttributeManager.Key.OUTPUTHEAD);

Expand All @@ -48,12 +47,10 @@ public void decode(FacesContext context, UIComponent component) {
public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
ResponseWriter writer = context.getResponseWriter();
writer.startElement("head", component);
RenderKitUtils.renderPassThruAttributes(context, writer, component, HEAD_ATTRIBUTES);

if (RenderKitUtils.isOutputHtml5Doctype(context)) {
String clientId = component.getClientId(context);
writer.writeAttribute("id", clientId, "clientId");
writeIdAttributeIfNecessary(context, writer, component);
}
RenderKitUtils.renderPassThruAttributes(context, writer, component, HEAD_ATTRIBUTES);
}

@Override
Expand All @@ -68,6 +65,16 @@ public void encodeEnd(FacesContext context, UIComponent component) throws IOExce
writer.endElement("head");
}

/**
* Do we render our children.
*
* @return false.
*/
@Override
public boolean getRendersChildren() {
return false;
}

// --------------------------------------------------------- Private Methods

private void encodeHeadResources(FacesContext context) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,20 @@
<component-type>jakarta.faces.Output</component-type>
<renderer-type>jakarta.faces.Head</renderer-type>
</component>
<attribute>
<description>
<![CDATA[
<span class="changed_added_4_1">
The component identifier for this component.
This value must be unique within the closest parent component that is a naming container.
The attribute is only rendered when the current doctype is a HTML5 doctype.
</span>
]]>
</description>
<name>id</name>
<required>false</required>
<type>java.lang.String</type>
</attribute>
<attribute>
<description>
<![CDATA[Direction indication for text that does not inherit directionality.
Expand Down Expand Up @@ -1278,6 +1292,20 @@
<component-type>jakarta.faces.Output</component-type>
<renderer-type>jakarta.faces.Body</renderer-type>
</component>
<attribute>
<description>
<![CDATA[
<span class="changed_added_4_1">
The component identifier for this component.
This value must be unique within the closest parent component that is a naming container.
The attribute is only rendered when the current doctype is a HTML5 doctype.
</span>
]]>
</description>
<name>id</name>
<required>false</required>
<type>java.lang.String</type>
</attribute>
<attribute>
<description>
<![CDATA[Direction indication for text that does not inherit directionality.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import jakarta.faces.application.Application;
import jakarta.faces.application.ProjectStage;
import jakarta.faces.component.Doctype;
import jakarta.faces.component.UIViewRoot;
import jakarta.faces.component.html.HtmlBody;
import jakarta.faces.context.FacesContext;
Expand Down Expand Up @@ -57,11 +58,18 @@ public void testEncodeBegin() throws Exception {
StringWriter writer = new StringWriter();
ResponseWriter testResponseWriter = new TestResponseWriter(writer);
FacesContext facesContext = PowerMock.createPartialMock(FacesContext.class, "getResponseWriter");
UIViewRoot viewRoot = PowerMock.createMock(UIViewRoot.class);
Doctype doctype = PowerMock.createMock(Doctype.class);
BodyRenderer bodyRenderer = new BodyRenderer();
HtmlBody htmlBody = new HtmlBody();
htmlBody.getAttributes().put("styleClass", "myclass");

expect(facesContext.getResponseWriter()).andReturn(testResponseWriter).anyTimes();
expect(facesContext.getViewRoot()).andReturn(viewRoot).anyTimes();
expect(viewRoot.getDoctype()).andReturn(doctype).anyTimes();
expect(doctype.getRootElement()).andReturn("html").anyTimes();
expect(doctype.getPublic()).andReturn(null).anyTimes();
expect(doctype.getSystem()).andReturn(null).anyTimes();

PowerMock.replay(facesContext);
bodyRenderer.encodeBegin(facesContext, htmlBody);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.junit.Test;
import org.powermock.api.easymock.PowerMock;

import jakarta.faces.component.Doctype;
import jakarta.faces.component.UIViewRoot;
import jakarta.faces.component.html.HtmlHead;
import jakarta.faces.context.FacesContext;
Expand All @@ -51,13 +52,26 @@ public void testDecode() {
*/
@Test
public void testEncodeBegin() throws Exception {
//
// TODO: Note we are not testing this method as its complexity is too
// high, because it uses WebConfiguration.getInstance to get
// configuration information that should be readily available to the
// renderer through either the FacesContext or the component being
// rendered.
//
StringWriter writer = new StringWriter();
ResponseWriter testResponseWriter = new TestResponseWriter(writer);
FacesContext facesContext = PowerMock.createPartialMock(FacesContext.class, "getResponseWriter");
UIViewRoot viewRoot = PowerMock.createMock(UIViewRoot.class);
Doctype doctype = PowerMock.createMock(Doctype.class);
HeadRenderer headRenderer = new HeadRenderer();
HtmlHead htmlHead = new HtmlHead();

expect(facesContext.getResponseWriter()).andReturn(testResponseWriter).anyTimes();
expect(facesContext.getViewRoot()).andReturn(viewRoot).anyTimes();
expect(viewRoot.getDoctype()).andReturn(doctype).anyTimes();
expect(doctype.getRootElement()).andReturn("html").anyTimes();
expect(doctype.getPublic()).andReturn(null).anyTimes();
expect(doctype.getSystem()).andReturn(null).anyTimes();

PowerMock.replay(facesContext);
headRenderer.encodeBegin(facesContext, htmlHead);
PowerMock.verify(facesContext);
String html = writer.toString();
assertTrue(html.contains("<head"));
}

/**
Expand Down

0 comments on commit 3c5a76b

Please sign in to comment.