Impl getAnonymousElementByAttr, r=jst, sr=ben

This commit is contained in:
hyatt%netscape.com 2001-01-27 00:43:54 +00:00
Родитель 06f8df58de
Коммит 61e8d2ad00
7 изменённых файлов: 261 добавлений и 23 удалений

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

@ -807,6 +807,7 @@ nsresult nsDocument::Init()
if (mNameSpaceManager) {
return NS_ERROR_ALREADY_INITIALIZED;
}
nsresult rv;
rv = NS_NewISupportsArray(getter_AddRefs(mChildren));
@ -1285,6 +1286,7 @@ void nsDocument::SetRootContent(nsIContent* aRoot)
} else if (aRoot) {
mChildren->AppendElement(aRoot);
}
mRootContent = aRoot;
}
@ -1293,7 +1295,6 @@ nsDocument::ChildAt(PRInt32 aIndex, nsIContent*& aResult) const
{
nsCOMPtr<nsIContent> content( dont_AddRef(NS_STATIC_CAST(nsIContent*, mChildren->ElementAt(aIndex))) );
NS_IF_ADDREF(aResult = content);
return NS_OK;
}
@ -2251,15 +2252,72 @@ nsDocument::GetBindingParent(nsIDOMNode* aNode, nsIDOMElement** aResult)
return NS_OK;
}
static nsresult
GetElementByAttribute(nsIContent* aContent,
nsIAtom* aAttrName,
const nsAReadableString& aAttrValue,
PRBool aUniversalMatch,
nsIDOMElement** aResult)
{
nsAutoString value;
nsresult rv = aContent->GetAttribute(kNameSpaceID_None, aAttrName, value);
if (rv == NS_CONTENT_ATTR_HAS_VALUE) {
if (aUniversalMatch || value.Equals(aAttrValue))
return aContent->QueryInterface(NS_GET_IID(nsIDOMElement), (void**)aResult);
}
PRInt32 childCount;
aContent->ChildCount(childCount);
for (PRInt32 i = 0; i < childCount; ++i) {
nsCOMPtr<nsIContent> current;
current->ChildAt(i, *getter_AddRefs(current));
GetElementByAttribute(current, aAttrName, aAttrValue, aUniversalMatch, aResult);
if (*aResult)
return NS_OK;
}
return NS_OK;
}
NS_IMETHODIMP
nsDocument::GetAnonymousElementByAttribute(const nsAReadableString& aAttrName,
nsDocument::GetAnonymousElementByAttribute(nsIDOMElement* aElement,
const nsAReadableString& aAttrName,
const nsAReadableString& aAttrValue,
nsIDOMElement** aResult)
{
*aResult = nsnull;
nsCOMPtr<nsIDOMNodeList> nodeList;
GetAnonymousNodes(aElement, getter_AddRefs(nodeList));
if (!nodeList)
return NS_OK;
nsCOMPtr<nsIAtom> attribute = getter_AddRefs(NS_NewAtom(aAttrName));
PRUint32 length;
nodeList->GetLength(&length);
PRBool universalMatch = aAttrValue.Equals(NS_LITERAL_STRING("*"));
for (PRUint32 i = 0; i < length; ++i) {
nsCOMPtr<nsIDOMNode> current;
nodeList->Item(i, getter_AddRefs(current));
nsCOMPtr<nsIContent> content(do_QueryInterface(current));
GetElementByAttribute(content, attribute, aAttrValue, universalMatch, aResult);
if (*aResult)
return NS_OK;
}
return NS_OK;
}
NS_IMETHODIMP
nsDocument::GetAnonymousNodes(nsIDOMElement* aElement,
nsIDOMNodeList** aResult)
@ -2274,8 +2332,7 @@ nsDocument::GetAnonymousNodes(nsIDOMElement* aElement,
PRBool dummy;
nsCOMPtr<nsIContent> dummyElt;
nsCOMPtr<nsIContent> content(do_QueryInterface(aElement));
return xblService->GetContentList(content, aResult,
getter_AddRefs(dummyElt), &dummy);
return xblService->GetContentList(content, aResult, getter_AddRefs(dummyElt), &dummy);
}
NS_IMETHODIMP

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

@ -2130,6 +2130,7 @@ nsXULDocument::AddContentModelBuilder(nsIRDFContentModelBuilder* aBuilder)
return mBuilders->AppendElement(aBuilder) ? NS_OK : NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsXULDocument::AddForwardReference(nsForwardReference* aRef)
{
@ -2818,12 +2819,70 @@ nsXULDocument::GetBindingParent(nsIDOMNode* aNode, nsIDOMElement** aResult)
return NS_OK;
}
static nsresult
GetElementByAttribute(nsIContent* aContent,
nsIAtom* aAttrName,
const nsAReadableString& aAttrValue,
PRBool aUniversalMatch,
nsIDOMElement** aResult)
{
nsAutoString value;
nsresult rv = aContent->GetAttribute(kNameSpaceID_None, aAttrName, value);
if (rv == NS_CONTENT_ATTR_HAS_VALUE) {
if (aUniversalMatch || value.Equals(aAttrValue))
return aContent->QueryInterface(NS_GET_IID(nsIDOMElement), (void**)aResult);
}
PRInt32 childCount;
aContent->ChildCount(childCount);
for (PRInt32 i = 0; i < childCount; ++i) {
nsCOMPtr<nsIContent> current;
current->ChildAt(i, *getter_AddRefs(current));
GetElementByAttribute(current, aAttrName, aAttrValue, aUniversalMatch, aResult);
if (*aResult)
return NS_OK;
}
return NS_OK;
}
NS_IMETHODIMP
nsXULDocument::GetAnonymousElementByAttribute(const nsAReadableString& aAttrName,
nsXULDocument::GetAnonymousElementByAttribute(nsIDOMElement* aElement,
const nsAReadableString& aAttrName,
const nsAReadableString& aAttrValue,
nsIDOMElement** aResult)
{
*aResult = nsnull;
nsCOMPtr<nsIDOMNodeList> nodeList;
GetAnonymousNodes(aElement, getter_AddRefs(nodeList));
if (!nodeList)
return NS_OK;
nsCOMPtr<nsIAtom> attribute = getter_AddRefs(NS_NewAtom(aAttrName));
PRUint32 length;
nodeList->GetLength(&length);
PRBool universalMatch = aAttrValue.Equals(NS_LITERAL_STRING("*"));
for (PRUint32 i = 0; i < length; ++i) {
nsCOMPtr<nsIDOMNode> current;
nodeList->Item(i, getter_AddRefs(current));
nsCOMPtr<nsIContent> content(do_QueryInterface(current));
GetElementByAttribute(content, attribute, aAttrValue, universalMatch, aResult);
if (*aResult)
return NS_OK;
}
return NS_OK;
}
@ -2841,8 +2900,7 @@ nsXULDocument::GetAnonymousNodes(nsIDOMElement* aElement,
PRBool dummy;
nsCOMPtr<nsIContent> dummyElt;
nsCOMPtr<nsIContent> content(do_QueryInterface(aElement));
return xblService->GetContentList(content, aResult,
getter_AddRefs(dummyElt), &dummy);
return xblService->GetContentList(content, aResult, getter_AddRefs(dummyElt), &dummy);
}
NS_IMETHODIMP

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

@ -43,7 +43,7 @@ public:
NS_IMETHOD GetAnonymousNodes(nsIDOMElement* aElt, nsIDOMNodeList** aReturn)=0;
NS_IMETHOD GetAnonymousElementByAttribute(const nsAReadableString& aAttrName, const nsAReadableString& aAttrValue, nsIDOMElement** aReturn)=0;
NS_IMETHOD GetAnonymousElementByAttribute(nsIDOMElement* aElt, const nsAReadableString& aAttrName, const nsAReadableString& aAttrValue, nsIDOMElement** aReturn)=0;
NS_IMETHOD AddBinding(nsIDOMElement* aElt, const nsAReadableString& aBindingURL)=0;
@ -57,7 +57,7 @@ public:
#define NS_DECL_IDOMDOCUMENTXBL \
NS_IMETHOD GetAnonymousNodes(nsIDOMElement* aElt, nsIDOMNodeList** aReturn); \
NS_IMETHOD GetAnonymousElementByAttribute(const nsAReadableString& aAttrName, const nsAReadableString& aAttrValue, nsIDOMElement** aReturn); \
NS_IMETHOD GetAnonymousElementByAttribute(nsIDOMElement* aElt, const nsAReadableString& aAttrName, const nsAReadableString& aAttrValue, nsIDOMElement** aReturn); \
NS_IMETHOD AddBinding(nsIDOMElement* aElt, const nsAReadableString& aBindingURL); \
NS_IMETHOD RemoveBinding(nsIDOMElement* aElt, const nsAReadableString& aBindingURL); \
NS_IMETHOD GetBindingParent(nsIDOMNode* aNode, nsIDOMElement** aReturn); \
@ -67,7 +67,7 @@ public:
#define NS_FORWARD_IDOMDOCUMENTXBL(_to) \
NS_IMETHOD GetAnonymousNodes(nsIDOMElement* aElt, nsIDOMNodeList** aReturn) { return _to GetAnonymousNodes(aElt, aReturn); } \
NS_IMETHOD GetAnonymousElementByAttribute(const nsAReadableString& aAttrName, const nsAReadableString& aAttrValue, nsIDOMElement** aReturn) { return _to GetAnonymousElementByAttribute(aAttrName, aAttrValue, aReturn); } \
NS_IMETHOD GetAnonymousElementByAttribute(nsIDOMElement* aElt, const nsAReadableString& aAttrName, const nsAReadableString& aAttrValue, nsIDOMElement** aReturn) { return _to GetAnonymousElementByAttribute(aElt, aAttrName, aAttrValue, aReturn); } \
NS_IMETHOD AddBinding(nsIDOMElement* aElt, const nsAReadableString& aBindingURL) { return _to AddBinding(aElt, aBindingURL); } \
NS_IMETHOD RemoveBinding(nsIDOMElement* aElt, const nsAReadableString& aBindingURL) { return _to RemoveBinding(aElt, aBindingURL); } \
NS_IMETHOD GetBindingParent(nsIDOMNode* aNode, nsIDOMElement** aReturn) { return _to GetBindingParent(aNode, aReturn); } \

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

@ -77,7 +77,7 @@ interface DocumentXBL {
{ 0x9f, 0x2c, 0xc1, 0x8d, 0xeb, 0x62, 0xee, 0x8b } } */
NodeList getAnonymousNodes(in Element elt);
Element getAnonymousElementByAttribute(in DOMString attrName, in DOMString attrValue);
Element getAnonymousElementByAttribute(in Element elt, in DOMString attrName, in DOMString attrValue);
void addBinding(in Element elt, in DOMString bindingURL);
void removeBinding(in Element elt, in DOMString bindingURL);

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

@ -1142,8 +1142,9 @@ DocumentXBLGetAnonymousElementByAttribute(JSContext *cx, JSObject *obj, uintN ar
}
nsIDOMElement* nativeRet;
nsAutoString b0;
nsCOMPtr<nsIDOMElement> b0;
nsAutoString b1;
nsAutoString b2;
// If there's no private data, this must be the prototype, so ignore
if (!nativeThis) {
return JS_TRUE;
@ -1158,14 +1159,21 @@ DocumentXBLGetAnonymousElementByAttribute(JSContext *cx, JSObject *obj, uintN ar
if (NS_FAILED(result)) {
return nsJSUtils::nsReportError(cx, obj, result);
}
if (argc < 2) {
if (argc < 3) {
return nsJSUtils::nsReportError(cx, obj, NS_ERROR_DOM_TOO_FEW_PARAMETERS_ERR);
}
nsJSUtils::nsConvertJSValToString(b0, cx, argv[0]);
if (JS_FALSE == nsJSUtils::nsConvertJSValToObject((nsISupports **)(void**)getter_AddRefs(b0),
kIElementIID,
NS_ConvertASCIItoUCS2("Element"),
cx,
argv[0])) {
return nsJSUtils::nsReportError(cx, obj, NS_ERROR_DOM_NOT_OBJECT_ERR);
}
nsJSUtils::nsConvertJSValToString(b1, cx, argv[1]);
nsJSUtils::nsConvertJSValToString(b2, cx, argv[2]);
result = nativeThis->GetAnonymousElementByAttribute(b0, b1, &nativeRet);
result = nativeThis->GetAnonymousElementByAttribute(b0, b1, b2, &nativeRet);
if (NS_FAILED(result)) {
return nsJSUtils::nsReportError(cx, obj, result);
}
@ -1625,7 +1633,7 @@ static JSFunctionSpec DocumentMethods[] =
{"getOverrideStyle", DocumentCSSGetOverrideStyle, 2},
{"createEvent", DocumentEventCreateEvent, 1},
{"getAnonymousNodes", DocumentXBLGetAnonymousNodes, 1},
{"getAnonymousElementByAttribute", DocumentXBLGetAnonymousElementByAttribute, 2},
{"getAnonymousElementByAttribute", DocumentXBLGetAnonymousElementByAttribute, 3},
{"addBinding", DocumentXBLAddBinding, 2},
{"removeBinding", DocumentXBLRemoveBinding, 2},
{"getBindingParent", DocumentXBLGetBindingParent, 1},

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

@ -807,6 +807,7 @@ nsresult nsDocument::Init()
if (mNameSpaceManager) {
return NS_ERROR_ALREADY_INITIALIZED;
}
nsresult rv;
rv = NS_NewISupportsArray(getter_AddRefs(mChildren));
@ -1285,6 +1286,7 @@ void nsDocument::SetRootContent(nsIContent* aRoot)
} else if (aRoot) {
mChildren->AppendElement(aRoot);
}
mRootContent = aRoot;
}
@ -1293,7 +1295,6 @@ nsDocument::ChildAt(PRInt32 aIndex, nsIContent*& aResult) const
{
nsCOMPtr<nsIContent> content( dont_AddRef(NS_STATIC_CAST(nsIContent*, mChildren->ElementAt(aIndex))) );
NS_IF_ADDREF(aResult = content);
return NS_OK;
}
@ -2251,15 +2252,72 @@ nsDocument::GetBindingParent(nsIDOMNode* aNode, nsIDOMElement** aResult)
return NS_OK;
}
static nsresult
GetElementByAttribute(nsIContent* aContent,
nsIAtom* aAttrName,
const nsAReadableString& aAttrValue,
PRBool aUniversalMatch,
nsIDOMElement** aResult)
{
nsAutoString value;
nsresult rv = aContent->GetAttribute(kNameSpaceID_None, aAttrName, value);
if (rv == NS_CONTENT_ATTR_HAS_VALUE) {
if (aUniversalMatch || value.Equals(aAttrValue))
return aContent->QueryInterface(NS_GET_IID(nsIDOMElement), (void**)aResult);
}
PRInt32 childCount;
aContent->ChildCount(childCount);
for (PRInt32 i = 0; i < childCount; ++i) {
nsCOMPtr<nsIContent> current;
current->ChildAt(i, *getter_AddRefs(current));
GetElementByAttribute(current, aAttrName, aAttrValue, aUniversalMatch, aResult);
if (*aResult)
return NS_OK;
}
return NS_OK;
}
NS_IMETHODIMP
nsDocument::GetAnonymousElementByAttribute(const nsAReadableString& aAttrName,
nsDocument::GetAnonymousElementByAttribute(nsIDOMElement* aElement,
const nsAReadableString& aAttrName,
const nsAReadableString& aAttrValue,
nsIDOMElement** aResult)
{
*aResult = nsnull;
nsCOMPtr<nsIDOMNodeList> nodeList;
GetAnonymousNodes(aElement, getter_AddRefs(nodeList));
if (!nodeList)
return NS_OK;
nsCOMPtr<nsIAtom> attribute = getter_AddRefs(NS_NewAtom(aAttrName));
PRUint32 length;
nodeList->GetLength(&length);
PRBool universalMatch = aAttrValue.Equals(NS_LITERAL_STRING("*"));
for (PRUint32 i = 0; i < length; ++i) {
nsCOMPtr<nsIDOMNode> current;
nodeList->Item(i, getter_AddRefs(current));
nsCOMPtr<nsIContent> content(do_QueryInterface(current));
GetElementByAttribute(content, attribute, aAttrValue, universalMatch, aResult);
if (*aResult)
return NS_OK;
}
return NS_OK;
}
NS_IMETHODIMP
nsDocument::GetAnonymousNodes(nsIDOMElement* aElement,
nsIDOMNodeList** aResult)
@ -2274,8 +2332,7 @@ nsDocument::GetAnonymousNodes(nsIDOMElement* aElement,
PRBool dummy;
nsCOMPtr<nsIContent> dummyElt;
nsCOMPtr<nsIContent> content(do_QueryInterface(aElement));
return xblService->GetContentList(content, aResult,
getter_AddRefs(dummyElt), &dummy);
return xblService->GetContentList(content, aResult, getter_AddRefs(dummyElt), &dummy);
}
NS_IMETHODIMP

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

@ -2130,6 +2130,7 @@ nsXULDocument::AddContentModelBuilder(nsIRDFContentModelBuilder* aBuilder)
return mBuilders->AppendElement(aBuilder) ? NS_OK : NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsXULDocument::AddForwardReference(nsForwardReference* aRef)
{
@ -2818,12 +2819,70 @@ nsXULDocument::GetBindingParent(nsIDOMNode* aNode, nsIDOMElement** aResult)
return NS_OK;
}
static nsresult
GetElementByAttribute(nsIContent* aContent,
nsIAtom* aAttrName,
const nsAReadableString& aAttrValue,
PRBool aUniversalMatch,
nsIDOMElement** aResult)
{
nsAutoString value;
nsresult rv = aContent->GetAttribute(kNameSpaceID_None, aAttrName, value);
if (rv == NS_CONTENT_ATTR_HAS_VALUE) {
if (aUniversalMatch || value.Equals(aAttrValue))
return aContent->QueryInterface(NS_GET_IID(nsIDOMElement), (void**)aResult);
}
PRInt32 childCount;
aContent->ChildCount(childCount);
for (PRInt32 i = 0; i < childCount; ++i) {
nsCOMPtr<nsIContent> current;
current->ChildAt(i, *getter_AddRefs(current));
GetElementByAttribute(current, aAttrName, aAttrValue, aUniversalMatch, aResult);
if (*aResult)
return NS_OK;
}
return NS_OK;
}
NS_IMETHODIMP
nsXULDocument::GetAnonymousElementByAttribute(const nsAReadableString& aAttrName,
nsXULDocument::GetAnonymousElementByAttribute(nsIDOMElement* aElement,
const nsAReadableString& aAttrName,
const nsAReadableString& aAttrValue,
nsIDOMElement** aResult)
{
*aResult = nsnull;
nsCOMPtr<nsIDOMNodeList> nodeList;
GetAnonymousNodes(aElement, getter_AddRefs(nodeList));
if (!nodeList)
return NS_OK;
nsCOMPtr<nsIAtom> attribute = getter_AddRefs(NS_NewAtom(aAttrName));
PRUint32 length;
nodeList->GetLength(&length);
PRBool universalMatch = aAttrValue.Equals(NS_LITERAL_STRING("*"));
for (PRUint32 i = 0; i < length; ++i) {
nsCOMPtr<nsIDOMNode> current;
nodeList->Item(i, getter_AddRefs(current));
nsCOMPtr<nsIContent> content(do_QueryInterface(current));
GetElementByAttribute(content, attribute, aAttrValue, universalMatch, aResult);
if (*aResult)
return NS_OK;
}
return NS_OK;
}
@ -2841,8 +2900,7 @@ nsXULDocument::GetAnonymousNodes(nsIDOMElement* aElement,
PRBool dummy;
nsCOMPtr<nsIContent> dummyElt;
nsCOMPtr<nsIContent> content(do_QueryInterface(aElement));
return xblService->GetContentList(content, aResult,
getter_AddRefs(dummyElt), &dummy);
return xblService->GetContentList(content, aResult, getter_AddRefs(dummyElt), &dummy);
}
NS_IMETHODIMP