Touched the files auto-generatedb y the IDL. Also implemented getElementByAttribute

for documents and for elements.
This commit is contained in:
hyatt%netscape.com 1999-02-23 02:40:51 +00:00
Родитель 69bed5464f
Коммит dbc9dd98bf
9 изменённых файлов: 661 добавлений и 15 удалений

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

@ -225,11 +225,7 @@ public:
virtual void Finalize(JSContext *aContext);
// nsIDOMXULElement
NS_IMETHOD DoCommand();
NS_IMETHOD AddBroadcastListener(const nsString& attr, nsIDOMNode* aNode);
NS_IMETHOD RemoveBroadcastListener(const nsString& attr, nsIDOMNode* aNode);
NS_DECL_IDOMXULELEMENT
// Implementation methods
nsresult GetResource(nsIRDFResource** aResource);
@ -242,6 +238,11 @@ public:
const nsString& aTagName,
nsRDFDOMNodeList* aElements);
static nsresult
GetElementsByAttribute(nsIDOMNode* aNode,
const nsString& aAttributeName,
const nsString& aAttributeValue,
nsRDFDOMNodeList* aElements);
private:
// pseudo-constants
@ -836,6 +837,28 @@ RDFElementImpl::GetElementsByTagName(const nsString& aName, nsIDOMNodeList** aRe
return NS_OK;
}
NS_IMETHODIMP
RDFElementImpl::GetElementsByAttribute(const nsString& aAttribute,
const nsString& aValue,
nsIDOMNodeList** aReturn)
{
nsresult rv;
nsRDFDOMNodeList* elements;
if (NS_FAILED(rv = nsRDFDOMNodeList::Create(&elements))) {
NS_ERROR("unable to create node list");
return rv;
}
nsIDOMNode* domElement;
if (NS_SUCCEEDED(rv = QueryInterface(nsIDOMNode::IID(), (void**) &domElement))) {
rv = GetElementsByAttribute(domElement, aAttribute, aValue, elements);
NS_RELEASE(domElement);
}
*aReturn = elements;
return NS_OK;
}
NS_IMETHODIMP
RDFElementImpl::Normalize()
@ -2048,3 +2071,72 @@ RDFElementImpl::GetElementsByTagName(nsIDOMNode* aNode,
return NS_OK;
}
nsresult
RDFElementImpl::GetElementsByAttribute(nsIDOMNode* aNode,
const nsString& aAttribute,
const nsString& aValue,
nsRDFDOMNodeList* aElements)
{
nsresult rv;
nsCOMPtr<nsIDOMNodeList> children;
if (NS_FAILED(rv = aNode->GetChildNodes( getter_AddRefs(children) ))) {
NS_ERROR("unable to get node's children");
return rv;
}
// no kids: terminate the recursion
if (! children)
return NS_OK;
PRUint32 length;
if (NS_FAILED(children->GetLength(&length))) {
NS_ERROR("unable to get node list's length");
return rv;
}
for (PRUint32 i = 0; i < length; ++i) {
nsCOMPtr<nsIDOMNode> child;
if (NS_FAILED(rv = children->Item(i, getter_AddRefs(child) ))) {
NS_ERROR("unable to get child from list");
return rv;
}
nsAutoString name;
nsCOMPtr<nsIContent> pContent;
pContent = do_QueryInterface(child);
PRInt32 namespaceID;
pContent->GetNameSpaceID(namespaceID);
nsIAtom* pAtom = NS_NewAtom(aAttribute);
nsString actualValue;
if (!pContent)
return rv;
rv = pContent->GetAttribute(namespaceID, pAtom, actualValue);
NS_IF_RELEASE(pAtom);
if (((rv == NS_CONTENT_ATTR_NO_VALUE || rv == NS_CONTENT_ATTR_HAS_VALUE) && aValue == "*") ||
(rv == NS_CONTENT_ATTR_HAS_VALUE && actualValue == aValue))
{
if (NS_FAILED(rv = aElements->AppendNode(child))) {
NS_ERROR("unable to append element to node list");
return rv;
}
}
// Now recursively look for children
if (NS_FAILED(rv = GetElementsByAttribute(child, aAttribute, aValue, aElements))) {
NS_ERROR("unable to recursively get elements by attribute");
return rv;
}
}
return NS_OK;
}

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

@ -548,6 +548,12 @@ public:
const nsString& aTagName,
nsRDFDOMNodeList* aElements);
static nsresult
GetElementsByAttribute(nsIDOMNode* aNode,
const nsString& aAttribute,
const nsString& aValue,
nsRDFDOMNodeList* aElements);
protected:
nsIContent*
FindContent(const nsIContent* aStartNode,
@ -2060,6 +2066,33 @@ XULDocumentImpl::GetElementsByTagName(const nsString& aTagName, nsIDOMNodeList**
return NS_OK;
}
NS_IMETHODIMP
XULDocumentImpl::GetElementsByAttribute(const nsString& aAttribute, const nsString& aValue,
nsIDOMNodeList** aReturn)
{
nsresult rv;
nsRDFDOMNodeList* elements;
if (NS_FAILED(rv = nsRDFDOMNodeList::Create(&elements))) {
NS_ERROR("unable to create node list");
return rv;
}
nsIContent* root = GetRootContent();
NS_ASSERTION(root != nsnull, "no doc root");
if (root != nsnull) {
nsIDOMNode* domRoot;
if (NS_SUCCEEDED(rv = root->QueryInterface(nsIDOMNode::IID(), (void**) &domRoot))) {
rv = GetElementsByAttribute(domRoot, aAttribute, aValue, elements);
NS_RELEASE(domRoot);
}
NS_RELEASE(root);
}
*aReturn = elements;
return NS_OK;
}
NS_IMETHODIMP
XULDocumentImpl::GetStyleSheets(nsIDOMStyleSheetCollection** aStyleSheets)
@ -2865,5 +2898,69 @@ XULDocumentImpl::GetElementsByTagName(nsIDOMNode* aNode,
return NS_OK;
}
nsresult
XULDocumentImpl::GetElementsByAttribute(nsIDOMNode* aNode,
const nsString& aAttribute,
const nsString& aValue,
nsRDFDOMNodeList* aElements)
{
nsresult rv;
nsAutoString name;
nsCOMPtr<nsIContent> pContent;
pContent = do_QueryInterface(aNode);
PRInt32 namespaceID;
pContent->GetNameSpaceID(namespaceID);
nsIAtom* pAtom = NS_NewAtom(aAttribute);
nsString actualValue;
rv = pContent->GetAttribute(namespaceID, pAtom, actualValue);
NS_IF_RELEASE(pAtom);
if (((rv == NS_CONTENT_ATTR_NO_VALUE || rv == NS_CONTENT_ATTR_HAS_VALUE) && aValue == "*") ||
(rv == NS_CONTENT_ATTR_HAS_VALUE && actualValue == aValue))
{
if (NS_FAILED(rv = aElements->AppendNode(aNode))) {
NS_ERROR("unable to append element to node list");
return rv;
}
}
nsCOMPtr<nsIDOMNodeList> children;
if (NS_FAILED(rv = aNode->GetChildNodes( getter_AddRefs(children) ))) {
NS_ERROR("unable to get node's children");
return rv;
}
// no kids: terminate the recursion
if (! children)
return NS_OK;
PRUint32 length;
if (NS_FAILED(children->GetLength(&length))) {
NS_ERROR("unable to get node list's length");
return rv;
}
for (PRUint32 i = 0; i < length; ++i) {
nsCOMPtr<nsIDOMNode> child;
if (NS_FAILED(rv = children->Item(i, getter_AddRefs(child) ))) {
NS_ERROR("unable to get child from list");
return rv;
}
if (NS_FAILED(rv = GetElementsByAttribute(child, aAttribute, aValue, aElements))) {
NS_ERROR("unable to recursively get elements by attribute");
return rv;
}
}
return NS_OK;
}

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

@ -28,6 +28,7 @@
#include "nsString.h"
#include "nsIDOMNode.h"
#include "nsIDOMXULDocument.h"
#include "nsIDOMNodeList.h"
static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID);
@ -35,9 +36,11 @@ static NS_DEFINE_IID(kIJSScriptObjectIID, NS_IJSSCRIPTOBJECT_IID);
static NS_DEFINE_IID(kIScriptGlobalObjectIID, NS_ISCRIPTGLOBALOBJECT_IID);
static NS_DEFINE_IID(kINodeIID, NS_IDOMNODE_IID);
static NS_DEFINE_IID(kIXULDocumentIID, NS_IDOMXULDOCUMENT_IID);
static NS_DEFINE_IID(kINodeListIID, NS_IDOMNODELIST_IID);
NS_DEF_PTR(nsIDOMNode);
NS_DEF_PTR(nsIDOMXULDocument);
NS_DEF_PTR(nsIDOMNodeList);
/***********************************************************************/
@ -164,6 +167,46 @@ XULDocumentGetElementByID(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
}
//
// Native method GetElementsByAttribute
//
PR_STATIC_CALLBACK(JSBool)
XULDocumentGetElementsByAttribute(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
nsIDOMXULDocument *nativeThis = (nsIDOMXULDocument*)JS_GetPrivate(cx, obj);
JSBool rBool = JS_FALSE;
nsIDOMNodeList* nativeRet;
nsAutoString b0;
nsAutoString b1;
*rval = JSVAL_NULL;
// If there's no private data, this must be the prototype, so ignore
if (nsnull == nativeThis) {
return JS_TRUE;
}
if (argc >= 2) {
nsJSUtils::nsConvertJSValToString(b0, cx, argv[0]);
nsJSUtils::nsConvertJSValToString(b1, cx, argv[1]);
if (NS_OK != nativeThis->GetElementsByAttribute(b0, b1, &nativeRet)) {
return JS_FALSE;
}
nsJSUtils::nsConvertObjectToJSVal(nativeRet, cx, rval);
}
else {
JS_ReportError(cx, "Function getElementsByAttribute requires 2 parameters");
return JS_FALSE;
}
return JS_TRUE;
}
/***********************************************************************/
//
// class for XULDocument
@ -197,6 +240,7 @@ static JSPropertySpec XULDocumentProperties[] =
static JSFunctionSpec XULDocumentMethods[] =
{
{"getElementByID", XULDocumentGetElementByID, 1},
{"getElementsByAttribute", XULDocumentGetElementsByAttribute, 2},
{0}
};

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

@ -28,6 +28,7 @@
#include "nsString.h"
#include "nsIDOMXULElement.h"
#include "nsIDOMNode.h"
#include "nsIDOMNodeList.h"
static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID);
@ -35,9 +36,11 @@ static NS_DEFINE_IID(kIJSScriptObjectIID, NS_IJSSCRIPTOBJECT_IID);
static NS_DEFINE_IID(kIScriptGlobalObjectIID, NS_ISCRIPTGLOBALOBJECT_IID);
static NS_DEFINE_IID(kIXULElementIID, NS_IDOMXULELEMENT_IID);
static NS_DEFINE_IID(kINodeIID, NS_IDOMNODE_IID);
static NS_DEFINE_IID(kINodeListIID, NS_IDOMNODELIST_IID);
NS_DEF_PTR(nsIDOMXULElement);
NS_DEF_PTR(nsIDOMNode);
NS_DEF_PTR(nsIDOMNodeList);
/***********************************************************************/
@ -250,6 +253,46 @@ XULElementDoCommand(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval
}
//
// Native method GetElementsByAttribute
//
PR_STATIC_CALLBACK(JSBool)
XULElementGetElementsByAttribute(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
nsIDOMXULElement *nativeThis = (nsIDOMXULElement*)JS_GetPrivate(cx, obj);
JSBool rBool = JS_FALSE;
nsIDOMNodeList* nativeRet;
nsAutoString b0;
nsAutoString b1;
*rval = JSVAL_NULL;
// If there's no private data, this must be the prototype, so ignore
if (nsnull == nativeThis) {
return JS_TRUE;
}
if (argc >= 2) {
nsJSUtils::nsConvertJSValToString(b0, cx, argv[0]);
nsJSUtils::nsConvertJSValToString(b1, cx, argv[1]);
if (NS_OK != nativeThis->GetElementsByAttribute(b0, b1, &nativeRet)) {
return JS_FALSE;
}
nsJSUtils::nsConvertObjectToJSVal(nativeRet, cx, rval);
}
else {
JS_ReportError(cx, "Function getElementsByAttribute requires 2 parameters");
return JS_FALSE;
}
return JS_TRUE;
}
/***********************************************************************/
//
// class for XULElement
@ -285,6 +328,7 @@ static JSFunctionSpec XULElementMethods[] =
{"addBroadcastListener", XULElementAddBroadcastListener, 2},
{"removeBroadcastListener", XULElementRemoveBroadcastListener, 2},
{"doCommand", XULElementDoCommand, 0},
{"getElementsByAttribute", XULElementGetElementsByAttribute, 2},
{0}
};

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

@ -28,6 +28,7 @@
#include "nsString.h"
#include "nsIDOMNode.h"
#include "nsIDOMXULDocument.h"
#include "nsIDOMNodeList.h"
static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID);
@ -35,9 +36,11 @@ static NS_DEFINE_IID(kIJSScriptObjectIID, NS_IJSSCRIPTOBJECT_IID);
static NS_DEFINE_IID(kIScriptGlobalObjectIID, NS_ISCRIPTGLOBALOBJECT_IID);
static NS_DEFINE_IID(kINodeIID, NS_IDOMNODE_IID);
static NS_DEFINE_IID(kIXULDocumentIID, NS_IDOMXULDOCUMENT_IID);
static NS_DEFINE_IID(kINodeListIID, NS_IDOMNODELIST_IID);
NS_DEF_PTR(nsIDOMNode);
NS_DEF_PTR(nsIDOMXULDocument);
NS_DEF_PTR(nsIDOMNodeList);
/***********************************************************************/
@ -164,6 +167,46 @@ XULDocumentGetElementByID(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
}
//
// Native method GetElementsByAttribute
//
PR_STATIC_CALLBACK(JSBool)
XULDocumentGetElementsByAttribute(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
nsIDOMXULDocument *nativeThis = (nsIDOMXULDocument*)JS_GetPrivate(cx, obj);
JSBool rBool = JS_FALSE;
nsIDOMNodeList* nativeRet;
nsAutoString b0;
nsAutoString b1;
*rval = JSVAL_NULL;
// If there's no private data, this must be the prototype, so ignore
if (nsnull == nativeThis) {
return JS_TRUE;
}
if (argc >= 2) {
nsJSUtils::nsConvertJSValToString(b0, cx, argv[0]);
nsJSUtils::nsConvertJSValToString(b1, cx, argv[1]);
if (NS_OK != nativeThis->GetElementsByAttribute(b0, b1, &nativeRet)) {
return JS_FALSE;
}
nsJSUtils::nsConvertObjectToJSVal(nativeRet, cx, rval);
}
else {
JS_ReportError(cx, "Function getElementsByAttribute requires 2 parameters");
return JS_FALSE;
}
return JS_TRUE;
}
/***********************************************************************/
//
// class for XULDocument
@ -197,6 +240,7 @@ static JSPropertySpec XULDocumentProperties[] =
static JSFunctionSpec XULDocumentMethods[] =
{
{"getElementByID", XULDocumentGetElementByID, 1},
{"getElementsByAttribute", XULDocumentGetElementsByAttribute, 2},
{0}
};

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

@ -28,6 +28,7 @@
#include "nsString.h"
#include "nsIDOMXULElement.h"
#include "nsIDOMNode.h"
#include "nsIDOMNodeList.h"
static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID);
@ -35,9 +36,11 @@ static NS_DEFINE_IID(kIJSScriptObjectIID, NS_IJSSCRIPTOBJECT_IID);
static NS_DEFINE_IID(kIScriptGlobalObjectIID, NS_ISCRIPTGLOBALOBJECT_IID);
static NS_DEFINE_IID(kIXULElementIID, NS_IDOMXULELEMENT_IID);
static NS_DEFINE_IID(kINodeIID, NS_IDOMNODE_IID);
static NS_DEFINE_IID(kINodeListIID, NS_IDOMNODELIST_IID);
NS_DEF_PTR(nsIDOMXULElement);
NS_DEF_PTR(nsIDOMNode);
NS_DEF_PTR(nsIDOMNodeList);
/***********************************************************************/
@ -250,6 +253,46 @@ XULElementDoCommand(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval
}
//
// Native method GetElementsByAttribute
//
PR_STATIC_CALLBACK(JSBool)
XULElementGetElementsByAttribute(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
nsIDOMXULElement *nativeThis = (nsIDOMXULElement*)JS_GetPrivate(cx, obj);
JSBool rBool = JS_FALSE;
nsIDOMNodeList* nativeRet;
nsAutoString b0;
nsAutoString b1;
*rval = JSVAL_NULL;
// If there's no private data, this must be the prototype, so ignore
if (nsnull == nativeThis) {
return JS_TRUE;
}
if (argc >= 2) {
nsJSUtils::nsConvertJSValToString(b0, cx, argv[0]);
nsJSUtils::nsConvertJSValToString(b1, cx, argv[1]);
if (NS_OK != nativeThis->GetElementsByAttribute(b0, b1, &nativeRet)) {
return JS_FALSE;
}
nsJSUtils::nsConvertObjectToJSVal(nativeRet, cx, rval);
}
else {
JS_ReportError(cx, "Function getElementsByAttribute requires 2 parameters");
return JS_FALSE;
}
return JS_TRUE;
}
/***********************************************************************/
//
// class for XULElement
@ -285,6 +328,7 @@ static JSFunctionSpec XULElementMethods[] =
{"addBroadcastListener", XULElementAddBroadcastListener, 2},
{"removeBroadcastListener", XULElementRemoveBroadcastListener, 2},
{"doCommand", XULElementDoCommand, 0},
{"getElementsByAttribute", XULElementGetElementsByAttribute, 2},
{0}
};

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

@ -225,11 +225,7 @@ public:
virtual void Finalize(JSContext *aContext);
// nsIDOMXULElement
NS_IMETHOD DoCommand();
NS_IMETHOD AddBroadcastListener(const nsString& attr, nsIDOMNode* aNode);
NS_IMETHOD RemoveBroadcastListener(const nsString& attr, nsIDOMNode* aNode);
NS_DECL_IDOMXULELEMENT
// Implementation methods
nsresult GetResource(nsIRDFResource** aResource);
@ -242,6 +238,11 @@ public:
const nsString& aTagName,
nsRDFDOMNodeList* aElements);
static nsresult
GetElementsByAttribute(nsIDOMNode* aNode,
const nsString& aAttributeName,
const nsString& aAttributeValue,
nsRDFDOMNodeList* aElements);
private:
// pseudo-constants
@ -836,6 +837,28 @@ RDFElementImpl::GetElementsByTagName(const nsString& aName, nsIDOMNodeList** aRe
return NS_OK;
}
NS_IMETHODIMP
RDFElementImpl::GetElementsByAttribute(const nsString& aAttribute,
const nsString& aValue,
nsIDOMNodeList** aReturn)
{
nsresult rv;
nsRDFDOMNodeList* elements;
if (NS_FAILED(rv = nsRDFDOMNodeList::Create(&elements))) {
NS_ERROR("unable to create node list");
return rv;
}
nsIDOMNode* domElement;
if (NS_SUCCEEDED(rv = QueryInterface(nsIDOMNode::IID(), (void**) &domElement))) {
rv = GetElementsByAttribute(domElement, aAttribute, aValue, elements);
NS_RELEASE(domElement);
}
*aReturn = elements;
return NS_OK;
}
NS_IMETHODIMP
RDFElementImpl::Normalize()
@ -2048,3 +2071,72 @@ RDFElementImpl::GetElementsByTagName(nsIDOMNode* aNode,
return NS_OK;
}
nsresult
RDFElementImpl::GetElementsByAttribute(nsIDOMNode* aNode,
const nsString& aAttribute,
const nsString& aValue,
nsRDFDOMNodeList* aElements)
{
nsresult rv;
nsCOMPtr<nsIDOMNodeList> children;
if (NS_FAILED(rv = aNode->GetChildNodes( getter_AddRefs(children) ))) {
NS_ERROR("unable to get node's children");
return rv;
}
// no kids: terminate the recursion
if (! children)
return NS_OK;
PRUint32 length;
if (NS_FAILED(children->GetLength(&length))) {
NS_ERROR("unable to get node list's length");
return rv;
}
for (PRUint32 i = 0; i < length; ++i) {
nsCOMPtr<nsIDOMNode> child;
if (NS_FAILED(rv = children->Item(i, getter_AddRefs(child) ))) {
NS_ERROR("unable to get child from list");
return rv;
}
nsAutoString name;
nsCOMPtr<nsIContent> pContent;
pContent = do_QueryInterface(child);
PRInt32 namespaceID;
pContent->GetNameSpaceID(namespaceID);
nsIAtom* pAtom = NS_NewAtom(aAttribute);
nsString actualValue;
if (!pContent)
return rv;
rv = pContent->GetAttribute(namespaceID, pAtom, actualValue);
NS_IF_RELEASE(pAtom);
if (((rv == NS_CONTENT_ATTR_NO_VALUE || rv == NS_CONTENT_ATTR_HAS_VALUE) && aValue == "*") ||
(rv == NS_CONTENT_ATTR_HAS_VALUE && actualValue == aValue))
{
if (NS_FAILED(rv = aElements->AppendNode(child))) {
NS_ERROR("unable to append element to node list");
return rv;
}
}
// Now recursively look for children
if (NS_FAILED(rv = GetElementsByAttribute(child, aAttribute, aValue, aElements))) {
NS_ERROR("unable to recursively get elements by attribute");
return rv;
}
}
return NS_OK;
}

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

@ -548,6 +548,12 @@ public:
const nsString& aTagName,
nsRDFDOMNodeList* aElements);
static nsresult
GetElementsByAttribute(nsIDOMNode* aNode,
const nsString& aAttribute,
const nsString& aValue,
nsRDFDOMNodeList* aElements);
protected:
nsIContent*
FindContent(const nsIContent* aStartNode,
@ -2060,6 +2066,33 @@ XULDocumentImpl::GetElementsByTagName(const nsString& aTagName, nsIDOMNodeList**
return NS_OK;
}
NS_IMETHODIMP
XULDocumentImpl::GetElementsByAttribute(const nsString& aAttribute, const nsString& aValue,
nsIDOMNodeList** aReturn)
{
nsresult rv;
nsRDFDOMNodeList* elements;
if (NS_FAILED(rv = nsRDFDOMNodeList::Create(&elements))) {
NS_ERROR("unable to create node list");
return rv;
}
nsIContent* root = GetRootContent();
NS_ASSERTION(root != nsnull, "no doc root");
if (root != nsnull) {
nsIDOMNode* domRoot;
if (NS_SUCCEEDED(rv = root->QueryInterface(nsIDOMNode::IID(), (void**) &domRoot))) {
rv = GetElementsByAttribute(domRoot, aAttribute, aValue, elements);
NS_RELEASE(domRoot);
}
NS_RELEASE(root);
}
*aReturn = elements;
return NS_OK;
}
NS_IMETHODIMP
XULDocumentImpl::GetStyleSheets(nsIDOMStyleSheetCollection** aStyleSheets)
@ -2865,5 +2898,69 @@ XULDocumentImpl::GetElementsByTagName(nsIDOMNode* aNode,
return NS_OK;
}
nsresult
XULDocumentImpl::GetElementsByAttribute(nsIDOMNode* aNode,
const nsString& aAttribute,
const nsString& aValue,
nsRDFDOMNodeList* aElements)
{
nsresult rv;
nsAutoString name;
nsCOMPtr<nsIContent> pContent;
pContent = do_QueryInterface(aNode);
PRInt32 namespaceID;
pContent->GetNameSpaceID(namespaceID);
nsIAtom* pAtom = NS_NewAtom(aAttribute);
nsString actualValue;
rv = pContent->GetAttribute(namespaceID, pAtom, actualValue);
NS_IF_RELEASE(pAtom);
if (((rv == NS_CONTENT_ATTR_NO_VALUE || rv == NS_CONTENT_ATTR_HAS_VALUE) && aValue == "*") ||
(rv == NS_CONTENT_ATTR_HAS_VALUE && actualValue == aValue))
{
if (NS_FAILED(rv = aElements->AppendNode(aNode))) {
NS_ERROR("unable to append element to node list");
return rv;
}
}
nsCOMPtr<nsIDOMNodeList> children;
if (NS_FAILED(rv = aNode->GetChildNodes( getter_AddRefs(children) ))) {
NS_ERROR("unable to get node's children");
return rv;
}
// no kids: terminate the recursion
if (! children)
return NS_OK;
PRUint32 length;
if (NS_FAILED(children->GetLength(&length))) {
NS_ERROR("unable to get node list's length");
return rv;
}
for (PRUint32 i = 0; i < length; ++i) {
nsCOMPtr<nsIDOMNode> child;
if (NS_FAILED(rv = children->Item(i, getter_AddRefs(child) ))) {
NS_ERROR("unable to get child from list");
return rv;
}
if (NS_FAILED(rv = GetElementsByAttribute(child, aAttribute, aValue, aElements))) {
NS_ERROR("unable to recursively get elements by attribute");
return rv;
}
}
return NS_OK;
}

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

@ -225,11 +225,7 @@ public:
virtual void Finalize(JSContext *aContext);
// nsIDOMXULElement
NS_IMETHOD DoCommand();
NS_IMETHOD AddBroadcastListener(const nsString& attr, nsIDOMNode* aNode);
NS_IMETHOD RemoveBroadcastListener(const nsString& attr, nsIDOMNode* aNode);
NS_DECL_IDOMXULELEMENT
// Implementation methods
nsresult GetResource(nsIRDFResource** aResource);
@ -242,6 +238,11 @@ public:
const nsString& aTagName,
nsRDFDOMNodeList* aElements);
static nsresult
GetElementsByAttribute(nsIDOMNode* aNode,
const nsString& aAttributeName,
const nsString& aAttributeValue,
nsRDFDOMNodeList* aElements);
private:
// pseudo-constants
@ -836,6 +837,28 @@ RDFElementImpl::GetElementsByTagName(const nsString& aName, nsIDOMNodeList** aRe
return NS_OK;
}
NS_IMETHODIMP
RDFElementImpl::GetElementsByAttribute(const nsString& aAttribute,
const nsString& aValue,
nsIDOMNodeList** aReturn)
{
nsresult rv;
nsRDFDOMNodeList* elements;
if (NS_FAILED(rv = nsRDFDOMNodeList::Create(&elements))) {
NS_ERROR("unable to create node list");
return rv;
}
nsIDOMNode* domElement;
if (NS_SUCCEEDED(rv = QueryInterface(nsIDOMNode::IID(), (void**) &domElement))) {
rv = GetElementsByAttribute(domElement, aAttribute, aValue, elements);
NS_RELEASE(domElement);
}
*aReturn = elements;
return NS_OK;
}
NS_IMETHODIMP
RDFElementImpl::Normalize()
@ -2048,3 +2071,72 @@ RDFElementImpl::GetElementsByTagName(nsIDOMNode* aNode,
return NS_OK;
}
nsresult
RDFElementImpl::GetElementsByAttribute(nsIDOMNode* aNode,
const nsString& aAttribute,
const nsString& aValue,
nsRDFDOMNodeList* aElements)
{
nsresult rv;
nsCOMPtr<nsIDOMNodeList> children;
if (NS_FAILED(rv = aNode->GetChildNodes( getter_AddRefs(children) ))) {
NS_ERROR("unable to get node's children");
return rv;
}
// no kids: terminate the recursion
if (! children)
return NS_OK;
PRUint32 length;
if (NS_FAILED(children->GetLength(&length))) {
NS_ERROR("unable to get node list's length");
return rv;
}
for (PRUint32 i = 0; i < length; ++i) {
nsCOMPtr<nsIDOMNode> child;
if (NS_FAILED(rv = children->Item(i, getter_AddRefs(child) ))) {
NS_ERROR("unable to get child from list");
return rv;
}
nsAutoString name;
nsCOMPtr<nsIContent> pContent;
pContent = do_QueryInterface(child);
PRInt32 namespaceID;
pContent->GetNameSpaceID(namespaceID);
nsIAtom* pAtom = NS_NewAtom(aAttribute);
nsString actualValue;
if (!pContent)
return rv;
rv = pContent->GetAttribute(namespaceID, pAtom, actualValue);
NS_IF_RELEASE(pAtom);
if (((rv == NS_CONTENT_ATTR_NO_VALUE || rv == NS_CONTENT_ATTR_HAS_VALUE) && aValue == "*") ||
(rv == NS_CONTENT_ATTR_HAS_VALUE && actualValue == aValue))
{
if (NS_FAILED(rv = aElements->AppendNode(child))) {
NS_ERROR("unable to append element to node list");
return rv;
}
}
// Now recursively look for children
if (NS_FAILED(rv = GetElementsByAttribute(child, aAttribute, aValue, aElements))) {
NS_ERROR("unable to recursively get elements by attribute");
return rv;
}
}
return NS_OK;
}