Skip to content

Commit

Permalink
https://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-790
Browse files Browse the repository at this point in the history
Fixed hidden client state for render="@ALL" and Ajax navigation (which performs
an implicit render="@ALL") use cases.
  • Loading branch information
stiemannkj1 committed Dec 8, 2016
1 parent 8ed9977 commit 467f9da
Show file tree
Hide file tree
Showing 11 changed files with 288 additions and 130 deletions.
221 changes: 97 additions & 124 deletions jsf-api/src/main/resources/jsf.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,41 @@ if (!((jsf && jsf.specversion && jsf.specversion >= 23000 ) &&
};
};

/**
* Add the hidden state fields (such as javax.faces.ViewState or javax.faces.ClientWindow) to the document after
* a partial response.
* @param stateId - The identifier of the type of state being handled (for example: "javax.faces.ViewState").
* @param context
* @param updateElement
* @param namingContainerId
* @param namingContainerPrefix
* @ignore
*/
var handleHiddenStateFields = function handleHiddenStateFields(stateId, context, updateElement,
namingContainerId, namingContainerPrefix) {

var renderAllContainer = getRenderAllContainer(context, updateElement, namingContainerId);
var firstChild = updateElement.firstChild;
var state = (typeof firstChild.wholeText !== "undefined") ? firstChild.wholeText : firstChild.nodeValue;

// Now set the state from the server into the DOM
// for any JSF form that is covered in the render target list.

var renderForms = getRenderForms(renderAllContainer, context, namingContainerPrefix);

for (var i = 0; i < renderForms.length; i++) {
var f = renderForms[i];
var field = getHiddenStateElement(stateId, f);
if (typeof field === 'undefined') {
field = document.createElement("input");
field.type = "hidden";
field.name = namingContainerPrefix + stateId;
f.appendChild(field);
}
field.value = state;
}
};

/**
* Utility function that determines if a file control exists
* for the form.
Expand Down Expand Up @@ -456,20 +491,54 @@ if (!((jsf && jsf.specversion && jsf.specversion >= 23000 ) &&
return null;
};

/**
* Get the document or element which is the anscestor of all the forms that need to be rendered. If
* the partial response is not rendering the entire view, then this function returns null.
* @param context
* @param updateElement
* @param namingContainerId
* @ignore
*/
var getRenderAllContainer = function getRenderAllContainer(context, updateElement, namingContainerId) {

if (context.render && context.render.indexOf("@all") >= 0) {
return document;
}
else {
var updateElements = updateElement.parentNode.getElementsByTagName("update");

for (var i = 0; i < updateElements.length; i++) {
var id = updateElements[i].getAttribute("id");

// If Ajax navigation occurs but render="@all" is not specified, the update id will be
// "javax.faces.ViewState" or "javax.faces.ViewBody", so check for that as well.
if (id === "javax.faces.ViewRoot" || id === "javax.faces.ViewBody") {
return document;
}

// Detect the portlet render all case and return the portlet element.
else if (namingContainerId && id === namingContainerId) {
return document.getElementById(namingContainerId);
}
}
}

return null;
};

/**
* Get an array of all JSF form elements covered in the render target list whose ID starts with the same
* &lt;VIEW_ROOT_CONTAINER_CLIENT_ID&gt; value as the submitting form.
* This array does not contain the submitting form itself.
*/
var getRenderForms = function getRenderForms(context, namingContainerPrefix) {
var getRenderForms = function getRenderForms(renderAllContainer, context, namingContainerPrefix) {
var renderForms = [];

var add = function(element) {
if (element && element.nodeName
&& element.nodeName.toLowerCase() == "form"
&& element.method == "post"
&& element.id
&& element.id != context.formid
&& element.id.indexOf(namingContainerPrefix) == 0)
{
renderForms.push(element);
Expand All @@ -481,25 +550,23 @@ if (!((jsf && jsf.specversion && jsf.specversion >= 23000 ) &&
add(forms[i]);
}
}
}
};

if (context.render) {
if (context.render.indexOf("@all") >= 0) {
add(document);
}
else {
var clientIds = context.render.split(" ");
if (renderAllContainer) {
add(renderAllContainer);
}
else if (context.render) {
var clientIds = context.render.split(" ");

for (var i = 0; i < clientIds.length; i++) {
if (clientIds.hasOwnProperty(i)) {
add(document.getElementById(clientIds[i]));
}
for (var i = 0; i < clientIds.length; i++) {
if (clientIds.hasOwnProperty(i)) {
add(document.getElementById(clientIds[i]));
}
}
}

return renderForms;
}
};

/**
* Check if a value exists in an array
Expand Down Expand Up @@ -1305,43 +1372,21 @@ if (!((jsf && jsf.specversion && jsf.specversion >= 23000 ) &&
};

/**
* Find javax.faces.ViewState field for a given form.
* Find a state field (such as javax.faces.ViewState or javax.faces.ClientWindow) for a given form.
* @param stateId
* @param form
* @ignore
*/
var getViewStateElement = function getViewStateElement(form) {
var viewStateElement = form['javax.faces.ViewState'];
var getHiddenStateElement = function getHiddenStateElement(stateId, form) {
var hiddenStateElement = form[stateId];

if (viewStateElement) {
return viewStateElement;
if (hiddenStateElement) {
return hiddenStateElement;
} else {
var formElements = form.elements;
for (var i = 0, length = formElements.length; i < length; i++) {
var formElement = formElements[i];
if (formElement.name && (formElement.name.indexOf('javax.faces.ViewState') >= 0)) {
return formElement;
}
}
}

return undefined;
};

/**
* Find javax.faces.ClientWindow field for a given form.
* @param form
* @ignore
*/
var getWindowIdElement = function getWindowIdElement(form) {
var windowIdElement = form['javax.faces.ClientWindow'];

if (windowIdElement) {
return windowIdElement;
} else {
var formElements = form.elements;
for (var i = 0, length = formElements.length; i < length; i++) {
var formElement = formElements[i];
if (formElement.name && (formElement.name.indexOf('javax.faces.ClientWindow') >= 0)) {
if (formElement.name && (formElement.name.indexOf(stateId) >= 0)) {
return formElement;
}
}
Expand All @@ -1356,93 +1401,22 @@ if (!((jsf && jsf.specversion && jsf.specversion >= 23000 ) &&
* @param context context of request
* @ignore
*/
var doUpdate = function doUpdate(element, context, namingContainerPrefix) {
var id, content, markup, state, windowId;
var stateForm, windowIdForm;
var doUpdate = function doUpdate(element, context, namingContainerId) {
var id, content, markup;
var scripts = []; // temp holding value for array of script nodes

id = element.getAttribute('id');
var namingContainerPrefix = namingContainerId ? (namingContainerId + jsf.separatorchar) : "";
var viewStateRegex = new RegExp(namingContainerPrefix + "javax.faces.ViewState" + jsf.separatorchar + ".+$");
var windowIdRegex = new RegExp(namingContainerPrefix + "javax.faces.ClientWindow" + jsf.separatorchar + ".+$");

if (id.match(viewStateRegex)) {

var firstChild = element.firstChild;
state = (typeof firstChild.wholeText !== 'undefined') ? firstChild.wholeText : firstChild.nodeValue;

// Now set the view state from the server into the DOM
// but only for the form that submitted the request.

stateForm = document.getElementById(context.formid);
if (!stateForm || !stateForm.elements) {
// if the form went away for some reason, or it lacks elements
// we're going to just return silently.
return;
}
var field = getViewStateElement(stateForm);
if (typeof field == 'undefined') {
field = document.createElement("input");
field.type = "hidden";
field.name = namingContainerPrefix + "javax.faces.ViewState";
stateForm.appendChild(field);
}
field.value = state;

// Now set the view state from the server into the DOM
// for any JSF form that is covered in the render target list.

var renderForms = getRenderForms(context, namingContainerPrefix);

for (var i = 0; i < renderForms.length; i++) {
var f = renderForms[i];
field = getViewStateElement(f);
if (typeof field === 'undefined') {
field = document.createElement("input");
field.type = "hidden";
field.name = namingContainerPrefix + "javax.faces.ViewState";
f.appendChild(field);
}
field.value = state;
}
handleHiddenStateFields("javax.faces.ViewState", context, element, namingContainerId,
namingContainerPrefix);
return;
} else if (id.match(windowIdRegex)) {

windowId = element.firstChild.nodeValue;

// Now set the windowId from the server into the DOM
// but only for the form that submitted the request.

windowIdForm = document.getElementById(context.formid);
if (!windowIdForm || !windowIdForm.elements) {
// if the form went away for some reason, or it lacks elements
// we're going to just return silently.
return;
}
var field = getWindowIdElement(windowIdForm);
if (typeof field == 'undefined') {
field = document.createElement("input");
field.type = "hidden";
field.name = namingContainerPrefix + "javax.faces.ClientWindow";
windowIdForm.appendChild(field);
}
field.value = windowId;

// Now set the windowId from the server into the DOM
// for any JSF form that is covered in the render target list.

var renderForms = getRenderForms(context, namingContainerPrefix);

for (var i = 0; i < renderForms.length; i++) {
var f = renderForms[i];
field = f.elements["javax.faces.ClientWindow"];
if (typeof field === 'undefined') {
field = document.createElement("input");
field.type = "hidden";
field.name = namingContainerPrefix + "javax.faces.ClientWindow";
f.appendChild(field);
}
field.value = windowId.nodeValue;
}
handleHiddenStateFields("javax.faces.ClientWindow", context, element, namingContainerId,
namingContainerPrefix);
return;
}

Expand Down Expand Up @@ -2503,7 +2477,7 @@ if (!((jsf && jsf.specversion && jsf.specversion >= 23000 ) &&
throw new Error("jsf.ajax.request: Method must be called within a form");
}

viewStateElement = getViewStateElement(form);
viewStateElement = getHiddenStateElement("javax.faces.ViewState", form);
if (!viewStateElement) {
throw new Error("jsf.ajax.request: Form has no view state element");
}
Expand Down Expand Up @@ -2931,7 +2905,6 @@ if (!((jsf && jsf.specversion && jsf.specversion >= 23000 ) &&

var partialResponse = xml.getElementsByTagName("partial-response")[0];
var namingContainerId = partialResponse.getAttribute("id");
var namingContainerPrefix = namingContainerId ? (namingContainerId + jsf.separatorchar) : "";
var responseType = partialResponse.firstChild;

for (var i = 0; i < partialResponse.childNodes.length; i++) {
Expand Down Expand Up @@ -2982,7 +2955,7 @@ if (!((jsf && jsf.specversion && jsf.specversion >= 23000 ) &&
for (var i = 0; i < changes.length; i++) {
switch (changes[i].nodeName) {
case "update":
doUpdate(changes[i], context, namingContainerPrefix);
doUpdate(changes[i], context, namingContainerId);
break;
case "delete":
doDelete(changes[i]);
Expand Down
9 changes: 9 additions & 0 deletions test/javaee8/ajax/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<!-- Pending: upgrade to htmlunit 2.24 when it is released to allow
Spec790IT.testSpec790AjaxNavigation() to pass. -->
<!--
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.24-SNAPSHOT</version>
</dependency>
-->
</dependencies>
<properties>
<netbeans.hint.deploy.server>gfv3ee6</netbeans.hint.deploy.server>
Expand Down
61 changes: 61 additions & 0 deletions test/javaee8/ajax/src/main/webapp/spec790AjaxNavigation.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<!DOCTYPE html>

<!--
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
Copyright (c) 1997-2016 Oracle and/or its affiliates. All rights reserved.
The contents of this file are subject to the terms of either the GNU
General Public License Version 2 only ("GPL") or the Common Development
and Distribution License("CDDL") (collectively, the "License"). You
may not use this file except in compliance with the License. You can
obtain a copy of the License at
https://glassfish.java.net/public/CDDL+GPL_1_1.html
or packager/legal/LICENSE.txt. See the License for the specific
language governing permissions and limitations under the License.
When distributing the software, include this License Header Notice in each
file and include the License file at packager/legal/LICENSE.txt.
GPL Classpath Exception:
Oracle designates this particular file as subject to the "Classpath"
exception as provided by Oracle in the GPL Version 2 section of the License
file that accompanied this code.
Modifications:
If applicable, add the following below the License Header, with the fields
enclosed by brackets [] replaced by your own identifying information:
"Portions Copyright [year] [name of copyright owner]"
Contributor(s):
If you wish your version of this file to be governed by only the CDDL or
only the GPL Version 2, indicate your decision by adding "[Contributor]
elects to include this software in this distribution under the [CDDL or GPL
Version 2] license." If you don't indicate a single choice of license, a
recipient has the option to distribute your version of this file under
either the CDDL, the GPL Version 2 or to extend the choice of license to
its licensees as provided above. However, if you add GPL Version 2 code
and therefore, elected the GPL Version 2 license, then the option applies
only if the new code is made subject to such option by the copyright
holder.
-->

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>JAVASERVERFACES_SPEC_PUBLIC-790 Ajax Navigation - Integration Test</title>
</h:head>
<h:body>
<h:panelGroup layout="block">
<h:form id="formNavigateViaAjax">
<h:commandButton id="button" action="spec790"
value="submit formNavigateViaAjax and navigate to spec790.xhtml via Ajax">
<f:ajax execute=":formNavigateViaAjax" render="@this" />
</h:commandButton>
</h:form>
</h:panelGroup>
</h:body>
</html>
Loading

0 comments on commit 467f9da

Please sign in to comment.