Skip to content

Commit 1f7e843

Browse files
authored
Merge pull request #5718 from eclipse-ee4j/faces_issue_1590_improvements_after_testing_in_4.0_and_4.1
Faces issue 1590 improvements after testing in 4.0 and 4.1
2 parents 9a1335f + 16b1d64 commit 1f7e843

6 files changed

Lines changed: 41 additions & 28 deletions

File tree

impl/src/main/java/org/glassfish/mojarra/application/view/FaceletViewHandlingStrategy.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
import jakarta.faces.component.Doctype;
8888
import jakarta.faces.component.EditableValueHolder;
8989
import jakarta.faces.component.UIComponent;
90+
import jakarta.faces.component.UIForm;
9091
import jakarta.faces.component.UIPanel;
9192
import jakarta.faces.component.UIViewRoot;
9293
import jakarta.faces.component.html.HtmlDoctype;
@@ -405,7 +406,7 @@ public void renderView(FacesContext ctx, UIViewRoot viewToRender) throws IOExcep
405406
*
406407
* Note if you flag a view as transient then we won't acquire the session as you are stating it does not need one.
407408
*/
408-
if (isServerStateSaving() && !viewToRender.isTransient()) {
409+
if (!viewToRender.isTransient() && extContext.getSession(false) == null && isServerStateSaving() && hasForm(ctx, viewToRender)) {
409410
getSession(ctx);
410411
}
411412

@@ -1841,6 +1842,25 @@ private HttpSession getSession(FacesContext context) {
18411842
return null;
18421843
}
18431844

1845+
/**
1846+
* @return true if the view contains at least one {@link UIForm}, in which case state will be written and a session
1847+
* is needed under server-side state saving.
1848+
*/
1849+
private static boolean hasForm(FacesContext context, UIViewRoot viewRoot) {
1850+
if (viewRoot == null || viewRoot.getChildCount() == 0) {
1851+
return false;
1852+
}
1853+
boolean[] found = { false };
1854+
viewRoot.visitTree(VisitContext.createVisitContext(context), (visitContext, target) -> {
1855+
if (target instanceof UIForm) {
1856+
found[0] = true;
1857+
return VisitResult.COMPLETE;
1858+
}
1859+
return VisitResult.ACCEPT;
1860+
});
1861+
return found[0];
1862+
}
1863+
18441864
/**
18451865
* Gets and if needed initializes the faceletFactory
18461866
*

impl/src/main/java/org/glassfish/mojarra/context/PartialViewContextImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ public void processPartial(PhaseId phaseId) {
307307
if (isRenderAll()) {
308308
renderAll(ctx, viewRoot);
309309
renderState(ctx);
310+
renderEvalScripts(ctx);
310311
doFlashPostPhaseActions(ctx);
311312
writer.endDocument();
312313
return;

impl/src/main/java/org/glassfish/mojarra/renderkit/html_basic/CommandLinkRenderer.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,15 @@ public void encodeEnd(FacesContext context, UIComponent component) throws IOExce
121121
writer.endElement("span");
122122
} else {
123123
writer.endElement("a");
124+
125+
String target = (String) component.getAttributes().get("target");
126+
if (target != null) {
127+
target = target.trim();
128+
} else {
129+
target = "";
130+
}
131+
Collection<ClientBehaviorContext.Parameter> params = getBehaviorParameters(component);
132+
RenderKitUtils.renderOnclickEventListener(context, component, params, target, true);
124133
}
125134
}
126135

@@ -163,16 +172,6 @@ protected void renderAsActive(FacesContext context, UIComponent command) throws
163172
// render the current value as link text.
164173
writeValue(command, writer);
165174
writer.flush();
166-
167-
String target = (String) command.getAttributes().get("target");
168-
if (target != null) {
169-
target = target.trim();
170-
} else {
171-
target = "";
172-
}
173-
174-
Collection<ClientBehaviorContext.Parameter> params = getBehaviorParameters(command);
175-
RenderKitUtils.renderOnclickEventListener(context, command, params, target, true);
176175
}
177176

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

impl/src/main/java/org/glassfish/mojarra/renderkit/html_basic/HtmlBasicRenderer.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
import jakarta.faces.application.FacesMessage;
4545
import jakarta.faces.component.NamingContainer;
46+
import jakarta.faces.component.UICommand;
4647
import jakarta.faces.component.UIComponent;
4748
import jakarta.faces.component.UIInput;
4849
import jakarta.faces.component.UIParameter;
@@ -602,13 +603,16 @@ protected boolean shouldWriteIdAttribute(UIComponent component) {
602603
// By default we only write the id attribute if:
603604
//
604605
// - We have a non-auto-generated id, or...
605-
// - We have client behaviors.
606+
// - We have client behaviors, or...
607+
// - The component is a UICommand (its CSP-style click listener is attached
608+
// via mojarra.ael(clientId, ...) which needs the id on the rendered element).
606609
//
607610
// We assume that if client behaviors are present, they
608611
// may need access to the id (AjaxBehavior certainly does).
609612

610613
String id;
611614
return null != (id = component.getId()) && (!id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX)
615+
|| component instanceof UICommand
612616
|| component instanceof ClientBehaviorHolder && !((ClientBehaviorHolder) component).getClientBehaviors().isEmpty());
613617
}
614618

impl/src/main/resources/META-INF/resources/jakarta.faces/faces-uncompressed.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,6 @@ if ( !( (window.faces && window.faces.specversion && window.faces.specversion >=
117117
return inputElementName in form ? form[inputElementName] : getElementByName(form,inputElementName);
118118
}
119119

120-
/**
121-
* append a new pair of parameter=value to a query string
122-
* @ignore
123-
*/
124120

125121
/**
126122
* return true if one of the dom elements contains
@@ -355,12 +351,13 @@ if ( !( (window.faces && window.faces.specversion && window.faces.specversion >=
355351
};
356352

357353
/**
358-
* Remove all the portion of code matching the script pattern from the passed string
354+
* Remove all the portion of code matching the script pattern from the passed string,
355+
* preserving scripts whose type is set to something other than text/javascript.
359356
* @param html a String containing a portion of html
360357
* @ignore
361358
*/
362359
const removeScripts = function removeScripts(html) {
363-
return html.replace(SCRIPT_TAG_REGEX, function(match, content) {
360+
return html.replace(SCRIPT_TAG_REGEX, (match) => {
364361
const type = match.match(TAG_ATTRIBUTE_TYPE_REGEX);
365362
if (!!type && type[1] !== "text/javascript") {
366363
return match; // keep non-text/javascript scripts
@@ -3228,17 +3225,9 @@ mojarra.l = function l(l) {
32283225
if (document.readyState === "complete") {
32293226
setTimeout(l);
32303227
}
3231-
else if (window.addEventListener) {
3232-
window.addEventListener("load", l, false);
3233-
}
3234-
else if (typeof window.onload === "function") {
3235-
const oldListener = window.onload;
3236-
window.onload = function() { oldListener(); l(); };
3237-
}
32383228
else {
3239-
window.onload = l;
3229+
window.addEventListener("load", l);
32403230
}
3241-
32423231
};
32433232

32443233
/**

impl/src/test/ts/spec/mojarra.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ describe("mojarra.l", () => {
778778

779779
(moj().l as Function)(callback);
780780

781-
expect(spy).toHaveBeenCalledWith("load", callback, false);
781+
expect(spy).toHaveBeenCalledWith("load", callback);
782782

783783
spy.mockRestore();
784784
if (originalReadyState) {

0 commit comments

Comments
 (0)