nsIContent* nsCoreUtils::GetRoleContent(nsINode *aNode) { nsCOMPtr<nsIContent> content(do_QueryInterface(aNode)); if (!content) { nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(aNode)); if (domDoc) { nsCOMPtr<nsIDOMHTMLDocument> htmlDoc(do_QueryInterface(aNode)); if (htmlDoc) { nsCOMPtr<nsIDOMHTMLElement> bodyElement; htmlDoc->GetBody(getter_AddRefs(bodyElement)); content = do_QueryInterface(bodyElement); } else { nsCOMPtr<nsIDOMElement> docElement; domDoc->GetDocumentElement(getter_AddRefs(docElement)); content = do_QueryInterface(docElement); } } } return content; }
nsresult nsTypeAheadFind::GetSearchContainers(nsISupports *aContainer, nsISelectionController *aSelectionController, bool aIsFirstVisiblePreferred, bool aFindPrev, nsIPresShell **aPresShell, nsPresContext **aPresContext) { NS_ENSURE_ARG_POINTER(aContainer); NS_ENSURE_ARG_POINTER(aPresShell); NS_ENSURE_ARG_POINTER(aPresContext); *aPresShell = nullptr; *aPresContext = nullptr; nsCOMPtr<nsIDocShell> docShell(do_QueryInterface(aContainer)); if (!docShell) return NS_ERROR_FAILURE; nsCOMPtr<nsIPresShell> presShell = docShell->GetPresShell(); nsRefPtr<nsPresContext> presContext; docShell->GetPresContext(getter_AddRefs(presContext)); if (!presShell || !presContext) return NS_ERROR_FAILURE; nsIDocument* doc = presShell->GetDocument(); if (!doc) return NS_ERROR_FAILURE; nsCOMPtr<nsIContent> rootContent; nsCOMPtr<nsIDOMHTMLDocument> htmlDoc(do_QueryInterface(doc)); if (htmlDoc) { nsCOMPtr<nsIDOMHTMLElement> bodyEl; htmlDoc->GetBody(getter_AddRefs(bodyEl)); rootContent = do_QueryInterface(bodyEl); } if (!rootContent) rootContent = doc->GetRootElement(); nsCOMPtr<nsIDOMNode> rootNode(do_QueryInterface(rootContent)); if (!rootNode) return NS_ERROR_FAILURE; if (!mSearchRange) { mSearchRange = new nsRange(doc); } nsCOMPtr<nsIDOMNode> searchRootNode = rootNode; // Hack for XMLPrettyPrinter. nsFind can't handle complex anonymous content. // If the root node has an XBL binding then there's not much we can do in // in general, but we can try searching the binding's first child, which // in the case of XMLPrettyPrinter contains the visible pretty-printed // content. nsXBLBinding* binding = rootContent->GetXBLBinding(); if (binding) { nsIContent* anonContent = binding->GetAnonymousContent(); if (anonContent) { searchRootNode = do_QueryInterface(anonContent->GetFirstChild()); } } mSearchRange->SelectNodeContents(searchRootNode); if (!mStartPointRange) { mStartPointRange = new nsRange(doc); } mStartPointRange->SetStart(searchRootNode, 0); mStartPointRange->Collapse(true); // collapse to start if (!mEndPointRange) { mEndPointRange = new nsRange(doc); } nsCOMPtr<nsINode> searchRootTmp = do_QueryInterface(searchRootNode); mEndPointRange->SetEnd(searchRootNode, searchRootTmp->Length()); mEndPointRange->Collapse(false); // collapse to end // Consider current selection as null if // it's not in the currently focused document nsCOMPtr<nsIDOMRange> currentSelectionRange; nsCOMPtr<nsIPresShell> selectionPresShell = GetPresShell(); if (aSelectionController && selectionPresShell && selectionPresShell == presShell) { nsCOMPtr<nsISelection> selection; aSelectionController->GetSelection( nsISelectionController::SELECTION_NORMAL, getter_AddRefs(selection)); if (selection) selection->GetRangeAt(0, getter_AddRefs(currentSelectionRange)); } if (!currentSelectionRange) { // Ensure visible range, move forward if necessary // This uses ignores the return value, but usese the side effect of // IsRangeVisible. It returns the first visible range after searchRange IsRangeVisible(presShell, presContext, mSearchRange, aIsFirstVisiblePreferred, true, getter_AddRefs(mStartPointRange), nullptr); } else { int32_t startOffset; nsCOMPtr<nsIDOMNode> startNode; if (aFindPrev) { currentSelectionRange->GetStartContainer(getter_AddRefs(startNode)); currentSelectionRange->GetStartOffset(&startOffset); } else { currentSelectionRange->GetEndContainer(getter_AddRefs(startNode)); currentSelectionRange->GetEndOffset(&startOffset); } if (!startNode) startNode = rootNode; // We need to set the start point this way, other methods haven't worked mStartPointRange->SelectNode(startNode); mStartPointRange->SetStart(startNode, startOffset); } mStartPointRange->Collapse(true); // collapse to start presShell.forget(aPresShell); presContext.forget(aPresContext); return NS_OK; }
/*--------------------------------------------------------------------------- OnStateChange ----------------------------------------------------------------------------*/ NS_IMETHODIMP nsEditingSession::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 aStateFlags, nsresult aStatus) { #ifdef NOISY_DOC_LOADING nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest)); if (channel) { nsCAutoString contentType; channel->GetContentType(contentType); if (!contentType.IsEmpty()) printf(" ++++++ MIMETYPE = %s\n", contentType.get()); } #endif // // A Request has started... // if (aStateFlags & nsIWebProgressListener::STATE_START) { #ifdef NOISY_DOC_LOADING { nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest)); if (channel) { nsCOMPtr<nsIURI> uri; channel->GetURI(getter_AddRefs(uri)); if (uri) { nsXPIDLCString spec; uri->GetSpec(spec); printf(" **** STATE_START: CHANNEL URI=%s, flags=%x\n", spec.get(), aStateFlags); } } else printf(" STATE_START: NO CHANNEL flags=%x\n", aStateFlags); } #endif // Page level notification... if (aStateFlags & nsIWebProgressListener::STATE_IS_NETWORK) { nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest)); StartPageLoad(channel); #ifdef NOISY_DOC_LOADING printf("STATE_START & STATE_IS_NETWORK flags=%x\n", aStateFlags); #endif } // Document level notification... if (aStateFlags & nsIWebProgressListener::STATE_IS_DOCUMENT && !(aStateFlags & nsIWebProgressListener::STATE_RESTORING)) { #ifdef NOISY_DOC_LOADING printf("STATE_START & STATE_IS_DOCUMENT flags=%x\n", aStateFlags); #endif bool progressIsForTargetDocument = IsProgressForTargetDocument(aWebProgress); if (progressIsForTargetDocument) { nsCOMPtr<nsIDOMWindow> window; aWebProgress->GetDOMWindow(getter_AddRefs(window)); nsCOMPtr<nsIDOMDocument> doc; window->GetDocument(getter_AddRefs(doc)); nsCOMPtr<nsIHTMLDocument> htmlDoc(do_QueryInterface(doc)); if (htmlDoc && htmlDoc->IsWriting()) { nsCOMPtr<nsIDOMHTMLDocument> htmlDomDoc = do_QueryInterface(doc); nsAutoString designMode; htmlDomDoc->GetDesignMode(designMode); if (designMode.EqualsLiteral("on")) { // This notification is for data coming in through // document.open/write/close(), ignore it. return NS_OK; } } mCanCreateEditor = true; StartDocumentLoad(aWebProgress, progressIsForTargetDocument); } } } // // A Request is being processed // else if (aStateFlags & nsIWebProgressListener::STATE_TRANSFERRING) { if (aStateFlags & nsIWebProgressListener::STATE_IS_DOCUMENT) { // document transfer started } } // // Got a redirection // else if (aStateFlags & nsIWebProgressListener::STATE_REDIRECTING) { if (aStateFlags & nsIWebProgressListener::STATE_IS_DOCUMENT) { // got a redirect } } // // A network or document Request has finished... // else if (aStateFlags & nsIWebProgressListener::STATE_STOP) { #ifdef NOISY_DOC_LOADING { nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest)); if (channel) { nsCOMPtr<nsIURI> uri; channel->GetURI(getter_AddRefs(uri)); if (uri) { nsXPIDLCString spec; uri->GetSpec(spec); printf(" **** STATE_STOP: CHANNEL URI=%s, flags=%x\n", spec.get(), aStateFlags); } } else printf(" STATE_STOP: NO CHANNEL flags=%x\n", aStateFlags); } #endif // Document level notification... if (aStateFlags & nsIWebProgressListener::STATE_IS_DOCUMENT) { nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest); EndDocumentLoad(aWebProgress, channel, aStatus, IsProgressForTargetDocument(aWebProgress)); #ifdef NOISY_DOC_LOADING printf("STATE_STOP & STATE_IS_DOCUMENT flags=%x\n", aStateFlags); #endif } // Page level notification... if (aStateFlags & nsIWebProgressListener::STATE_IS_NETWORK) { nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest); (void)EndPageLoad(aWebProgress, channel, aStatus); #ifdef NOISY_DOC_LOADING printf("STATE_STOP & STATE_IS_NETWORK flags=%x\n", aStateFlags); #endif } } return NS_OK; }
void nsHTMLContentSerializer::SerializeAttributes(nsIContent* aContent, nsIDOMElement *aOriginalElement, nsAString& aTagPrefix, const nsAString& aTagNamespaceURI, nsIAtom* aTagName, nsAString& aStr) { PRInt32 count = aContent->GetAttrCount(); if (!count) return; nsresult rv; nsAutoString nameStr, valueStr; NS_NAMED_LITERAL_STRING(_mozStr, "_moz"); // HTML5 parser stored them in the order they were parsed so we want to // loop forward in that case. nsIDocument* doc = aContent->GetOwnerDocument(); PRBool caseSensitive = doc && doc->IsCaseSensitive(); PRBool loopForward = PR_FALSE; if (!caseSensitive) { nsCOMPtr<nsIHTMLDocument> htmlDoc(do_QueryInterface(doc)); if (htmlDoc) { loopForward = nsHtml5Module::sEnabled; } } PRInt32 index, limit, step; if (loopForward) { index = 0; limit = count; step = 1; } else { // Loop backward over the attributes, since the order they are stored in is // the opposite of the order they were parsed in (see bug 213347 for reason). index = count - 1; limit = -1; step = -1; } for (; index != limit; index += step) { const nsAttrName* name = aContent->GetAttrNameAt(index); PRInt32 namespaceID = name->NamespaceID(); nsIAtom* attrName = name->LocalName(); // Filter out any attribute starting with [-|_]moz const char* sharedName; attrName->GetUTF8String(&sharedName); if ((('_' == *sharedName) || ('-' == *sharedName)) && !nsCRT::strncmp(sharedName+1, kMozStr, PRUint32(sizeof(kMozStr)-1))) { continue; } aContent->GetAttr(namespaceID, attrName, valueStr); // // Filter out special case of <br type="_moz"> or <br _moz*>, // used by the editor. Bug 16988. Yuck. // if (aTagName == nsGkAtoms::br && attrName == nsGkAtoms::type && StringBeginsWith(valueStr, _mozStr)) { continue; } if (mIsCopying && mIsFirstChildOfOL && (aTagName == nsGkAtoms::li) && (attrName == nsGkAtoms::value)){ // This is handled separately in SerializeLIValueAttribute() continue; } PRBool isJS = IsJavaScript(aContent, attrName, namespaceID, valueStr); if (((attrName == nsGkAtoms::href) || (attrName == nsGkAtoms::src))) { // Make all links absolute when converting only the selection: if (mFlags & nsIDocumentEncoder::OutputAbsoluteLinks) { // Would be nice to handle OBJECT and APPLET tags, // but that gets more complicated since we have to // search the tag list for CODEBASE as well. // For now, just leave them relative. nsCOMPtr<nsIURI> uri = aContent->GetBaseURI(); if (uri) { nsAutoString absURI; rv = NS_MakeAbsoluteURI(absURI, valueStr, uri); if (NS_SUCCEEDED(rv)) { valueStr = absURI; } } } // Need to escape URI. nsAutoString tempURI(valueStr); if (!isJS && NS_FAILED(EscapeURI(aContent, tempURI, valueStr))) valueStr = tempURI; } if (mRewriteEncodingDeclaration && aTagName == nsGkAtoms::meta && attrName == nsGkAtoms::content) { // If we're serializing a <meta http-equiv="content-type">, // use the proper value, rather than what's in the document. nsAutoString header; aContent->GetAttr(kNameSpaceID_None, nsGkAtoms::httpEquiv, header); if (header.LowerCaseEqualsLiteral("content-type")) { valueStr = NS_LITERAL_STRING("text/html; charset=") + NS_ConvertASCIItoUTF16(mCharset); } } attrName->ToString(nameStr); // Expand shorthand attribute. if (IsShorthandAttr(attrName, aTagName) && valueStr.IsEmpty()) { valueStr = nameStr; } SerializeAttr(EmptyString(), nameStr, valueStr, aStr, !isJS); } }
nsresult nsTypeAheadFind::GetSearchContainers(nsISupports *aContainer, nsISelectionController *aSelectionController, PRBool aIsFirstVisiblePreferred, PRBool aFindPrev, nsIPresShell **aPresShell, nsPresContext **aPresContext) { NS_ENSURE_ARG_POINTER(aContainer); NS_ENSURE_ARG_POINTER(aPresShell); NS_ENSURE_ARG_POINTER(aPresContext); *aPresShell = nsnull; *aPresContext = nsnull; nsCOMPtr<nsIDocShell> docShell(do_QueryInterface(aContainer)); if (!docShell) return NS_ERROR_FAILURE; nsCOMPtr<nsIPresShell> presShell; docShell->GetPresShell(getter_AddRefs(presShell)); nsRefPtr<nsPresContext> presContext; docShell->GetPresContext(getter_AddRefs(presContext)); if (!presShell || !presContext) return NS_ERROR_FAILURE; nsIDocument* doc = presShell->GetDocument(); if (!doc) return NS_ERROR_FAILURE; nsCOMPtr<nsIContent> rootContent; nsCOMPtr<nsIDOMHTMLDocument> htmlDoc(do_QueryInterface(doc)); if (htmlDoc) { nsCOMPtr<nsIDOMHTMLElement> bodyEl; htmlDoc->GetBody(getter_AddRefs(bodyEl)); rootContent = do_QueryInterface(bodyEl); } if (!rootContent) rootContent = doc->GetRootElement(); nsCOMPtr<nsIDOMNode> rootNode(do_QueryInterface(rootContent)); if (!rootNode) return NS_ERROR_FAILURE; PRUint32 childCount = rootContent->GetChildCount(); mSearchRange->SelectNodeContents(rootNode); mEndPointRange->SetEnd(rootNode, childCount); mEndPointRange->Collapse(PR_FALSE); // collapse to end // Consider current selection as null if // it's not in the currently focused document nsCOMPtr<nsIDOMRange> currentSelectionRange; nsCOMPtr<nsIPresShell> selectionPresShell = GetPresShell(); if (aSelectionController && selectionPresShell && selectionPresShell == presShell) { nsCOMPtr<nsISelection> selection; aSelectionController->GetSelection( nsISelectionController::SELECTION_NORMAL, getter_AddRefs(selection)); if (selection) selection->GetRangeAt(0, getter_AddRefs(currentSelectionRange)); } if (!currentSelectionRange) { // Ensure visible range, move forward if necessary // This uses ignores the return value, but usese the side effect of // IsRangeVisible. It returns the first visible range after searchRange IsRangeVisible(presShell, presContext, mSearchRange, aIsFirstVisiblePreferred, PR_TRUE, getter_AddRefs(mStartPointRange), nsnull); } else { PRInt32 startOffset; nsCOMPtr<nsIDOMNode> startNode; if (aFindPrev) { currentSelectionRange->GetStartContainer(getter_AddRefs(startNode)); currentSelectionRange->GetStartOffset(&startOffset); } else { currentSelectionRange->GetEndContainer(getter_AddRefs(startNode)); currentSelectionRange->GetEndOffset(&startOffset); } if (!startNode) startNode = rootNode; // We need to set the start point this way, other methods haven't worked mStartPointRange->SelectNode(startNode); mStartPointRange->SetStart(startNode, startOffset); } mStartPointRange->Collapse(PR_TRUE); // collapse to start *aPresShell = presShell; NS_ADDREF(*aPresShell); *aPresContext = presContext; NS_ADDREF(*aPresContext); return NS_OK; }
nsresult HeaderSniffer::GetFilename (const GulCString &contentDisposition, const GulCString &contentType, GulCString &defaultFileName) { PRBool fix_extension = PR_FALSE; PRBool use_text_mime = PR_TRUE; if (!contentDisposition.IsEmpty()) { /* 1 Use the HTTP header suggestion. */ nsresult rv; nsCOMPtr<nsIMIMEHeaderParam> mimehdrpar = do_GetService(NS_MIMEHEADERPARAM_CONTRACTID); if (mimehdrpar) { GulCString fallbackCharset; if (mFinalURL) { mFinalURL->GetOriginCharset(fallbackCharset); } GulString fileName; rv = mimehdrpar->GetParameter (contentDisposition, "filename", fallbackCharset, PR_TRUE, nsnull, fileName); if (NS_FAILED(rv) || fileName.IsEmpty()) { rv = mimehdrpar->GetParameter (contentDisposition, "name", fallbackCharset, PR_TRUE, nsnull, fileName); } if (NS_SUCCEEDED(rv) && !fileName.IsEmpty()) { defaultFileName = fileName; } } } /* 2 For file URLs, use the file name. */ if (defaultFileName.IsEmpty()) { nsCOMPtr<nsIURL> url(do_QueryInterface(mFinalURL)); if (url) { url->GetFileName (defaultFileName); fix_extension = PR_TRUE; use_text_mime = PR_FALSE; } } /* 3 Use the title of the document. */ if (defaultFileName.IsEmpty() && mDocument) { nsCOMPtr<nsIDOMHTMLDocument> htmlDoc(do_QueryInterface(mDocument)); if (htmlDoc) { GulString title; htmlDoc->GetTitle (title); defaultFileName = title; fix_extension = PR_TRUE; } } /* 4 Use the directory */ if (defaultFileName.IsEmpty()) { GulCString pathStr; mFinalURL->GetPath (pathStr); if (pathStr.Length() && pathStr.get()[pathStr.Length()-1] == '/' ) { pathStr.Cut (pathStr.Length()-1, 1); const char *path = pathStr.get(); const char *sep = strrchr (path, '/'); if (sep) { defaultFileName = sep+1; fix_extension = PR_TRUE; } } } /* 5 Use the host. */ if (defaultFileName.IsEmpty() && mOriginalURL) { mOriginalURL->GetHost (defaultFileName); fix_extension = PR_TRUE; } /* 6 One last case to handle about:blank and other untitled pages. */ if (defaultFileName.IsEmpty()) { defaultFileName = _("Untitled"); fix_extension = PR_TRUE; } /* Finally, add the extension on if need be */ if (!fix_extension) { return NS_OK; } /* Get the extension */ GulCString extension; GetDefaultExtension (defaultFileName, contentType, use_text_mime, extension ); if (extension.Length()) { nsCOMPtr<nsIURL> url (do_CreateInstance(NS_STANDARDURL_CONTRACTID)); url->SetFilePath( defaultFileName ); GulCString current; url->GetFileExtension( current ); if (!current.Equals (extension)) { defaultFileName += "."; defaultFileName += extension; } } return NS_OK; }