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_VIEWROOT: "javax.faces.ViewRoot", 54 P_VIEWHEAD: "javax.faces.ViewHead", 55 P_VIEWBODY: "javax.faces.ViewBody", 56 57 58 /** 59 * uses response to start Html element replacement 60 * 61 * @param {Object} request (xhrRequest) - xhr request object 62 * @param {Object} context (Map) - AJAX context 63 * 64 * A special handling has to be added to the update cycle 65 * according to the JSDoc specs if the CDATA block contains html tags the outer rim must be stripped 66 * if the CDATA block contains a head section the document head must be replaced 67 * and if the CDATA block contains a body section the document body must be replaced! 68 * 69 */ 70 processResponse : function(request, context) { 71 //mfinternal handling, note, the mfinternal is only optional 72 //according to the spec 73 context._mfInternal = context._mfInternal || {}; 74 var mfInternal = context._mfInternal; 75 76 //the temporary data is hosted here 77 mfInternal._updateElems = []; 78 mfInternal._updateForms = []; 79 mfInternal.appliedViewState = null; 80 81 try { 82 var _Impl = this.attr("impl"), _Lang = this._Lang; 83 // TODO: 84 // Solution from 85 // http://www.codingforums.com/archive/index.php/t-47018.html 86 // to solve IE error 1072896658 when a Java server sends iso88591 87 // istead of ISO-8859-1 88 89 if (!request || !_Lang.exists(request, "responseXML")) { 90 throw this.makeException(new Error(), _Impl.EMPTY_RESPONSE, _Impl.EMPTY_RESPONSE, this._nameSpace, "processResponse", ""); 91 } 92 //check for a parseError under certain browsers 93 94 var xmlContent = request.responseXML; 95 //ie6+ keeps the parsing response under xmlContent.parserError 96 //while the rest of the world keeps it as element under the first node 97 var xmlErr = _Lang.fetchXMLErrorMessage(request.responseText || request.response, xmlContent) 98 if (xmlErr) { 99 throw this._raiseError(new Error(),xmlErr.errorMessage+"\n"+xmlErr.sourceText+"\n"+xmlErr.visualError+"\n", "processResponse"); 100 } 101 var partials = xmlContent.childNodes[0]; 102 if ('undefined' == typeof partials || partials == null) { 103 throw this._raiseError(new Error(),"No child nodes for response", "processResponse"); 104 105 } else { 106 if (partials.tagName != this.RESP_PARTIAL) { 107 // IE 8 sees XML Header as first sibling ... 108 partials = partials.nextSibling; 109 if (!partials || partials.tagName != this.RESP_PARTIAL) { 110 throw this._raiseError(new Error(), "Partial response not set","processResponse"); 111 } 112 } 113 } 114 115 var childNodesLength = partials.childNodes.length; 116 117 for (var loop = 0; loop < childNodesLength; loop++) { 118 var childNode = partials.childNodes[loop]; 119 var tagName = childNode.tagName; 120 /** 121 * <eval> 122 * <![CDATA[javascript]]> 123 * </eval> 124 */ 125 126 //this ought to be enough for eval 127 //however the run scripts still makes sense 128 //in the update and insert area for components 129 //which do not use the response writer properly 130 //we might add this one as custom option in update and 131 //insert! 132 if (tagName == this.CMD_ERROR) { 133 this.processError(request, context, childNode); 134 } else if (tagName == this.CMD_REDIRECT) { 135 this.processRedirect(request, context, childNode); 136 } else if (tagName == this.CMD_CHANGES) { 137 this.processChanges(request, context, childNode); 138 } 139 } 140 141 //fixup missing viewStates due to spec deficiencies 142 this.fixViewStates(context); 143 144 //spec jsdoc, the success event must be sent from response 145 _Impl.sendEvent(request, context, _Impl["SUCCESS"]); 146 147 } finally { 148 delete mfInternal._updateElems; 149 delete mfInternal._updateForms; 150 delete mfInternal.appliedViewState; 151 } 152 }, 153 154 /** 155 * fixes the viewstates in the current page 156 * 157 * @param context 158 */ 159 fixViewStates : function(context) { 160 var _Lang = this._Lang; 161 var mfInternal = context._mfInternal; 162 163 if (null == mfInternal.appliedViewState) { 164 return; 165 } 166 167 //if we set our no portlet env we safely can update all forms with 168 //the new viewstate 169 if (this._RT.getLocalOrGlobalConfig(context, "no_portlet_env", false)) { 170 for (var cnt = document.forms.length - 1; cnt >= 0; cnt --) { 171 this._setVSTForm(context, document.forms[cnt]); 172 } 173 return; 174 } 175 176 // Now update the forms that were not replaced but forced to be updated, because contains child ajax tags 177 // we should only update forms with view state hidden field. If by some reason, the form was set to be 178 // updated but the form was replaced, it does not have hidden view state, so later in changeTrace processing the 179 // view state is updated. 180 181 //set the viewstates of all outer forms parents of our updated elements 182 var _T = this; 183 _Lang.arrForEach(mfInternal._updateForms, function(elem) { 184 _T._setVSTForm(context, elem); 185 }, 0, this); 186 187 //set the viewstate of all forms within our updated elements 188 _Lang.arrForEach(mfInternal._updateElems, function(elem) { 189 _T._setVSTInnerForms(context, elem); 190 }, 0, this); 191 } 192 , 193 194 /** 195 * sets the viewstate element in a given form 196 * 197 * @param theForm the form to which the element has to be set to 198 * @param context the current request context 199 */ 200 _setVSTForm: function(context, theForm) { 201 theForm = this._Lang.byId(theForm); 202 var mfInternal = context._mfInternal; 203 204 if (!theForm) return; 205 206 //in IE7 looking up form elements with complex names (such as 'javax.faces.ViewState') fails in certain cases 207 //iterate through the form elements to find the element, instead 208 var viewStateField = null; 209 if (theForm.elements) { 210 var elements = theForm.elements; 211 for (var i = 0, l = elements.length; i < l; i++) { 212 var e = elements[i]; 213 if (e.name == this.P_VIEWSTATE) { 214 viewStateField = e; 215 } 216 } 217 } 218 219 if (viewStateField) { 220 this._Dom.setAttribute(viewStateField, "value", mfInternal.appliedViewState); 221 } else if (!viewStateField) { 222 var element = this._Dom.getDummyPlaceHolder(); 223 //spec error, two elements with the same id should not be there, TODO recheck the space if the name does not suffice alone 224 element.innerHTML = ["<input type='hidden'", "id='", this.P_VIEWSTATE ,"' name='", this.P_VIEWSTATE ,"' value='" , mfInternal.appliedViewState , "' />"].join(""); 225 //now we go to proper dom handling after having to deal with another ie screwup 226 try { 227 theForm.appendChild(element.childNodes[0]); 228 } finally { 229 element.innerHTML = ""; 230 } 231 } 232 } 233 , 234 235 _setVSTInnerForms: function(context, elem) { 236 237 var _Lang = this._Lang, _Dom = this._Dom; 238 elem = _Dom.byIdOrName(elem); 239 //elem not found for whatever reason 240 //https://issues.apache.org/jira/browse/MYFACES-3544 241 if(!elem) return; 242 243 var replacedForms = _Dom.findByTagName(elem, "form", false); 244 var applyVST = _Lang.hitch(this, function(elem) { 245 this._setVSTForm(context, elem); 246 }); 247 248 try { 249 _Lang.arrForEach(replacedForms, applyVST, 0, this); 250 } finally { 251 applyVST = null; 252 } 253 }, 254 255 /** 256 * processes an incoming error from the response 257 * which is hosted under the <error> tag 258 * @param request the current request 259 * @param context the contect object 260 * @param node the node in the xml hosting the error message 261 */ 262 processError : function(request, context, node) { 263 /** 264 * <error> 265 * <error-name>String</error-name> 266 * <error-message><![CDATA[message]]></error-message> 267 * <error> 268 */ 269 var errorName = node.firstChild.textContent || "", 270 errorMessage = node.childNodes[1].firstChild.data || ""; 271 272 this.attr("impl").sendError(request, context, this.attr("impl").SERVER_ERROR, errorName, errorMessage, "myfaces._impl.xhrCore._AjaxResponse", "processError"); 273 }, 274 275 /** 276 * processes an incoming xml redirect directive from the ajax response 277 * @param request the request object 278 * @param context the context 279 * @param node the node hosting the redirect data 280 */ 281 processRedirect : function(request, context, node) { 282 /** 283 * <redirect url="url to redirect" /> 284 */ 285 var _Lang = this._Lang; 286 var redirectUrl = node.getAttribute("url"); 287 if (!redirectUrl) { 288 throw this._raiseError(new Error(),_Lang.getMessage("ERR_RED_URL", null, "_AjaxResponse.processRedirect"),"processRedirect"); 289 } 290 redirectUrl = _Lang.trim(redirectUrl); 291 if (redirectUrl == "") { 292 return false; 293 } 294 window.location = redirectUrl; 295 return true; 296 } 297 , 298 299 /** 300 * main entry point for processing the changes 301 * it deals with the <changes> node of the 302 * response 303 * 304 * @param request the xhr request object 305 * @param context the context map 306 * @param node the changes node to be processed 307 */ 308 processChanges : function(request, context, node) { 309 var changes = node.childNodes; 310 var _Lang = this._Lang; 311 //note we need to trace the changes which could affect our insert update or delete 312 //se that we can realign our ViewStates afterwards 313 //the realignment must happen post change processing 314 315 for (var i = 0; i < changes.length; i++) { 316 317 switch (changes[i].tagName) { 318 319 case this.CMD_UPDATE: 320 this.processUpdate(request, context, changes[i]); 321 break; 322 case this.CMD_EVAL: 323 _Lang.globalEval(changes[i].firstChild.data); 324 break; 325 case this.CMD_INSERT: 326 this.processInsert(request, context, changes[i]); 327 break; 328 case this.CMD_DELETE: 329 this.processDelete(request, context, changes[i]); 330 break; 331 case this.CMD_ATTRIBUTES: 332 this.processAttributes(request, context, changes[i]); 333 break; 334 case this.CMD_EXTENSION: 335 break; 336 default: 337 throw this._raiseError(new Error(),"_AjaxResponse.processChanges: Illegal Command Issued","processChanges"); 338 } 339 } 340 341 return true; 342 } 343 , 344 345 /** 346 * First sub-step process a pending update tag 347 * 348 * @param request the xhr request object 349 * @param context the context map 350 * @param node the changes node to be processed 351 */ 352 processUpdate : function(request, context, node) { 353 if (node.getAttribute('id') == this.P_VIEWSTATE) { 354 //update the submitting forms viewstate to the new value 355 // The source form has to be pulled out of the CURRENT document first because the context object 356 // may refer to an invalid document if an update of the entire body has occurred before this point. 357 var mfInternal = context._mfInternal, 358 fuzzyFormDetection = this._Lang.hitch(this._Dom, this._Dom.fuzzyFormDetection); 359 var elemId = (mfInternal._mfSourceControlId)? mfInternal._mfSourceControlId: 360 ((context.source)?context.source.id: null); 361 362 //theoretically a source of null can be given, then our form detection fails for 363 //the source element case and hence updateviewstate is skipped for the source 364 //form, but still render targets still can get the viewstate 365 var sourceForm = (mfInternal && mfInternal["_mfSourceFormId"] && 366 document.forms[mfInternal["_mfSourceFormId"]]) ? 367 document.forms[mfInternal["_mfSourceFormId"]] : ((elemId)? fuzzyFormDetection(elemId): null); 368 369 mfInternal.appliedViewState = node.firstChild.nodeValue; 370 //source form could not be determined either over the form identifer or the element 371 //we now skip this phase and just add everything we need for the fixup code 372 373 if (!sourceForm) { 374 //no source form found is not an error because 375 //we might be able to recover one way or the other 376 return true; 377 } 378 379 mfInternal._updateForms.push(sourceForm.id); 380 //this._setVSTForm(sourceForm); 381 } 382 else { 383 // response may contain several blocks 384 var cDataBlock = this._Dom.concatCDATABlocks(node), 385 resultNode = null, 386 pushOpRes = this._Lang.hitch(this, this._pushOperationResult); 387 388 switch (node.getAttribute('id')) { 389 case this.P_VIEWROOT: 390 391 cDataBlock = cDataBlock.substring(cDataBlock.indexOf("<html")); 392 393 var parsedData = this._replaceHead(request, context, cDataBlock); 394 395 resultNode = ('undefined' != typeof parsedData && null != parsedData) ? this._replaceBody(request, context, cDataBlock, parsedData) : this._replaceBody(request, context, cDataBlock); 396 if (resultNode) { 397 pushOpRes(context, resultNode); 398 } 399 break; 400 case this.P_VIEWHEAD: 401 //we cannot replace the head, almost no browser allows this, some of them throw errors 402 //others simply ignore it or replace it and destroy the dom that way! 403 this._replaceHead(request, context, cDataBlock); 404 405 break; 406 case this.P_VIEWBODY: 407 //we assume the cdata block is our body including the tag 408 resultNode = this._replaceBody(request, context, cDataBlock); 409 if (resultNode) { 410 pushOpRes(context, resultNode); 411 } 412 break; 413 414 default: 415 resultNode = this.replaceHtmlItem(request, context, node.getAttribute('id'), cDataBlock); 416 if (resultNode) { 417 pushOpRes(context, resultNode); 418 } 419 break; 420 } 421 } 422 423 return true; 424 } 425 , 426 427 _pushOperationResult: function(context, resultNode) { 428 var mfInternal = context._mfInternal; 429 var pushSubnode = this._Lang.hitch(this, function(currNode) { 430 var parentForm = this._Dom.getParent(currNode, "form"); 431 //if possible we work over the ids 432 //so that elements later replaced are referenced 433 //at the latest possibility 434 if (null != parentForm) { 435 mfInternal._updateForms.push(parentForm.id || parentForm); 436 } 437 else { 438 mfInternal._updateElems.push(currNode.id || currNode); 439 } 440 }); 441 var isArr = 'undefined' != typeof resultNode.length && 'undefined' == typeof resultNode.nodeType; 442 if (isArr && resultNode.length) { 443 for (var cnt = 0; cnt < resultNode.length; cnt++) { 444 pushSubnode(resultNode[cnt]); 445 } 446 } else if (!isArr) { 447 pushSubnode(resultNode); 448 } 449 450 } 451 , 452 453 /** 454 * replaces a current head theoretically, 455 * pratically only the scripts are evaled anew since nothing else 456 * can be changed. 457 * 458 * @param request the current request 459 * @param context the ajax context 460 * @param newData the data to be processed 461 * 462 * @return an xml representation of the page for further processing if possible 463 */ 464 _replaceHead: function(request, context, newData) { 465 466 var _Lang = this._Lang, 467 _Dom = this._Dom, 468 isWebkit = this._RT.browser.isWebKit, 469 //we have to work around an xml parsing bug in Webkit 470 //see https://issues.apache.org/jira/browse/MYFACES-3061 471 doc = (!isWebkit) ? _Lang.parseXML(newData) : null, 472 newHead = null; 473 474 if (!isWebkit && _Lang.isXMLParseError(doc)) { 475 doc = _Lang.parseXML(newData.replace(/<!\-\-[\s\n]*<!\-\-/g, "<!--").replace(/\/\/-->[\s\n]*\/\/-->/g, "//-->")); 476 } 477 478 if (isWebkit || _Lang.isXMLParseError(doc)) { 479 //the standard xml parser failed we retry with the stripper 480 var parser = new (this._RT.getGlobalConfig("updateParser", myfaces._impl._util._HtmlStripper))(); 481 var headData = parser.parse(newData, "head"); 482 //We cannot avoid it here, but we have reduced the parsing now down to the bare minimum 483 //for further processing 484 newHead = _Lang.parseXML("<head>" + headData + "</head>"); 485 //last and slowest option create a new head element and let the browser 486 //do its slow job 487 if (_Lang.isXMLParseError(newHead)) { 488 try { 489 newHead = _Dom.createElement("head"); 490 newHead.innerHTML = headData; 491 } catch (e) { 492 //we give up no further fallbacks 493 throw this._raiseError(new Error(),"Error head replacement failed reason:" + e.toString(),"_replaceHead"); 494 } 495 } 496 } else { 497 //parser worked we go on 498 newHead = doc.getElementsByTagName("head")[0]; 499 } 500 501 var oldTags = _Dom.findByTagNames(document.getElementsByTagName("head")[0], {"link": true, "style":true}); 502 _Dom.runCss(newHead, true); 503 _Dom.deleteItems(oldTags); 504 505 //var oldTags = _Dom.findByTagNames(document.getElementsByTagName("head")[0], {"script": true}); 506 //_Dom.deleteScripts(oldTags); 507 _Dom.runScripts(newHead, true); 508 509 return doc; 510 }, 511 512 513 /** 514 * special method to handle the body dom manipulation, 515 * replacing the entire body does not work fully by simply adding a second body 516 * and by creating a range instead we have to work around that by dom creating a second 517 * body and then filling it properly! 518 * 519 * @param {Object} request our request object 520 * @param {Object} context (Map) the response context 521 * @param {String} newData the markup which replaces the old dom node! 522 * @param {Node} parsedData (optional) preparsed XML representation data of the current document 523 */ 524 _replaceBody : function(request, context, newData /*varargs*/) { 525 var _RT = this._RT, 526 _Dom = this._Dom, 527 _Lang = this._Lang, 528 529 oldBody = document.getElementsByTagName("body")[0], 530 placeHolder = document.createElement("div"), 531 isWebkit = _RT.browser.isWebKit; 532 533 placeHolder.id = "myfaces_bodyplaceholder"; 534 535 _Dom._removeChildNodes(oldBody); 536 oldBody.innerHTML = ""; 537 oldBody.appendChild(placeHolder); 538 539 var bodyData, doc = null, parser; 540 541 //we have to work around an xml parsing bug in Webkit 542 //see https://issues.apache.org/jira/browse/MYFACES-3061 543 if (!isWebkit) { 544 doc = (arguments.length > 3) ? arguments[3] : _Lang.parseXML(newData); 545 } 546 547 if (!isWebkit && _Lang.isXMLParseError(doc)) { 548 doc = _Lang.parseXML(newData.replace(/<!\-\-[\s\n]*<!\-\-/g, "<!--").replace(/\/\/-->[\s\n]*\/\/-->/g, "//-->")); 549 } 550 551 if (isWebkit || _Lang.isXMLParseError(doc)) { 552 //the standard xml parser failed we retry with the stripper 553 554 parser = new (_RT.getGlobalConfig("updateParser", myfaces._impl._util._HtmlStripper))(); 555 556 bodyData = parser.parse(newData, "body"); 557 } else { 558 //parser worked we go on 559 var newBodyData = doc.getElementsByTagName("body")[0]; 560 561 //speedwise we serialize back into the code 562 //for code reduction, speedwise we will take a small hit 563 //there which we will clean up in the future, but for now 564 //this is ok, I guess, since replace body only is a small subcase 565 //bodyData = _Lang.serializeChilds(newBodyData); 566 var browser = _RT.browser; 567 if (!browser.isIEMobile || browser.isIEMobile >= 7) { 568 //TODO check what is failing there 569 for (var cnt = 0; cnt < newBodyData.attributes.length; cnt++) { 570 var value = newBodyData.attributes[cnt].value; 571 if (value) 572 _Dom.setAttribute(oldBody, newBodyData.attributes[cnt].name, value); 573 } 574 } 575 } 576 //we cannot serialize here, due to escape problems 577 //we must parse, this is somewhat unsafe but should be safe enough 578 parser = new (_RT.getGlobalConfig("updateParser", myfaces._impl._util._HtmlStripper))(); 579 bodyData = parser.parse(newData, "body"); 580 581 var returnedElement = this.replaceHtmlItem(request, context, placeHolder, bodyData); 582 583 if (returnedElement) { 584 this._pushOperationResult(context, returnedElement); 585 } 586 return returnedElement; 587 } 588 , 589 590 /** 591 * Replaces HTML elements through others and handle errors if the occur in the replacement part 592 * 593 * @param {Object} request (xhrRequest) 594 * @param {Object} context (Map) 595 * @param {Object} itemIdToReplace (String|Node) - ID of the element to replace 596 * @param {String} markup - the new tag 597 */ 598 replaceHtmlItem : function(request, context, itemIdToReplace, markup) { 599 var _Lang = this._Lang, _Dom = this._Dom; 600 601 var item = (!_Lang.isString(itemIdToReplace)) ? itemIdToReplace : 602 _Dom.byIdOrName(itemIdToReplace); 603 604 if (!item) { 605 throw this._raiseError(_Lang.getMessage("ERR_ITEM_ID_NOTFOUND", null, "_AjaxResponse.replaceHtmlItem", (itemIdToReplace) ? itemIdToReplace.toString() : "undefined")); 606 } 607 return _Dom.outerHTML(item, markup); 608 }, 609 610 /** 611 * xml insert command handler 612 * 613 * @param request the ajax request element 614 * @param context the context element holding the data 615 * @param node the xml node holding the insert data 616 * @return true upon successful completion, false otherwise 617 * 618 **/ 619 processInsert: function(request, context, node) { 620 /*remapping global namespaces for speed and readability reasons*/ 621 var _Dom = this._Dom, 622 _Lang = this._Lang, 623 //determine which path to go: 624 insertData = this._parseInsertData(request, context, node); 625 626 if (!insertData) return false; 627 628 var opNode = _Dom.byIdOrName(insertData.opId); 629 if (!opNode) { 630 throw this._raiseError(new Error(),_Lang.getMessage("ERR_PPR_INSERTBEFID_1", null, "_AjaxResponse.processInsert", insertData.opId),"processInsert"); 631 } 632 633 //call insertBefore or insertAfter in our dom routines 634 var replacementFragment = _Dom[insertData.insertType](opNode, insertData.cDataBlock); 635 if (replacementFragment) { 636 this._pushOperationResult(context, replacementFragment); 637 } 638 return true; 639 }, 640 641 /** 642 * determines the corner data from the insert tag parsing process 643 * 644 * 645 * @param request request 646 * @param context context 647 * @param node the current node pointing to the insert tag 648 * @return false if the parsing failed, otherwise a map with follwing attributes 649 * <ul> 650 * <li>inserType - a ponter to a constant which maps the direct function name for the insert operation </li> 651 * <li>opId - the before or after id </li> 652 * <li>cDataBlock - the html cdata block which needs replacement </li> 653 * </ul> 654 * 655 * TODO we have to find a mechanism to replace the direct sendError calls with a javascript exception 656 * which we then can use for cleaner error code handling 657 */ 658 _parseInsertData: function(request, context, node) { 659 var _Lang = this._Lang, 660 _Dom = this._Dom, 661 concatCDATA = _Dom.concatCDATABlocks, 662 663 INSERT_TYPE_BEFORE = "insertBefore", 664 INSERT_TYPE_AFTER = "insertAfter", 665 666 id = node.getAttribute("id"), 667 beforeId = node.getAttribute("before"), 668 afterId = node.getAttribute("after"), 669 ret = {}; 670 671 //now we have to make a distinction between two different parsing paths 672 //due to a spec malalignment 673 //a <insert id="... beforeId|AfterId ="... 674 //b <insert><before id="..., <insert> <after id=".... 675 //see https://issues.apache.org/jira/browse/MYFACES-3318 676 //simple id, case1 677 if (id && beforeId && !afterId) { 678 ret.insertType = INSERT_TYPE_BEFORE; 679 ret.opId = beforeId; 680 ret.cDataBlock = concatCDATA(node); 681 682 //<insert id=".. afterId=".. 683 } else if (id && !beforeId && afterId) { 684 ret.insertType = INSERT_TYPE_AFTER; 685 ret.opId = afterId; 686 ret.cDataBlock = concatCDATA(node); 687 688 //<insert><before id="... <insert><after id="... 689 } else if (!id) { 690 var opType = node.childNodes[0].tagName; 691 692 if (opType != "before" && opType != "after") { 693 throw this._raiseError(new Error(),_Lang.getMessage("ERR_PPR_INSERTBEFID"),"_parseInsertData"); 694 } 695 opType = opType.toLowerCase(); 696 var beforeAfterId = node.childNodes[0].getAttribute("id"); 697 ret.insertType = (opType == "before") ? INSERT_TYPE_BEFORE : INSERT_TYPE_AFTER; 698 ret.opId = beforeAfterId; 699 ret.cDataBlock = concatCDATA(node.childNodes[0]); 700 } else { 701 throw this._raiseError(new Error(),[_Lang.getMessage("ERR_PPR_IDREQ"), 702 "\n ", 703 _Lang.getMessage("ERR_PPR_INSERTBEFID")].join(""),"_parseInsertData"); 704 } 705 ret.opId = _Lang.trim(ret.opId); 706 return ret; 707 }, 708 709 processDelete : function(request, context, node) { 710 711 var _Lang = this._Lang, 712 _Dom = this._Dom, 713 deleteId = node.getAttribute('id'); 714 715 if (!deleteId) { 716 throw this._raiseError(new Error(),_Lang.getMessage("ERR_PPR_UNKNOWNCID", null, "_AjaxResponse.processDelete", ""),"processDelete"); 717 } 718 719 var item = _Dom.byIdOrName(deleteId); 720 if (!item) { 721 throw this._raiseError(new Error(),_Lang.getMessage("ERR_PPR_UNKNOWNCID", null, "_AjaxResponse.processDelete", deleteId),"processDelete"); 722 } 723 724 var parentForm = this._Dom.getParent(item, "form"); 725 if (null != parentForm) { 726 context._mfInternal._updateForms.push(parentForm); 727 } 728 _Dom.deleteItem(item); 729 730 return true; 731 } 732 , 733 734 processAttributes : function(request, context, node) { 735 //we now route into our attributes function to bypass 736 //IE quirks mode incompatibilities to the biggest possible extent 737 //most browsers just have to do a setAttributes but IE 738 //behaves as usual not like the official standard 739 //myfaces._impl._util.this._Dom.setAttribute(domNode, attribute, value; 740 741 var _Lang = this._Lang, 742 //<attributes id="id of element"> <attribute name="attribute name" value="attribute value" />* </attributes> 743 elemId = node.getAttribute('id'); 744 745 if (!elemId) { 746 throw this._raiseError(new Error(),"Error in attributes, id not in xml markup","processAttributes"); 747 } 748 var childNodes = node.childNodes; 749 750 if (!childNodes) { 751 return false; 752 } 753 for (var loop2 = 0; loop2 < childNodes.length; loop2++) { 754 var attributesNode = childNodes[loop2], 755 attrName = attributesNode.getAttribute("name"), 756 attrValue = attributesNode.getAttribute("value"); 757 758 if (!attrName) { 759 continue; 760 } 761 762 attrName = _Lang.trim(attrName); 763 /*no value means reset*/ 764 //value can be of boolean value hence full check 765 if ('undefined' == typeof attrValue || null == attrValue) { 766 attrValue = ""; 767 } 768 769 switch (elemId) { 770 case this.P_VIEWROOT: 771 throw this._raiseError(new Error(),_Lang.getMessage("ERR_NO_VIEWROOTATTR", null, "_AjaxResponse.processAttributes"),"processAttributes"); 772 773 case this.P_VIEWHEAD: 774 throw this._raiseError(new Error(),_Lang.getMessage("ERR_NO_HEADATTR", null, "_AjaxResponse.processAttributes"),"processAttributes"); 775 776 case this.P_VIEWBODY: 777 var element = document.getElementsByTagName("body")[0]; 778 this._Dom.setAttribute(element, attrName, attrValue); 779 break; 780 781 default: 782 this._Dom.setAttribute(document.getElementById(elemId), attrName, attrValue); 783 break; 784 } 785 } 786 return true; 787 }, 788 789 /** 790 * internal helper which raises an error in the 791 * format we need for further processing 792 * 793 * @param message the message 794 * @param title the title of the error (optional) 795 * @param name the name of the error (optional) 796 */ 797 _raiseError: function(error, message, caller, title, name) { 798 var _Impl = this.attr("impl"); 799 var finalTitle = title || _Impl.MALFORMEDXML; 800 var finalName = name || _Impl.MALFORMEDXML; 801 var finalMessage = message || ""; 802 803 return this._Lang.makeException(error, finalTitle, finalName, this._nameSpace, caller || ( (arguments.caller) ? arguments.caller.toString() : "_raiseError"), finalMessage); 804 } 805 }); 806