Back to index

lightning-sunbird  0.9+nobinonly
nsXFormsInputElement.cpp
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 <allan@beaufour.dk>
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 #include "nsXFormsDelegateStub.h"
00041 #include "nsISchema.h"
00042 #include "nsIStringBundle.h"
00043 #include "nsServiceManagerUtils.h"
00044 
00045 class nsXFormsInputElement : public nsXFormsDelegateStub
00046 {
00047 public:
00048 
00049   NS_IMETHOD IsTypeAllowed(PRUint16 aType, PRBool *aIsAllowed,
00050                            nsRestrictionFlag *aRestriction,
00051                            nsAString &aUnallowedTypes);
00052 
00053   virtual PRBool IsContentAllowed();
00054 
00055   nsXFormsInputElement(const nsAString& aType)
00056     : nsXFormsDelegateStub(aType)
00057     {}
00058 };
00059 
00060 NS_IMETHODIMP
00061 nsXFormsInputElement::IsTypeAllowed(PRUint16 aType, PRBool *aIsAllowed,
00062                                     nsRestrictionFlag *aRestriction,
00063                                     nsAString &aUnallowedTypes)
00064 {
00065   NS_ENSURE_ARG_POINTER(aRestriction);
00066   NS_ENSURE_ARG_POINTER(aIsAllowed);
00067   *aRestriction = eTypes_Exclusive;
00068   *aIsAllowed = PR_FALSE;
00069 
00070   // For input and secret elements, the bound type can't be xsd:hexBinary
00071   // or xsd:base64Binary
00072 
00073   if (aType != nsISchemaBuiltinType::BUILTIN_TYPE_HEXBINARY &&
00074       aType != nsISchemaBuiltinType::BUILTIN_TYPE_BASE64BINARY) {
00075     *aIsAllowed = PR_TRUE;
00076     return NS_OK;
00077   }
00078 
00079   // build the string of types that inputs can't bind to
00080   aUnallowedTypes.AssignLiteral("xsd:base64Binary xsd:hexBinary");
00081   return NS_OK;
00082 }
00083 
00084 PRBool
00085 nsXFormsInputElement::IsContentAllowed()
00086 {
00087   PRBool isAllowed = PR_TRUE;
00088 
00089   // For input and secret elements, complexContent is not allowed.
00090   PRBool isComplex = IsContentComplex();
00091   if (isComplex) {
00092     isAllowed = PR_FALSE;
00093   }
00094   return isAllowed;
00095 }
00096 
00097 class nsXFormsTextareaElement : public nsXFormsDelegateStub
00098 {
00099 public:
00100 
00101   NS_IMETHOD IsTypeAllowed(PRUint16 aType, PRBool *aIsAllowed,
00102                            nsRestrictionFlag *aRestriction,
00103                            nsAString &aAllowedTypes);
00104 
00105   virtual PRBool IsContentAllowed();
00106 
00107   nsXFormsTextareaElement()
00108     : nsXFormsDelegateStub(NS_LITERAL_STRING("textarea"))
00109     {}
00110 };
00111 
00112 NS_IMETHODIMP
00113 nsXFormsTextareaElement::IsTypeAllowed(PRUint16 aType, PRBool *aIsAllowed,
00114                                        nsRestrictionFlag *aRestriction,
00115                                        nsAString &aAllowedTypes)
00116 {
00117   NS_ENSURE_ARG_POINTER(aRestriction);
00118   NS_ENSURE_ARG_POINTER(aIsAllowed);
00119   *aRestriction = eTypes_Inclusive;
00120   *aIsAllowed = PR_FALSE;
00121 
00122   // Textareas can only be bound to types of xsd:string (or a type derived from
00123   // xsd:string).
00124 
00125   if (aType == nsISchemaBuiltinType::BUILTIN_TYPE_STRING) {
00126     *aIsAllowed = PR_TRUE;
00127     return NS_OK;
00128   }
00129 
00130   // build the string of types that textareas can bind to
00131   aAllowedTypes.AssignLiteral("xsd:string");
00132   return NS_OK;
00133 }
00134 
00135 PRBool
00136 nsXFormsTextareaElement::IsContentAllowed()
00137 {
00138   PRBool isAllowed = PR_TRUE;
00139 
00140   // Textarea elements may not be bound to complexContent.
00141   PRBool isComplex = IsContentComplex();
00142   if (isComplex) {
00143     isAllowed = PR_FALSE;
00144   }
00145   return isAllowed;
00146 }
00147 
00148 // Creators
00149 
00150 NS_HIDDEN_(nsresult)
00151 NS_NewXFormsInputElement(nsIXTFElement **aResult)
00152 {
00153   *aResult = new nsXFormsInputElement(NS_LITERAL_STRING("input"));
00154   if (!*aResult)
00155     return NS_ERROR_OUT_OF_MEMORY;
00156 
00157   NS_ADDREF(*aResult);
00158   return NS_OK;
00159 }
00160 
00161 NS_HIDDEN_(nsresult)
00162 NS_NewXFormsSecretElement(nsIXTFElement **aResult)
00163 {
00164   *aResult = new nsXFormsInputElement(NS_LITERAL_STRING("secret"));
00165   if (!*aResult)
00166     return NS_ERROR_OUT_OF_MEMORY;
00167 
00168   NS_ADDREF(*aResult);
00169   return NS_OK;
00170 }
00171 
00172 NS_HIDDEN_(nsresult)
00173 NS_NewXFormsTextAreaElement(nsIXTFElement **aResult)
00174 {
00175   *aResult = new nsXFormsTextareaElement();
00176   if (!*aResult)
00177     return NS_ERROR_OUT_OF_MEMORY;
00178 
00179   NS_ADDREF(*aResult);
00180   return NS_OK;
00181 }