XForms output element implementation. Patch by allan@beaufour.dk, r=bryner, bug 267300.

This commit is contained in:
bryner%brianryner.com 2004-11-04 23:53:01 +00:00
Родитель 717e3cab7d
Коммит 84b2a653a2
7 изменённых файлов: 411 добавлений и 6 удалений

Просмотреть файл

@ -77,6 +77,7 @@ CPPSRCS = \
nsXFormsUtils.cpp \
nsXFormsModelElement.cpp \
nsXFormsInputElement.cpp \
nsXFormsOutputElement.cpp \
nsXFormsSubmissionElement.cpp \
nsXFormsStubElement.cpp \
nsXFormsInstanceElement.cpp \

Просмотреть файл

@ -44,6 +44,7 @@
#include "nsString.h"
NS_HIDDEN_(nsresult) NS_NewXFormsInputElement(nsIXTFElement **aElement);
NS_HIDDEN_(nsresult) NS_NewXFormsOutputElement(nsIXTFElement **aElement);
NS_IMPL_ISUPPORTS1(nsXFormsElementFactory, nsIXTFElementFactory)
@ -59,6 +60,8 @@ nsXFormsElementFactory::CreateElement(const nsAString& aTagName,
return NS_NewXFormsStubElement(aElement);
if (aTagName.EqualsLiteral("input"))
return NS_NewXFormsInputElement(aElement);
if (aTagName.EqualsLiteral("output"))
return NS_NewXFormsOutputElement(aElement);
if (aTagName.EqualsLiteral("submission"))
return NS_NewXFormsSubmissionElement(aElement);

Просмотреть файл

@ -352,6 +352,7 @@ nsXFormsInputElement::Blur(nsIDOMEvent *aEvent)
nsCOMPtr<nsIDOMXPathResult> result =
nsXFormsUtils::EvaluateNodeBinding(mElement,
nsXFormsUtils::ELEMENT_WITH_MODEL_ATTR,
NS_LITERAL_STRING("ref"),
EmptyString(),
nsIDOMXPathResult::FIRST_ORDERED_NODE_TYPE,
getter_AddRefs(modelNode),
@ -393,6 +394,7 @@ nsXFormsInputElement::Refresh()
nsCOMPtr<nsIDOMXPathResult> result =
nsXFormsUtils::EvaluateNodeBinding(mElement,
nsXFormsUtils::ELEMENT_WITH_MODEL_ATTR,
NS_LITERAL_STRING("ref"),
EmptyString(),
nsIDOMXPathResult::FIRST_ORDERED_NODE_TYPE,
getter_AddRefs(modelNode),

Просмотреть файл

@ -0,0 +1,394 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla XForms support.
*
* The Initial Developer of the Original Code is
* Novell, Inc.
* Portions created by the Initial Developer are Copyright (C) 2004
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Allan Beaufour <abeaufour@novell.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifdef DEBUG
//#define DEBUG_XF_OUTPUT
#endif
#include "nsIXTFXMLVisual.h"
#include "nsIXTFXMLVisualWrapper.h"
#include "nsAutoPtr.h"
#include "nsCOMPtr.h"
#include "nsMemory.h"
#include "nsString.h"
#include "nsIDOM3Node.h"
#include "nsIDOMDocument.h"
#include "nsIDOMElement.h"
#include "nsIDOMEventTarget.h"
#include "nsIDOMXPathExpression.h"
#include "nsIDOMXPathResult.h"
#include "nsISchema.h"
#include "nsIModelElementPrivate.h"
#include "nsIXFormsControl.h"
#include "nsXFormsAtoms.h"
#include "nsXFormsUtils.h"
static const nsIID sScriptingIIDs[] = {
NS_IDOMELEMENT_IID,
NS_IDOM3NODE_IID,
NS_IDOMEVENTTARGET_IID
};
/**
* Implementation of the XForms \<output\> element.
*
* @see http://www.w3.org/TR/xforms/slice8.html#ui-output
*/
class nsXFormsOutputElement : public nsIXTFXMLVisual,
public nsIXFormsControl
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIXTFXMLVISUAL
NS_DECL_NSIXTFVISUAL
NS_DECL_NSIXTFELEMENT
NS_DECL_NSIXFORMSCONTROL
nsXFormsOutputElement() : mElement(nsnull) {}
private:
nsCOMPtr<nsIDOMElement> mHTMLElement;
nsIDOMElement *mElement;
};
NS_IMPL_ADDREF(nsXFormsOutputElement)
NS_IMPL_RELEASE(nsXFormsOutputElement)
NS_INTERFACE_MAP_BEGIN(nsXFormsOutputElement)
NS_INTERFACE_MAP_ENTRY(nsIXTFXMLVisual)
NS_INTERFACE_MAP_ENTRY(nsIXTFElement)
NS_INTERFACE_MAP_ENTRY(nsIXFormsControl)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIXTFXMLVisual)
NS_INTERFACE_MAP_END
// nsIXTFXMLVisual
NS_IMETHODIMP
nsXFormsOutputElement::OnCreated(nsIXTFXMLVisualWrapper *aWrapper)
{
#ifdef DEBUG_XF_OUTPUT
printf("nsXFormsOutputElement::OnCreated()\n");
#endif
aWrapper->SetNotificationMask(nsIXTFElement::NOTIFY_WILL_SET_ATTRIBUTE |
nsIXTFElement::NOTIFY_ATTRIBUTE_SET |
nsIXTFElement::NOTIFY_PARENT_CHANGED);
nsresult rv;
nsCOMPtr<nsIDOMElement> node;
rv = aWrapper->GetElementNode(getter_AddRefs(node));
NS_ENSURE_SUCCESS(rv, rv);
// It's ok to keep a weak pointer to mElement. mElement will have an
// owning reference to this object, so as long as we null out mElement in
// OnDestroyed, it will always be valid.
mElement = node;
NS_ASSERTION(mElement, "Wrapper is not an nsIDOMElement, we'll crash soon");
nsCOMPtr<nsIDOMDocument> domDoc;
rv = node->GetOwnerDocument(getter_AddRefs(domDoc));
NS_ENSURE_SUCCESS(rv, rv);
rv = domDoc->CreateElementNS(NS_LITERAL_STRING(NS_NAMESPACE_XHTML),
NS_LITERAL_STRING("span"),
getter_AddRefs(mHTMLElement));
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
// nsIXTFVisual
NS_IMETHODIMP
nsXFormsOutputElement::GetVisualContent(nsIDOMElement **aElement)
{
NS_IF_ADDREF(*aElement = mHTMLElement);
return NS_OK;
}
NS_IMETHODIMP
nsXFormsOutputElement::GetInsertionPoint(nsIDOMElement **aElement)
{
NS_IF_ADDREF(*aElement = mHTMLElement);
return NS_OK;
}
NS_IMETHODIMP
nsXFormsOutputElement::GetApplyDocumentStyleSheets(PRBool *aApply)
{
*aApply = PR_TRUE;
return NS_OK;
}
NS_IMETHODIMP
nsXFormsOutputElement::DidLayout()
{
return NS_OK;
}
// nsIXTFElement
NS_IMETHODIMP
nsXFormsOutputElement::OnDestroyed()
{
mElement = nsnull;
mHTMLElement = nsnull;
return NS_OK;
}
NS_IMETHODIMP
nsXFormsOutputElement::GetElementType(PRUint32 *aType)
{
*aType = ELEMENT_TYPE_XML_VISUAL;
return NS_OK;
}
NS_IMETHODIMP
nsXFormsOutputElement::GetIsAttributeHandler(PRBool *aIsHandler)
{
*aIsHandler = PR_FALSE;
return NS_OK;
}
NS_IMETHODIMP
nsXFormsOutputElement::GetScriptingInterfaces(PRUint32 *aCount, nsIID ***aArray)
{
return nsXFormsUtils::CloneScriptingInterfaces(sScriptingIIDs,
NS_ARRAY_LENGTH(sScriptingIIDs),
aCount, aArray);
}
NS_IMETHODIMP
nsXFormsOutputElement::WillChangeDocument(nsIDOMDocument *aNewDocument)
{
return NS_OK;
}
NS_IMETHODIMP
nsXFormsOutputElement::DocumentChanged(nsIDOMDocument *aNewDocument)
{
return NS_OK;
}
NS_IMETHODIMP
nsXFormsOutputElement::WillChangeParent(nsIDOMElement *aNewParent)
{
return NS_OK;
}
NS_IMETHODIMP
nsXFormsOutputElement::ParentChanged(nsIDOMElement *aNewParent)
{
// We need to re-evaluate our instance data binding when our parent
// changes, since xmlns declarations in effect could have changed.
if (aNewParent) {
Refresh();
}
return NS_OK;
}
NS_IMETHODIMP
nsXFormsOutputElement::WillInsertChild(nsIDOMNode *aChild, PRUint32 aIndex)
{
return NS_OK;
}
NS_IMETHODIMP
nsXFormsOutputElement::ChildInserted(nsIDOMNode *aChild, PRUint32 aIndex)
{
return NS_OK;
}
NS_IMETHODIMP
nsXFormsOutputElement::WillAppendChild(nsIDOMNode *aChild)
{
return NS_OK;
}
NS_IMETHODIMP
nsXFormsOutputElement::ChildAppended(nsIDOMNode *aChild)
{
return NS_OK;
}
NS_IMETHODIMP
nsXFormsOutputElement::WillRemoveChild(PRUint32 aIndex)
{
return NS_OK;
}
NS_IMETHODIMP
nsXFormsOutputElement::ChildRemoved(PRUint32 aIndex)
{
return NS_OK;
}
NS_IMETHODIMP
nsXFormsOutputElement::WillSetAttribute(nsIAtom *aName, const nsAString &aValue)
{
if (aName == nsXFormsAtoms::bind || aName == nsXFormsAtoms::ref) {
nsCOMPtr<nsIDOMElement> bindElement;
nsCOMPtr<nsIModelElementPrivate> model;
model = do_QueryInterface(
nsXFormsUtils::GetModelAndBind(mElement,
nsXFormsUtils::ELEMENT_WITH_MODEL_ATTR,
getter_AddRefs(bindElement)));
if (model)
model->RemoveFormControl(this);
}
return NS_OK;
}
NS_IMETHODIMP
nsXFormsOutputElement::AttributeSet(nsIAtom *aName, const nsAString &aValue)
{
if (aName == nsXFormsAtoms::bind || aName == nsXFormsAtoms::ref) {
Refresh();
}
return NS_OK;
}
NS_IMETHODIMP
nsXFormsOutputElement::WillRemoveAttribute(nsIAtom *aName)
{
return NS_OK;
}
NS_IMETHODIMP
nsXFormsOutputElement::AttributeRemoved(nsIAtom *aName)
{
return NS_OK;
}
NS_IMETHODIMP
nsXFormsOutputElement::DoneAddingChildren()
{
return NS_OK;
}
NS_IMETHODIMP
nsXFormsOutputElement::HandleDefault(nsIDOMEvent *aEvent, PRBool *aHandled)
{
*aHandled = PR_FALSE;
return NS_OK;
}
// other methods
NS_IMETHODIMP
nsXFormsOutputElement::Refresh()
{
#ifdef DEBUG_XF_OUTPUT
printf("nsXFormsOutputElement::Refresh()\n");
#endif
if (!mHTMLElement)
return NS_OK;
nsresult rv;
PRBool hasRef;
rv = mElement->HasAttribute(NS_LITERAL_STRING("ref"), &hasRef);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIDOMNode> modelNode;
nsCOMPtr<nsIDOMElement> bindElement;
nsCOMPtr<nsIDOMXPathResult> result;
if (hasRef) {
result = nsXFormsUtils::EvaluateNodeBinding(mElement,
nsXFormsUtils::ELEMENT_WITH_MODEL_ATTR,
NS_LITERAL_STRING("ref"),
EmptyString(),
nsIDOMXPathResult::FIRST_ORDERED_NODE_TYPE,
getter_AddRefs(modelNode),
getter_AddRefs(bindElement));
} else {
result = nsXFormsUtils::EvaluateNodeBinding(mElement,
nsXFormsUtils::ELEMENT_WITH_MODEL_ATTR,
NS_LITERAL_STRING("value"),
EmptyString(),
nsIDOMXPathResult::STRING_TYPE,
getter_AddRefs(modelNode),
getter_AddRefs(bindElement));
}
nsCOMPtr<nsIModelElementPrivate> model = do_QueryInterface(modelNode);
if (model) {
model->AddFormControl(this);
if (result) {
nsAutoString text;
if (hasRef) {
nsCOMPtr<nsIDOMNode> resultNode;
result->GetSingleNodeValue(getter_AddRefs(resultNode));
nsXFormsUtils::GetNodeValue(resultNode, text);
} else {
rv = result->GetStringValue(text);
NS_ENSURE_SUCCESS(rv, rv);
}
nsCOMPtr<nsIDOM3Node> dom3Node = do_QueryInterface(mHTMLElement);
NS_ENSURE_TRUE(mHTMLElement, NS_ERROR_FAILURE);
rv = dom3Node->SetTextContent(text);
NS_ENSURE_SUCCESS(rv, rv);
}
}
return NS_OK;
}
NS_HIDDEN_(nsresult)
NS_NewXFormsOutputElement(nsIXTFElement **aResult)
{
*aResult = new nsXFormsOutputElement();
if (!*aResult)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(*aResult);
return NS_OK;
}

Просмотреть файл

@ -496,6 +496,7 @@ nsXFormsSubmissionElement::GetSelectedInstanceData(nsIDOMNode **result)
nsCOMPtr<nsIDOMElement> bind;
nsCOMPtr<nsIDOMXPathResult> xpRes =
nsXFormsUtils::EvaluateNodeBinding(mElement, 0,
NS_LITERAL_STRING("ref"),
NS_LITERAL_STRING("/"),
nsIDOMXPathResult::FIRST_ORDERED_NODE_TYPE,
getter_AddRefs(model),

Просмотреть файл

@ -21,6 +21,7 @@
*
* Contributor(s):
* Brian Ryner <bryner@brianryner.com>
* Allan Beaufour <abeaufour@novell.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@ -252,6 +253,7 @@ nsXFormsUtils::EvaluateNodeset(nsIDOMElement *aElement, PRUint16 aResultType)
/* static */ already_AddRefed<nsIDOMXPathResult>
nsXFormsUtils::EvaluateNodeBinding(nsIDOMElement *aElement,
PRUint32 aElementFlags,
const nsString &aBindingAttr,
const nsString &aDefaultRef,
PRUint16 aResultType,
nsIDOMNode **aModel,
@ -272,9 +274,9 @@ nsXFormsUtils::EvaluateNodeBinding(nsIDOMElement *aElement,
if (*aBind)
return EvaluateNodeset(*aBind, aResultType);
// If not, we expect there to be a ref attribute.
// If not, we expect there to be a |aBindingAttr| attribute.
nsAutoString expr;
aElement->GetAttribute(NS_LITERAL_STRING("ref"), expr);
aElement->GetAttribute(aBindingAttr, expr);
if (expr.IsEmpty())
{

Просмотреть файл

@ -21,6 +21,7 @@
*
* Contributor(s):
* Brian Ryner <bryner@brianryner.com>
* Allan Beaufour <abeaufour@novell.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@ -90,10 +91,10 @@ public:
nsIDOMElement **aBindElement);
/**
* Evaluate a 'bind' or 'ref' attribute on |aElement|. |aResultType| is
* used as the desired result type for the XPath evaluation. The model and
* bind elements (if applicable) are located as part of this evaluation,
* and are returned (addrefed) in |aModel| and |aBind|.
* Evaluate a 'bind' or |aBindingAttr| attribute on |aElement|.
* |aResultType| is used as the desired result type for the XPath evaluation.
* The model and bind elements (if applicable) are located as part of this
* evaluation, and are returned (addrefed) in |aModel| and |aBind|.
*
* The return value is an XPathResult as returned from
* nsIDOMXPathEvaluator::Evaluate().
@ -101,6 +102,7 @@ public:
static NS_HIDDEN_(already_AddRefed<nsIDOMXPathResult>)
EvaluateNodeBinding(nsIDOMElement *aElement,
PRUint32 aElementFlags,
const nsString &aBindingAttr,
const nsString &aDefaultRef,
PRUint16 aResultType,
nsIDOMNode **aModel,