зеркало из https://github.com/mozilla/gecko-dev.git
new files
This commit is contained in:
Родитель
e05c75c1c9
Коммит
3a4638a52c
|
@ -0,0 +1,504 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (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/NPL/
|
||||
*
|
||||
* 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 Communicator client code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Netscape Communications
|
||||
* Corporation. Portions created by Netscape are Copyright (C) 1998
|
||||
* Netscape Communications Corporation. All Rights Reserved.
|
||||
*/
|
||||
|
||||
#include "nsDOMAttribute.h"
|
||||
#include "nsGenericElement.h"
|
||||
#include "nsIContent.h"
|
||||
#include "nsIDOMScriptObjectFactory.h"
|
||||
// XXX Need to move text node and comment creation
|
||||
// out of HTML.
|
||||
#include "nsHTMLParts.h"
|
||||
#include "nsIHTMLContent.h"
|
||||
|
||||
static NS_DEFINE_IID(kIDOMAttrIID, NS_IDOMATTR_IID);
|
||||
static NS_DEFINE_IID(kIDOMAttributePrivateIID, NS_IDOMATTRIBUTEPRIVATE_IID);
|
||||
static NS_DEFINE_IID(kIDOMTextIID, NS_IDOMTEXT_IID);
|
||||
static NS_DEFINE_IID(kIDOMNodeListIID, NS_IDOMNODELIST_IID);
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
nsDOMAttribute::nsDOMAttribute(nsIContent* aContent,
|
||||
const nsString& aName,
|
||||
const nsString& aValue)
|
||||
: mName(aName), mValue(aValue)
|
||||
{
|
||||
NS_INIT_REFCNT();
|
||||
// We don't add a reference to our content. It will tell us
|
||||
// to drop our reference when it goes away.
|
||||
mContent = aContent;
|
||||
mScriptObject = nsnull;
|
||||
mChild = nsnull;
|
||||
mChildList = nsnull;
|
||||
}
|
||||
|
||||
nsDOMAttribute::~nsDOMAttribute()
|
||||
{
|
||||
NS_IF_RELEASE(mChild);
|
||||
NS_IF_RELEASE(mChildList);
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttribute::QueryInterface(REFNSIID aIID, void** aInstancePtr)
|
||||
{
|
||||
if (NULL == aInstancePtr) {
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
}
|
||||
if (aIID.Equals(kIDOMAttrIID)) {
|
||||
nsIDOMAttr* tmp = this;
|
||||
*aInstancePtr = (void*)tmp;
|
||||
NS_ADDREF_THIS();
|
||||
return NS_OK;
|
||||
}
|
||||
if (aIID.Equals(kIScriptObjectOwnerIID)) {
|
||||
nsIScriptObjectOwner* tmp = this;
|
||||
*aInstancePtr = (void*)tmp;
|
||||
NS_ADDREF_THIS();
|
||||
return NS_OK;
|
||||
}
|
||||
if (aIID.Equals(kIDOMAttributePrivateIID)) {
|
||||
nsIDOMAttributePrivate* tmp = this;
|
||||
*aInstancePtr = (void*)tmp;
|
||||
NS_ADDREF_THIS();
|
||||
return NS_OK;
|
||||
}
|
||||
if (aIID.Equals(kIDOMNodeIID)) {
|
||||
nsIDOMNode* tmp = this;
|
||||
*aInstancePtr = (void*)tmp;
|
||||
NS_ADDREF_THIS();
|
||||
return NS_OK;
|
||||
}
|
||||
if (aIID.Equals(kISupportsIID)) {
|
||||
nsIDOMAttr* tmp1 = this;
|
||||
nsISupports* tmp2 = tmp1;
|
||||
*aInstancePtr = (void*)tmp2;
|
||||
NS_ADDREF_THIS();
|
||||
return NS_OK;
|
||||
}
|
||||
return NS_NOINTERFACE;
|
||||
}
|
||||
|
||||
NS_IMPL_ADDREF(nsDOMAttribute)
|
||||
NS_IMPL_RELEASE(nsDOMAttribute)
|
||||
|
||||
void
|
||||
nsDOMAttribute::DropReference()
|
||||
{
|
||||
mContent = nsnull;
|
||||
}
|
||||
|
||||
void
|
||||
nsDOMAttribute::SetContent(nsIContent* aContent)
|
||||
{
|
||||
mContent = aContent;
|
||||
}
|
||||
|
||||
void
|
||||
nsDOMAttribute::SetName(const nsString& aName)
|
||||
{
|
||||
mName.SetString(aName);
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttribute::GetScriptObject(nsIScriptContext *aContext,
|
||||
void** aScriptObject)
|
||||
{
|
||||
nsresult res = NS_OK;
|
||||
if (nsnull == mScriptObject) {
|
||||
nsIDOMScriptObjectFactory *factory;
|
||||
|
||||
res = nsGenericElement::GetScriptObjectFactory(&factory);
|
||||
if (NS_OK != res) {
|
||||
return res;
|
||||
}
|
||||
|
||||
res = factory->NewScriptAttr(aContext,
|
||||
(nsISupports *)(nsIDOMAttr *)this,
|
||||
(nsISupports *)mContent,
|
||||
(void **)&mScriptObject);
|
||||
NS_RELEASE(factory);
|
||||
}
|
||||
*aScriptObject = mScriptObject;
|
||||
return res;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttribute::SetScriptObject(void *aScriptObject)
|
||||
{
|
||||
mScriptObject = aScriptObject;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttribute::GetName(nsString& aName)
|
||||
{
|
||||
aName.SetString(mName);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttribute::GetValue(nsString& aValue)
|
||||
{
|
||||
nsresult result = NS_OK;
|
||||
if (nsnull != mContent) {
|
||||
nsIAtom* nameAtom;
|
||||
PRInt32 nameSpaceID;
|
||||
nsresult attrResult;
|
||||
|
||||
mContent->ParseAttributeString(mName, nameAtom, nameSpaceID);
|
||||
attrResult = mContent->GetAttribute(nameSpaceID, nameAtom, mValue);
|
||||
if (NS_CONTENT_ATTR_NOT_THERE == attrResult) {
|
||||
mValue.Truncate();
|
||||
}
|
||||
NS_IF_RELEASE(nameAtom);
|
||||
}
|
||||
aValue.SetString(mValue);
|
||||
return result;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttribute::SetValue(const nsString& aValue)
|
||||
{
|
||||
nsresult result = NS_OK;
|
||||
if (nsnull != mContent) {
|
||||
nsIAtom* nameAtom;
|
||||
PRInt32 nameSpaceID;
|
||||
|
||||
mContent->ParseAttributeString(mName, nameAtom, nameSpaceID);
|
||||
result = mContent->SetAttribute(nameSpaceID, nameAtom, aValue, PR_TRUE);
|
||||
NS_IF_RELEASE(nameAtom);
|
||||
}
|
||||
mValue.SetString(aValue);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttribute::GetSpecified(PRBool* aSpecified)
|
||||
{
|
||||
nsresult result = NS_OK;
|
||||
if (nsnull == mContent) {
|
||||
*aSpecified = PR_FALSE;
|
||||
}
|
||||
else {
|
||||
nsAutoString value;
|
||||
nsresult attrResult;
|
||||
nsIAtom* nameAtom;
|
||||
PRInt32 nameSpaceID;
|
||||
|
||||
mContent->ParseAttributeString(mName, nameAtom, nameSpaceID);
|
||||
attrResult = mContent->GetAttribute(nameSpaceID, nameAtom, value);
|
||||
NS_IF_RELEASE(nameAtom);
|
||||
if (NS_CONTENT_ATTR_HAS_VALUE == attrResult) {
|
||||
*aSpecified = PR_TRUE;
|
||||
}
|
||||
else {
|
||||
*aSpecified = PR_FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::GetNodeName(nsString& aNodeName)
|
||||
{
|
||||
return GetName(aNodeName);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::GetNodeValue(nsString& aNodeValue)
|
||||
{
|
||||
return GetValue(aNodeValue);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::SetNodeValue(const nsString& aNodeValue)
|
||||
{
|
||||
// You can't actually do this, but we'll fail silently
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::GetNodeType(PRUint16* aNodeType)
|
||||
{
|
||||
*aNodeType = (PRUint16)nsIDOMNode::ATTRIBUTE_NODE;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::GetParentNode(nsIDOMNode** aParentNode)
|
||||
{
|
||||
*aParentNode = nsnull;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::GetChildNodes(nsIDOMNodeList** aChildNodes)
|
||||
{
|
||||
if (nsnull == mChildList) {
|
||||
mChildList = new nsAttributeChildList(this);
|
||||
if (nsnull == mChildList) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
NS_ADDREF(mChildList);
|
||||
}
|
||||
|
||||
return mChildList->QueryInterface(kIDOMNodeListIID, (void**)aChildNodes);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::HasChildNodes(PRBool* aHasChildNodes)
|
||||
{
|
||||
*aHasChildNodes = PR_FALSE;
|
||||
if (nsnull != mChild) {
|
||||
*aHasChildNodes = PR_TRUE;
|
||||
}
|
||||
else if (nsnull != mContent) {
|
||||
nsAutoString value;
|
||||
|
||||
GetValue(value);
|
||||
if (0 < value.Length()) {
|
||||
*aHasChildNodes = PR_TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::GetFirstChild(nsIDOMNode** aFirstChild)
|
||||
{
|
||||
nsAutoString value;
|
||||
nsresult result;
|
||||
|
||||
result = GetValue(value);
|
||||
if (NS_OK != result) {
|
||||
return result;
|
||||
}
|
||||
if (0 < value.Length()) {
|
||||
if (nsnull == mChild) {
|
||||
nsIHTMLContent* content;
|
||||
|
||||
result = NS_NewTextNode(&content);
|
||||
if (NS_OK != result) {
|
||||
return result;
|
||||
}
|
||||
result = content->QueryInterface(kIDOMTextIID, (void**)&mChild);
|
||||
NS_RELEASE(content);
|
||||
}
|
||||
mChild->SetData(value);
|
||||
result = mChild->QueryInterface(kIDOMNodeIID, (void**)aFirstChild);
|
||||
}
|
||||
else {
|
||||
*aFirstChild = nsnull;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::GetLastChild(nsIDOMNode** aLastChild)
|
||||
{
|
||||
return GetFirstChild(aLastChild);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::GetPreviousSibling(nsIDOMNode** aPreviousSibling)
|
||||
{
|
||||
*aPreviousSibling = nsnull;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::GetNextSibling(nsIDOMNode** aNextSibling)
|
||||
{
|
||||
*aNextSibling = nsnull;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::GetAttributes(nsIDOMNamedNodeMap** aAttributes)
|
||||
{
|
||||
*aAttributes = nsnull;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::InsertBefore(nsIDOMNode* aNewChild, nsIDOMNode* aRefChild, nsIDOMNode** aReturn)
|
||||
{
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::ReplaceChild(nsIDOMNode* aNewChild, nsIDOMNode* aOldChild, nsIDOMNode** aReturn)
|
||||
{
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::RemoveChild(nsIDOMNode* aOldChild, nsIDOMNode** aReturn)
|
||||
{
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::AppendChild(nsIDOMNode* aNewChild, nsIDOMNode** aReturn)
|
||||
{
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::CloneNode(PRBool aDeep, nsIDOMNode** aReturn)
|
||||
{
|
||||
nsDOMAttribute* newAttr;
|
||||
|
||||
if (nsnull != mContent) {
|
||||
nsAutoString value;
|
||||
nsIAtom* nameAtom;
|
||||
PRInt32 nameSpaceID;
|
||||
|
||||
mContent->ParseAttributeString(mName, nameAtom, nameSpaceID);
|
||||
mContent->GetAttribute(nameSpaceID, nameAtom, value);
|
||||
newAttr = new nsDOMAttribute(nsnull, mName, value);
|
||||
}
|
||||
else {
|
||||
newAttr = new nsDOMAttribute(nsnull, mName, mValue);
|
||||
}
|
||||
|
||||
if (nsnull == newAttr) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
return newAttr->QueryInterface(kIDOMNodeIID, (void**)aReturn);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::GetOwnerDocument(nsIDOMDocument** aOwnerDocument)
|
||||
{
|
||||
// XXX TBI
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
nsAttributeChildList::nsAttributeChildList(nsDOMAttribute* aAttribute)
|
||||
{
|
||||
NS_INIT_REFCNT();
|
||||
// Don't increment the reference count. The attribute will tell
|
||||
// us when it's going away
|
||||
mAttribute = aAttribute;
|
||||
mScriptObject = nsnull;
|
||||
}
|
||||
|
||||
nsAttributeChildList::~nsAttributeChildList()
|
||||
{
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsAttributeChildList::QueryInterface(REFNSIID aIID, void** aInstancePtr)
|
||||
{
|
||||
if (NULL == aInstancePtr) {
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
}
|
||||
if (aIID.Equals(kIDOMNodeListIID)) {
|
||||
nsIDOMNodeList* tmp = this;
|
||||
*aInstancePtr = (void*)tmp;
|
||||
NS_ADDREF_THIS();
|
||||
return NS_OK;
|
||||
}
|
||||
if (aIID.Equals(kIScriptObjectOwnerIID)) {
|
||||
nsIScriptObjectOwner* tmp = this;
|
||||
*aInstancePtr = (void*)tmp;
|
||||
NS_ADDREF_THIS();
|
||||
return NS_OK;
|
||||
}
|
||||
if (aIID.Equals(kISupportsIID)) {
|
||||
nsIDOMNodeList* tmp1 = this;
|
||||
nsISupports* tmp2 = tmp1;
|
||||
*aInstancePtr = (void*)tmp2;
|
||||
NS_ADDREF_THIS();
|
||||
return NS_OK;
|
||||
}
|
||||
return NS_NOINTERFACE;
|
||||
}
|
||||
|
||||
NS_IMPL_ADDREF(nsAttributeChildList)
|
||||
NS_IMPL_RELEASE(nsAttributeChildList)
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsAttributeChildList::GetLength(PRUint32* aLength)
|
||||
{
|
||||
nsAutoString value;
|
||||
|
||||
*aLength = 0;
|
||||
if (nsnull != mAttribute) {
|
||||
mAttribute->GetValue(value);
|
||||
if (0 < value.Length()) {
|
||||
*aLength = 1;
|
||||
}
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsAttributeChildList::Item(PRUint32 aIndex, nsIDOMNode** aReturn)
|
||||
{
|
||||
*aReturn = nsnull;
|
||||
if ((nsnull != mAttribute) && (0 == aIndex)) {
|
||||
mAttribute->GetFirstChild(aReturn);
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsAttributeChildList::GetScriptObject(nsIScriptContext* aContext,
|
||||
void** aScriptObject)
|
||||
{
|
||||
nsresult res = NS_OK;
|
||||
if (nsnull == mScriptObject) {
|
||||
nsIDOMScriptObjectFactory *factory;
|
||||
|
||||
res = nsGenericElement::GetScriptObjectFactory(&factory);
|
||||
if (NS_OK != res) {
|
||||
return res;
|
||||
}
|
||||
|
||||
res = factory->NewScriptNodeList(aContext,
|
||||
(nsISupports *)(nsIDOMNodeList *)this,
|
||||
(nsISupports *)(nsIDOMAttr*)mAttribute,
|
||||
(void **)&mScriptObject);
|
||||
NS_RELEASE(factory);
|
||||
}
|
||||
*aScriptObject = mScriptObject;
|
||||
return res;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsAttributeChildList::SetScriptObject(void *aScriptObject)
|
||||
{
|
||||
mScriptObject = aScriptObject;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
void
|
||||
nsAttributeChildList::DropReference()
|
||||
{
|
||||
mAttribute = nsnull;
|
||||
}
|
|
@ -0,0 +1,130 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (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/NPL/
|
||||
*
|
||||
* 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 Communicator client code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Netscape Communications
|
||||
* Corporation. Portions created by Netscape are Copyright (C) 1998
|
||||
* Netscape Communications Corporation. All Rights Reserved.
|
||||
*/
|
||||
#ifndef nsDOMAttribute_h___
|
||||
#define nsDOMAttribute_h___
|
||||
|
||||
#include "nsIDOMAttr.h"
|
||||
#include "nsIDOMText.h"
|
||||
#include "nsIDOMNodeList.h"
|
||||
#include "nsIScriptObjectOwner.h"
|
||||
#include "nsString.h"
|
||||
|
||||
class nsIContent;
|
||||
class nsDOMAttribute;
|
||||
|
||||
#define NS_IDOMATTRIBUTEPRIVATE_IID \
|
||||
{0xa6cf90dd, 0x15b3, 0x11d2, \
|
||||
{0x93, 0x2e, 0x00, 0x80, 0x5f, 0x8a, 0xdd, 0x32}}
|
||||
|
||||
class nsIDOMAttributePrivate : public nsISupports {
|
||||
public:
|
||||
virtual void DropReference() = 0;
|
||||
virtual void SetContent(nsIContent* aContent) = 0;
|
||||
virtual void SetName(const nsString& aName) = 0;
|
||||
};
|
||||
|
||||
// bogus child list for an attribute
|
||||
class nsAttributeChildList : public nsIDOMNodeList,
|
||||
public nsIScriptObjectOwner
|
||||
{
|
||||
public:
|
||||
nsAttributeChildList(nsDOMAttribute* aAttribute);
|
||||
~nsAttributeChildList();
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
// interface nsIDOMNodeList
|
||||
NS_IMETHOD GetLength(PRUint32* aLength);
|
||||
NS_IMETHOD Item(PRUint32 aIndex, nsIDOMNode** aReturn);
|
||||
|
||||
// interface nsIScriptObjectOwner
|
||||
NS_IMETHOD GetScriptObject(nsIScriptContext* aContext, void** aScriptObject);
|
||||
NS_IMETHOD SetScriptObject(void *aScriptObject);
|
||||
|
||||
void DropReference();
|
||||
|
||||
protected:
|
||||
nsDOMAttribute* mAttribute;
|
||||
void *mScriptObject;
|
||||
};
|
||||
|
||||
// Attribute helper class used to wrap up an attribute with a dom
|
||||
// object that implements nsIDOMAttr and nsIDOMNode and
|
||||
// nsIScriptObjectOwner
|
||||
class nsDOMAttribute : public nsIDOMAttr,
|
||||
public nsIScriptObjectOwner,
|
||||
public nsIDOMAttributePrivate
|
||||
{
|
||||
public:
|
||||
nsDOMAttribute(nsIContent* aContent,
|
||||
const nsString& aName,
|
||||
const nsString& aValue);
|
||||
~nsDOMAttribute();
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
NS_IMETHOD GetScriptObject(nsIScriptContext* aContext, void** aScriptObject);
|
||||
NS_IMETHOD SetScriptObject(void *aScriptObject);
|
||||
|
||||
// nsIDOMAttr interface
|
||||
NS_IMETHOD GetSpecified(PRBool* aSpecified);
|
||||
NS_IMETHOD GetName(nsString& aReturn);
|
||||
NS_IMETHOD GetValue(nsString& aReturn);
|
||||
NS_IMETHOD SetValue(const nsString& aValue);
|
||||
|
||||
// nsIDOMNode interface
|
||||
NS_IMETHOD GetNodeName(nsString& aNodeName);
|
||||
NS_IMETHOD GetNodeValue(nsString& aNodeValue);
|
||||
NS_IMETHOD SetNodeValue(const nsString& aNodeValue);
|
||||
NS_IMETHOD GetNodeType(PRUint16* aNodeType);
|
||||
NS_IMETHOD GetParentNode(nsIDOMNode** aParentNode);
|
||||
NS_IMETHOD GetChildNodes(nsIDOMNodeList** aChildNodes);
|
||||
NS_IMETHOD HasChildNodes(PRBool* aHasChildNodes);
|
||||
NS_IMETHOD GetFirstChild(nsIDOMNode** aFirstChild);
|
||||
NS_IMETHOD GetLastChild(nsIDOMNode** aLastChild);
|
||||
NS_IMETHOD GetPreviousSibling(nsIDOMNode** aPreviousSibling);
|
||||
NS_IMETHOD GetNextSibling(nsIDOMNode** aNextSibling);
|
||||
NS_IMETHOD GetAttributes(nsIDOMNamedNodeMap** aAttributes);
|
||||
NS_IMETHOD InsertBefore(nsIDOMNode* aNewChild, nsIDOMNode* aRefChild,
|
||||
nsIDOMNode** aReturn);
|
||||
NS_IMETHOD ReplaceChild(nsIDOMNode* aNewChild, nsIDOMNode* aOldChild,
|
||||
nsIDOMNode** aReturn);
|
||||
NS_IMETHOD RemoveChild(nsIDOMNode* aOldChild, nsIDOMNode** aReturn);
|
||||
NS_IMETHOD AppendChild(nsIDOMNode* aNewChild, nsIDOMNode** aReturn);
|
||||
NS_IMETHOD CloneNode(PRBool aDeep, nsIDOMNode** aReturn);
|
||||
NS_IMETHOD GetOwnerDocument(nsIDOMDocument** aOwnerDocument);
|
||||
|
||||
// nsIDOMAttributePrivate interface
|
||||
virtual void DropReference();
|
||||
virtual void SetContent(nsIContent* aContent);
|
||||
virtual void SetName(const nsString& aName);
|
||||
|
||||
private:
|
||||
nsIContent* mContent;
|
||||
nsString mName;
|
||||
nsString mValue;
|
||||
// XXX For now, there's only a single child - a text
|
||||
// element representing the value
|
||||
nsIDOMText* mChild;
|
||||
nsAttributeChildList* mChildList;
|
||||
void* mScriptObject;
|
||||
};
|
||||
|
||||
|
||||
#endif /* nsDOMAttribute_h___ */
|
|
@ -0,0 +1,444 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (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/NPL/
|
||||
*
|
||||
* 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 Communicator client code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Netscape Communications
|
||||
* Corporation. Portions created by Netscape are Copyright (C) 1998
|
||||
* Netscape Communications Corporation. All Rights Reserved.
|
||||
*/
|
||||
|
||||
#include "nsDOMAttributeMap.h"
|
||||
#include "nsDOMAttribute.h"
|
||||
#include "nsGenericElement.h"
|
||||
#include "nsIContent.h"
|
||||
#include "nsIDOMScriptObjectFactory.h"
|
||||
#include "nsINameSpaceManager.h"
|
||||
|
||||
static NS_DEFINE_IID(kIDOMNamedNodeMapIID, NS_IDOMNAMEDNODEMAP_IID);
|
||||
static NS_DEFINE_IID(kIDOMAttrIID, NS_IDOMATTR_IID);
|
||||
static NS_DEFINE_IID(kIDOMAttributePrivateIID, NS_IDOMATTRIBUTEPRIVATE_IID);
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
nsDOMAttributeMap::nsDOMAttributeMap(nsIContent* aContent)
|
||||
: mContent(aContent)
|
||||
{
|
||||
NS_INIT_REFCNT();
|
||||
mScriptObject = nsnull;
|
||||
mAttributes = nsnull;
|
||||
// We don't add a reference to our content. If it goes away,
|
||||
// we'll be told to drop our reference
|
||||
}
|
||||
|
||||
PR_CALLBACK PRIntn
|
||||
RemoveAttributes(PLHashEntry* he, PRIntn i, void* arg)
|
||||
{
|
||||
nsIDOMAttr* attr = (nsIDOMAttr*)he->value;
|
||||
char* str = (char*)he->key;
|
||||
|
||||
if (nsnull != attr) {
|
||||
nsIDOMAttributePrivate* attrPrivate;
|
||||
attr->QueryInterface(kIDOMAttributePrivateIID, (void**)&attrPrivate);
|
||||
attrPrivate->DropReference();
|
||||
NS_RELEASE(attrPrivate);
|
||||
NS_RELEASE(attr);
|
||||
}
|
||||
delete [] str;
|
||||
|
||||
return HT_ENUMERATE_REMOVE;
|
||||
}
|
||||
|
||||
PR_CALLBACK PRIntn
|
||||
DropReferencesInAttributes(PLHashEntry* he, PRIntn i, void* arg)
|
||||
{
|
||||
nsDOMAttribute* attr = (nsDOMAttribute*)he->value;
|
||||
|
||||
if (nsnull != attr) {
|
||||
nsIDOMAttributePrivate* attrPrivate;
|
||||
attr->QueryInterface(kIDOMAttributePrivateIID, (void**)&attrPrivate);
|
||||
attrPrivate->DropReference();
|
||||
NS_RELEASE(attrPrivate);
|
||||
}
|
||||
|
||||
return HT_ENUMERATE_NEXT;
|
||||
}
|
||||
|
||||
nsDOMAttributeMap::~nsDOMAttributeMap()
|
||||
{
|
||||
if (nsnull != mAttributes) {
|
||||
PL_HashTableEnumerateEntries(mAttributes, RemoveAttributes, nsnull);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
nsDOMAttributeMap::DropReference()
|
||||
{
|
||||
mContent = nsnull;
|
||||
if (nsnull != mAttributes) {
|
||||
PL_HashTableEnumerateEntries(mAttributes, DropReferencesInAttributes, nsnull);
|
||||
}
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttributeMap::QueryInterface(REFNSIID aIID, void** aInstancePtr)
|
||||
{
|
||||
if (NULL == aInstancePtr) {
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
}
|
||||
if (aIID.Equals(kIDOMNamedNodeMapIID)) {
|
||||
nsIDOMNamedNodeMap* tmp = this;
|
||||
*aInstancePtr = (void*)tmp;
|
||||
NS_ADDREF_THIS();
|
||||
return NS_OK;
|
||||
}
|
||||
if (aIID.Equals(kIScriptObjectOwnerIID)) {
|
||||
nsIScriptObjectOwner* tmp = this;
|
||||
*aInstancePtr = (void*)tmp;
|
||||
NS_ADDREF_THIS();
|
||||
return NS_OK;
|
||||
}
|
||||
if (aIID.Equals(kISupportsIID)) {
|
||||
nsIDOMNamedNodeMap* tmp1 = this;
|
||||
nsISupports* tmp2 = tmp1;
|
||||
*aInstancePtr = (void*)tmp2;
|
||||
NS_ADDREF_THIS();
|
||||
return NS_OK;
|
||||
}
|
||||
return NS_NOINTERFACE;
|
||||
}
|
||||
|
||||
NS_IMPL_ADDREF(nsDOMAttributeMap)
|
||||
NS_IMPL_RELEASE(nsDOMAttributeMap)
|
||||
|
||||
nsresult
|
||||
nsDOMAttributeMap::GetScriptObject(nsIScriptContext *aContext,
|
||||
void** aScriptObject)
|
||||
{
|
||||
nsresult res = NS_OK;
|
||||
if (nsnull == mScriptObject) {
|
||||
nsIDOMScriptObjectFactory *factory;
|
||||
|
||||
res = nsGenericElement::GetScriptObjectFactory(&factory);
|
||||
if (NS_OK != res) {
|
||||
return res;
|
||||
}
|
||||
|
||||
res = factory->NewScriptNamedNodeMap(aContext,
|
||||
(nsISupports *)(nsIDOMNamedNodeMap *)this,
|
||||
(nsISupports *)mContent,
|
||||
(void**)&mScriptObject);
|
||||
NS_RELEASE(factory);
|
||||
}
|
||||
*aScriptObject = mScriptObject;
|
||||
return res;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttributeMap::SetScriptObject(void *aScriptObject)
|
||||
{
|
||||
mScriptObject = aScriptObject;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
PLHashTable*
|
||||
nsDOMAttributeMap::GetAttributeTable()
|
||||
{
|
||||
if ((nsnull == mAttributes) && (nsnull != mContent)) {
|
||||
PRInt32 count;
|
||||
mContent->GetAttributeCount(count);
|
||||
mAttributes = PL_NewHashTable(count, PL_HashString, PL_CompareStrings,
|
||||
PL_CompareValues, nsnull, nsnull);
|
||||
}
|
||||
|
||||
return mAttributes;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttributeMap::GetNamedItemCommon(const nsString& aAttrName,
|
||||
PRInt32 aNameSpaceID,
|
||||
nsIAtom* aNameAtom,
|
||||
nsIDOMNode** aAttribute)
|
||||
{
|
||||
nsIDOMAttr* attribute;
|
||||
char buf[128];
|
||||
nsresult result = NS_OK;
|
||||
|
||||
PLHashTable* attrHash = GetAttributeTable();
|
||||
aAttrName.ToCString(buf, sizeof(buf));
|
||||
if (nsnull != attrHash) {
|
||||
attribute = (nsIDOMAttr*)PL_HashTableLookup(attrHash, buf);
|
||||
if (nsnull == attribute) {
|
||||
nsresult attrResult;
|
||||
nsAutoString value;
|
||||
attrResult = mContent->GetAttribute(aNameSpaceID, aNameAtom, value);
|
||||
if (NS_CONTENT_ATTR_NOT_THERE != attrResult) {
|
||||
nsDOMAttribute* domAttribute;
|
||||
domAttribute = new nsDOMAttribute(mContent, aAttrName, value);
|
||||
if (nsnull == domAttribute) {
|
||||
result = NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
else {
|
||||
result = domAttribute->QueryInterface(kIDOMAttrIID,
|
||||
(void **)&attribute);
|
||||
char* hashKey = aAttrName.ToNewCString();
|
||||
PL_HashTableAdd(attrHash, hashKey, attribute);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (nsnull != attribute) {
|
||||
result = attribute->QueryInterface(kIDOMNodeIID, (void**)aAttribute);
|
||||
}
|
||||
else {
|
||||
*aAttribute = nsnull;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void
|
||||
nsDOMAttributeMap::GetNormalizedName(PRInt32 aNameSpaceID,
|
||||
nsIAtom* aNameAtom,
|
||||
nsString& aAttrName)
|
||||
{
|
||||
nsIAtom* prefix;
|
||||
aAttrName.Truncate();
|
||||
mContent->GetNameSpacePrefix(aNameSpaceID, prefix);
|
||||
|
||||
if (nsnull != prefix) {
|
||||
prefix->ToString(aAttrName);
|
||||
aAttrName.Append(":");
|
||||
NS_RELEASE(prefix);
|
||||
}
|
||||
|
||||
if (nsnull != aNameAtom) {
|
||||
nsAutoString tmp;
|
||||
|
||||
aNameAtom->ToString(tmp);
|
||||
aAttrName.Append(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttributeMap::GetNamedItem(const nsString &aAttrName,
|
||||
nsIDOMNode** aAttribute)
|
||||
{
|
||||
nsresult result = NS_OK;
|
||||
if (nsnull != mContent) {
|
||||
nsIAtom* nameAtom;
|
||||
PRInt32 nameSpaceID;
|
||||
nsAutoString normalizedName;
|
||||
|
||||
mContent->ParseAttributeString(aAttrName, nameAtom, nameSpaceID);
|
||||
GetNormalizedName(nameSpaceID, nameAtom, normalizedName);
|
||||
result = GetNamedItemCommon(normalizedName,
|
||||
nameSpaceID,
|
||||
nameAtom,
|
||||
aAttribute);
|
||||
NS_IF_RELEASE(nameAtom);
|
||||
}
|
||||
else {
|
||||
*aAttribute = nsnull;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttributeMap::SetNamedItem(nsIDOMNode *aNode, nsIDOMNode **aReturn)
|
||||
{
|
||||
nsresult result = NS_OK;
|
||||
nsIDOMAttr* attribute;
|
||||
|
||||
if ((nsnull != mContent) && (nsnull != aNode)) {
|
||||
result = aNode->QueryInterface(kIDOMAttrIID, (void**)&attribute);
|
||||
if (NS_OK == result) {
|
||||
PLHashTable* attrHash;
|
||||
|
||||
attrHash = GetAttributeTable();
|
||||
if (nsnull != attrHash) {
|
||||
nsIDOMNode* oldAttribute;
|
||||
nsIDOMAttributePrivate* attrPrivate;
|
||||
nsAutoString name, value;
|
||||
char buf[128];
|
||||
char* key;
|
||||
nsIAtom* nameAtom;
|
||||
PRInt32 nameSpaceID;
|
||||
|
||||
// Get normalized attribute name
|
||||
attribute->GetName(name);
|
||||
mContent->ParseAttributeString(name, nameAtom, nameSpaceID);
|
||||
GetNormalizedName(nameSpaceID, nameAtom, name);
|
||||
name.ToCString(buf, sizeof(buf));
|
||||
result = GetNamedItemCommon(name, nameSpaceID, nameAtom, &oldAttribute);
|
||||
|
||||
if (nsnull != oldAttribute) {
|
||||
nsIDOMAttributePrivate* oldAttributePrivate;
|
||||
PLHashEntry** he;
|
||||
|
||||
// Remove the attribute from the hash table, cleaning
|
||||
// the hash table entry as we go about it.
|
||||
he = PL_HashTableRawLookup(attrHash, PL_HashString(buf), buf);
|
||||
key = (char*)(*he)->key;
|
||||
PL_HashTableRemove(attrHash, buf);
|
||||
if (nsnull != key) {
|
||||
delete [] key;
|
||||
}
|
||||
|
||||
result = oldAttribute->QueryInterface(kIDOMAttributePrivateIID,
|
||||
(void **)&oldAttributePrivate);
|
||||
if (NS_OK == result) {
|
||||
oldAttributePrivate->DropReference();
|
||||
NS_RELEASE(oldAttributePrivate);
|
||||
}
|
||||
|
||||
*aReturn = oldAttribute;
|
||||
|
||||
// Drop the reference held in the hash table
|
||||
NS_RELEASE(oldAttribute);
|
||||
}
|
||||
else {
|
||||
*aReturn = nsnull;
|
||||
}
|
||||
|
||||
attribute->GetValue(value);
|
||||
|
||||
// Associate the new attribute with the content
|
||||
// XXX Need to fail if it's already associated with other
|
||||
// content
|
||||
key = name.ToNewCString();
|
||||
result = attribute->QueryInterface(kIDOMAttributePrivateIID,
|
||||
(void **)&attrPrivate);
|
||||
if (NS_OK == result) {
|
||||
attrPrivate->SetContent(mContent);
|
||||
attrPrivate->SetName(name);
|
||||
NS_RELEASE(attrPrivate);
|
||||
}
|
||||
|
||||
// Add the new attribute node to the hash table (maintaining
|
||||
// a reference to it)
|
||||
PL_HashTableAdd(attrHash, key, attribute);
|
||||
|
||||
// Set the attribute on the content
|
||||
result = mContent->SetAttribute(nameSpaceID, nameAtom, value, PR_TRUE);
|
||||
NS_IF_RELEASE(nameAtom);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
*aReturn = nsnull;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttributeMap::RemoveNamedItem(const nsString& aName, nsIDOMNode** aReturn)
|
||||
{
|
||||
nsresult result = NS_OK;
|
||||
if (nsnull != mContent) {
|
||||
PLHashTable* attrHash;
|
||||
|
||||
attrHash = GetAttributeTable();
|
||||
if (nsnull != attrHash) {
|
||||
nsIDOMNode* attribute;
|
||||
nsIDOMAttributePrivate* attrPrivate;
|
||||
char buf[128];
|
||||
char* key;
|
||||
nsIAtom* nameAtom;
|
||||
PRInt32 nameSpaceID;
|
||||
nsAutoString name;
|
||||
|
||||
mContent->ParseAttributeString(aName, nameAtom, nameSpaceID);
|
||||
GetNormalizedName(nameSpaceID, nameAtom, name);
|
||||
name.ToCString(buf, sizeof(buf));
|
||||
result = GetNamedItemCommon(name, nameSpaceID, nameAtom, &attribute);
|
||||
|
||||
if (nsnull != attribute) {
|
||||
PLHashEntry** he;
|
||||
|
||||
// Remove the attribute from the hash table, cleaning
|
||||
// the hash table entry as we go about it.
|
||||
he = PL_HashTableRawLookup(attrHash, PL_HashString(buf), buf);
|
||||
key = (char*)(*he)->key;
|
||||
|
||||
PL_HashTableRemove(attrHash, buf);
|
||||
if (nsnull != key) {
|
||||
delete [] key;
|
||||
}
|
||||
|
||||
result = attribute->QueryInterface(kIDOMAttributePrivateIID,
|
||||
(void **)&attrPrivate);
|
||||
if (NS_OK == result) {
|
||||
attrPrivate->DropReference();
|
||||
NS_RELEASE(attrPrivate);
|
||||
}
|
||||
|
||||
*aReturn = attribute;
|
||||
|
||||
// Drop the reference held in the hash table
|
||||
NS_RELEASE(attribute);
|
||||
}
|
||||
else {
|
||||
*aReturn = nsnull;
|
||||
}
|
||||
|
||||
// Unset the attribute in the content
|
||||
result = mContent->UnsetAttribute(nameSpaceID, nameAtom, PR_TRUE);
|
||||
NS_IF_RELEASE(nameAtom);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
nsresult
|
||||
nsDOMAttributeMap::Item(PRUint32 aIndex, nsIDOMNode** aReturn)
|
||||
{
|
||||
PRInt32 nameSpaceID;
|
||||
nsIAtom* nameAtom = nsnull;
|
||||
nsresult result = NS_OK;
|
||||
if ((nsnull != mContent) &&
|
||||
NS_SUCCEEDED(mContent->GetAttributeNameAt(aIndex,
|
||||
nameSpaceID,
|
||||
nameAtom))) {
|
||||
nsAutoString attrName;
|
||||
|
||||
GetNormalizedName(nameSpaceID, nameAtom, attrName);
|
||||
result = GetNamedItemCommon(attrName, nameSpaceID, nameAtom, aReturn);
|
||||
NS_IF_RELEASE(nameAtom);
|
||||
}
|
||||
else {
|
||||
*aReturn = nsnull;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttributeMap::GetLength(PRUint32 *aLength)
|
||||
{
|
||||
PRInt32 n;
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
if (nsnull != mContent) {
|
||||
rv = mContent->GetAttributeCount(n);
|
||||
*aLength = PRUint32(n);
|
||||
}
|
||||
else {
|
||||
*aLength = 0;
|
||||
}
|
||||
return rv;
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (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/NPL/
|
||||
*
|
||||
* 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 Communicator client code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Netscape Communications
|
||||
* Corporation. Portions created by Netscape are Copyright (C) 1998
|
||||
* Netscape Communications Corporation. All Rights Reserved.
|
||||
*/
|
||||
#ifndef nsDOMAttributeMap_h___
|
||||
#define nsDOMAttributeMap_h___
|
||||
|
||||
#include "nsIDOMNamedNodeMap.h"
|
||||
#include "nsIScriptObjectOwner.h"
|
||||
#include "nsVoidArray.h"
|
||||
#include "nsString.h"
|
||||
#include "plhash.h"
|
||||
|
||||
class nsIContent;
|
||||
|
||||
// Helper class that implements the nsIDOMNamedNodeMap interface.
|
||||
class nsDOMAttributeMap : public nsIDOMNamedNodeMap,
|
||||
public nsIScriptObjectOwner
|
||||
{
|
||||
public:
|
||||
nsDOMAttributeMap(nsIContent* aContent);
|
||||
~nsDOMAttributeMap();
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
NS_IMETHOD GetScriptObject(nsIScriptContext* aContext, void** aScriptObject);
|
||||
NS_IMETHOD SetScriptObject(void *aScriptObject);
|
||||
|
||||
// nsIDOMNamedNodeMap interface
|
||||
NS_IMETHOD GetLength(PRUint32* aSize);
|
||||
NS_IMETHOD GetNamedItem(const nsString& aName, nsIDOMNode** aReturn);
|
||||
NS_IMETHOD SetNamedItem(nsIDOMNode* aNode, nsIDOMNode** aReturn);
|
||||
NS_IMETHOD RemoveNamedItem(const nsString& aName, nsIDOMNode** aReturn);
|
||||
NS_IMETHOD Item(PRUint32 aIndex, nsIDOMNode** aReturn);
|
||||
|
||||
void DropReference();
|
||||
|
||||
protected:
|
||||
nsresult GetNamedItemCommon(const nsString& aAttrName,
|
||||
PRInt32 aNameSpaceID,
|
||||
nsIAtom* aNameAtom,
|
||||
nsIDOMNode** aAttribute);
|
||||
void GetNormalizedName(PRInt32 aNameSpaceID,
|
||||
nsIAtom* aNameAtom,
|
||||
nsString& aAttrName);
|
||||
PLHashTable* GetAttributeTable();
|
||||
|
||||
private:
|
||||
nsIContent* mContent;
|
||||
PLHashTable* mAttributes;
|
||||
void* mScriptObject;
|
||||
};
|
||||
|
||||
|
||||
#endif /* nsDOMAttributeMap_h___ */
|
|
@ -0,0 +1,504 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (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/NPL/
|
||||
*
|
||||
* 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 Communicator client code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Netscape Communications
|
||||
* Corporation. Portions created by Netscape are Copyright (C) 1998
|
||||
* Netscape Communications Corporation. All Rights Reserved.
|
||||
*/
|
||||
|
||||
#include "nsDOMAttribute.h"
|
||||
#include "nsGenericElement.h"
|
||||
#include "nsIContent.h"
|
||||
#include "nsIDOMScriptObjectFactory.h"
|
||||
// XXX Need to move text node and comment creation
|
||||
// out of HTML.
|
||||
#include "nsHTMLParts.h"
|
||||
#include "nsIHTMLContent.h"
|
||||
|
||||
static NS_DEFINE_IID(kIDOMAttrIID, NS_IDOMATTR_IID);
|
||||
static NS_DEFINE_IID(kIDOMAttributePrivateIID, NS_IDOMATTRIBUTEPRIVATE_IID);
|
||||
static NS_DEFINE_IID(kIDOMTextIID, NS_IDOMTEXT_IID);
|
||||
static NS_DEFINE_IID(kIDOMNodeListIID, NS_IDOMNODELIST_IID);
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
nsDOMAttribute::nsDOMAttribute(nsIContent* aContent,
|
||||
const nsString& aName,
|
||||
const nsString& aValue)
|
||||
: mName(aName), mValue(aValue)
|
||||
{
|
||||
NS_INIT_REFCNT();
|
||||
// We don't add a reference to our content. It will tell us
|
||||
// to drop our reference when it goes away.
|
||||
mContent = aContent;
|
||||
mScriptObject = nsnull;
|
||||
mChild = nsnull;
|
||||
mChildList = nsnull;
|
||||
}
|
||||
|
||||
nsDOMAttribute::~nsDOMAttribute()
|
||||
{
|
||||
NS_IF_RELEASE(mChild);
|
||||
NS_IF_RELEASE(mChildList);
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttribute::QueryInterface(REFNSIID aIID, void** aInstancePtr)
|
||||
{
|
||||
if (NULL == aInstancePtr) {
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
}
|
||||
if (aIID.Equals(kIDOMAttrIID)) {
|
||||
nsIDOMAttr* tmp = this;
|
||||
*aInstancePtr = (void*)tmp;
|
||||
NS_ADDREF_THIS();
|
||||
return NS_OK;
|
||||
}
|
||||
if (aIID.Equals(kIScriptObjectOwnerIID)) {
|
||||
nsIScriptObjectOwner* tmp = this;
|
||||
*aInstancePtr = (void*)tmp;
|
||||
NS_ADDREF_THIS();
|
||||
return NS_OK;
|
||||
}
|
||||
if (aIID.Equals(kIDOMAttributePrivateIID)) {
|
||||
nsIDOMAttributePrivate* tmp = this;
|
||||
*aInstancePtr = (void*)tmp;
|
||||
NS_ADDREF_THIS();
|
||||
return NS_OK;
|
||||
}
|
||||
if (aIID.Equals(kIDOMNodeIID)) {
|
||||
nsIDOMNode* tmp = this;
|
||||
*aInstancePtr = (void*)tmp;
|
||||
NS_ADDREF_THIS();
|
||||
return NS_OK;
|
||||
}
|
||||
if (aIID.Equals(kISupportsIID)) {
|
||||
nsIDOMAttr* tmp1 = this;
|
||||
nsISupports* tmp2 = tmp1;
|
||||
*aInstancePtr = (void*)tmp2;
|
||||
NS_ADDREF_THIS();
|
||||
return NS_OK;
|
||||
}
|
||||
return NS_NOINTERFACE;
|
||||
}
|
||||
|
||||
NS_IMPL_ADDREF(nsDOMAttribute)
|
||||
NS_IMPL_RELEASE(nsDOMAttribute)
|
||||
|
||||
void
|
||||
nsDOMAttribute::DropReference()
|
||||
{
|
||||
mContent = nsnull;
|
||||
}
|
||||
|
||||
void
|
||||
nsDOMAttribute::SetContent(nsIContent* aContent)
|
||||
{
|
||||
mContent = aContent;
|
||||
}
|
||||
|
||||
void
|
||||
nsDOMAttribute::SetName(const nsString& aName)
|
||||
{
|
||||
mName.SetString(aName);
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttribute::GetScriptObject(nsIScriptContext *aContext,
|
||||
void** aScriptObject)
|
||||
{
|
||||
nsresult res = NS_OK;
|
||||
if (nsnull == mScriptObject) {
|
||||
nsIDOMScriptObjectFactory *factory;
|
||||
|
||||
res = nsGenericElement::GetScriptObjectFactory(&factory);
|
||||
if (NS_OK != res) {
|
||||
return res;
|
||||
}
|
||||
|
||||
res = factory->NewScriptAttr(aContext,
|
||||
(nsISupports *)(nsIDOMAttr *)this,
|
||||
(nsISupports *)mContent,
|
||||
(void **)&mScriptObject);
|
||||
NS_RELEASE(factory);
|
||||
}
|
||||
*aScriptObject = mScriptObject;
|
||||
return res;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttribute::SetScriptObject(void *aScriptObject)
|
||||
{
|
||||
mScriptObject = aScriptObject;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttribute::GetName(nsString& aName)
|
||||
{
|
||||
aName.SetString(mName);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttribute::GetValue(nsString& aValue)
|
||||
{
|
||||
nsresult result = NS_OK;
|
||||
if (nsnull != mContent) {
|
||||
nsIAtom* nameAtom;
|
||||
PRInt32 nameSpaceID;
|
||||
nsresult attrResult;
|
||||
|
||||
mContent->ParseAttributeString(mName, nameAtom, nameSpaceID);
|
||||
attrResult = mContent->GetAttribute(nameSpaceID, nameAtom, mValue);
|
||||
if (NS_CONTENT_ATTR_NOT_THERE == attrResult) {
|
||||
mValue.Truncate();
|
||||
}
|
||||
NS_IF_RELEASE(nameAtom);
|
||||
}
|
||||
aValue.SetString(mValue);
|
||||
return result;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttribute::SetValue(const nsString& aValue)
|
||||
{
|
||||
nsresult result = NS_OK;
|
||||
if (nsnull != mContent) {
|
||||
nsIAtom* nameAtom;
|
||||
PRInt32 nameSpaceID;
|
||||
|
||||
mContent->ParseAttributeString(mName, nameAtom, nameSpaceID);
|
||||
result = mContent->SetAttribute(nameSpaceID, nameAtom, aValue, PR_TRUE);
|
||||
NS_IF_RELEASE(nameAtom);
|
||||
}
|
||||
mValue.SetString(aValue);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttribute::GetSpecified(PRBool* aSpecified)
|
||||
{
|
||||
nsresult result = NS_OK;
|
||||
if (nsnull == mContent) {
|
||||
*aSpecified = PR_FALSE;
|
||||
}
|
||||
else {
|
||||
nsAutoString value;
|
||||
nsresult attrResult;
|
||||
nsIAtom* nameAtom;
|
||||
PRInt32 nameSpaceID;
|
||||
|
||||
mContent->ParseAttributeString(mName, nameAtom, nameSpaceID);
|
||||
attrResult = mContent->GetAttribute(nameSpaceID, nameAtom, value);
|
||||
NS_IF_RELEASE(nameAtom);
|
||||
if (NS_CONTENT_ATTR_HAS_VALUE == attrResult) {
|
||||
*aSpecified = PR_TRUE;
|
||||
}
|
||||
else {
|
||||
*aSpecified = PR_FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::GetNodeName(nsString& aNodeName)
|
||||
{
|
||||
return GetName(aNodeName);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::GetNodeValue(nsString& aNodeValue)
|
||||
{
|
||||
return GetValue(aNodeValue);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::SetNodeValue(const nsString& aNodeValue)
|
||||
{
|
||||
// You can't actually do this, but we'll fail silently
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::GetNodeType(PRUint16* aNodeType)
|
||||
{
|
||||
*aNodeType = (PRUint16)nsIDOMNode::ATTRIBUTE_NODE;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::GetParentNode(nsIDOMNode** aParentNode)
|
||||
{
|
||||
*aParentNode = nsnull;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::GetChildNodes(nsIDOMNodeList** aChildNodes)
|
||||
{
|
||||
if (nsnull == mChildList) {
|
||||
mChildList = new nsAttributeChildList(this);
|
||||
if (nsnull == mChildList) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
NS_ADDREF(mChildList);
|
||||
}
|
||||
|
||||
return mChildList->QueryInterface(kIDOMNodeListIID, (void**)aChildNodes);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::HasChildNodes(PRBool* aHasChildNodes)
|
||||
{
|
||||
*aHasChildNodes = PR_FALSE;
|
||||
if (nsnull != mChild) {
|
||||
*aHasChildNodes = PR_TRUE;
|
||||
}
|
||||
else if (nsnull != mContent) {
|
||||
nsAutoString value;
|
||||
|
||||
GetValue(value);
|
||||
if (0 < value.Length()) {
|
||||
*aHasChildNodes = PR_TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::GetFirstChild(nsIDOMNode** aFirstChild)
|
||||
{
|
||||
nsAutoString value;
|
||||
nsresult result;
|
||||
|
||||
result = GetValue(value);
|
||||
if (NS_OK != result) {
|
||||
return result;
|
||||
}
|
||||
if (0 < value.Length()) {
|
||||
if (nsnull == mChild) {
|
||||
nsIHTMLContent* content;
|
||||
|
||||
result = NS_NewTextNode(&content);
|
||||
if (NS_OK != result) {
|
||||
return result;
|
||||
}
|
||||
result = content->QueryInterface(kIDOMTextIID, (void**)&mChild);
|
||||
NS_RELEASE(content);
|
||||
}
|
||||
mChild->SetData(value);
|
||||
result = mChild->QueryInterface(kIDOMNodeIID, (void**)aFirstChild);
|
||||
}
|
||||
else {
|
||||
*aFirstChild = nsnull;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::GetLastChild(nsIDOMNode** aLastChild)
|
||||
{
|
||||
return GetFirstChild(aLastChild);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::GetPreviousSibling(nsIDOMNode** aPreviousSibling)
|
||||
{
|
||||
*aPreviousSibling = nsnull;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::GetNextSibling(nsIDOMNode** aNextSibling)
|
||||
{
|
||||
*aNextSibling = nsnull;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::GetAttributes(nsIDOMNamedNodeMap** aAttributes)
|
||||
{
|
||||
*aAttributes = nsnull;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::InsertBefore(nsIDOMNode* aNewChild, nsIDOMNode* aRefChild, nsIDOMNode** aReturn)
|
||||
{
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::ReplaceChild(nsIDOMNode* aNewChild, nsIDOMNode* aOldChild, nsIDOMNode** aReturn)
|
||||
{
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::RemoveChild(nsIDOMNode* aOldChild, nsIDOMNode** aReturn)
|
||||
{
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::AppendChild(nsIDOMNode* aNewChild, nsIDOMNode** aReturn)
|
||||
{
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::CloneNode(PRBool aDeep, nsIDOMNode** aReturn)
|
||||
{
|
||||
nsDOMAttribute* newAttr;
|
||||
|
||||
if (nsnull != mContent) {
|
||||
nsAutoString value;
|
||||
nsIAtom* nameAtom;
|
||||
PRInt32 nameSpaceID;
|
||||
|
||||
mContent->ParseAttributeString(mName, nameAtom, nameSpaceID);
|
||||
mContent->GetAttribute(nameSpaceID, nameAtom, value);
|
||||
newAttr = new nsDOMAttribute(nsnull, mName, value);
|
||||
}
|
||||
else {
|
||||
newAttr = new nsDOMAttribute(nsnull, mName, mValue);
|
||||
}
|
||||
|
||||
if (nsnull == newAttr) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
return newAttr->QueryInterface(kIDOMNodeIID, (void**)aReturn);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttribute::GetOwnerDocument(nsIDOMDocument** aOwnerDocument)
|
||||
{
|
||||
// XXX TBI
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
nsAttributeChildList::nsAttributeChildList(nsDOMAttribute* aAttribute)
|
||||
{
|
||||
NS_INIT_REFCNT();
|
||||
// Don't increment the reference count. The attribute will tell
|
||||
// us when it's going away
|
||||
mAttribute = aAttribute;
|
||||
mScriptObject = nsnull;
|
||||
}
|
||||
|
||||
nsAttributeChildList::~nsAttributeChildList()
|
||||
{
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsAttributeChildList::QueryInterface(REFNSIID aIID, void** aInstancePtr)
|
||||
{
|
||||
if (NULL == aInstancePtr) {
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
}
|
||||
if (aIID.Equals(kIDOMNodeListIID)) {
|
||||
nsIDOMNodeList* tmp = this;
|
||||
*aInstancePtr = (void*)tmp;
|
||||
NS_ADDREF_THIS();
|
||||
return NS_OK;
|
||||
}
|
||||
if (aIID.Equals(kIScriptObjectOwnerIID)) {
|
||||
nsIScriptObjectOwner* tmp = this;
|
||||
*aInstancePtr = (void*)tmp;
|
||||
NS_ADDREF_THIS();
|
||||
return NS_OK;
|
||||
}
|
||||
if (aIID.Equals(kISupportsIID)) {
|
||||
nsIDOMNodeList* tmp1 = this;
|
||||
nsISupports* tmp2 = tmp1;
|
||||
*aInstancePtr = (void*)tmp2;
|
||||
NS_ADDREF_THIS();
|
||||
return NS_OK;
|
||||
}
|
||||
return NS_NOINTERFACE;
|
||||
}
|
||||
|
||||
NS_IMPL_ADDREF(nsAttributeChildList)
|
||||
NS_IMPL_RELEASE(nsAttributeChildList)
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsAttributeChildList::GetLength(PRUint32* aLength)
|
||||
{
|
||||
nsAutoString value;
|
||||
|
||||
*aLength = 0;
|
||||
if (nsnull != mAttribute) {
|
||||
mAttribute->GetValue(value);
|
||||
if (0 < value.Length()) {
|
||||
*aLength = 1;
|
||||
}
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsAttributeChildList::Item(PRUint32 aIndex, nsIDOMNode** aReturn)
|
||||
{
|
||||
*aReturn = nsnull;
|
||||
if ((nsnull != mAttribute) && (0 == aIndex)) {
|
||||
mAttribute->GetFirstChild(aReturn);
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsAttributeChildList::GetScriptObject(nsIScriptContext* aContext,
|
||||
void** aScriptObject)
|
||||
{
|
||||
nsresult res = NS_OK;
|
||||
if (nsnull == mScriptObject) {
|
||||
nsIDOMScriptObjectFactory *factory;
|
||||
|
||||
res = nsGenericElement::GetScriptObjectFactory(&factory);
|
||||
if (NS_OK != res) {
|
||||
return res;
|
||||
}
|
||||
|
||||
res = factory->NewScriptNodeList(aContext,
|
||||
(nsISupports *)(nsIDOMNodeList *)this,
|
||||
(nsISupports *)(nsIDOMAttr*)mAttribute,
|
||||
(void **)&mScriptObject);
|
||||
NS_RELEASE(factory);
|
||||
}
|
||||
*aScriptObject = mScriptObject;
|
||||
return res;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsAttributeChildList::SetScriptObject(void *aScriptObject)
|
||||
{
|
||||
mScriptObject = aScriptObject;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
void
|
||||
nsAttributeChildList::DropReference()
|
||||
{
|
||||
mAttribute = nsnull;
|
||||
}
|
|
@ -0,0 +1,130 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (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/NPL/
|
||||
*
|
||||
* 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 Communicator client code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Netscape Communications
|
||||
* Corporation. Portions created by Netscape are Copyright (C) 1998
|
||||
* Netscape Communications Corporation. All Rights Reserved.
|
||||
*/
|
||||
#ifndef nsDOMAttribute_h___
|
||||
#define nsDOMAttribute_h___
|
||||
|
||||
#include "nsIDOMAttr.h"
|
||||
#include "nsIDOMText.h"
|
||||
#include "nsIDOMNodeList.h"
|
||||
#include "nsIScriptObjectOwner.h"
|
||||
#include "nsString.h"
|
||||
|
||||
class nsIContent;
|
||||
class nsDOMAttribute;
|
||||
|
||||
#define NS_IDOMATTRIBUTEPRIVATE_IID \
|
||||
{0xa6cf90dd, 0x15b3, 0x11d2, \
|
||||
{0x93, 0x2e, 0x00, 0x80, 0x5f, 0x8a, 0xdd, 0x32}}
|
||||
|
||||
class nsIDOMAttributePrivate : public nsISupports {
|
||||
public:
|
||||
virtual void DropReference() = 0;
|
||||
virtual void SetContent(nsIContent* aContent) = 0;
|
||||
virtual void SetName(const nsString& aName) = 0;
|
||||
};
|
||||
|
||||
// bogus child list for an attribute
|
||||
class nsAttributeChildList : public nsIDOMNodeList,
|
||||
public nsIScriptObjectOwner
|
||||
{
|
||||
public:
|
||||
nsAttributeChildList(nsDOMAttribute* aAttribute);
|
||||
~nsAttributeChildList();
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
// interface nsIDOMNodeList
|
||||
NS_IMETHOD GetLength(PRUint32* aLength);
|
||||
NS_IMETHOD Item(PRUint32 aIndex, nsIDOMNode** aReturn);
|
||||
|
||||
// interface nsIScriptObjectOwner
|
||||
NS_IMETHOD GetScriptObject(nsIScriptContext* aContext, void** aScriptObject);
|
||||
NS_IMETHOD SetScriptObject(void *aScriptObject);
|
||||
|
||||
void DropReference();
|
||||
|
||||
protected:
|
||||
nsDOMAttribute* mAttribute;
|
||||
void *mScriptObject;
|
||||
};
|
||||
|
||||
// Attribute helper class used to wrap up an attribute with a dom
|
||||
// object that implements nsIDOMAttr and nsIDOMNode and
|
||||
// nsIScriptObjectOwner
|
||||
class nsDOMAttribute : public nsIDOMAttr,
|
||||
public nsIScriptObjectOwner,
|
||||
public nsIDOMAttributePrivate
|
||||
{
|
||||
public:
|
||||
nsDOMAttribute(nsIContent* aContent,
|
||||
const nsString& aName,
|
||||
const nsString& aValue);
|
||||
~nsDOMAttribute();
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
NS_IMETHOD GetScriptObject(nsIScriptContext* aContext, void** aScriptObject);
|
||||
NS_IMETHOD SetScriptObject(void *aScriptObject);
|
||||
|
||||
// nsIDOMAttr interface
|
||||
NS_IMETHOD GetSpecified(PRBool* aSpecified);
|
||||
NS_IMETHOD GetName(nsString& aReturn);
|
||||
NS_IMETHOD GetValue(nsString& aReturn);
|
||||
NS_IMETHOD SetValue(const nsString& aValue);
|
||||
|
||||
// nsIDOMNode interface
|
||||
NS_IMETHOD GetNodeName(nsString& aNodeName);
|
||||
NS_IMETHOD GetNodeValue(nsString& aNodeValue);
|
||||
NS_IMETHOD SetNodeValue(const nsString& aNodeValue);
|
||||
NS_IMETHOD GetNodeType(PRUint16* aNodeType);
|
||||
NS_IMETHOD GetParentNode(nsIDOMNode** aParentNode);
|
||||
NS_IMETHOD GetChildNodes(nsIDOMNodeList** aChildNodes);
|
||||
NS_IMETHOD HasChildNodes(PRBool* aHasChildNodes);
|
||||
NS_IMETHOD GetFirstChild(nsIDOMNode** aFirstChild);
|
||||
NS_IMETHOD GetLastChild(nsIDOMNode** aLastChild);
|
||||
NS_IMETHOD GetPreviousSibling(nsIDOMNode** aPreviousSibling);
|
||||
NS_IMETHOD GetNextSibling(nsIDOMNode** aNextSibling);
|
||||
NS_IMETHOD GetAttributes(nsIDOMNamedNodeMap** aAttributes);
|
||||
NS_IMETHOD InsertBefore(nsIDOMNode* aNewChild, nsIDOMNode* aRefChild,
|
||||
nsIDOMNode** aReturn);
|
||||
NS_IMETHOD ReplaceChild(nsIDOMNode* aNewChild, nsIDOMNode* aOldChild,
|
||||
nsIDOMNode** aReturn);
|
||||
NS_IMETHOD RemoveChild(nsIDOMNode* aOldChild, nsIDOMNode** aReturn);
|
||||
NS_IMETHOD AppendChild(nsIDOMNode* aNewChild, nsIDOMNode** aReturn);
|
||||
NS_IMETHOD CloneNode(PRBool aDeep, nsIDOMNode** aReturn);
|
||||
NS_IMETHOD GetOwnerDocument(nsIDOMDocument** aOwnerDocument);
|
||||
|
||||
// nsIDOMAttributePrivate interface
|
||||
virtual void DropReference();
|
||||
virtual void SetContent(nsIContent* aContent);
|
||||
virtual void SetName(const nsString& aName);
|
||||
|
||||
private:
|
||||
nsIContent* mContent;
|
||||
nsString mName;
|
||||
nsString mValue;
|
||||
// XXX For now, there's only a single child - a text
|
||||
// element representing the value
|
||||
nsIDOMText* mChild;
|
||||
nsAttributeChildList* mChildList;
|
||||
void* mScriptObject;
|
||||
};
|
||||
|
||||
|
||||
#endif /* nsDOMAttribute_h___ */
|
|
@ -0,0 +1,444 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (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/NPL/
|
||||
*
|
||||
* 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 Communicator client code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Netscape Communications
|
||||
* Corporation. Portions created by Netscape are Copyright (C) 1998
|
||||
* Netscape Communications Corporation. All Rights Reserved.
|
||||
*/
|
||||
|
||||
#include "nsDOMAttributeMap.h"
|
||||
#include "nsDOMAttribute.h"
|
||||
#include "nsGenericElement.h"
|
||||
#include "nsIContent.h"
|
||||
#include "nsIDOMScriptObjectFactory.h"
|
||||
#include "nsINameSpaceManager.h"
|
||||
|
||||
static NS_DEFINE_IID(kIDOMNamedNodeMapIID, NS_IDOMNAMEDNODEMAP_IID);
|
||||
static NS_DEFINE_IID(kIDOMAttrIID, NS_IDOMATTR_IID);
|
||||
static NS_DEFINE_IID(kIDOMAttributePrivateIID, NS_IDOMATTRIBUTEPRIVATE_IID);
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
nsDOMAttributeMap::nsDOMAttributeMap(nsIContent* aContent)
|
||||
: mContent(aContent)
|
||||
{
|
||||
NS_INIT_REFCNT();
|
||||
mScriptObject = nsnull;
|
||||
mAttributes = nsnull;
|
||||
// We don't add a reference to our content. If it goes away,
|
||||
// we'll be told to drop our reference
|
||||
}
|
||||
|
||||
PR_CALLBACK PRIntn
|
||||
RemoveAttributes(PLHashEntry* he, PRIntn i, void* arg)
|
||||
{
|
||||
nsIDOMAttr* attr = (nsIDOMAttr*)he->value;
|
||||
char* str = (char*)he->key;
|
||||
|
||||
if (nsnull != attr) {
|
||||
nsIDOMAttributePrivate* attrPrivate;
|
||||
attr->QueryInterface(kIDOMAttributePrivateIID, (void**)&attrPrivate);
|
||||
attrPrivate->DropReference();
|
||||
NS_RELEASE(attrPrivate);
|
||||
NS_RELEASE(attr);
|
||||
}
|
||||
delete [] str;
|
||||
|
||||
return HT_ENUMERATE_REMOVE;
|
||||
}
|
||||
|
||||
PR_CALLBACK PRIntn
|
||||
DropReferencesInAttributes(PLHashEntry* he, PRIntn i, void* arg)
|
||||
{
|
||||
nsDOMAttribute* attr = (nsDOMAttribute*)he->value;
|
||||
|
||||
if (nsnull != attr) {
|
||||
nsIDOMAttributePrivate* attrPrivate;
|
||||
attr->QueryInterface(kIDOMAttributePrivateIID, (void**)&attrPrivate);
|
||||
attrPrivate->DropReference();
|
||||
NS_RELEASE(attrPrivate);
|
||||
}
|
||||
|
||||
return HT_ENUMERATE_NEXT;
|
||||
}
|
||||
|
||||
nsDOMAttributeMap::~nsDOMAttributeMap()
|
||||
{
|
||||
if (nsnull != mAttributes) {
|
||||
PL_HashTableEnumerateEntries(mAttributes, RemoveAttributes, nsnull);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
nsDOMAttributeMap::DropReference()
|
||||
{
|
||||
mContent = nsnull;
|
||||
if (nsnull != mAttributes) {
|
||||
PL_HashTableEnumerateEntries(mAttributes, DropReferencesInAttributes, nsnull);
|
||||
}
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttributeMap::QueryInterface(REFNSIID aIID, void** aInstancePtr)
|
||||
{
|
||||
if (NULL == aInstancePtr) {
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
}
|
||||
if (aIID.Equals(kIDOMNamedNodeMapIID)) {
|
||||
nsIDOMNamedNodeMap* tmp = this;
|
||||
*aInstancePtr = (void*)tmp;
|
||||
NS_ADDREF_THIS();
|
||||
return NS_OK;
|
||||
}
|
||||
if (aIID.Equals(kIScriptObjectOwnerIID)) {
|
||||
nsIScriptObjectOwner* tmp = this;
|
||||
*aInstancePtr = (void*)tmp;
|
||||
NS_ADDREF_THIS();
|
||||
return NS_OK;
|
||||
}
|
||||
if (aIID.Equals(kISupportsIID)) {
|
||||
nsIDOMNamedNodeMap* tmp1 = this;
|
||||
nsISupports* tmp2 = tmp1;
|
||||
*aInstancePtr = (void*)tmp2;
|
||||
NS_ADDREF_THIS();
|
||||
return NS_OK;
|
||||
}
|
||||
return NS_NOINTERFACE;
|
||||
}
|
||||
|
||||
NS_IMPL_ADDREF(nsDOMAttributeMap)
|
||||
NS_IMPL_RELEASE(nsDOMAttributeMap)
|
||||
|
||||
nsresult
|
||||
nsDOMAttributeMap::GetScriptObject(nsIScriptContext *aContext,
|
||||
void** aScriptObject)
|
||||
{
|
||||
nsresult res = NS_OK;
|
||||
if (nsnull == mScriptObject) {
|
||||
nsIDOMScriptObjectFactory *factory;
|
||||
|
||||
res = nsGenericElement::GetScriptObjectFactory(&factory);
|
||||
if (NS_OK != res) {
|
||||
return res;
|
||||
}
|
||||
|
||||
res = factory->NewScriptNamedNodeMap(aContext,
|
||||
(nsISupports *)(nsIDOMNamedNodeMap *)this,
|
||||
(nsISupports *)mContent,
|
||||
(void**)&mScriptObject);
|
||||
NS_RELEASE(factory);
|
||||
}
|
||||
*aScriptObject = mScriptObject;
|
||||
return res;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttributeMap::SetScriptObject(void *aScriptObject)
|
||||
{
|
||||
mScriptObject = aScriptObject;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
PLHashTable*
|
||||
nsDOMAttributeMap::GetAttributeTable()
|
||||
{
|
||||
if ((nsnull == mAttributes) && (nsnull != mContent)) {
|
||||
PRInt32 count;
|
||||
mContent->GetAttributeCount(count);
|
||||
mAttributes = PL_NewHashTable(count, PL_HashString, PL_CompareStrings,
|
||||
PL_CompareValues, nsnull, nsnull);
|
||||
}
|
||||
|
||||
return mAttributes;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttributeMap::GetNamedItemCommon(const nsString& aAttrName,
|
||||
PRInt32 aNameSpaceID,
|
||||
nsIAtom* aNameAtom,
|
||||
nsIDOMNode** aAttribute)
|
||||
{
|
||||
nsIDOMAttr* attribute;
|
||||
char buf[128];
|
||||
nsresult result = NS_OK;
|
||||
|
||||
PLHashTable* attrHash = GetAttributeTable();
|
||||
aAttrName.ToCString(buf, sizeof(buf));
|
||||
if (nsnull != attrHash) {
|
||||
attribute = (nsIDOMAttr*)PL_HashTableLookup(attrHash, buf);
|
||||
if (nsnull == attribute) {
|
||||
nsresult attrResult;
|
||||
nsAutoString value;
|
||||
attrResult = mContent->GetAttribute(aNameSpaceID, aNameAtom, value);
|
||||
if (NS_CONTENT_ATTR_NOT_THERE != attrResult) {
|
||||
nsDOMAttribute* domAttribute;
|
||||
domAttribute = new nsDOMAttribute(mContent, aAttrName, value);
|
||||
if (nsnull == domAttribute) {
|
||||
result = NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
else {
|
||||
result = domAttribute->QueryInterface(kIDOMAttrIID,
|
||||
(void **)&attribute);
|
||||
char* hashKey = aAttrName.ToNewCString();
|
||||
PL_HashTableAdd(attrHash, hashKey, attribute);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (nsnull != attribute) {
|
||||
result = attribute->QueryInterface(kIDOMNodeIID, (void**)aAttribute);
|
||||
}
|
||||
else {
|
||||
*aAttribute = nsnull;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void
|
||||
nsDOMAttributeMap::GetNormalizedName(PRInt32 aNameSpaceID,
|
||||
nsIAtom* aNameAtom,
|
||||
nsString& aAttrName)
|
||||
{
|
||||
nsIAtom* prefix;
|
||||
aAttrName.Truncate();
|
||||
mContent->GetNameSpacePrefix(aNameSpaceID, prefix);
|
||||
|
||||
if (nsnull != prefix) {
|
||||
prefix->ToString(aAttrName);
|
||||
aAttrName.Append(":");
|
||||
NS_RELEASE(prefix);
|
||||
}
|
||||
|
||||
if (nsnull != aNameAtom) {
|
||||
nsAutoString tmp;
|
||||
|
||||
aNameAtom->ToString(tmp);
|
||||
aAttrName.Append(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttributeMap::GetNamedItem(const nsString &aAttrName,
|
||||
nsIDOMNode** aAttribute)
|
||||
{
|
||||
nsresult result = NS_OK;
|
||||
if (nsnull != mContent) {
|
||||
nsIAtom* nameAtom;
|
||||
PRInt32 nameSpaceID;
|
||||
nsAutoString normalizedName;
|
||||
|
||||
mContent->ParseAttributeString(aAttrName, nameAtom, nameSpaceID);
|
||||
GetNormalizedName(nameSpaceID, nameAtom, normalizedName);
|
||||
result = GetNamedItemCommon(normalizedName,
|
||||
nameSpaceID,
|
||||
nameAtom,
|
||||
aAttribute);
|
||||
NS_IF_RELEASE(nameAtom);
|
||||
}
|
||||
else {
|
||||
*aAttribute = nsnull;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttributeMap::SetNamedItem(nsIDOMNode *aNode, nsIDOMNode **aReturn)
|
||||
{
|
||||
nsresult result = NS_OK;
|
||||
nsIDOMAttr* attribute;
|
||||
|
||||
if ((nsnull != mContent) && (nsnull != aNode)) {
|
||||
result = aNode->QueryInterface(kIDOMAttrIID, (void**)&attribute);
|
||||
if (NS_OK == result) {
|
||||
PLHashTable* attrHash;
|
||||
|
||||
attrHash = GetAttributeTable();
|
||||
if (nsnull != attrHash) {
|
||||
nsIDOMNode* oldAttribute;
|
||||
nsIDOMAttributePrivate* attrPrivate;
|
||||
nsAutoString name, value;
|
||||
char buf[128];
|
||||
char* key;
|
||||
nsIAtom* nameAtom;
|
||||
PRInt32 nameSpaceID;
|
||||
|
||||
// Get normalized attribute name
|
||||
attribute->GetName(name);
|
||||
mContent->ParseAttributeString(name, nameAtom, nameSpaceID);
|
||||
GetNormalizedName(nameSpaceID, nameAtom, name);
|
||||
name.ToCString(buf, sizeof(buf));
|
||||
result = GetNamedItemCommon(name, nameSpaceID, nameAtom, &oldAttribute);
|
||||
|
||||
if (nsnull != oldAttribute) {
|
||||
nsIDOMAttributePrivate* oldAttributePrivate;
|
||||
PLHashEntry** he;
|
||||
|
||||
// Remove the attribute from the hash table, cleaning
|
||||
// the hash table entry as we go about it.
|
||||
he = PL_HashTableRawLookup(attrHash, PL_HashString(buf), buf);
|
||||
key = (char*)(*he)->key;
|
||||
PL_HashTableRemove(attrHash, buf);
|
||||
if (nsnull != key) {
|
||||
delete [] key;
|
||||
}
|
||||
|
||||
result = oldAttribute->QueryInterface(kIDOMAttributePrivateIID,
|
||||
(void **)&oldAttributePrivate);
|
||||
if (NS_OK == result) {
|
||||
oldAttributePrivate->DropReference();
|
||||
NS_RELEASE(oldAttributePrivate);
|
||||
}
|
||||
|
||||
*aReturn = oldAttribute;
|
||||
|
||||
// Drop the reference held in the hash table
|
||||
NS_RELEASE(oldAttribute);
|
||||
}
|
||||
else {
|
||||
*aReturn = nsnull;
|
||||
}
|
||||
|
||||
attribute->GetValue(value);
|
||||
|
||||
// Associate the new attribute with the content
|
||||
// XXX Need to fail if it's already associated with other
|
||||
// content
|
||||
key = name.ToNewCString();
|
||||
result = attribute->QueryInterface(kIDOMAttributePrivateIID,
|
||||
(void **)&attrPrivate);
|
||||
if (NS_OK == result) {
|
||||
attrPrivate->SetContent(mContent);
|
||||
attrPrivate->SetName(name);
|
||||
NS_RELEASE(attrPrivate);
|
||||
}
|
||||
|
||||
// Add the new attribute node to the hash table (maintaining
|
||||
// a reference to it)
|
||||
PL_HashTableAdd(attrHash, key, attribute);
|
||||
|
||||
// Set the attribute on the content
|
||||
result = mContent->SetAttribute(nameSpaceID, nameAtom, value, PR_TRUE);
|
||||
NS_IF_RELEASE(nameAtom);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
*aReturn = nsnull;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMAttributeMap::RemoveNamedItem(const nsString& aName, nsIDOMNode** aReturn)
|
||||
{
|
||||
nsresult result = NS_OK;
|
||||
if (nsnull != mContent) {
|
||||
PLHashTable* attrHash;
|
||||
|
||||
attrHash = GetAttributeTable();
|
||||
if (nsnull != attrHash) {
|
||||
nsIDOMNode* attribute;
|
||||
nsIDOMAttributePrivate* attrPrivate;
|
||||
char buf[128];
|
||||
char* key;
|
||||
nsIAtom* nameAtom;
|
||||
PRInt32 nameSpaceID;
|
||||
nsAutoString name;
|
||||
|
||||
mContent->ParseAttributeString(aName, nameAtom, nameSpaceID);
|
||||
GetNormalizedName(nameSpaceID, nameAtom, name);
|
||||
name.ToCString(buf, sizeof(buf));
|
||||
result = GetNamedItemCommon(name, nameSpaceID, nameAtom, &attribute);
|
||||
|
||||
if (nsnull != attribute) {
|
||||
PLHashEntry** he;
|
||||
|
||||
// Remove the attribute from the hash table, cleaning
|
||||
// the hash table entry as we go about it.
|
||||
he = PL_HashTableRawLookup(attrHash, PL_HashString(buf), buf);
|
||||
key = (char*)(*he)->key;
|
||||
|
||||
PL_HashTableRemove(attrHash, buf);
|
||||
if (nsnull != key) {
|
||||
delete [] key;
|
||||
}
|
||||
|
||||
result = attribute->QueryInterface(kIDOMAttributePrivateIID,
|
||||
(void **)&attrPrivate);
|
||||
if (NS_OK == result) {
|
||||
attrPrivate->DropReference();
|
||||
NS_RELEASE(attrPrivate);
|
||||
}
|
||||
|
||||
*aReturn = attribute;
|
||||
|
||||
// Drop the reference held in the hash table
|
||||
NS_RELEASE(attribute);
|
||||
}
|
||||
else {
|
||||
*aReturn = nsnull;
|
||||
}
|
||||
|
||||
// Unset the attribute in the content
|
||||
result = mContent->UnsetAttribute(nameSpaceID, nameAtom, PR_TRUE);
|
||||
NS_IF_RELEASE(nameAtom);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
nsresult
|
||||
nsDOMAttributeMap::Item(PRUint32 aIndex, nsIDOMNode** aReturn)
|
||||
{
|
||||
PRInt32 nameSpaceID;
|
||||
nsIAtom* nameAtom = nsnull;
|
||||
nsresult result = NS_OK;
|
||||
if ((nsnull != mContent) &&
|
||||
NS_SUCCEEDED(mContent->GetAttributeNameAt(aIndex,
|
||||
nameSpaceID,
|
||||
nameAtom))) {
|
||||
nsAutoString attrName;
|
||||
|
||||
GetNormalizedName(nameSpaceID, nameAtom, attrName);
|
||||
result = GetNamedItemCommon(attrName, nameSpaceID, nameAtom, aReturn);
|
||||
NS_IF_RELEASE(nameAtom);
|
||||
}
|
||||
else {
|
||||
*aReturn = nsnull;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsDOMAttributeMap::GetLength(PRUint32 *aLength)
|
||||
{
|
||||
PRInt32 n;
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
if (nsnull != mContent) {
|
||||
rv = mContent->GetAttributeCount(n);
|
||||
*aLength = PRUint32(n);
|
||||
}
|
||||
else {
|
||||
*aLength = 0;
|
||||
}
|
||||
return rv;
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (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/NPL/
|
||||
*
|
||||
* 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 Communicator client code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Netscape Communications
|
||||
* Corporation. Portions created by Netscape are Copyright (C) 1998
|
||||
* Netscape Communications Corporation. All Rights Reserved.
|
||||
*/
|
||||
#ifndef nsDOMAttributeMap_h___
|
||||
#define nsDOMAttributeMap_h___
|
||||
|
||||
#include "nsIDOMNamedNodeMap.h"
|
||||
#include "nsIScriptObjectOwner.h"
|
||||
#include "nsVoidArray.h"
|
||||
#include "nsString.h"
|
||||
#include "plhash.h"
|
||||
|
||||
class nsIContent;
|
||||
|
||||
// Helper class that implements the nsIDOMNamedNodeMap interface.
|
||||
class nsDOMAttributeMap : public nsIDOMNamedNodeMap,
|
||||
public nsIScriptObjectOwner
|
||||
{
|
||||
public:
|
||||
nsDOMAttributeMap(nsIContent* aContent);
|
||||
~nsDOMAttributeMap();
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
NS_IMETHOD GetScriptObject(nsIScriptContext* aContext, void** aScriptObject);
|
||||
NS_IMETHOD SetScriptObject(void *aScriptObject);
|
||||
|
||||
// nsIDOMNamedNodeMap interface
|
||||
NS_IMETHOD GetLength(PRUint32* aSize);
|
||||
NS_IMETHOD GetNamedItem(const nsString& aName, nsIDOMNode** aReturn);
|
||||
NS_IMETHOD SetNamedItem(nsIDOMNode* aNode, nsIDOMNode** aReturn);
|
||||
NS_IMETHOD RemoveNamedItem(const nsString& aName, nsIDOMNode** aReturn);
|
||||
NS_IMETHOD Item(PRUint32 aIndex, nsIDOMNode** aReturn);
|
||||
|
||||
void DropReference();
|
||||
|
||||
protected:
|
||||
nsresult GetNamedItemCommon(const nsString& aAttrName,
|
||||
PRInt32 aNameSpaceID,
|
||||
nsIAtom* aNameAtom,
|
||||
nsIDOMNode** aAttribute);
|
||||
void GetNormalizedName(PRInt32 aNameSpaceID,
|
||||
nsIAtom* aNameAtom,
|
||||
nsString& aAttrName);
|
||||
PLHashTable* GetAttributeTable();
|
||||
|
||||
private:
|
||||
nsIContent* mContent;
|
||||
PLHashTable* mAttributes;
|
||||
void* mScriptObject;
|
||||
};
|
||||
|
||||
|
||||
#endif /* nsDOMAttributeMap_h___ */
|
|
@ -0,0 +1,207 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (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/NPL/
|
||||
*
|
||||
* 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 Communicator client code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Netscape Communications
|
||||
* Corporation. Portions created by Netscape are Copyright (C) 1998
|
||||
* Netscape Communications Corporation. All Rights Reserved.
|
||||
*/
|
||||
#include "nsGenericXMLElement.h"
|
||||
|
||||
#include "nsIAtom.h"
|
||||
#include "nsIDocument.h"
|
||||
#include "nsIDOMAttr.h"
|
||||
#include "nsIDOMNamedNodeMap.h"
|
||||
#include "nsIDOMNodeList.h"
|
||||
#include "nsIDOMDocument.h"
|
||||
#include "nsIDOMDocumentFragment.h"
|
||||
#include "nsIDOMRange.h"
|
||||
#include "nsRange.h"
|
||||
#include "nsIScriptContextOwner.h"
|
||||
#include "nsIScriptGlobalObject.h"
|
||||
#include "nsIScriptObjectOwner.h"
|
||||
#include "nsISizeOfHandler.h"
|
||||
#include "nsISupportsArray.h"
|
||||
#include "nsIURL.h"
|
||||
#include "nsFrame.h"
|
||||
#include "nsIPresShell.h"
|
||||
#include "nsIView.h"
|
||||
#include "nsIViewManager.h"
|
||||
#include "nsString.h"
|
||||
#include "nsDOMCID.h"
|
||||
#include "nsIServiceManager.h"
|
||||
#include "nsIDOMScriptObjectFactory.h"
|
||||
#include "nsINameSpaceManager.h"
|
||||
#include "prprf.h"
|
||||
#include "prmem.h"
|
||||
|
||||
static NS_DEFINE_IID(kIDOMAttrIID, NS_IDOMATTR_IID);
|
||||
static NS_DEFINE_IID(kIDOMNamedNodeMapIID, NS_IDOMNAMEDNODEMAP_IID);
|
||||
static NS_DEFINE_IID(kIDOMNodeListIID, NS_IDOMNODELIST_IID);
|
||||
static NS_DEFINE_IID(kIDOMDocumentIID, NS_IDOMDOCUMENT_IID);
|
||||
static NS_DEFINE_IID(kIDOMDocumentFragmentIID, NS_IDOMDOCUMENTFRAGMENT_IID);
|
||||
|
||||
nsGenericXMLElement::nsGenericXMLElement()
|
||||
{
|
||||
mNameSpace = nsnull;
|
||||
mNameSpacePrefix = nsnull;
|
||||
mNameSpaceID = kNameSpaceID_None;
|
||||
}
|
||||
|
||||
nsGenericXMLElement::~nsGenericXMLElement()
|
||||
{
|
||||
NS_IF_RELEASE(mNameSpace);
|
||||
NS_IF_RELEASE(mNameSpacePrefix);
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsGenericXMLElement::CopyInnerTo(nsIContent* aSrcContent,
|
||||
nsGenericXMLElement* aDst)
|
||||
{
|
||||
nsresult result = nsGenericContainerElement::CopyInnerTo(aSrcContent, aDst);
|
||||
if (NS_OK == result) {
|
||||
aDst->mNameSpacePrefix = mNameSpacePrefix;
|
||||
NS_IF_ADDREF(mNameSpacePrefix);
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsGenericXMLElement::GetScriptObject(nsIScriptContext* aContext,
|
||||
void** aScriptObject)
|
||||
{
|
||||
nsresult res = NS_OK;
|
||||
|
||||
// XXX Yuck! Reaching into the generic content object isn't good.
|
||||
nsDOMSlots *slots = GetDOMSlots();
|
||||
if (nsnull == slots->mScriptObject) {
|
||||
nsIDOMScriptObjectFactory *factory;
|
||||
|
||||
res = nsGenericElement::GetScriptObjectFactory(&factory);
|
||||
if (NS_OK != res) {
|
||||
return res;
|
||||
}
|
||||
|
||||
nsAutoString tag;
|
||||
mTag->ToString(tag);
|
||||
|
||||
res = factory->NewScriptXMLElement(tag, aContext,
|
||||
(nsISupports *)(nsIDOMElement *)this,
|
||||
mParent, (void**)&slots->mScriptObject);
|
||||
NS_RELEASE(factory);
|
||||
|
||||
char tagBuf[50];
|
||||
tag.ToCString(tagBuf, sizeof(tagBuf));
|
||||
|
||||
if (nsnull != mDocument) {
|
||||
aContext->AddNamedReference((void *)&slots->mScriptObject,
|
||||
slots->mScriptObject,
|
||||
tagBuf);
|
||||
}
|
||||
}
|
||||
*aScriptObject = slots->mScriptObject;
|
||||
return res;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsGenericXMLElement::ParseAttributeString(const nsString& aStr,
|
||||
nsIAtom*& aName,
|
||||
PRInt32& aNameSpaceID)
|
||||
{
|
||||
nsAutoString attrName(aStr);
|
||||
nsIAtom* nameSpaceAtom = nsGenericElement::CutNameSpacePrefix(attrName);
|
||||
nsIAtom* nameAtom = NS_NewAtom(attrName);
|
||||
aNameSpaceID = kNameSpaceID_None;
|
||||
|
||||
if ((nsnull != nameSpaceAtom) && (nsnull != mNameSpace)) {
|
||||
mNameSpace->FindNameSpaceID(nameSpaceAtom, aNameSpaceID);
|
||||
}
|
||||
|
||||
aName = nameAtom;
|
||||
NS_IF_RELEASE(nameSpaceAtom);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsGenericXMLElement::GetNameSpacePrefix(PRInt32 aNameSpaceID,
|
||||
nsIAtom*& aPrefix)
|
||||
{
|
||||
if (nsnull != mNameSpace) {
|
||||
return mNameSpace->FindNameSpacePrefix(aNameSpaceID, aPrefix);
|
||||
}
|
||||
else {
|
||||
aPrefix = nsnull;
|
||||
return NS_OK;
|
||||
}
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsGenericXMLElement::SetNameSpacePrefix(nsIAtom* aNameSpacePrefix)
|
||||
{
|
||||
NS_IF_RELEASE(mNameSpacePrefix);
|
||||
|
||||
mNameSpacePrefix = aNameSpacePrefix;
|
||||
|
||||
NS_IF_ADDREF(mNameSpacePrefix);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsGenericXMLElement::GetNameSpacePrefix(nsIAtom*& aNameSpacePrefix) const
|
||||
{
|
||||
aNameSpacePrefix = mNameSpacePrefix;
|
||||
|
||||
NS_IF_ADDREF(mNameSpacePrefix);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsGenericXMLElement::SetNameSpaceID(PRInt32 aNameSpaceID)
|
||||
{
|
||||
mNameSpaceID = aNameSpaceID;
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsGenericXMLElement::GetNameSpaceID(PRInt32& aNameSpaceID) const
|
||||
{
|
||||
aNameSpaceID = mNameSpaceID;
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsGenericXMLElement::SetContainingNameSpace(nsINameSpace* aNameSpace)
|
||||
{
|
||||
NS_IF_RELEASE(mNameSpace);
|
||||
|
||||
mNameSpace = aNameSpace;
|
||||
|
||||
NS_IF_ADDREF(mNameSpace);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsGenericXMLElement::GetContainingNameSpace(nsINameSpace*& aNameSpace) const
|
||||
{
|
||||
aNameSpace = mNameSpace;
|
||||
|
||||
NS_IF_ADDREF(mNameSpace);
|
||||
|
||||
return NS_OK;
|
||||
}
|
|
@ -0,0 +1,89 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (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/NPL/
|
||||
*
|
||||
* 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 Communicator client code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Netscape Communications
|
||||
* Corporation. Portions created by Netscape are Copyright (C) 1998
|
||||
* Netscape Communications Corporation. All Rights Reserved.
|
||||
*/
|
||||
#ifndef nsGenericXMLElement_h___
|
||||
#define nsGenericXMLElement_h___
|
||||
|
||||
#include "nsGenericElement.h"
|
||||
#include "nsIDOMHTMLElement.h"
|
||||
#include "nsIContent.h"
|
||||
#include "nsVoidArray.h"
|
||||
#include "nsIJSScriptObject.h"
|
||||
#include "nsINameSpaceManager.h" // for kNameSpaceID_HTML
|
||||
#include "nsINameSpace.h"
|
||||
|
||||
class nsGenericXMLElement : public nsGenericContainerElement {
|
||||
public:
|
||||
nsGenericXMLElement();
|
||||
~nsGenericXMLElement();
|
||||
|
||||
nsresult CopyInnerTo(nsIContent* aSrcContent,
|
||||
nsGenericXMLElement* aDest);
|
||||
|
||||
// Implementation for nsIDOMElement
|
||||
nsresult GetAttribute(const nsString& aName, nsString& aReturn)
|
||||
{
|
||||
return nsGenericContainerElement::GetAttribute(aName, aReturn);
|
||||
}
|
||||
nsresult SetAttribute(const nsString& aName, const nsString& aValue)
|
||||
{
|
||||
return nsGenericContainerElement::SetAttribute(aName, aValue);
|
||||
}
|
||||
|
||||
// nsIContent
|
||||
nsresult SetAttribute(PRInt32 aNameSpaceID, nsIAtom* aName,
|
||||
const nsString& aValue,
|
||||
PRBool aNotify)
|
||||
{
|
||||
return nsGenericContainerElement::SetAttribute(aNameSpaceID, aName,
|
||||
aValue, aNotify);
|
||||
}
|
||||
nsresult GetAttribute(PRInt32 aNameSpaceID, nsIAtom* aName,
|
||||
nsString& aResult) const
|
||||
{
|
||||
return nsGenericContainerElement::GetAttribute(aNameSpaceID, aName,
|
||||
aResult);
|
||||
}
|
||||
nsresult ParseAttributeString(const nsString& aStr,
|
||||
nsIAtom*& aName,
|
||||
PRInt32& aNameSpaceID);
|
||||
nsresult GetNameSpacePrefix(PRInt32 aNameSpaceID,
|
||||
nsIAtom*& aPrefix);
|
||||
|
||||
|
||||
// nsIXMLContent
|
||||
nsresult SetContainingNameSpace(nsINameSpace* aNameSpace);
|
||||
nsresult GetContainingNameSpace(nsINameSpace*& aNameSpace) const;
|
||||
|
||||
nsresult SetNameSpacePrefix(nsIAtom* aNameSpace);
|
||||
nsresult GetNameSpacePrefix(nsIAtom*& aNameSpace) const;
|
||||
nsresult SetNameSpaceID(PRInt32 aNameSpaceId);
|
||||
nsresult GetNameSpaceID(PRInt32& aNameSpaceID) const;
|
||||
|
||||
// nsIScriptObjectOwner
|
||||
nsresult GetScriptObject(nsIScriptContext* aContext,
|
||||
void** aScriptObject);
|
||||
|
||||
|
||||
|
||||
nsINameSpace* mNameSpace;
|
||||
nsIAtom* mNameSpacePrefix;
|
||||
PRInt32 mNameSpaceID;
|
||||
};
|
||||
|
||||
#endif /* nsGenericXMLElement_h__ */
|
Загрузка…
Ссылка в новой задаче