Factor out GetNodeValue and SetNodeValue. r=darin.

This commit is contained in:
bryner%brianryner.com 2004-11-04 23:26:39 +00:00
parent f892cb43e2
commit e5c56c7cb8
5 changed files with 176 additions and 147 deletions

View File

@ -376,24 +376,7 @@ nsXFormsInputElement::Blur(nsIDOMEvent *aEvent)
mInput->GetValue(value);
}
PRUint16 nodeType = 0;
singleNode->GetNodeType(&nodeType);
switch (nodeType) {
case nsIDOMNode::ATTRIBUTE_NODE:
case nsIDOMNode::TEXT_NODE:
singleNode->SetNodeValue(value);
break;
case nsIDOMNode::ELEMENT_NODE:
{
nsCOMPtr<nsIDOM3Node> node = do_QueryInterface(singleNode);
NS_ASSERTION(node, "DOM Nodes must support DOM3 interfaces");
node->SetTextContent(value);
break;
}
}
nsXFormsUtils::SetNodeValue(singleNode, value);
return NS_OK;
}
@ -425,27 +408,8 @@ nsXFormsInputElement::Refresh()
result->GetSingleNodeValue(getter_AddRefs(resultNode));
if (resultNode) {
PRUint16 nodeType = 0;
resultNode->GetNodeType(&nodeType);
nsAutoString text;
switch (nodeType) {
case nsIDOMNode::TEXT_NODE:
case nsIDOMNode::ATTRIBUTE_NODE:
resultNode->GetNodeValue(text);
break;
case nsIDOMNode::ELEMENT_NODE:
{
nsCOMPtr<nsIDOMNode> firstChild;
resultNode->GetFirstChild(getter_AddRefs(firstChild));
if (firstChild)
firstChild->GetNodeValue(text);
break;
}
default:
NS_ERROR("form control references invalid node type in instance data");
}
nsXFormsUtils::GetNodeValue(resultNode, text);
nsCOMPtr<nsISchemaType> type;
model->GetTypeForControl(this, getter_AddRefs(type));

View File

@ -43,6 +43,7 @@
#include "nsIDOMDocument.h"
#include "nsIDOMText.h"
#include "nsXFormsXPathNode.h"
#include "nsXFormsUtils.h"
MOZ_DECL_CTOR_COUNTER(nsXFormsMDG)
@ -128,112 +129,15 @@ nsXFormsMDG::Clear() {
}
nsresult
nsXFormsMDG::GetNodeValue(nsIDOMNode* aContextNode, nsAString& aNodeValue)
{
nsresult rv;
nsCOMPtr<nsIDOMNode> childNode;
PRUint16 nodeType;
rv = aContextNode->GetNodeType(&nodeType);
NS_ENSURE_SUCCESS(rv, rv);
switch(nodeType) {
case nsIDOMNode::ATTRIBUTE_NODE:
case nsIDOMNode::TEXT_NODE:
case nsIDOMNode::CDATA_SECTION_NODE:
case nsIDOMNode::PROCESSING_INSTRUCTION_NODE:
case nsIDOMNode::COMMENT_NODE:
rv = aContextNode->GetNodeValue(aNodeValue);
NS_ENSURE_SUCCESS(rv, rv);
break;
case nsIDOMNode::ELEMENT_NODE:
rv = aContextNode->GetFirstChild(getter_AddRefs(childNode));
if (NS_FAILED(rv) || !childNode) {
// No child
aNodeValue.Truncate(0);
} else {
PRUint16 childType;
rv = childNode->GetNodeType(&childType);
NS_ENSURE_SUCCESS(rv, rv);
if ( childType == nsIDOMNode::TEXT_NODE
|| childType == nsIDOMNode::CDATA_SECTION_NODE) {
rv = childNode->GetNodeValue(aNodeValue);
NS_ENSURE_SUCCESS(rv, rv);
} else {
// Not a text child
aNodeValue.Truncate(0);
}
}
break;
default:
// Asked for a node which cannot have a text child
// TODO: Should return more specific error?
return NS_ERROR_ILLEGAL_VALUE;
break;
}
return NS_OK;
}
nsresult
nsXFormsMDG::SetNodeValue(nsIDOMNode* aContextNode, nsAString& aNodeValue, PRBool aMarkNode)
nsXFormsMDG::SetNodeValue(nsIDOMNode* aContextNode, const nsString& aNodeValue,
PRBool aMarkNode)
{
if (IsReadonly(aContextNode)) {
// TODO: Better feedback for readonly nodes?
return NS_OK;
}
nsresult rv;
nsCOMPtr<nsIDOMNode> childNode;
PRUint16 nodeType;
rv = aContextNode->GetNodeType(&nodeType);
NS_ENSURE_SUCCESS(rv, rv);
switch(nodeType) {
case nsIDOMNode::ATTRIBUTE_NODE:
case nsIDOMNode::TEXT_NODE:
case nsIDOMNode::CDATA_SECTION_NODE:
case nsIDOMNode::PROCESSING_INSTRUCTION_NODE:
case nsIDOMNode::COMMENT_NODE:
// TODO: Check existing value, and ignore if same??
rv = aContextNode->SetNodeValue(aNodeValue);
NS_ENSURE_SUCCESS(rv, rv);
break;
case nsIDOMNode::ELEMENT_NODE:
rv = aContextNode->GetFirstChild(getter_AddRefs(childNode));
NS_ENSURE_SUCCESS(rv, rv);
if (!childNode) {
rv = CreateNewChild(aContextNode, aNodeValue);
NS_ENSURE_SUCCESS(rv, rv);
} else {
PRUint16 childType;
rv = childNode->GetNodeType(&childType);
NS_ENSURE_SUCCESS(rv, rv);
if ( childType == nsIDOMNode::TEXT_NODE
|| childType == nsIDOMNode::CDATA_SECTION_NODE) {
rv = childNode->SetNodeValue(aNodeValue);
NS_ENSURE_SUCCESS(rv, rv);
} else {
// Not a text child, create a new one
rv = CreateNewChild(aContextNode, aNodeValue, childNode);
NS_ENSURE_SUCCESS(rv, rv);
}
}
break;
default:
// Unsupported nodeType
// TODO: Should return more specific error?
return NS_ERROR_ILLEGAL_VALUE;
break;
}
nsXFormsUtils::SetNodeValue(aContextNode, aNodeValue);
// NB: Never reached for Readonly nodes.
if (aMarkNode) {

View File

@ -132,16 +132,9 @@ public:
* @param aNodeValue The value
* @param aMarkNode Mark node as changed?
*/
nsresult SetNodeValue(nsIDOMNode* aContextNode, nsAString& aNodeValue, PRBool aMarkNode = PR_FALSE);
nsresult SetNodeValue(nsIDOMNode* aContextNode, const nsString& aNodeValue,
PRBool aMarkNode = PR_FALSE);
/**
* Get the value of a node. (used by nsXFormsMDG)
* @param aContextNode The node to get the value for
* @param aNodeValue The value of the node
*/
nsresult GetNodeValue(nsIDOMNode* aContextNode, nsAString& aNodeValue);
PRBool IsConstraint(nsIDOMNode* aContextNode);
PRBool IsValid(nsIDOMNode* aContextNode);
PRBool ShouldDispatchValid(nsIDOMNode* aContextNode);

View File

@ -48,6 +48,7 @@
#include "nsIDOMXPathResult.h"
#include "nsIDOMXPathNSResolver.h"
#include "nsIDOMDocument.h"
#include "nsIDOMText.h"
#include "nsIXFormsModelElement.h"
/* static */ nsIDOMNode*
@ -305,6 +306,159 @@ nsXFormsUtils::EvaluateNodeBinding(nsIDOMElement *aElement,
return EvaluateXPath(expr, docElement, aElement, aResultType);
}
/* static */ void
nsXFormsUtils::GetNodeValue(nsIDOMNode* aDataNode, nsString& aNodeValue)
{
PRUint16 nodeType;
aDataNode->GetNodeType(&nodeType);
switch(nodeType) {
case nsIDOMNode::ATTRIBUTE_NODE:
case nsIDOMNode::TEXT_NODE:
// "Returns the string-value of the node."
aDataNode->GetNodeValue(aNodeValue);
return;
case nsIDOMNode::ELEMENT_NODE:
{
// "If text child nodes are present, returns the string-value of the
// first text child node. Otherwise, returns "" (the empty string)".
// Find the first child text node.
nsCOMPtr<nsIDOMNodeList> childNodes;
aDataNode->GetChildNodes(getter_AddRefs(childNodes));
if (childNodes) {
nsCOMPtr<nsIDOMNode> child;
PRUint32 childCount;
childNodes->GetLength(&childCount);
for (PRUint32 i = 0; i < childCount; ++i) {
childNodes->Item(i, getter_AddRefs(child));
NS_ASSERTION(child, "DOMNodeList length is wrong!");
child->GetNodeType(&nodeType);
if (nodeType == nsIDOMNode::TEXT_NODE) {
child->GetNodeValue(aNodeValue);
return;
}
}
}
// No child text nodes. Return an empty string.
}
break;
default:
// namespace, processing instruction, comment, XPath root node
NS_WARNING("String value for this node type is not defined");
}
aNodeValue.Truncate(0);
}
/* static */ void
nsXFormsUtils::SetNodeValue(nsIDOMNode* aDataNode, const nsString& aNodeValue)
{
PRUint16 nodeType;
aDataNode->GetNodeType(&nodeType);
switch(nodeType) {
case nsIDOMNode::ATTRIBUTE_NODE:
// "The string-value of the attribute is replaced with a string
// corresponding to the new value."
aDataNode->SetNodeValue(aNodeValue);
break;
case nsIDOMNode::TEXT_NODE:
// "The text node is replaced with a new one corresponding to the new
// value".
{
nsCOMPtr<nsIDOMDocument> document;
aDataNode->GetOwnerDocument(getter_AddRefs(document));
if (!document)
break;
nsCOMPtr<nsIDOMText> textNode;
document->CreateTextNode(aNodeValue, getter_AddRefs(textNode));
if (!textNode)
break;
nsCOMPtr<nsIDOMNode> parentNode;
aDataNode->GetParentNode(getter_AddRefs(parentNode));
if (parentNode) {
nsCOMPtr<nsIDOMNode> childReturn;
parentNode->ReplaceChild(textNode, aDataNode,
getter_AddRefs(childReturn));
}
break;
}
case nsIDOMNode::ELEMENT_NODE:
{
// "If the element has any child text nodes, the first text node is
// replaced with one corresponding to the new value."
// Start by creating a text node for the new value.
nsCOMPtr<nsIDOMDocument> document;
aDataNode->GetOwnerDocument(getter_AddRefs(document));
if (!document)
break;
nsCOMPtr<nsIDOMText> textNode;
document->CreateTextNode(aNodeValue, getter_AddRefs(textNode));
if (!textNode)
break;
// Now find the first child text node.
nsCOMPtr<nsIDOMNodeList> childNodes;
aDataNode->GetChildNodes(getter_AddRefs(childNodes));
if (!childNodes)
break;
nsCOMPtr<nsIDOMNode> child, childReturn;
PRUint32 childCount;
childNodes->GetLength(&childCount);
for (PRUint32 i = 0; i < childCount; ++i) {
childNodes->Item(i, getter_AddRefs(child));
NS_ASSERTION(child, "DOMNodeList length is wrong!");
child->GetNodeType(&nodeType);
if (nodeType == nsIDOMNode::TEXT_NODE) {
// We found one, replace it with our new text node.
aDataNode->ReplaceChild(textNode, child,
getter_AddRefs(childReturn));
return;
}
}
// "If no child text nodes are present, a text node is created,
// corresponding to the new value, and appended as the first child node."
// XXX This is a bit vague since "appended as the first child node"
// implies that there are no child nodes at all, but all we've
// established is that there are no child _text_nodes.
// Taking this to mean "inserted as the first child node" until this is
// clarified.
aDataNode->GetFirstChild(getter_AddRefs(child));
if (child)
aDataNode->InsertBefore(textNode, child, getter_AddRefs(childReturn));
else
aDataNode->AppendChild(textNode, getter_AddRefs(childReturn));
}
break;
default:
NS_WARNING("Trying to set node value for unsupported node type");
}
}
/* static */ nsresult
nsXFormsUtils::CloneScriptingInterfaces(const nsIID *aIIDList,
unsigned int aIIDCount,

View File

@ -135,6 +135,20 @@ public:
nsIDOMNode *aResolverNode,
PRUint16 aResultType);
/**
* Given a node in the instance data, get its string value according
* to section 8.1.1 of the XForms specification.
*/
static NS_HIDDEN_(void) GetNodeValue(nsIDOMNode *aDataNode,
nsString &aNodeValue);
/**
* Given a node in the instance data and a string, store the value according
* to section 10.1.9 of the XForms specification.
*/
static NS_HIDDEN_(void) SetNodeValue(nsIDOMNode *aDataNode,
const nsString &aNodeValue);
/**
* Clone the set of IIDs in |aIIDList| into |aOutArray|.
* |aOutCount| is set to |aIIDCount|.