From b11125a802b3075cd4db27f847fb1235683b3c64 Mon Sep 17 00:00:00 2001 From: "hyatt%netscape.com" Date: Tue, 9 Mar 1999 19:30:41 +0000 Subject: [PATCH] Fixes to the broadcasters and observers. I made HTML elements work with observes nodes, and I changed some SetAttribute calls to use the DOM instead of the nsIContent APIs. --- content/xul/content/src/nsXULElement.cpp | 10 +-- content/xul/document/src/nsXULDocument.cpp | 28 +++------ rdf/content/src/nsRDFElement.cpp | 10 +-- rdf/content/src/nsRDFXULBuilder.cpp | 71 ++++++++++++++++++++++ rdf/content/src/nsXULDocument.cpp | 28 +++------ rdf/content/src/nsXULElement.cpp | 10 +-- 6 files changed, 98 insertions(+), 59 deletions(-) diff --git a/content/xul/content/src/nsXULElement.cpp b/content/xul/content/src/nsXULElement.cpp index 62c0beff61bd..a734d9e75b89 100644 --- a/content/xul/content/src/nsXULElement.cpp +++ b/content/xul/content/src/nsXULElement.cpp @@ -2184,26 +2184,22 @@ RDFElementImpl::AddBroadcastListener(const nsString& attr, nsIDOMElement* anElem // We need to sync up the initial attribute value. nsCOMPtr listener( do_QueryInterface(anElement) ); - // Retrieve our namespace - PRInt32 namespaceID; - GetNameSpaceID(namespaceID); - // Find out if the attribute is even present at all. nsString attrValue; nsIAtom* kAtom = NS_NewAtom(attr); - nsresult result = GetAttribute(namespaceID, kAtom, attrValue); + nsresult result = GetAttribute(kNameSpaceID_None, kAtom, attrValue); PRBool attrPresent = (result == NS_CONTENT_ATTR_NO_VALUE || result == NS_CONTENT_ATTR_HAS_VALUE); if (attrPresent) { // Set the attribute - listener->SetAttribute(namespaceID, kAtom, attrValue, PR_TRUE); + anElement->SetAttribute(attr, attrValue); } else { // Unset the attribute - listener->UnsetAttribute(namespaceID, kAtom, PR_FALSE); + anElement->RemoveAttribute(attr); } NS_RELEASE(kAtom); diff --git a/content/xul/document/src/nsXULDocument.cpp b/content/xul/document/src/nsXULDocument.cpp index 2a309f228af1..02768a21467b 100644 --- a/content/xul/document/src/nsXULDocument.cpp +++ b/content/xul/document/src/nsXULDocument.cpp @@ -563,8 +563,6 @@ public: protected: // pseudo constants static PRInt32 gRefCnt; - static nsIAtom* kAttributeAtom; - static nsIAtom* kElementAtom; static nsIAtom* kIdAtom; static nsIAtom* kObservesAtom; @@ -608,8 +606,6 @@ protected: }; PRInt32 XULDocumentImpl::gRefCnt; -nsIAtom* XULDocumentImpl::kAttributeAtom; -nsIAtom* XULDocumentImpl::kElementAtom; nsIAtom* XULDocumentImpl::kIdAtom; nsIAtom* XULDocumentImpl::kObservesAtom; @@ -649,8 +645,6 @@ XULDocumentImpl::XULDocumentImpl(void) } if (gRefCnt++ == 0) { - kAttributeAtom = NS_NewAtom("attribute"); - kElementAtom = NS_NewAtom("element"); kIdAtom = NS_NewAtom("id"); kObservesAtom = NS_NewAtom("observes"); } @@ -681,8 +675,6 @@ XULDocumentImpl::~XULDocumentImpl() NS_IF_RELEASE(mLineBreaker); if (--gRefCnt == 0) { - NS_IF_RELEASE(kAttributeAtom); - NS_IF_RELEASE(kElementAtom); NS_IF_RELEASE(kIdAtom); NS_IF_RELEASE(kObservesAtom); } @@ -1974,23 +1966,21 @@ XULDocumentImpl::CreateContents(nsIContent* aElement) if (!tag) break; - nsString tagName; - tag->ToString(tagName); - - if (tagName.EqualsIgnoreCase("observes")) + if (tag == kObservesAtom) { // Find the node that we're supposed to be // observing and perform the hookup. nsString elementValue; nsString attributeValue; - - childContent->GetAttribute(kNameSpaceID_None, - kElementAtom, - elementValue); - childContent->GetAttribute(kNameSpaceID_None, - kAttributeAtom, - attributeValue); + nsCOMPtr domContent; + domContent = do_QueryInterface(childContent); + + domContent->GetAttribute("element", + elementValue); + + domContent->GetAttribute("attribute", + attributeValue); nsIDOMElement* domElement = nsnull; GetElementById(elementValue, &domElement); diff --git a/rdf/content/src/nsRDFElement.cpp b/rdf/content/src/nsRDFElement.cpp index 62c0beff61bd..a734d9e75b89 100644 --- a/rdf/content/src/nsRDFElement.cpp +++ b/rdf/content/src/nsRDFElement.cpp @@ -2184,26 +2184,22 @@ RDFElementImpl::AddBroadcastListener(const nsString& attr, nsIDOMElement* anElem // We need to sync up the initial attribute value. nsCOMPtr listener( do_QueryInterface(anElement) ); - // Retrieve our namespace - PRInt32 namespaceID; - GetNameSpaceID(namespaceID); - // Find out if the attribute is even present at all. nsString attrValue; nsIAtom* kAtom = NS_NewAtom(attr); - nsresult result = GetAttribute(namespaceID, kAtom, attrValue); + nsresult result = GetAttribute(kNameSpaceID_None, kAtom, attrValue); PRBool attrPresent = (result == NS_CONTENT_ATTR_NO_VALUE || result == NS_CONTENT_ATTR_HAS_VALUE); if (attrPresent) { // Set the attribute - listener->SetAttribute(namespaceID, kAtom, attrValue, PR_TRUE); + anElement->SetAttribute(attr, attrValue); } else { // Unset the attribute - listener->UnsetAttribute(namespaceID, kAtom, PR_FALSE); + anElement->RemoveAttribute(attr); } NS_RELEASE(kAtom); diff --git a/rdf/content/src/nsRDFXULBuilder.cpp b/rdf/content/src/nsRDFXULBuilder.cpp index 2f6f072c914c..bd0e07ed0531 100644 --- a/rdf/content/src/nsRDFXULBuilder.cpp +++ b/rdf/content/src/nsRDFXULBuilder.cpp @@ -60,6 +60,8 @@ #include "nsString.h" #include "rdf.h" #include "rdfutil.h" +#include "nsIDOMXULElement.h" +#include "nsIDOMXULDocument.h" // XXX These are needed as scaffolding until we get to a more // DOM-based solution. @@ -1414,6 +1416,75 @@ RDFXULBuilderImpl::CreateHTMLElement(nsIRDFResource* aResource, return rv; } + // The observes relationship has to be hooked up here, since the children + // were already built. We'll miss out on it if we don't plug in here. + // Now that the contents have been created, perform broadcaster + // hookups if any of the children are observes nodes. + // XXX: Initial sync-up doesn't work, since no document observer exists + // yet. + PRInt32 childCount; + element->ChildCount(childCount); + for (PRInt32 j = 0; j < childCount; j++) + { + nsIContent* childContent = nsnull; + element->ChildAt(j, childContent); + + if (!childContent) + break; + + nsIAtom* tag = nsnull; + childContent->GetTag(tag); + + if (!tag) + break; + + nsString tagName; + tag->ToString(tagName); + + if (tagName == "observes") + { + // Find the node that we're supposed to be + // observing and perform the hookup. + nsString elementValue; + nsString attributeValue; + nsCOMPtr domContent; + domContent = do_QueryInterface(childContent); + + domContent->GetAttribute("element", + elementValue); + + domContent->GetAttribute("attribute", + attributeValue); + + nsIDOMElement* domElement = nsnull; + nsCOMPtr xulDoc; + xulDoc = do_QueryInterface(doc); + + if (xulDoc) + xulDoc->GetElementById(elementValue, &domElement); + + if (!domElement) + break; + + // We have a DOM element to bind to. Add a broadcast + // listener to that element, but only if it's a XUL element. + // XXX: Handle context nodes. + nsCOMPtr listener( do_QueryInterface(element) ); + nsCOMPtr broadcaster( do_QueryInterface(domElement) ); + if (listener) + { + broadcaster->AddBroadcastListener(attributeValue, + listener); + } + + NS_RELEASE(domElement); + } + + NS_RELEASE(childContent); + NS_RELEASE(tag); + } + + return NS_OK; } diff --git a/rdf/content/src/nsXULDocument.cpp b/rdf/content/src/nsXULDocument.cpp index 2a309f228af1..02768a21467b 100644 --- a/rdf/content/src/nsXULDocument.cpp +++ b/rdf/content/src/nsXULDocument.cpp @@ -563,8 +563,6 @@ public: protected: // pseudo constants static PRInt32 gRefCnt; - static nsIAtom* kAttributeAtom; - static nsIAtom* kElementAtom; static nsIAtom* kIdAtom; static nsIAtom* kObservesAtom; @@ -608,8 +606,6 @@ protected: }; PRInt32 XULDocumentImpl::gRefCnt; -nsIAtom* XULDocumentImpl::kAttributeAtom; -nsIAtom* XULDocumentImpl::kElementAtom; nsIAtom* XULDocumentImpl::kIdAtom; nsIAtom* XULDocumentImpl::kObservesAtom; @@ -649,8 +645,6 @@ XULDocumentImpl::XULDocumentImpl(void) } if (gRefCnt++ == 0) { - kAttributeAtom = NS_NewAtom("attribute"); - kElementAtom = NS_NewAtom("element"); kIdAtom = NS_NewAtom("id"); kObservesAtom = NS_NewAtom("observes"); } @@ -681,8 +675,6 @@ XULDocumentImpl::~XULDocumentImpl() NS_IF_RELEASE(mLineBreaker); if (--gRefCnt == 0) { - NS_IF_RELEASE(kAttributeAtom); - NS_IF_RELEASE(kElementAtom); NS_IF_RELEASE(kIdAtom); NS_IF_RELEASE(kObservesAtom); } @@ -1974,23 +1966,21 @@ XULDocumentImpl::CreateContents(nsIContent* aElement) if (!tag) break; - nsString tagName; - tag->ToString(tagName); - - if (tagName.EqualsIgnoreCase("observes")) + if (tag == kObservesAtom) { // Find the node that we're supposed to be // observing and perform the hookup. nsString elementValue; nsString attributeValue; - - childContent->GetAttribute(kNameSpaceID_None, - kElementAtom, - elementValue); - childContent->GetAttribute(kNameSpaceID_None, - kAttributeAtom, - attributeValue); + nsCOMPtr domContent; + domContent = do_QueryInterface(childContent); + + domContent->GetAttribute("element", + elementValue); + + domContent->GetAttribute("attribute", + attributeValue); nsIDOMElement* domElement = nsnull; GetElementById(elementValue, &domElement); diff --git a/rdf/content/src/nsXULElement.cpp b/rdf/content/src/nsXULElement.cpp index 62c0beff61bd..a734d9e75b89 100644 --- a/rdf/content/src/nsXULElement.cpp +++ b/rdf/content/src/nsXULElement.cpp @@ -2184,26 +2184,22 @@ RDFElementImpl::AddBroadcastListener(const nsString& attr, nsIDOMElement* anElem // We need to sync up the initial attribute value. nsCOMPtr listener( do_QueryInterface(anElement) ); - // Retrieve our namespace - PRInt32 namespaceID; - GetNameSpaceID(namespaceID); - // Find out if the attribute is even present at all. nsString attrValue; nsIAtom* kAtom = NS_NewAtom(attr); - nsresult result = GetAttribute(namespaceID, kAtom, attrValue); + nsresult result = GetAttribute(kNameSpaceID_None, kAtom, attrValue); PRBool attrPresent = (result == NS_CONTENT_ATTR_NO_VALUE || result == NS_CONTENT_ATTR_HAS_VALUE); if (attrPresent) { // Set the attribute - listener->SetAttribute(namespaceID, kAtom, attrValue, PR_TRUE); + anElement->SetAttribute(attr, attrValue); } else { // Unset the attribute - listener->UnsetAttribute(namespaceID, kAtom, PR_FALSE); + anElement->RemoveAttribute(attr); } NS_RELEASE(kAtom);