Import 3.0 beta 3 tarball
[mozilla-extra.git] / extensions / xforms / nsXFormsSetValueElement.cpp
blobb0c44951e5c5c8dc23b10f362669a50ca4717882
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Mozilla XForms support.
17 * The Initial Developer of the Original Code is
18 * Olli Pettay.
19 * Portions created by the Initial Developer are Copyright (C) 2004
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Olli Pettay <Olli.Pettay@helsinki.fi> (original author)
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsXFormsActionModuleBase.h"
40 #include "nsXFormsActionElement.h"
41 #include "nsIDOM3Node.h"
42 #include "nsIDOMXPathResult.h"
43 #include "nsIDOMDocument.h"
44 #include "nsIDOMEvent.h"
45 #include "nsIDOMElement.h"
46 #include "nsIXFormsModelElement.h"
47 #include "nsXFormsModelElement.h"
48 #include "nsXFormsUtils.h"
50 class nsXFormsSetValueElement : public nsXFormsActionModuleBase
52 public:
53 nsXFormsSetValueElement();
54 virtual nsresult HandleSingleAction(nsIDOMEvent* aEvent,
55 nsIXFormsActionElement *aParentAction);
58 nsXFormsSetValueElement::nsXFormsSetValueElement()
62 nsresult
63 nsXFormsSetValueElement::HandleSingleAction(nsIDOMEvent *aEvent,
64 nsIXFormsActionElement *aParentAction)
66 nsCOMPtr<nsIModelElementPrivate> modelPriv;
67 nsCOMPtr<nsIDOMNode> singleNode;
68 PRBool succeeded =
69 nsXFormsUtils::GetSingleNodeBinding(mElement,
70 getter_AddRefs(singleNode),
71 getter_AddRefs(modelPriv));
73 if (!succeeded | !modelPriv | !singleNode)
74 return NS_OK;
76 nsAutoString value;
77 nsAutoString valueAttr;
78 nsresult rv;
79 mElement->GetAttribute(NS_LITERAL_STRING("value"), valueAttr);
81 if (!valueAttr.IsEmpty()) {
82 // According to the XForms Errata, the context node for the XPath expression
83 // stored in @value should be the node that setvalue is bound to.
84 nsCOMPtr<nsIDOMXPathResult> xpResult;
85 rv = nsXFormsUtils::EvaluateXPath(valueAttr, singleNode, mElement,
86 nsIDOMXPathResult::STRING_TYPE,
87 getter_AddRefs(xpResult));
88 NS_ENSURE_SUCCESS(rv, rv);
89 if (!xpResult)
90 return NS_OK;
91 xpResult->GetStringValue(value);
92 } else {
93 nsCOMPtr<nsIDOM3Node> n3(do_QueryInterface(mElement));
94 n3->GetTextContent(value);
97 PRBool changed;
98 rv = modelPriv->SetNodeValue(singleNode, value, !aParentAction, &changed);
99 NS_ENSURE_SUCCESS(rv, rv);
101 if (changed && aParentAction) {
102 aParentAction->SetRecalculate(modelPriv, PR_TRUE);
103 aParentAction->SetRevalidate(modelPriv, PR_TRUE);
104 aParentAction->SetRefresh(modelPriv, PR_TRUE);
107 return NS_OK;
110 NS_HIDDEN_(nsresult)
111 NS_NewXFormsSetValueElement(nsIXTFElement **aResult)
113 *aResult = new nsXFormsSetValueElement();
114 if (!*aResult)
115 return NS_ERROR_OUT_OF_MEMORY;
117 NS_ADDREF(*aResult);
118 return NS_OK;