Back to index

lightning-sunbird  0.9+nobinonly
nsXFormsUtils.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla XForms support.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * IBM Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *  Brian Ryner <bryner@brianryner.com>
00024  *  Allan Beaufour <abeaufour@novell.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifndef nsXFormsUtils_h_
00041 #define nsXFormsUtils_h_
00042 
00043 #include "prtypes.h"
00044 #include "nsCOMPtr.h"
00045 #include "nsCOMArray.h"
00046 #include "nsIDOMNode.h"
00047 #include "nsIDocument.h"
00048 #include "nsIDOMXPathResult.h"
00049 #include "nsIModelElementPrivate.h"
00050 #include "nsIScriptError.h"
00051 #include "nsVoidArray.h"
00052 #include "nsIDOMWindowInternal.h"
00053 #include "nsXFormsDOMEvent.h"
00054 
00055 class nsIDOMElement;
00056 class nsIXFormsModelElement;
00057 class nsIURI;
00058 class nsString;
00059 class nsIMutableArray;
00060 class nsIDOMEvent;
00061 
00062 #define NS_NAMESPACE_XFORMS              "http://www.w3.org/2002/xforms"
00063 #define NS_NAMESPACE_XHTML               "http://www.w3.org/1999/xhtml"
00064 #define NS_NAMESPACE_XML_SCHEMA          "http://www.w3.org/2001/XMLSchema"
00065 #define NS_NAMESPACE_XML_SCHEMA_INSTANCE "http://www.w3.org/2001/XMLSchema-instance"
00066 #define NS_NAMESPACE_MOZ_XFORMS_TYPE     "http://www.mozilla.org/projects/xforms/2005/type"
00067 #define NS_NAMESPACE_SOAP_ENVELOPE       "http://schemas.xmlsoap.org/soap/envelope/"
00068 #define NS_NAMESPACE_MOZ_XFORMS_LAZY     "http://www.mozilla.org/projects/xforms/2005/lazy"
00069 
00070 #define PREF_WAIT_LIMIT                  "dom.max_script_run_time"
00071 
00076 #define NS_OK_XFORMS_NOREFRESH \
00077 NS_ERROR_GENERATE_SUCCESS(NS_ERROR_MODULE_GENERAL, 1)
00078 #define NS_OK_XFORMS_DEFERRED \
00079 NS_ERROR_GENERATE_SUCCESS(NS_ERROR_MODULE_GENERAL, 2)
00080 #define NS_OK_XFORMS_NOTREADY \
00081 NS_ERROR_GENERATE_SUCCESS(NS_ERROR_MODULE_GENERAL, 3)
00082 
00083 #define NS_ERROR_XFORMS_UNION_TYPE \
00084 NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_GENERAL, 3002)
00085 
00095 enum nsXFormsEvent {
00096   eEvent_ModelConstruct,
00097   eEvent_ModelConstructDone,
00098   eEvent_Ready,
00099   eEvent_ModelDestruct,
00100   eEvent_Previous,
00101   eEvent_Next,
00102   eEvent_Focus,
00103   eEvent_Help,
00104   eEvent_Hint,
00105   eEvent_Rebuild,
00106   eEvent_Refresh,
00107   eEvent_Revalidate,
00108   eEvent_Recalculate,
00109   eEvent_Reset,
00110   eEvent_Submit,
00111   eEvent_DOMActivate,
00112   eEvent_ValueChanged,
00113   eEvent_Select,
00114   eEvent_Deselect,
00115   eEvent_ScrollFirst,
00116   eEvent_ScrollLast,
00117   eEvent_Insert,
00118   eEvent_Delete,
00119   eEvent_Valid,
00120   eEvent_Invalid,
00121   eEvent_DOMFocusIn,
00122   eEvent_DOMFocusOut,
00123   eEvent_Readonly,
00124   eEvent_Readwrite,
00125   eEvent_Required,
00126   eEvent_Optional,
00127   eEvent_Enabled,
00128   eEvent_Disabled,
00129   eEvent_InRange,
00130   eEvent_OutOfRange,
00131   eEvent_SubmitDone,
00132   eEvent_SubmitError,
00133   eEvent_BindingException,
00134   eEvent_LinkException,
00135   eEvent_LinkError,
00136   eEvent_ComputeException,
00137   eEvent_MozHintOff,
00138   eEvent_SubmitSerialize
00139 };
00140 
00141 struct EventData
00142 {
00143   const char *name;
00144   PRBool      canCancel;
00145   PRBool      canBubble;
00146 };
00147 
00148 extern const EventData sXFormsEventsEntries[43];
00149 
00150 // Default intrinsic state for XForms Controls
00151 extern const PRInt32 kDefaultIntrinsicState;
00152 
00153 // Disabled intrinsic state for XForms Controls
00154 extern const PRInt32 kDisabledIntrinsicState;
00155 
00160 class nsXFormsUtils
00161 {
00162 public:
00163 
00168   enum {
00172     ELEMENT_WITH_MODEL_ATTR = 1 << 0
00173   };
00174 
00178   static NS_HIDDEN_(nsresult)
00179     Init();
00180 
00184   static NS_HIDDEN_(nsresult)
00185     Shutdown();
00186 
00193   static NS_HIDDEN_(PRBool)
00194     GetParentModel(nsIDOMElement           *aBindElement,
00195                    nsIModelElementPrivate **aModel);
00196 
00215   static NS_HIDDEN_(nsresult)
00216     GetNodeContext(nsIDOMElement           *aElement,
00217                    PRUint32                 aElementFlags,
00218                    nsIModelElementPrivate **aModel,
00219                    nsIDOMElement          **aBindElement,
00220                    PRBool                  *aOuterBind,
00221                    nsIXFormsControl       **aParentControl,
00222                    nsIDOMNode             **aContextNode,
00223                    PRInt32                 *aContextPosition = nsnull,
00224                    PRInt32                 *aContextSize = nsnull,
00225                    PRBool                   aUseBindAttr = PR_TRUE);
00226 
00238   static NS_HIDDEN_(already_AddRefed<nsIModelElementPrivate>)
00239     GetModel(nsIDOMElement     *aElement,
00240              nsIXFormsControl **aParentControl = nsnull,
00241              PRUint32           aElementFlags = ELEMENT_WITH_MODEL_ATTR,
00242              nsIDOMNode       **aContextNode = nsnull);
00243 
00244 
00255   static NS_HIDDEN_(nsresult)
00256     EvaluateNodeBinding(nsIDOMElement           *aElement,
00257                         PRUint32                 aElementFlags,
00258                         const nsString          &aBindingAttr,
00259                         const nsString          &aDefaultRef,
00260                         PRUint16                 aResultType,
00261                         nsIModelElementPrivate **aModel,
00262                         nsIDOMXPathResult      **aResult,
00263                         PRBool                  *aUsesModelBind,
00264                         nsIXFormsControl       **aParentControl = nsnull,
00265                         nsCOMArray<nsIDOMNode>  *aDeps = nsnull,
00266                         nsStringArray           *aIndexesUsed = nsnull);
00267 
00274   static NS_HIDDEN_(nsresult)
00275     EvaluateXPath(const nsAString        &aExpression,
00276                   nsIDOMNode             *aContextNode,
00277                   nsIDOMNode             *aResolverNode,
00278                   PRUint16                aResultType,
00279                   nsIDOMXPathResult     **aResult,
00280                   PRInt32                 aContextPosition = 1,
00281                   PRInt32                 aContextSize = 1,
00282                   nsCOMArray<nsIDOMNode> *aSet = nsnull,
00283                   nsStringArray          *aIndexesUsed = nsnull);
00284 
00289   static NS_HIDDEN_(void) GetNodeValue(nsIDOMNode *aDataNode,
00290                                        nsAString  &aNodeValue);
00291 
00297   static NS_HIDDEN_(PRBool)
00298     GetSingleNodeBinding(nsIDOMElement* aElement, nsIDOMNode** aNode,
00299                          nsIModelElementPrivate** aModel);
00300 
00306   static NS_HIDDEN_(PRBool)
00307     GetSingleNodeBindingValue(nsIDOMElement* aElement, nsString& aValue);
00308 
00317   static NS_HIDDEN_(PRBool)
00318     SetSingleNodeBindingValue(nsIDOMElement *aElement, const nsAString &aValue,
00319                               PRBool *aChanged);
00328   static NS_HIDDEN_(nsresult)
00329     DispatchEvent(nsIDOMNode* aTarget, nsXFormsEvent aEvent,
00330                   PRBool *aDefaultActionEnabled = nsnull,
00331                   nsIDOMElement *aSrcElement = nsnull,
00332                   nsCOMArray<nsIXFormsContextInfo> *aContextInfo = nsnull);
00333 
00334   static NS_HIDDEN_(nsresult)
00335     DispatchDeferredEvents(nsIDOMDocument* aDocument);
00336 
00344   static NS_HIDDEN_(nsresult)
00345     SetEventTrusted(nsIDOMEvent* aEvent, nsIDOMNode* aRelatedNode);
00346 
00354   static NS_HIDDEN_(PRBool)
00355     EventHandlingAllowed(nsIDOMEvent* aEvent, nsIDOMNode* aTarget);
00356 
00361   static NS_HIDDEN_(PRBool)
00362     IsXFormsEvent(const nsAString& aEvent,
00363                   PRBool& aCancelable,
00364                   PRBool& aBubbles);
00365 
00370   static NS_HIDDEN_(void)
00371     GetEventDefaults(const nsAString& aEvent,
00372                      PRBool& aCancelable,
00373                      PRBool& aBubbles);
00374 
00379   static NS_HIDDEN_(nsresult) CloneScriptingInterfaces(const nsIID *aIIDList,
00380                                                        unsigned int aIIDCount,
00381                                                        PRUint32 *aOutCount,
00382                                                        nsIID ***aOutArray);
00383 
00400   static NS_HIDDEN_(nsresult) FindParentContext(nsIDOMElement           *aElement,
00401                                                 nsIModelElementPrivate **aModel,
00402                                                 nsIXFormsControl       **aParentControl,
00403                                                 nsIDOMNode             **aContextNode,
00404                                                 PRInt32                 *aContextPosition,
00405                                                 PRInt32                 *aContextSize);
00406 
00408   enum ConnectionType {
00410     kXFormsActionSend = 1,
00411 
00413     kXFormsActionLoad = 2,
00414 
00416     kXFormsActionLoadSend = 3
00417   };
00418 
00427   static NS_HIDDEN_(PRBool) CheckConnectionAllowed(nsIDOMElement *aElement,
00428                                                    nsIURI        *aTestURI,
00429                                                    ConnectionType aType = kXFormsActionLoad);
00430 
00435   static NS_HIDDEN_(PRBool) IsXFormsElement(nsIDOMNode* aNode, 
00436                                             const nsAString& aName);
00440   static NS_HIDDEN_(PRBool) IsLabelElement(nsIDOMNode *aElement);
00441   
00445   static NS_HIDDEN_(PRBool) FocusControl(nsIDOMElement *aElement);
00446 
00450   static NS_HIDDEN_(void) MakeUniqueAndSort(nsCOMArray<nsIDOMNode> *aArray);
00451 
00455   static NS_HIDDEN_(nsresult) GetInstanceNodeForData(nsIDOMNode *aInstanceDataNode,
00456                                                      nsIDOMNode  **aInstanceNode);
00457 
00466   static NS_HIDDEN_(nsresult) ParseTypeFromNode(nsIDOMNode *aInstanceData,
00467                                                 nsAString  &aType,
00468                                                 nsAString  &aNSUri);
00469 
00488   static NS_HIDDEN_(void) ReportError(const nsAString  &aMessageName,
00489                                       const PRUnichar **aParams,
00490                                       PRUint32          aParamLength,
00491                                       nsIDOMNode       *aElement,
00492                                       nsIDOMNode       *aContext,
00493                                       PRUint32          aErrorFlag = nsIScriptError::errorFlag,
00494                                       PRBool            aLiteralMessage = PR_FALSE);
00495 
00505   static NS_HIDDEN_(void) ReportError(const nsAString &aMessageName,
00506                                       nsIDOMNode      *aElement = nsnull,
00507                                       PRUint32         aErrorFlag = nsIScriptError::errorFlag)
00508     {
00509       nsXFormsUtils::ReportError(aMessageName, nsnull, 0, aElement, aElement, aErrorFlag, PR_FALSE);
00510     }
00511 
00524   static NS_HIDDEN_(void) ReportErrorMessage(const nsAString &aMessage,
00525                                              nsIDOMNode      *aElement = nsnull,
00526                                              PRUint32         aErrorFlag = nsIScriptError::errorFlag)
00527     {
00528       nsXFormsUtils::ReportError(aMessage, nsnull, 0, aElement, aElement, aErrorFlag, PR_TRUE);
00529     }
00530 
00537   static NS_HIDDEN_(PRBool) IsDocumentReadyForBind(nsIDOMElement *aElement);
00538 
00561   static NS_HIDDEN_(nsresult) GetElementById(const nsAString  &aId,
00562                                              const PRBool      aOnlyXForms,
00563                                              nsIDOMElement    *aCaller,
00564                                              nsIDOMElement   **aElement);
00565 
00580   static NS_HIDDEN_(nsresult) GetElementByContextId(nsIDOMElement   *aRefNode,
00581                                                     const nsAString &aId,
00582                                                     nsIDOMElement   **aElement);
00583 
00593   static NS_HIDDEN_(PRBool) HandleFatalError(nsIDOMElement   *aElement,
00594                                              const nsAString &aName);
00595 
00600   static NS_HIDDEN_(PRBool) AreEntitiesEqual(nsIDOMNamedNodeMap *aEntities1,
00601                                              nsIDOMNamedNodeMap *aEntities2);
00602 
00607   static NS_HIDDEN_(PRBool) AreNotationsEqual(nsIDOMNamedNodeMap *aNotations1,
00608                                               nsIDOMNamedNodeMap *aNotations2);
00609 
00625   static NS_HIDDEN_(PRBool) AreNodesEqual(nsIDOMNode *aFirstNode,
00626                                           nsIDOMNode *aSecondNode,
00627                                           PRBool      aAlreadyNormalized = PR_FALSE);
00628 
00635   static NS_HIDDEN_(nsresult) GetWindowFromDocument(nsIDOMDocument        *aDoc,
00636                                                     nsIDOMWindowInternal **aWindow);
00637 
00644   static NS_HIDDEN_(PRBool) NodeHasItemset(nsIDOMNode *aNode);
00645 
00653   static NS_HIDDEN_(PRBool) AskStopWaiting(nsIDOMElement *aElement);
00654 
00661   static NS_HIDDEN_(PRInt32) waitLimit;
00662 
00663   static NS_HIDDEN_(void) BinaryToHex(const char *aBuffer, PRUint32 aCount,
00664                                       PRUnichar **aHexString);
00665 
00674    static NS_HIDDEN_(nsresult) GetTimeZone(const nsAString &aTime,
00675                                            nsAString &aResult);
00676 
00677 private:
00690   static NS_HIDDEN_(PRBool) CheckSameOrigin(nsIDocument   *aBaseDocument,
00691                                             nsIURI        *aTestURI,
00692                                             ConnectionType aType = kXFormsActionLoad);
00693 
00704   static NS_HIDDEN_(PRBool) CheckContentPolicy(nsIDOMElement *aElement,
00705                                                nsIDocument   *aDoc,
00706                                                nsIURI        *aURI);
00707 
00708 };
00709 
00710 #endif