1 /* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to you under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /** 18 * @class 19 * @name _AjaxResponse 20 * @memberOf myfaces._impl.xhrCore 21 * @extends myfaces._impl.core.Object 22 * @description 23 * This singleton is responsible for handling the standardized xml ajax response 24 * Note: since the semantic processing can be handled about 90% in a functional 25 * style we make this class stateless. Every state information is stored 26 * temporarily in the context. 27 * 28 * The singleton approach also improves performance 29 * due to less object gc compared to the old instance approach. 30 * 31 */ 32 _MF_SINGLTN(_PFX_XHR + "_AjaxResponse", _MF_OBJECT, /** @lends myfaces._impl.xhrCore._AjaxResponse.prototype */ { 33 34 /*partial response types*/ 35 RESP_PARTIAL: "partial-response", 36 RESP_TYPE_ERROR: "error", 37 RESP_TYPE_REDIRECT: "redirect", 38 RESP_TYPE_CHANGES: "changes", 39 40 /*partial commands*/ 41 CMD_CHANGES: "changes", 42 CMD_UPDATE: "update", 43 CMD_DELETE: "delete", 44 CMD_INSERT: "insert", 45 CMD_EVAL: "eval", 46 CMD_ERROR: "error", 47 CMD_ATTRIBUTES: "attributes", 48 CMD_EXTENSION: "extension", 49 CMD_REDIRECT: "redirect", 50 51 /*other constants*/ 52 P_VIEWSTATE: "javax.faces.ViewState", 53 P_CLIENTWINDOW: "javax.faces.ClientWindow", 54 P_VIEWROOT: "javax.faces.ViewRoot", 55 P_VIEWHEAD: "javax.faces.ViewHead", 56 P_VIEWBODY: "javax.faces.ViewBody", 57 P_RESOURCE: "javax.faces.Resource", 58 59 /** 60 * uses response to start Html element replacement 61 * 62 * @param {Object} request (xhrRequest) - xhr request object 63 * @param {Object} context (Map) - AJAX context 64 * 65 * A special handling has to be added to the update cycle 66 * according to the JSDoc specs if the CDATA block contains html tags the outer rim must be stripped 67 * if the CDATA block contains a head section the document head must be replaced 68 * and if the CDATA block contains a body section the document body must be replaced! 69 * 70 */ 71 processResponse: function (request, context) { 72 //mfinternal handling, note, the mfinternal is only optional 73 //according to the spec 74 context._mfInternal = context._mfInternal || {}; 75 var mfInternal = context._mfInternal; 76 77 //the temporary data is hosted here 78 mfInternal._updateElems = []; 79 mfInternal._updateForms = []; 80 mfInternal.appliedViewState = null; 81 mfInternal.appliedClientWindow = null; 82 mfInternal.namingModeId = null; 83 84 85 try { 86 var _Impl = this.attr("impl"), _Lang = this._Lang; 87 // TODO: 88 // Solution from 89 // http://www.codingforums.com/archive/index.php/t-47018.html 90 // to solve IE error 1072896658 when a Java server sends iso88591 91 // istead of ISO-8859-1 92 93 if (!request || !_Lang.exists(request, "responseXML")) { 94 throw this.makeException(new Error(), _Impl.EMPTY_RESPONSE, _Impl.EMPTY_RESPONSE, this._nameSpace, "processResponse", ""); 95 } 96 //check for a parseError under certain browsers 97 98 var xmlContent = request.responseXML; 99 //ie6+ keeps the parsing response under xmlContent.parserError 100 //while the rest of the world keeps it as element under the first node 101 var xmlErr = _Lang.fetchXMLErrorMessage(request.responseText || request.response, xmlContent) 102 if (xmlErr) { 103 throw this._raiseError(new Error(), xmlErr.errorMessage + "\n" + xmlErr.sourceText + "\n" + xmlErr.visualError + "\n", "processResponse"); 104 } 105 var partials = xmlContent.childNodes[0]; 106 if ('undefined' == typeof partials || partials == null) { 107 throw this._raiseError(new Error(), "No child nodes for response", "processResponse"); 108 109 } else { 110 if (partials.tagName != this.RESP_PARTIAL) { 111 // IE 8 sees XML Header as first sibling ... 112 partials = partials.nextSibling; 113 if (!partials || partials.tagName != this.RESP_PARTIAL) { 114 throw this._raiseError(new Error(), "Partial response not set", "processResponse"); 115 } 116 } 117 } 118 119 120 /** 121 * jsf 2.3 naming mode partial response, 122 * we either viewstate all forms (non id mode) 123 * or the forms under the viewroot defined by id 124 * 125 * @type {string} ... the naming mode id is set or an empty string 126 * definitely not a null value to avoid type confusions later on 127 */ 128 mfInternal.namingModeId = (partials.id || ""); 129 130 131 var childNodesLength = partials.childNodes.length; 132 133 for (var loop = 0; loop < childNodesLength; loop++) { 134 var childNode = partials.childNodes[loop]; 135 var tagName = childNode.tagName; 136 /** 137 * <eval> 138 * <![CDATA[javascript]]> 139 * </eval> 140 */ 141 142 //this ought to be enough for eval 143 //however the run scripts still makes sense 144 //in the update and insert area for components 145 //which do not use the response writer properly 146 //we might add this one as custom option in update and 147 //insert! 148 if (tagName == this.CMD_ERROR) { 149 this.processError(request, context, childNode); 150 } else if (tagName == this.CMD_REDIRECT) { 151 this.processRedirect(request, context, childNode); 152 } else if (tagName == this.CMD_CHANGES) { 153 this.processChanges(request, context, childNode); 154 } 155 } 156 157 //fixup missing viewStates due to spec deficiencies 158 if (mfInternal.appliedViewState) { 159 this.fixViewStates(context); 160 } 161 if (mfInternal.appliedClientWindow) { 162 this.fixClientWindows(context); 163 } 164 165 //spec jsdoc, the success event must be sent from response 166 _Impl.sendEvent(request, context, _Impl["SUCCESS"]); 167 } catch (e) { 168 169 if (window.console && window.console.error) { 170 //any error should be logged 171 console.error(e); 172 } 173 throw e; 174 } finally { 175 delete mfInternal._updateElems; 176 delete mfInternal._updateForms; 177 delete mfInternal.appliedViewState; 178 delete mfInternal.appliedClientWindow; 179 delete mfInternal.namingModeId; 180 } 181 }, 182 183 /** 184 * fixes the viewstates in the current page 185 * 186 * @param context 187 */ 188 fixViewStates: function (context) { 189 var _Lang = this._Lang; 190 var mfInternal = context._mfInternal; 191 192 if (null == mfInternal.appliedViewState) { 193 return; 194 } 195 196 /** 197 * JSF 2.3 we set all the viewstates under a given declared viewRoot or all forms 198 * if none is given 199 */ 200 this._updateJSFClientArtifacts(context, mfInternal.appliedViewState, this.P_VIEWSTATE); 201 }, 202 203 204 fixClientWindows: function (context, theForm) { 205 var _Lang = this._Lang; 206 var mfInternal = context._mfInternal; 207 208 if (null == mfInternal.appliedClientWindow) { 209 return; 210 } 211 212 /** 213 * JSF 2.3 we set all the viewstates under a given declared viewRoot or all forms 214 * if none is given 215 */ 216 217 this._updateJSFClientArtifacts(context, mfInternal.appliedClientWindow, this.P_CLIENTWINDOW); 218 219 }, 220 221 222 /** 223 * sets the a jsf artifact element with a given identifier to a new value or adds this element 224 * 225 * @param theForm {Node} the form to which the element has to be set to 226 * @param context the current request context 227 */ 228 _applyJSFArtifactValueToForm: function (context, theForm, value, identifier) { 229 230 if (!theForm) return; 231 var _Lang = this._Lang; 232 var _Dom = this._Dom; 233 var prefix = this._getPrefix(context); 234 235 //in IE7 looking up form elements with complex names (such as 'javax.faces.ViewState') fails in certain cases 236 //iterate through the form elements to find the element, instead 237 var fieldsFound = []; 238 239 var elements = theForm.elements; 240 for (var i = 0, l = elements.length; i < l; i++) { 241 var e = elements[i]; 242 //https://issues.apache.org/jira/browse/MYFACES-4230 243 //ie11 has a deviation from the standard behavior, we have to remap the null/undefined name 244 //to an empty string 245 var eName = e.name || ""; 246 247 if (eName.indexOf(identifier) != -1) { 248 fieldsFound.push(e); 249 } 250 } 251 252 if (fieldsFound.length) { 253 _Lang.arrForEach(fieldsFound, function (fieldFound) { 254 _Dom.setAttribute(fieldFound, "value", value); 255 }); 256 } else { 257 var element = this._Dom.getDummyPlaceHolder(); 258 259 //per JSF 2.3 spec the identifier of the element must be unique in the dom tree 260 //otherwise we will break the html spec here 261 element.innerHTML = ["<input type='hidden'", "id='", this._fetchUniqueId(prefix, identifier), "' name='", this._getNamingContainerPrefix(context) + identifier, "' value='", value, "' />"].join(""); 262 //now we go to proper dom handling after having to deal with another ie screw-up 263 try { 264 theForm.appendChild(element.childNodes[0]); 265 } finally { 266 element.innerHTML = ""; 267 } 268 } 269 }, 270 271 _fetchUniqueId: function(prefix, identifier) { 272 var cnt = 1; 273 var retVal = prefix + identifier + jsf.separatorchar + cnt; 274 while(this._Dom.byId(retVal) != null) { 275 cnt++; 276 retVal = prefix + identifier + jsf.separatorchar + cnt; 277 } 278 return retVal; 279 }, 280 281 /** 282 * updates/inserts the jsf client artifacts under a given viewroot element 283 * 284 * @param context the client context holding all request context data and some internal data 285 * @param elem the root to start with, must be a dom node not an identifier 286 * @param value the new value 287 * @param identifier the identifier for the client artifact aka javax.faces.ViewState, ClientWindowId etc... 288 * 289 * @private 290 */ 291 _updateJSFClientArtifacts: function (context, value, identifier) { 292 293 //elem not found for whatever reason 294 //https://issues.apache.org/jira/browse/MYFACES-3544 295 296 var prefix = this._getPrefix(context); 297 298 //do we still need the issuing form update? I guess it is needed. 299 //jsf spec 2.3 and earlier all issuing forms must update 300 var sourceForm = (context._mfInternal._mfSourceFormId) ? this._Dom.byId(context._mfInternal._mfSourceFormId) : null; 301 if (sourceForm) { 302 sourceForm = this._Dom.byId(sourceForm); 303 if (sourceForm) { 304 //some cases where the source form cannot be updated 305 //because it is gone 306 this._applyJSFArtifactValueToForm(context, sourceForm, value, identifier); 307 } 308 } 309 310 311 312 var viewRoot = this._getViewRoot(context); 313 var forms = this._Dom.findByTagNames(viewRoot, {"form": 1}) || []; 314 315 //since the spec thanks to the over intrusive portlet api still is broken 316 //we need our old fallback hack for proper handling without having 317 //to deal with multiple render targets. 318 319 320 if(this._RT.getLocalOrGlobalConfig(context, "no_portlet_env", false)) { 321 322 //We update all elements under viewroot 323 //this clearly violates the jsf 2.3 jsdocs 324 //however I think that the jsdocs were sloppily updated 325 //because just updating the render targets under one viewroot and the issuing form 326 //again would leave broken viewstates, in the end the portlet spec is at fault here 327 //which came late to the game and expected all frameworks to adapt to their needs. 328 //instead of properly adapting to the frameworks 329 //now the viewroot mechanism per se would work, but people are dropping 330 //jsf 2.3 into old portlet containers which then expose the legacy behavior 331 //of having just one view root. 332 this._Lang.arrForEach(forms, this._Lang.hitch(this, function (elem) { 333 //update all forms which start with prefix (all render and execute targets 334 this._applyJSFArtifactValueToForm(context, elem, value, identifier); 335 })); 336 } else { 337 338 339 //check for a portlet condition a present viewroot 340 341 var viewRootId = viewRoot.id || ""; 342 343 for(var cnt = 0; cnt < context._mfInternal._updateForms.length; cnt++) { 344 var updateForm = context._mfInternal._updateForms[cnt]; 345 346 //follow the spec 2.3 path 1:1 we update the forms hosting the render targets which start 347 //with the viewroot 348 //if there is a viewroot present, however we seem to have a bug in myfaces 349 //even if we have a naming container response we 350 //cannot rely on the naming container being prefixed 351 352 //This atm is not bad, because we safely can assume 353 //that if no viewroot can be found we are under 354 //one single viewroot and can omit the prefix check 355 //(aka fallback into the old behavior) 356 357 358 if(updateForm.indexOf(viewRootId) != 0) { 359 continue; 360 } else { //either an empty viewroot, or a namespace match 361 this._applyJSFArtifactValueToForm(context, this._Dom.byId(updateForm), value, identifier); 362 } 363 } 364 365 } 366 367 }, 368 369 _getViewRoot: function (context) { 370 var prefix = this._getPrefix(context); 371 if (prefix == "") { 372 return document.getElementsByTagName("body")[0]; 373 } 374 prefix = prefix.substr(0, prefix.length - 1); 375 var viewRoot = document.getElementById(prefix); 376 if (viewRoot) { 377 return viewRoot; 378 } 379 return document.getElementsByTagName("body")[0]; 380 }, 381 382 383 _getPrefix: function (context) { 384 var mfInternal = context._mfInternal; 385 var prefix = mfInternal.namingModeId; 386 if (prefix != "") { 387 prefix = prefix + jsf.separatorchar; 388 } 389 return prefix; 390 }, 391 392 _getNamingContainerPrefix: function(context) { 393 var mfInternal = context._mfInternal; 394 var prefix = myfaces._impl.xhrCore._AjaxUtils._$ncRemap(context._mfInternal, ""); 395 return prefix; 396 }, 397 398 /** 399 * processes an incoming error from the response 400 * which is hosted under the <error> tag 401 * @param request the current request 402 * @param context the contect object 403 * @param node the node in the xml hosting the error message 404 */ 405 processError: function (request, context, node) { 406 /** 407 * <error> 408 * <error-name>String</error-name> 409 * <error-message><![CDATA[message]]></error-message> 410 * <error> 411 */ 412 var errorName = node.firstChild.textContent || node.firstChild.text || "", 413 errorMessage = node.childNodes[1].firstChild.data || ""; 414 415 this.attr("impl").sendError(request, context, this.attr("impl").SERVER_ERROR, errorName, errorMessage, "myfaces._impl.xhrCore._AjaxResponse", "processError"); 416 }, 417 418 /** 419 * processes an incoming xml redirect directive from the ajax response 420 * @param request the request object 421 * @param context the context 422 * @param node the node hosting the redirect data 423 */ 424 processRedirect: function (request, context, node) { 425 /** 426 * <redirect url="url to redirect" /> 427 */ 428 var _Lang = this._Lang; 429 var redirectUrl = node.getAttribute("url"); 430 if (!redirectUrl) { 431 throw this._raiseError(new Error(), _Lang.getMessage("ERR_RED_URL", null, "_AjaxResponse.processRedirect"), "processRedirect"); 432 } 433 redirectUrl = _Lang.trim(redirectUrl); 434 if (redirectUrl == "") { 435 return false; 436 } 437 window.location = redirectUrl; 438 return true; 439 } 440 , 441 442 /** 443 * main entry point for processing the changes 444 * it deals with the <changes> node of the 445 * response 446 * 447 * @param request the xhr request object 448 * @param context the context map 449 * @param node the changes node to be processed 450 */ 451 processChanges: function (request, context, node) { 452 var changes = node.childNodes; 453 var _Lang = this._Lang; 454 //note we need to trace the changes which could affect our insert update or delete 455 //se that we can realign our ViewStates afterwards 456 //the realignment must happen post change processing 457 458 for (var i = 0; i < changes.length; i++) { 459 460 switch (changes[i].tagName) { 461 462 case this.CMD_UPDATE: 463 this.processUpdate(request, context, changes[i]); 464 break; 465 //this one needs a csp spec extension for the global eval 466 //for now we recycle the csp for this case from the jsf.js file 467 case this.CMD_EVAL: 468 _Lang.globalEval(changes[i].firstChild.data); 469 break; 470 case this.CMD_INSERT: 471 this.processInsert(request, context, changes[i]); 472 break; 473 case this.CMD_DELETE: 474 this.processDelete(request, context, changes[i]); 475 break; 476 case this.CMD_ATTRIBUTES: 477 this.processAttributes(request, context, changes[i]); 478 break; 479 case this.CMD_EXTENSION: 480 break; 481 case undefined: 482 // ignoring white spaces 483 break; 484 default: 485 throw this._raiseError(new Error(), "_AjaxResponse.processChanges: Illegal Command Issued", "processChanges"); 486 } 487 } 488 489 return true; 490 }, 491 492 /** 493 * First sub-step process a pending update tag 494 * 495 * @param request the xhr request object 496 * @param context the context map 497 * @param node the changes node to be processed 498 */ 499 processUpdate: function (request, context, node) { 500 var mfInternal = context._mfInternal; 501 if ((node.getAttribute('id').indexOf(this.P_VIEWSTATE) != -1) || (node.getAttribute('id').indexOf(this.P_CLIENTWINDOW) != -1)) { 502 if (node.getAttribute('id').indexOf(this.P_VIEWSTATE) != -1) { 503 mfInternal.appliedViewState = this._Dom.concatCDATABlocks(node);//node.firstChild.nodeValue; 504 } else if (node.getAttribute('id').indexOf(this.P_CLIENTWINDOW) != -1) { 505 mfInternal.appliedClientWindow = node.firstChild.nodeValue; 506 } 507 } 508 else { 509 // response may contain several blocks 510 var cDataBlock = this._Dom.concatCDATABlocks(node), 511 resultNode = null, 512 pushOpRes = this._Lang.hitch(this, this._pushOperationResult); 513 514 switch (node.getAttribute('id')) { 515 case this.P_VIEWROOT: 516 517 cDataBlock = cDataBlock.substring(cDataBlock.indexOf("<html")); 518 519 var parsedData = this._replaceHead(request, context, cDataBlock); 520 521 ('undefined' != typeof parsedData && null != parsedData) ? this._replaceBody(request, context, cDataBlock, parsedData) : this._replaceBody(request, context, cDataBlock); 522 523 break; 524 case this.P_VIEWHEAD: 525 //we cannot replace the head, almost no browser allows this, some of them throw errors 526 //others simply ignore it or replace it and destroy the dom that way! 527 this._replaceHead(request, context, cDataBlock); 528 529 break; 530 case this.P_VIEWBODY: 531 //we assume the cdata block is our body including the tag 532 resultNode = this._replaceBody(request, context, cDataBlock); 533 if (resultNode) { 534 pushOpRes(context, resultNode); 535 } 536 break; 537 case this.P_RESOURCE: 538 539 this._addResourceToHead(request, context, cDataBlock); 540 break; 541 default: 542 543 resultNode = this.replaceHtmlItem(request, context, node.getAttribute('id'), cDataBlock); 544 if (resultNode) { 545 pushOpRes(context, resultNode); 546 } 547 break; 548 } 549 } 550 551 return true; 552 }, 553 554 _pushOperationResult: function(context, resultNode) { 555 var mfInternal = context._mfInternal; 556 var pushSubnode = this._Lang.hitch(this, function(currNode) { 557 var parentForm = this._Dom.getParent(currNode, "form"); 558 //if possible we work over the ids 559 //so that elements later replaced are referenced 560 //at the latest possibility 561 if (null != parentForm) { 562 mfInternal._updateForms.push(parentForm.id || parentForm); 563 } 564 else { 565 mfInternal._updateElems.push(currNode.id || currNode); 566 } 567 }); 568 569 var pushEmbedded = this._Lang.hitch(this, function(currNode) { 570 if(currNode.tagName && this._Lang.equalsIgnoreCase(currNode.tagName, "form")) { 571 if(currNode.id) { //should not happen but just in case someone manipulates the html 572 mfInternal._updateForms.push(currNode.id); 573 } 574 } else { 575 var childForms = this._Dom.findByTagName(currNode, "form"); 576 if(childForms && childForms.length) { 577 for(var cnt = 0; cnt < childForms.length; cnt++) { 578 if(childForms[cnt].id) { 579 mfInternal._updateForms.push(childForms[cnt].id); 580 } 581 } 582 } 583 } 584 585 }); 586 587 588 var isArr = 'undefined' != typeof resultNode.length && 'undefined' == typeof resultNode.nodeType; 589 if (isArr && resultNode.length) { 590 for (var cnt = 0; cnt < resultNode.length; cnt++) { 591 pushSubnode(resultNode[cnt]); 592 pushEmbedded(resultNode[cnt]); 593 } 594 } else if (!isArr) { 595 pushSubnode(resultNode); 596 pushEmbedded(resultNode); 597 } 598 599 }, 600 601 602 /** 603 * replaces a current head theoretically, 604 * pratically only the scripts are evaled anew since nothing else 605 * can be changed. 606 * 607 * @param request the current request 608 * @param context the ajax context 609 * @param newData the data to be processed 610 * 611 * @return an xml representation of the page for further processing if possible 612 */ 613 _replaceHead: function (request, context, newData) { 614 615 var _Lang = this._Lang, 616 _Dom = this._Dom; 617 618 var newDom = _Dom.fromMarkup(newData); 619 var newHead = newDom.getElementsByTagName("head")[0]; 620 var oldTags = document.head.childNodes; 621 622 _Dom.deleteItems(_Lang.objToArray(oldTags)); 623 _Dom.appendToHead(newHead); 624 625 626 return document.head; 627 }, 628 629 _addResourceToHead: function (request, context, newData) { 630 this._Dom.appendToHead(newData); 631 }, 632 633 /** 634 * special method to handle the body dom manipulation, 635 * replacing the entire body does not work fully by simply adding a second body 636 * and by creating a range instead we have to work around that by dom creating a second 637 * body and then filling it properly! 638 * 639 * @param {Object} request our request object 640 * @param {Object} context (Map) the response context 641 * @param {String} newData the markup which replaces the old dom node! 642 * @param {Node} parsedData (optional) preparsed XML representation data of the current document 643 */ 644 _replaceBody: function (request, context, newData /*varargs*/) { 645 var _RT = this._RT, 646 _Dom = this._Dom, 647 648 oldBody = document.getElementsByTagName("body")[0], 649 placeHolder = document.createElement("div"); 650 651 placeHolder.id = "myfaces_bodyplaceholder"; 652 653 var newDom = _Dom.fromMarkup(newData); 654 var newBodyData = newDom.getElementsByTagName("body")[0]; 655 656 _Dom._removeChildNodes(oldBody); 657 oldBody.innerHTML = ""; 658 oldBody.appendChild(placeHolder); 659 660 661 //speedwise we serialize back into the code 662 //for code reduction, speedwise we will take a small hit 663 //there which we will clean up in the future, but for now 664 //this is ok, I guess, since replace body only is a small subcase 665 //bodyData = _Lang.serializeChilds(newBodyData); 666 var browser = _RT.browser; 667 if (!browser.isIEMobile || browser.isIEMobile >= 7) { 668 //TODO check what is failing there 669 for (var cnt = 0; cnt < newBodyData.attributes.length; cnt++) { 670 var value = newBodyData.attributes[cnt].value; 671 if (value) 672 _Dom.setAttribute(oldBody, newBodyData.attributes[cnt].name, value); 673 } 674 } 675 676 var returnedElement = this.replaceHtmlItem(request, context, placeHolder, newBodyData.innerHTML); 677 678 if (returnedElement) { 679 this._pushOperationResult(context, returnedElement); 680 } 681 return returnedElement; 682 }, 683 684 /** 685 * Replaces HTML elements through others and handle errors if the occur in the replacement part 686 * 687 * @param {Object} request (xhrRequest) 688 * @param {Object} context (Map) 689 * @param {Object} itemIdToReplace (String|Node) - ID of the element to replace 690 * @param {String} markup - the new tag 691 */ 692 replaceHtmlItem: function (request, context, itemIdToReplace, markup) { 693 var _Lang = this._Lang, _Dom = this._Dom; 694 695 var item = (!_Lang.isString(itemIdToReplace)) ? itemIdToReplace : 696 _Dom.byIdOrName(itemIdToReplace); 697 698 if (!item) { 699 throw this._raiseError(new Error(), _Lang.getMessage("ERR_ITEM_ID_NOTFOUND", null, "_AjaxResponse.replaceHtmlItem", (itemIdToReplace) ? itemIdToReplace.toString() : "undefined"), "replaceHtmlItem"); 700 } 701 return _Dom.outerHTML(item, markup, this._RT.getLocalOrGlobalConfig(context, "preserveFocus", false)); 702 }, 703 704 /** 705 * xml insert command handler 706 * 707 * @param request the ajax request element 708 * @param context the context element holding the data 709 * @param node the xml node holding the insert data 710 * @return true upon successful completion, false otherwise 711 * 712 **/ 713 processInsert: function (request, context, node) { 714 /*remapping global namespaces for speed and readability reasons*/ 715 var _Dom = this._Dom, 716 _Lang = this._Lang, 717 //determine which path to go: 718 insertData = this._parseInsertData(request, context, node); 719 720 if (!insertData) return false; 721 722 var opNode = _Dom.byIdOrName(insertData.opId); 723 if (!opNode) { 724 throw this._raiseError(new Error(), _Lang.getMessage("ERR_PPR_INSERTBEFID_1", null, "_AjaxResponse.processInsert", insertData.opId), "processInsert"); 725 } 726 727 //call insertBefore or insertAfter in our dom routines 728 var replacementFragment = _Dom[insertData.insertType](opNode, insertData.cDataBlock); 729 if (replacementFragment) { 730 this._pushOperationResult(context, replacementFragment); 731 } 732 return true; 733 }, 734 735 /** 736 * determines the corner data from the insert tag parsing process 737 * 738 * 739 * @param request request 740 * @param context context 741 * @param node the current node pointing to the insert tag 742 * @return false if the parsing failed, otherwise a map with follwing attributes 743 * <ul> 744 * <li>inserType - a ponter to a constant which maps the direct function name for the insert operation </li> 745 * <li>opId - the before or after id </li> 746 * <li>cDataBlock - the html cdata block which needs replacement </li> 747 * </ul> 748 * 749 * TODO we have to find a mechanism to replace the direct sendError calls with a javascript exception 750 * which we then can use for cleaner error code handling 751 */ 752 _parseInsertData: function (request, context, node) { 753 var _Lang = this._Lang, 754 _Dom = this._Dom, 755 concatCDATA = _Dom.concatCDATABlocks, 756 757 INSERT_TYPE_BEFORE = "insertBefore", 758 INSERT_TYPE_AFTER = "insertAfter", 759 760 id = node.getAttribute("id"), 761 beforeId = node.getAttribute("before"), 762 afterId = node.getAttribute("after"), 763 ret = {}; 764 765 //now we have to make a distinction between two different parsing paths 766 //due to a spec malalignment 767 //a <insert id="... beforeId|AfterId ="... 768 //b <insert><before id="..., <insert> <after id=".... 769 //see https://issues.apache.org/jira/browse/MYFACES-3318 770 //simple id, case1 771 if (id && beforeId && !afterId) { 772 ret.insertType = INSERT_TYPE_BEFORE; 773 ret.opId = beforeId; 774 ret.cDataBlock = concatCDATA(node); 775 776 //<insert id=".. afterId=".. 777 } else if (id && !beforeId && afterId) { 778 ret.insertType = INSERT_TYPE_AFTER; 779 ret.opId = afterId; 780 ret.cDataBlock = concatCDATA(node); 781 782 //<insert><before id="... <insert><after id="... 783 } else if (!id) { 784 var opType = node.childNodes[0].tagName; 785 786 if (opType != "before" && opType != "after") { 787 throw this._raiseError(new Error(), _Lang.getMessage("ERR_PPR_INSERTBEFID"), "_parseInsertData"); 788 } 789 opType = opType.toLowerCase(); 790 var beforeAfterId = node.childNodes[0].getAttribute("id"); 791 ret.insertType = (opType == "before") ? INSERT_TYPE_BEFORE : INSERT_TYPE_AFTER; 792 ret.opId = beforeAfterId; 793 ret.cDataBlock = concatCDATA(node.childNodes[0]); 794 } else { 795 throw this._raiseError(new Error(), [_Lang.getMessage("ERR_PPR_IDREQ"), 796 "\n ", 797 _Lang.getMessage("ERR_PPR_INSERTBEFID")].join(""), "_parseInsertData"); 798 } 799 ret.opId = _Lang.trim(ret.opId); 800 return ret; 801 }, 802 803 processDelete: function (request, context, node) { 804 805 var _Lang = this._Lang, 806 _Dom = this._Dom, 807 deleteId = node.getAttribute('id'); 808 809 if (!deleteId) { 810 throw this._raiseError(new Error(), _Lang.getMessage("ERR_PPR_UNKNOWNCID", null, "_AjaxResponse.processDelete", ""), "processDelete"); 811 } 812 813 var item = _Dom.byIdOrName(deleteId); 814 if (!item) { 815 throw this._raiseError(new Error(), _Lang.getMessage("ERR_PPR_UNKNOWNCID", null, "_AjaxResponse.processDelete", deleteId), "processDelete"); 816 } 817 818 var parentForm = this._Dom.getParent(item, "form"); 819 if (null != parentForm) { 820 context._mfInternal._updateForms.push(parentForm); 821 } 822 _Dom.deleteItem(item); 823 824 return true; 825 }, 826 827 processAttributes: function (request, context, node) { 828 //we now route into our attributes function to bypass 829 //IE quirks mode incompatibilities to the biggest possible extent 830 //most browsers just have to do a setAttributes but IE 831 //behaves as usual not like the official standard 832 //myfaces._impl._util.this._Dom.setAttribute(domNode, attribute, value; 833 834 var _Lang = this._Lang, 835 //<attributes id="id of element"> <attribute name="attribute name" value="attribute value" />* </attributes> 836 elemId = node.getAttribute('id'); 837 838 if (!elemId) { 839 throw this._raiseError(new Error(), "Error in attributes, id not in xml markup", "processAttributes"); 840 } 841 var childNodes = node.childNodes; 842 843 if (!childNodes) { 844 return false; 845 } 846 for (var loop2 = 0; loop2 < childNodes.length; loop2++) { 847 var attributesNode = childNodes[loop2], 848 attrName = attributesNode.getAttribute("name"), 849 attrValue = attributesNode.getAttribute("value"); 850 851 if (!attrName) { 852 continue; 853 } 854 855 attrName = _Lang.trim(attrName); 856 /*no value means reset*/ 857 //value can be of boolean value hence full check 858 if ('undefined' == typeof attrValue || null == attrValue) { 859 attrValue = ""; 860 } 861 862 switch (elemId) { 863 case this.P_VIEWROOT: 864 throw this._raiseError(new Error(), _Lang.getMessage("ERR_NO_VIEWROOTATTR", null, "_AjaxResponse.processAttributes"), "processAttributes"); 865 866 case this.P_VIEWHEAD: 867 throw this._raiseError(new Error(), _Lang.getMessage("ERR_NO_HEADATTR", null, "_AjaxResponse.processAttributes"), "processAttributes"); 868 869 case this.P_VIEWBODY: 870 var element = document.getElementsByTagName("body")[0]; 871 this._Dom.setAttribute(element, attrName, attrValue); 872 break; 873 874 default: 875 this._Dom.setAttribute(document.getElementById(elemId), attrName, attrValue); 876 break; 877 } 878 } 879 return true; 880 }, 881 882 /** 883 * internal helper which raises an error in the 884 * format we need for further processing 885 * 886 * @param message the message 887 * @param title the title of the error (optional) 888 * @param name the name of the error (optional) 889 */ 890 _raiseError: function (error, message, caller, title, name) { 891 var _Impl = this.attr("impl"); 892 var finalTitle = title || _Impl.MALFORMEDXML; 893 var finalName = name || _Impl.MALFORMEDXML; 894 var finalMessage = message || ""; 895 896 return this._Lang.makeException(error, finalTitle, finalName, this._nameSpace, caller || ( (arguments.caller) ? arguments.caller.toString() : "_raiseError"), finalMessage); 897 } 898 }); 899