static const DirTable dirAttributes[] = { {"ltr", IBMBIDI_TEXTDIRECTION_LTR}, {"rtl", IBMBIDI_TEXTDIRECTION_RTL}, {0} };   NS_IMETHODIMP nsDocument::GetDir(nsAString& aDirection) { nsCOMPtr<nsIPresShell> shell = (nsIPresShell*)mPresShells.SafeElementAt(0); if (shell) { nsCOMPtr<nsPresContext> context; shell->GetPresContext(getter_AddRefs(context)); if (context) { PRUint32 options = context->GetBidi(); for (const DirTable* elt = dirAttributes; elt->mName; elt++) { if (GET_BIDI_OPTION_DIRECTION(options) == elt->mValue) { CopyASCIItoUTF16(elt->mName, aDirection); break; } } } }  return NS_OK; }   NS_IMETHODIMP nsDocument::SetDir(const nsAString& aDirection) { nsIPresShell *shell = NS_STATIC_CAST(nsIPresShell *, mPresShells.SafeElementAt(0));  if (!shell) { return NS_OK; }  nsCOMPtr<nsPresContext> context; shell->GetPresContext(getter_AddRefs(context)); NS_ENSURE_TRUE(context, NS_ERROR_UNEXPECTED);  PRUint32 options = context->GetBidi();  for (const DirTable* elt = dirAttributes; elt->mName; elt++) { if (aDirection == NS_ConvertASCIItoUCS2(elt->mName)) { if (GET_BIDI_OPTION_DIRECTION(options) != elt->mValue) { SET_BIDI_OPTION_DIRECTION(options, elt->mValue); context->SetBidi(options, PR_TRUE); }  break; } }  return NS_OK; }      NS_IMETHODIMP nsDocument::GetNodeName(nsAString& aNodeName) { aNodeName.AssignLiteral("#document");  return NS_OK; }  NS_IMETHODIMP nsDocument::GetNodeValue(nsAString& aNodeValue) { SetDOMStringToNull(aNodeValue);  return NS_OK; }  NS_IMETHODIMP nsDocument::SetNodeValue(const nsAString& aNodeValue) {   return NS_OK; }  NS_IMETHODIMP nsDocument::GetNodeType(PRUint16* aNodeType) { *aNodeType = nsIDOMNode::DOCUMENT_NODE;  return NS_OK; }  NS_IMETHODIMP nsDocument::GetParentNode(nsIDOMNode** aParentNode) { *aParentNode = nsnull;  return NS_OK; }  NS_IMETHODIMP nsDocument::GetChildNodes(nsIDOMNodeList** aChildNodes) { if (!mChildNodes) { mChildNodes = new nsDocumentChildNodes(this); if (!mChildNodes) { return NS_ERROR_OUT_OF_MEMORY; } }  return CallQueryInterface(mChildNodes.get(), aChildNodes); }  NS_IMETHODIMP nsDocument::HasChildNodes(PRBool* aHasChildNodes) { NS_ENSURE_ARG(aHasChildNodes);  *aHasChildNodes = (mChildren.Count() != 0);  return NS_OK; }  NS_IMETHODIMP nsDocument::HasAttributes(PRBool* aHasAttributes) { NS_ENSURE_ARG(aHasAttributes);  *aHasAttributes = PR_FALSE;  return NS_OK; }  NS_IMETHODIMP nsDocument::GetFirstChild(nsIDOMNode** aFirstChild) { if (mChildren.Count()) { return CallQueryInterface(mChildren[0], aFirstChild); }  *aFirstChild = nsnull;  return NS_OK; }  NS_IMETHODIMP nsDocument::GetLastChild(nsIDOMNode** aLastChild) { PRInt32 count = mChildren.Count(); if (count) { return CallQueryInterface(mChildren[count-1], aLastChild); }  *aLastChild = nsnull;  return NS_OK; }  NS_IMETHODIMP nsDocument::GetPreviousSibling(nsIDOMNode** aPreviousSibling) { *aPreviousSibling = nsnull;  return NS_OK; }  NS_IMETHODIMP nsDocument::GetNextSibling(nsIDOMNode** aNextSibling) { *aNextSibling = nsnull;  return NS_OK; }  NS_IMETHODIMP nsDocument::GetAttributes(nsIDOMNamedNodeMap** aAttributes) { *aAttributes = nsnull;  return NS_OK; }  NS_IMETHODIMP nsDocument::GetNamespaceURI(nsAString& aNamespaceURI) { SetDOMStringToNull(aNamespaceURI);  return NS_OK; }  NS_IMETHODIMP nsDocument::GetPrefix(nsAString& aPrefix) { SetDOMStringToNull(aPrefix);  return NS_OK; }  NS_IMETHODIMP nsDocument::SetPrefix(const nsAString& aPrefix) { return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR; }  NS_IMETHODIMP nsDocument::GetLocalName(nsAString& aLocalName) { SetDOMStringToNull(aLocalName);  return NS_OK; }  nsresult nsDocument::IsAllowedAsChild(PRUint16 aNodeType, nsIContent* aRefContent) { if (aNodeType != nsIDOMNode::COMMENT_NODE && aNodeType != nsIDOMNode::ELEMENT_NODE && aNodeType != nsIDOMNode::PROCESSING_INSTRUCTION_NODE && aNodeType != nsIDOMNode::DOCUMENT_TYPE_NODE) { return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR; }  if (aNodeType == nsIDOMNode::ELEMENT_NODE && mRootContent && mRootContent != aRefContent) {   return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR; }  if (aNodeType == nsIDOMNode::DOCUMENT_TYPE_NODE) { nsCOMPtr<nsIDOMDocumentType> docType; GetDoctype(getter_AddRefs(docType));  nsCOMPtr<nsIContent> docTypeContent = do_QueryInterface(docType); if (docTypeContent && docTypeContent != aRefContent) {   return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR; } }  return NS_OK; }  NS_IMETHODIMP nsDocument::InsertBefore(nsIDOMNode* aNewChild, nsIDOMNode* aRefChild, nsIDOMNode** aReturn) { *aReturn = nsnull;  NS_ENSURE_ARG(aNewChild);  nsresult rv = nsContentUtils::CheckSameOrigin(this, aNewChild); if (NS_FAILED(rv)) { return rv; }  PRUint16 nodeType; aNewChild->GetNodeType(&nodeType);  rv = IsAllowedAsChild(nodeType, nsnull); if (NS_FAILED(rv)) { return rv; }  nsCOMPtr<nsIContent> content = do_QueryInterface(aNewChild); if (!content) { return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR; }  PRInt32 indx; if (!aRefChild) { if (nodeType == nsIDOMNode::DOCUMENT_TYPE_NODE && mRootContent) {  return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR; }  indx = mChildren.Count(); mChildren.AppendObject(content); } else { nsCOMPtr<nsIContent> refContent(do_QueryInterface(aRefChild)); if (!refContent) { return NS_ERROR_DOM_NOT_FOUND_ERR; }  indx = mChildren.IndexOf(refContent); if (indx == -1) {  return NS_ERROR_DOM_NOT_FOUND_ERR; }  if (nodeType == nsIDOMNode::DOCUMENT_TYPE_NODE && mRootContent && indx > mChildren.IndexOf(mRootContent)) {  return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR; }  mChildren.InsertObjectAt(content, indx); }    if (nodeType == nsIDOMNode::ELEMENT_NODE) { mRootContent = content; }  content->SetDocument(this, PR_TRUE, PR_TRUE); ContentInserted(nsnull, content, indx);  NS_ADDREF(*aReturn = aNewChild);  return NS_OK; }  NS_IMETHODIMP nsDocument::ReplaceChild(nsIDOMNode* aNewChild, nsIDOMNode* aOldChild, nsIDOMNode** aReturn) { *aReturn = nsnull;  NS_ENSURE_ARG(aNewChild && aOldChild);  nsCOMPtr<nsIContent> refContent(do_QueryInterface(aOldChild)); if (!refContent) { return NS_ERROR_DOM_NOT_FOUND_ERR; }  nsCOMPtr<nsIContent> content = do_QueryInterface(aNewChild); if (!content) { return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR; }  nsresult rv = nsContentUtils::CheckSameOrigin(this, aNewChild); if (NS_FAILED(rv)) { return rv; }  PRUint16 nodeType; aNewChild->GetNodeType(&nodeType);  rv = IsAllowedAsChild(nodeType, refContent); if (NS_FAILED(rv)) { return rv; }  PRInt32 indx = mChildren.IndexOf(refContent); if (indx == -1) {  return NS_ERROR_DOM_NOT_FOUND_ERR; }  if (nodeType == nsIDOMNode::DOCUMENT_TYPE_NODE && mRootContent && indx > mChildren.IndexOf(mRootContent)) {  return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR; }  ContentRemoved(nsnull, refContent, indx); refContent->SetDocument(nsnull, PR_TRUE, PR_TRUE);  mChildren.ReplaceObjectAt(content, indx);  if (nodeType == nsIDOMNode::ELEMENT_NODE) { mRootContent = content; }  content->SetDocument(this, PR_TRUE, PR_TRUE); ContentInserted(nsnull, content, indx);  NS_ADDREF(*aReturn = aNewChild);  return rv; }  NS_IMETHODIMP nsDocument::RemoveChild(nsIDOMNode* aOldChild, nsIDOMNode** aReturn) { *aReturn = nsnull;  NS_ENSURE_TRUE(aOldChild, NS_ERROR_NULL_POINTER);  nsCOMPtr<nsIContent> content(do_QueryInterface(aOldChild)); if (!content) { return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR; }  PRInt32 indx = mChildren.IndexOf(content); if (indx == -1) { return NS_ERROR_DOM_NOT_FOUND_ERR; }  ContentRemoved(nsnull, content, indx);  mChildren.RemoveObjectAt(indx); if (content == mRootContent) mRootContent = nsnull;  content->SetDocument(nsnull, PR_TRUE, PR_TRUE);  *aReturn = aOldChild; NS_ADDREF(aOldChild);  return NS_OK; }  NS_IMETHODIMP nsDocument::AppendChild(nsIDOMNode* aNewChild, nsIDOMNode** aReturn) { return InsertBefore(aNewChild, nsnull, aReturn); }  NS_IMETHODIMP nsDocument::CloneNode(PRBool aDeep, nsIDOMNode** aReturn) {  *aReturn = nsnull;  return NS_OK; }  NS_IMETHODIMP nsDocument::Normalize() { PRInt32 count = mChildren.Count(); for (PRInt32 i = 0; i < count; ++i) { nsCOMPtr<nsIDOMNode> node(do_QueryInterface(mChildren[i]));  if (node) { node->Normalize(); } }  return NS_OK; }  NS_IMETHODIMP nsDocument::IsSupported(const nsAString& aFeature, const nsAString& aVersion, PRBool* aReturn) { return nsGenericElement::InternalIsSupported(aFeature, aVersion, aReturn); }  NS_IMETHODIMP nsDocument::GetBaseURI(nsAString &aURI) { nsCAutoString spec; if (mDocumentBaseURI) { mDocumentBaseURI->GetSpec(spec); }  CopyUTF8toUTF16(spec, aURI);  return NS_OK; }  NS_IMETHODIMP nsDocument::GetTextContent(nsAString &aTextContent) { SetDOMStringToNull(aTextContent);  return NS_OK; }  NS_IMETHODIMP nsDocument::SetTextContent(const nsAString& aTextContent) { return NS_OK; }   NS_IMETHODIMP nsDocument::CompareDocumentPosition(nsIDOMNode* aOther, PRUint16* aReturn) { NS_ENSURE_ARG_POINTER(aOther); NS_PRECONDITION(aReturn, "Must have an out parameter");  if (this == aOther) {   *aReturn = 0;  return NS_OK; }  PRUint16 mask = 0;  nsCOMPtr<nsIContent> otherContent(do_QueryInterface(aOther)); if (!otherContent) { PRUint16 otherNodeType = 0; aOther->GetNodeType(&otherNodeType); NS_ASSERTION(otherNodeType == nsIDOMNode::DOCUMENT_NODE || otherNodeType == nsIDOMNode::ATTRIBUTE_NODE, "Hmm, this really _should_ support nsIContent..."); if (otherNodeType == nsIDOMNode::ATTRIBUTE_NODE) { nsCOMPtr<nsIDOMAttr> otherAttr(do_QueryInterface(aOther)); NS_ASSERTION(otherAttr, "Attributes really should be supporting " "nsIDOMAttr you know...");  nsCOMPtr<nsIDOMElement> otherOwnerEl; otherAttr->GetOwnerElement(getter_AddRefs(otherOwnerEl)); if (otherOwnerEl) {     return CompareDocumentPosition(otherOwnerEl, aReturn); } }      mask |= (nsIDOM3Node::DOCUMENT_POSITION_DISCONNECTED | nsIDOM3Node::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC);  *aReturn = mask; return NS_OK; }  if (this == otherContent->GetDocument()) {   mask |= (nsIDOM3Node::DOCUMENT_POSITION_CONTAINED_BY | nsIDOM3Node::DOCUMENT_POSITION_FOLLOWING); } else {     mask |= (nsIDOM3Node::DOCUMENT_POSITION_DISCONNECTED | nsIDOM3Node::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC); }  *aReturn = mask;  return NS_OK; }  NS_IMETHODIMP nsDocument::IsSameNode(nsIDOMNode* aOther, PRBool* aReturn) { PRBool sameNode = PR_FALSE;  if (this == aOther) { sameNode = PR_TRUE; }  *aReturn = sameNode;  return NS_OK; }  NS_IMETHODIMP nsDocument::IsEqualNode(nsIDOMNode* aOther, PRBool* aReturn) { NS_NOTYETIMPLEMENTED("nsDocument::IsEqualNode()");  return NS_ERROR_NOT_IMPLEMENTED; }  NS_IMETHODIMP nsDocument::IsDefaultNamespace(const nsAString& aNamespaceURI, PRBool* aReturn) { NS_NOTYETIMPLEMENTED("nsDocument::IsDefaultNamespace()");  return NS_ERROR_NOT_IMPLEMENTED; }  NS_IMETHODIMP nsDocument::GetFeature(const nsAString& aFeature, const nsAString& aVersion, nsISupports** aReturn) { NS_NOTYETIMPLEMENTED("nsDocument::GetFeature()");  return NS_ERROR_NOT_IMPLEMENTED; }  NS_IMETHODIMP nsDocument::SetUserData(const nsAString& aKey, nsIVariant* aData, nsIDOMUserDataHandler* aHandler, nsIVariant** aReturn) { NS_NOTYETIMPLEMENTED("nsDocument::SetUserData()");  return NS_ERROR_NOT_IMPLEMENTED; }  NS_IMETHODIMP nsDocument::GetUserData(const nsAString& aKey, nsIVariant** aReturn) { NS_NOTYETIMPLEMENTED("nsDocument::GetUserData()");  return NS_ERROR_NOT_IMPLEMENTED; }   NS_IMETHODIMP nsDocument::LookupPrefix(const nsAString& aNamespaceURI, nsAString& aPrefix) { aPrefix.Truncate();  return NS_OK; }  NS_IMETHODIMP nsDocument::LookupNamespaceURI(const nsAString& aNamespacePrefix, nsAString& aNamespaceURI) { aNamespaceURI.Truncate();  return NS_OK; }  NS_IMETHODIMP nsDocument::GetActualEncoding(nsAString& aActualEncoding) { return GetCharacterSet(aActualEncoding); }  NS_IMETHODIMP nsDocument::GetXmlEncoding(nsAString& aXmlEncoding) { if (mXMLDeclarationBits & XML_DECLARATION_BITS_DECLARATION_EXISTS && mXMLDeclarationBits & XML_DECLARATION_BITS_ENCODING_EXISTS) {   GetActualEncoding(aXmlEncoding); } else { SetDOMStringToNull(aXmlEncoding); }  return NS_OK; }  NS_IMETHODIMP nsDocument::GetXmlStandalone(PRBool *aXmlStandalone) { *aXmlStandalone = mXMLDeclarationBits & XML_DECLARATION_BITS_DECLARATION_EXISTS && mXMLDeclarationBits & XML_DECLARATION_BITS_STANDALONE_EXISTS && mXMLDeclarationBits & XML_DECLARATION_BITS_STANDALONE_YES;  return NS_OK; }  NS_IMETHODIMP nsDocument::SetXmlStandalone(PRBool aXmlStandalone) { return NS_ERROR_NOT_IMPLEMENTED; }  NS_IMETHODIMP nsDocument::GetXmlVersion(nsAString& aXmlVersion) {    aXmlVersion.AssignLiteral("1.0");  return NS_OK; }  NS_IMETHODIMP nsDocument::SetXmlVersion(const nsAString& aXmlVersion) { return NS_ERROR_NOT_IMPLEMENTED; }  NS_IMETHODIMP nsDocument::GetStrictErrorChecking(PRBool *aStrictErrorChecking) {  *aStrictErrorChecking = PR_TRUE; return NS_OK; }  NS_IMETHODIMP nsDocument::SetStrictErrorChecking(PRBool aStrictErrorChecking) {  return NS_OK; }  NS_IMETHODIMP nsDocument::GetDocumentURI(nsAString& aDocumentURI) { if (mDocumentURI) { nsCAutoString uri; mDocumentURI->GetSpec(uri); CopyUTF8toUTF16(uri, aDocumentURI); } else { SetDOMStringToNull(aDocumentURI); }  return NS_OK; }  NS_IMETHODIMP nsDocument::SetDocumentURI(const nsAString& aDocumentURI) {   return NS_ERROR_NOT_IMPLEMENTED; }  NS_IMETHODIMP nsDocument::AdoptNode(nsIDOMNode *source, nsIDOMNode **aReturn) {   return NS_ERROR_NOT_IMPLEMENTED; }  NS_IMETHODIMP nsDocument::GetDomConfig(nsIDOMDOMConfiguration **aConfig) { return NS_ERROR_NOT_IMPLEMENTED; }  NS_IMETHODIMP nsDocument::NormalizeDocument() {   return Normalize(); }  NS_IMETHODIMP nsDocument::RenameNode(nsIDOMNode *aNode, const nsAString& namespaceURI, const nsAString& qualifiedName, nsIDOMNode **aReturn) { if (!aNode) {  return NS_ERROR_DOM_NOT_SUPPORTED_ERR; }  PRUint16 nodeType; aNode->GetNodeType(&nodeType); if (nodeType == nsIDOMNode::ELEMENT_NODE || nodeType == nsIDOMNode::ATTRIBUTE_NODE) {  return NS_ERROR_NOT_IMPLEMENTED; }  return NS_ERROR_DOM_NOT_SUPPORTED_ERR; }   NS_IMETHODIMP nsDocument::GetOwnerDocument(nsIDOMDocument** aOwnerDocument) { *aOwnerDocument = nsnull;  return NS_OK; }  nsresult nsDocument::GetListenerManager(nsIEventListenerManager **aInstancePtrResult) { if (mListenerManager) { *aInstancePtrResult = mListenerManager; NS_ADDREF(*aInstancePtrResult);  return NS_OK; }  nsresult rv = NS_NewEventListenerManager(getter_AddRefs(mListenerManager)); NS_ENSURE_SUCCESS(rv, rv);  mListenerManager->SetListenerTarget(NS_STATIC_CAST(nsIDocument *,this));  *aInstancePtrResult = mListenerManager; NS_ADDREF(*aInstancePtrResult);  return NS_OK; }  nsresult nsDocument::HandleEvent(nsIDOMEvent *aEvent) { PRBool noDefault;  return DispatchEvent(aEvent, &noDefault); }  NS_IMETHODIMP nsDocument::GetSystemEventGroup(nsIDOMEventGroup **aGroup) { nsCOMPtr<nsIEventListenerManager> manager; if (NS_SUCCEEDED(GetListenerManager(getter_AddRefs(manager))) && manager) { return manager->GetSystemEventGroupLM(aGroup); }  return NS_ERROR_FAILURE; }  nsresult nsDocument::HandleDOMEvent(nsPresContext* aPresContext, nsEvent* aEvent, nsIDOMEvent** aDOMEvent, PRUint32 aFlags, nsEventStatus* aEventStatus) { nsresult mRet = NS_OK; PRBool externalDOMEvent = PR_FALSE;  nsIDOMEvent* domEvent = nsnull;  if (NS_EVENT_FLAG_INIT & aFlags) { if (aDOMEvent) { if (*aDOMEvent) { externalDOMEvent = PR_TRUE; } } else { aDOMEvent = &domEvent; } aEvent->flags |= aFlags; aFlags &= ~(NS_EVENT_FLAG_CANT_BUBBLE | NS_EVENT_FLAG_CANT_CANCEL); aFlags |= NS_EVENT_FLAG_BUBBLE | NS_EVENT_FLAG_CAPTURE; }   if (NS_EVENT_FLAG_CAPTURE & aFlags && nsnull != mScriptGlobalObject) { mScriptGlobalObject->HandleDOMEvent(aPresContext, aEvent, aDOMEvent, aFlags & NS_EVENT_CAPTURE_MASK, aEventStatus); }       if (mListenerManager && !(NS_EVENT_FLAG_CANT_BUBBLE & aEvent->flags && NS_EVENT_FLAG_BUBBLE & aFlags && !(NS_EVENT_FLAG_INIT & aFlags))) { aEvent->flags |= aFlags; mListenerManager->HandleEvent(aPresContext, aEvent, aDOMEvent, this, aFlags, aEventStatus); aEvent->flags &= ~aFlags; }   if (NS_EVENT_FLAG_BUBBLE & aFlags && nsnull != mScriptGlobalObject) { mScriptGlobalObject->HandleDOMEvent(aPresContext, aEvent, aDOMEvent, aFlags & NS_EVENT_BUBBLE_MASK, aEventStatus); }  if (NS_EVENT_FLAG_INIT & aFlags) {   if (*aDOMEvent && !externalDOMEvent) { nsrefcnt rc; NS_RELEASE2(*aDOMEvent, rc); if (0 != rc) {     nsCOMPtr<nsIPrivateDOMEvent> privateEvent = do_QueryInterface(*aDOMEvent); if (privateEvent) { privateEvent->DuplicatePrivateData(); } } aDOMEvent = nsnull; } }  return mRet; }  nsresult nsDocument::AddEventListenerByIID(nsIDOMEventListener *aListener, const nsIID& aIID) { nsCOMPtr<nsIEventListenerManager> manager;  GetListenerManager(getter_AddRefs(manager)); if (manager) { manager->AddEventListenerByIID(aListener, aIID, NS_EVENT_FLAG_BUBBLE); return NS_OK; }  return NS_ERROR_FAILURE; }  nsresult nsDocument::RemoveEventListenerByIID(nsIDOMEventListener *aListener, const nsIID& aIID) { if (!mListenerManager) { return NS_ERROR_FAILURE; }  mListenerManager->RemoveEventListenerByIID(aListener, aIID, NS_EVENT_FLAG_BUBBLE); return NS_OK; }  nsresult nsDocument::AddEventListener(const nsAString& aType, nsIDOMEventListener* aListener, PRBool aUseCapture) { return AddGroupedEventListener(aType, aListener, aUseCapture, nsnull); }  nsresult nsDocument::RemoveEventListener(const nsAString& aType, nsIDOMEventListener* aListener, PRBool aUseCapture) { return RemoveGroupedEventListener(aType, aListener, aUseCapture, nsnull); }  NS_IMETHODIMP nsDocument::DispatchEvent(nsIDOMEvent* aEvent, PRBool *_retval) {  PRInt32 count = GetNumberOfShells(); if (count == 0) return NS_OK;  nsIPresShell *shell = GetShellAt(0); if (!shell) return NS_ERROR_FAILURE;   nsCOMPtr<nsPresContext> presContext; shell->GetPresContext(getter_AddRefs(presContext));  return presContext->EventStateManager()-> DispatchNewEvent(NS_STATIC_CAST(nsIDOMDocument*, this), aEvent, _retval); }  NS_IMETHODIMP nsDocument::AddGroupedEventListener(const nsAString& aType, nsIDOMEventListener *aListener, PRBool aUseCapture, nsIDOMEventGroup *aEvtGrp) { nsCOMPtr<nsIEventListenerManager> manager;  nsresult rv = GetListenerManager(getter_AddRefs(manager)); if (NS_SUCCEEDED(rv) && manager) { PRInt32 flags = aUseCapture ? NS_EVENT_FLAG_CAPTURE : NS_EVENT_FLAG_BUBBLE;  manager->AddEventListenerByType(aListener, aType, flags, aEvtGrp); return NS_OK; }  return rv; }  NS_IMETHODIMP nsDocument::RemoveGroupedEventListener(const nsAString& aType, nsIDOMEventListener *aListener, PRBool aUseCapture, nsIDOMEventGroup *aEvtGrp) { if (!mListenerManager) { return NS_ERROR_FAILURE; }  PRInt32 flags = aUseCapture ? NS_EVENT_FLAG_CAPTURE : NS_EVENT_FLAG_BUBBLE;  mListenerManager->RemoveEventListenerByType(aListener, aType, flags, aEvtGrp); return NS_OK; }  NS_IMETHODIMP nsDocument::CanTrigger(const nsAString & type, PRBool *_retval) { return NS_ERROR_NOT_IMPLEMENTED; }  NS_IMETHODIMP nsDocument::IsRegisteredHere(const nsAString & type, PRBool *_retval) { return NS_ERROR_NOT_IMPLEMENTED; }  NS_IMETHODIMP nsDocument::CreateEvent(const nsAString& aEventType, nsIDOMEvent** aReturn) { NS_ENSURE_ARG_POINTER(aReturn); *aReturn = nsnull;    nsIPresShell *shell = GetShellAt(0); nsCOMPtr<nsPresContext> presContext;  if (shell) {  shell->GetPresContext(getter_AddRefs(presContext)); }  nsCOMPtr<nsIEventListenerManager> manager; GetListenerManager(getter_AddRefs(manager)); if (manager) { return manager->CreateEvent(presContext, nsnull, aEventType, aReturn); }  return NS_ERROR_FAILURE; }  NS_IMETHODIMP nsDocument::CreateEventGroup(nsIDOMEventGroup **aInstancePtrResult) { nsresult rv; nsCOMPtr<nsIDOMEventGroup> group(do_CreateInstance(kDOMEventGroupCID, &rv)); NS_ENSURE_SUCCESS(rv, rv);  *aInstancePtrResult = group; NS_ADDREF(*aInstancePtrResult);  return NS_OK; }  void nsDocument::FlushPendingNotifications(mozFlushType aType) { if (aType == (aType & (Flush_Content | Flush_SinkNotifications)) || !mScriptGlobalObject) {  return; }      nsCOMPtr<nsIDocShellTreeItem> docShellAsItem = do_QueryInterface(mScriptGlobalObject->GetDocShell());  if (docShellAsItem) { nsCOMPtr<nsIDocShellTreeItem> docShellParent; docShellAsItem->GetSameTypeParent(getter_AddRefs(docShellParent));  nsCOMPtr<nsIDOMWindow> win(do_GetInterface(docShellParent));  if (win) { nsCOMPtr<nsIDOMDocument> dom_doc; win->GetDocument(getter_AddRefs(dom_doc));  nsCOMPtr<nsIDocument> doc(do_QueryInterface(dom_doc));  if (doc) {   doc->FlushPendingNotifications(aType); } } }  PRInt32 i, count = mPresShells.Count();  for (i = 0; i < count; i++) { nsCOMPtr<nsIPresShell> shell = NS_STATIC_CAST(nsIPresShell*, mPresShells[i]);  if (shell) { shell->FlushPendingNotifications(aType); } } }  void nsDocument::AddReference(void *aKey, nsISupports *aReference) { nsVoidKey key(aKey);  if (mScriptGlobalObject) { mContentWrapperHash.Put(&key, aReference); } }  already_AddRefed<nsISupports> nsDocument::RemoveReference(void *aKey) { nsVoidKey key(aKey);  nsISupports* oldReference; mContentWrapperHash.Remove(&key, &oldReference); return oldReference; }  nsIScriptEventManager* nsDocument::GetScriptEventManager() { if (!mScriptEventManager) { mScriptEventManager = new nsScriptEventManager(this);  }  return mScriptEventManager; }  void nsDocument::SetXMLDeclaration(const nsAString& aVersion, const nsAString& aEncoding, const nsAString& aStandalone) { if (aVersion.IsEmpty()) { mXMLDeclarationBits = 0; return; }  mXMLDeclarationBits = XML_DECLARATION_BITS_DECLARATION_EXISTS;  if (!aEncoding.IsEmpty()) { mXMLDeclarationBits |= XML_DECLARATION_BITS_ENCODING_EXISTS; }  if (aStandalone.EqualsLiteral("yes")) { mXMLDeclarationBits |= XML_DECLARATION_BITS_STANDALONE_EXISTS | XML_DECLARATION_BITS_STANDALONE_YES; } else if (aStandalone.EqualsLiteral("no")) { mXMLDeclarationBits |= XML_DECLARATION_BITS_STANDALONE_EXISTS; } }  void nsDocument::GetXMLDeclaration(nsAString& aVersion, nsAString& aEncoding, nsAString& aStandalone) { aVersion.Truncate(); aEncoding.Truncate(); aStandalone.Truncate();  if (!(mXMLDeclarationBits & XML_DECLARATION_BITS_DECLARATION_EXISTS)) { return; }   aVersion.AssignLiteral("1.0");  if (mXMLDeclarationBits & XML_DECLARATION_BITS_ENCODING_EXISTS) {   GetCharacterSet(aEncoding); }  if (mXMLDeclarationBits & XML_DECLARATION_BITS_STANDALONE_EXISTS) { if (mXMLDeclarationBits & XML_DECLARATION_BITS_STANDALONE_YES) { aStandalone.AssignLiteral("yes"); } else { aStandalone.AssignLiteral("no"); } } }  PRBool nsDocument::IsScriptEnabled() { nsCOMPtr<nsIScriptSecurityManager> sm(do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID)); NS_ENSURE_TRUE(sm, PR_TRUE);  nsIPrincipal* principal = GetPrincipal(); NS_ENSURE_TRUE(principal, PR_TRUE);  nsIScriptGlobalObject* globalObject = GetScriptGlobalObject(); NS_ENSURE_TRUE(globalObject, PR_TRUE);  nsIScriptContext *scriptContext = globalObject->GetContext(); NS_ENSURE_TRUE(scriptContext, PR_TRUE);  JSContext* cx = (JSContext *) scriptContext->GetNativeContext(); NS_ENSURE_TRUE(cx, PR_TRUE);  PRBool enabled; nsresult rv = sm->CanExecuteScripts(cx, principal, &enabled); NS_ENSURE_SUCCESS(rv, PR_TRUE); return enabled; }  nsresult nsDocument::GetRadioGroup(const nsAString& aName, nsRadioGroupStruct **aRadioGroup) { nsStringKey key(aName); nsRadioGroupStruct *radioGroup = NS_STATIC_CAST(nsRadioGroupStruct *, mRadioGroups.Get(&key));  if (!radioGroup) { radioGroup = new nsRadioGroupStruct(); NS_ENSURE_TRUE(radioGroup, NS_ERROR_OUT_OF_MEMORY); mRadioGroups.Put(&key, radioGroup); }  *aRadioGroup = radioGroup;  return NS_OK; }  NS_IMETHODIMP nsDocument::SetCurrentRadioButton(const nsAString& aName, nsIDOMHTMLInputElement* aRadio) { nsRadioGroupStruct* radioGroup = nsnull; GetRadioGroup(aName, &radioGroup); if (radioGroup) { radioGroup->mSelectedRadioButton = aRadio; }  return NS_OK; }  NS_IMETHODIMP nsDocument::GetCurrentRadioButton(const nsAString& aName, nsIDOMHTMLInputElement** aRadio) { nsRadioGroupStruct* radioGroup = nsnull; GetRadioGroup(aName, &radioGroup); if (radioGroup) { *aRadio = radioGroup->mSelectedRadioButton; NS_IF_ADDREF(*aRadio); }  return NS_OK; }  NS_IMETHODIMP nsDocument::GetNextRadioButton(const nsAString& aName, const PRBool aPrevious, nsIDOMHTMLInputElement*  aFocusedRadio, nsIDOMHTMLInputElement** aRadioOut) {     *aRadioOut = nsnull;  nsRadioGroupStruct* radioGroup = nsnull; GetRadioGroup(aName, &radioGroup); if (!radioGroup) { return NS_ERROR_FAILURE; }    nsCOMPtr<nsIDOMHTMLInputElement> currentRadio; if (aFocusedRadio) { currentRadio = aFocusedRadio; } else { currentRadio = radioGroup->mSelectedRadioButton; if (!currentRadio) { return NS_ERROR_FAILURE; } } nsCOMPtr<nsIFormControl> radioControl(do_QueryInterface(currentRadio)); PRInt32 index = radioGroup->mRadioButtons.IndexOf(radioControl); if (index < 0) { return NS_ERROR_FAILURE; }  PRInt32 numRadios = radioGroup->mRadioButtons.Count(); PRBool disabled; nsCOMPtr<nsIDOMHTMLInputElement> radio; do { if (aPrevious) { if (--index < 0) { index = numRadios -1; } } else if (++index >= numRadios) { index = 0; } radio = do_QueryInterface(NS_STATIC_CAST(nsIFormControl*, radioGroup->mRadioButtons.ElementAt(index))); NS_ASSERTION(radio, "mRadioButtons holding a non-radio button"); radio->GetDisabled(&disabled); } while (disabled && radio != currentRadio);  NS_IF_ADDREF(*aRadioOut = radio); return NS_OK; }  NS_IMETHODIMP nsDocument::AddToRadioGroup(const nsAString& aName, nsIFormControl* aRadio) { nsRadioGroupStruct* radioGroup = nsnull; GetRadioGroup(aName, &radioGroup); if (radioGroup) { radioGroup->mRadioButtons.AppendElement(aRadio); NS_IF_ADDREF(aRadio); }  return NS_OK; }  NS_IMETHODIMP nsDocument::RemoveFromRadioGroup(const nsAString& aName, nsIFormControl* aRadio) { nsRadioGroupStruct* radioGroup = nsnull; GetRadioGroup(aName, &radioGroup); if (radioGroup) { if (radioGroup->mRadioButtons.RemoveElement(aRadio)) { NS_IF_RELEASE(aRadio); } }  return NS_OK; }  NS_IMETHODIMP nsDocument::WalkRadioGroup(const nsAString& aName, nsIRadioVisitor* aVisitor) { nsRadioGroupStruct* radioGroup = nsnull; GetRadioGroup(aName, &radioGroup); if (!radioGroup) { return NS_OK; }  PRBool stop = PR_FALSE; for (int i = 0; i < radioGroup->mRadioButtons.Count(); i++) { aVisitor->Visit(NS_STATIC_CAST(nsIFormControl *, radioGroup->mRadioButtons.ElementAt(i)), &stop); if (stop) { return NS_OK; } }  return NS_OK; }  void nsDocument::RetrieveRelevantHeaders(nsIChannel *aChannel) { nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aChannel); PRTime modDate = LL_ZERO; nsresult rv;  if (httpChannel) { rv = httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("last-modified"), mLastModified);  if (NS_FAILED(rv)) { mLastModified.Truncate(); }   rv = httpChannel->GetRequestHeader(NS_LITERAL_CSTRING("referer"), mReferrer); if (NS_FAILED(rv)) { mReferrer.Truncate(); } } else { nsCOMPtr<nsIFileChannel> fileChannel = do_QueryInterface(aChannel); if (fileChannel) { nsCOMPtr<nsIFile> file; fileChannel->GetFile(getter_AddRefs(file)); if (file) { PRTime msecs; rv = file->GetLastModifiedTime(&msecs);  if (NS_SUCCEEDED(rv)) { PRInt64 intermediateValue; LL_I2L(intermediateValue, PR_USEC_PER_MSEC); LL_MUL(modDate, msecs, intermediateValue); } } } }  if (mLastModified.IsEmpty() && LL_IS_ZERO(modDate)) {    modDate = PR_Now(); }  if (LL_NE(modDate, LL_ZERO)) { PRExplodedTime prtime; char buf[100];  PR_ExplodeTime(modDate, PR_LocalTimeParameters, &prtime);     PR_FormatTime(buf, sizeof buf, #ifdef XP_WIN "%#c", #else "%c", #endif &prtime); mLastModified.Assign(buf); } }  nsresult nsDocument::CreateElem(nsIAtom *aName, nsIAtom *aPrefix, PRInt32 aNamespaceID, PRBool aDocumentDefaultType, nsIContent **aResult) { *aResult = nsnull;  PRInt32 elementType = aDocumentDefaultType ? mDefaultElementType : aNamespaceID;  nsCOMPtr<nsINodeInfo> nodeInfo; nsresult rv = mNodeInfoManager->GetNodeInfo(aName, aPrefix, aNamespaceID, getter_AddRefs(nodeInfo)); NS_ENSURE_SUCCESS(rv, rv);  return CreateElement(nodeInfo, elementType, aResult); }  nsresult nsDocument::CreateElement(nsINodeInfo *aNodeInfo, PRInt32 aElementType, nsIContent** aResult) { nsCOMPtr<nsIContent> content; nsresult rv = NS_NewElement(getter_AddRefs(content), aElementType, aNodeInfo); NS_ENSURE_SUCCESS(rv, rv);  content->SetContentID(mNextContentID++);  content.swap(*aResult);  return NS_OK; }
NS_INTERFACE_MAP_BEGIN(nsXPathDocumentTearoff)  NS_INTERFACE_MAP_ENTRY(nsIDOMXPathEvaluator)NS_INTERFACE_MAP_END_AGGREGATED(mDocument)NS_IMPL_ADDREF_USING_AGGREGATOR(nsXPathDocumentTearoff, mDocument)NS_IMPL_RELEASE_USING_AGGREGATOR(nsXPathDocumentTearoff, mDocument)class OrphansEntry : public PLDHashEntryHdr{public:  nsIContent *mKey; // must be first, to look like PLDHashEntryStub};