michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set sw=2 ts=2 et tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsHTMLDocument.h" michael@0: michael@0: #include "mozilla/DebugOnly.h" michael@0: #include "mozilla/dom/HTMLAllCollection.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsGlobalWindow.h" michael@0: #include "nsXPIDLString.h" michael@0: #include "nsPrintfCString.h" michael@0: #include "nsReadableUtils.h" michael@0: #include "nsUnicharUtils.h" michael@0: #include "nsIHTMLContentSink.h" michael@0: #include "nsIXMLContentSink.h" michael@0: #include "nsHTMLParts.h" michael@0: #include "nsHTMLStyleSheet.h" michael@0: #include "nsGkAtoms.h" michael@0: #include "nsIPresShell.h" michael@0: #include "nsPresContext.h" michael@0: #include "nsIDOMNode.h" // for Find michael@0: #include "nsIDOMNodeList.h" michael@0: #include "nsIDOMElement.h" michael@0: #include "nsPIDOMWindow.h" michael@0: #include "nsDOMString.h" michael@0: #include "nsIStreamListener.h" michael@0: #include "nsIURI.h" michael@0: #include "nsIIOService.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsIContentViewerContainer.h" michael@0: #include "nsIContentViewer.h" michael@0: #include "nsIMarkupDocumentViewer.h" michael@0: #include "nsDocShell.h" michael@0: #include "nsDocShellLoadTypes.h" michael@0: #include "nsIWebNavigation.h" michael@0: #include "nsIBaseWindow.h" michael@0: #include "nsIWebShellServices.h" michael@0: #include "nsIScriptContext.h" michael@0: #include "nsIXPConnect.h" michael@0: #include "nsContentList.h" michael@0: #include "nsError.h" michael@0: #include "nsIPrincipal.h" michael@0: #include "nsJSPrincipals.h" michael@0: #include "nsIScriptSecurityManager.h" michael@0: #include "nsAttrName.h" michael@0: #include "nsNodeUtils.h" michael@0: michael@0: #include "nsNetCID.h" michael@0: #include "nsICookieService.h" michael@0: michael@0: #include "nsIServiceManager.h" michael@0: #include "nsIConsoleService.h" michael@0: #include "nsIComponentManager.h" michael@0: #include "nsParserCIID.h" michael@0: #include "nsIDOMHTMLElement.h" michael@0: #include "nsIDOMHTMLHeadElement.h" michael@0: #include "nsNameSpaceManager.h" michael@0: #include "nsGenericHTMLElement.h" michael@0: #include "mozilla/css/Loader.h" michael@0: #include "nsIHttpChannel.h" michael@0: #include "nsIFile.h" michael@0: #include "nsFrameSelection.h" michael@0: #include "nsISelectionPrivate.h"//for toStringwithformat code michael@0: michael@0: #include "nsContentUtils.h" michael@0: #include "nsJSUtils.h" michael@0: #include "nsIDocumentInlines.h" michael@0: #include "nsIDocumentEncoder.h" //for outputting selection michael@0: #include "nsICachingChannel.h" michael@0: #include "nsIContentViewer.h" michael@0: #include "nsIWyciwygChannel.h" michael@0: #include "nsIScriptElement.h" michael@0: #include "nsIScriptError.h" michael@0: #include "nsIMutableArray.h" michael@0: #include "nsArrayUtils.h" michael@0: #include "nsIEffectiveTLDService.h" michael@0: michael@0: //AHMED 12-2 michael@0: #include "nsBidiUtils.h" michael@0: michael@0: #include "mozilla/dom/EncodingUtils.h" michael@0: #include "mozilla/dom/FallbackEncoding.h" michael@0: #include "nsIEditingSession.h" michael@0: #include "nsIEditor.h" michael@0: #include "nsNodeInfoManager.h" michael@0: #include "nsIPlaintextEditor.h" michael@0: #include "nsIHTMLEditor.h" michael@0: #include "nsIEditorStyleSheets.h" michael@0: #include "nsIInlineSpellChecker.h" michael@0: #include "nsRange.h" michael@0: #include "mozAutoDocUpdate.h" michael@0: #include "nsCCUncollectableMarker.h" michael@0: #include "nsHtml5Module.h" michael@0: #include "prprf.h" michael@0: #include "mozilla/dom/Element.h" michael@0: #include "mozilla/Preferences.h" michael@0: #include "nsMimeTypes.h" michael@0: #include "nsIRequest.h" michael@0: #include "nsHtml5TreeOpExecutor.h" michael@0: #include "nsHtml5Parser.h" michael@0: #include "nsIDOMJSWindow.h" michael@0: #include "nsSandboxFlags.h" michael@0: #include "nsIImageDocument.h" michael@0: #include "mozilla/dom/HTMLBodyElement.h" michael@0: #include "mozilla/dom/HTMLDocumentBinding.h" michael@0: #include "nsCharsetSource.h" michael@0: #include "nsIStringBundle.h" michael@0: #include "nsDOMClassInfo.h" michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::dom; michael@0: michael@0: #define NS_MAX_DOCUMENT_WRITE_DEPTH 20 michael@0: michael@0: #include "prtime.h" michael@0: michael@0: //#define DEBUG_charset michael@0: michael@0: static NS_DEFINE_CID(kCParserCID, NS_PARSER_CID); michael@0: michael@0: uint32_t nsHTMLDocument::gWyciwygSessionCnt = 0; michael@0: michael@0: // this function will return false if the command is not recognized michael@0: // inCommandID will be converted as necessary for internal operations michael@0: // inParam will be converted as necessary for internal operations michael@0: // outParam will be Empty if no parameter is needed or if returning a boolean michael@0: // outIsBoolean will determine whether to send param as a boolean or string michael@0: // outBooleanParam will not be set unless outIsBoolean michael@0: static bool ConvertToMidasInternalCommand(const nsAString & inCommandID, michael@0: const nsAString & inParam, michael@0: nsACString& outCommandID, michael@0: nsACString& outParam, michael@0: bool& isBoolean, michael@0: bool& boolValue); michael@0: michael@0: static bool ConvertToMidasInternalCommand(const nsAString & inCommandID, michael@0: nsACString& outCommandID); michael@0: michael@0: // ================================================================== michael@0: // = michael@0: // ================================================================== michael@0: static nsresult michael@0: RemoveFromAgentSheets(nsCOMArray &aAgentSheets, const nsAString& url) michael@0: { michael@0: nsCOMPtr uri; michael@0: nsresult rv = NS_NewURI(getter_AddRefs(uri), url); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: for (int32_t i = aAgentSheets.Count() - 1; i >= 0; --i) { michael@0: nsIStyleSheet* sheet = aAgentSheets[i]; michael@0: nsIURI* sheetURI = sheet->GetSheetURI(); michael@0: michael@0: bool equals = false; michael@0: uri->Equals(sheetURI, &equals); michael@0: if (equals) { michael@0: aAgentSheets.RemoveObjectAt(i); michael@0: } michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: NS_NewHTMLDocument(nsIDocument** aInstancePtrResult, bool aLoadedAsData) michael@0: { michael@0: nsRefPtr doc = new nsHTMLDocument(); michael@0: michael@0: nsresult rv = doc->Init(); michael@0: michael@0: if (NS_FAILED(rv)) { michael@0: *aInstancePtrResult = nullptr; michael@0: return rv; michael@0: } michael@0: michael@0: doc->SetLoadedAsData(aLoadedAsData); michael@0: doc.forget(aInstancePtrResult); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: // NOTE! nsDocument::operator new() zeroes out all members, so don't michael@0: // bother initializing members to 0. michael@0: michael@0: nsHTMLDocument::nsHTMLDocument() michael@0: : nsDocument("text/html") michael@0: { michael@0: // NOTE! nsDocument::operator new() zeroes out all members, so don't michael@0: // bother initializing members to 0. michael@0: michael@0: mIsRegularHTML = true; michael@0: mDefaultElementType = kNameSpaceID_XHTML; michael@0: mCompatMode = eCompatibility_NavQuirks; michael@0: } michael@0: michael@0: nsHTMLDocument::~nsHTMLDocument() michael@0: { michael@0: } michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_INHERITED(nsHTMLDocument, nsDocument, michael@0: mAll, michael@0: mImages, michael@0: mApplets, michael@0: mEmbeds, michael@0: mLinks, michael@0: mAnchors, michael@0: mScripts, michael@0: mForms, michael@0: mFormControls, michael@0: mWyciwygChannel, michael@0: mMidasCommandManager) michael@0: michael@0: NS_IMPL_ADDREF_INHERITED(nsHTMLDocument, nsDocument) michael@0: NS_IMPL_RELEASE_INHERITED(nsHTMLDocument, nsDocument) michael@0: michael@0: // QueryInterface implementation for nsHTMLDocument michael@0: NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED(nsHTMLDocument) michael@0: NS_INTERFACE_TABLE_INHERITED(nsHTMLDocument, nsIHTMLDocument, michael@0: nsIDOMHTMLDocument) michael@0: NS_INTERFACE_TABLE_TAIL_INHERITING(nsDocument) michael@0: michael@0: JSObject* michael@0: nsHTMLDocument::WrapNode(JSContext* aCx) michael@0: { michael@0: return HTMLDocumentBinding::Wrap(aCx, this); michael@0: } michael@0: michael@0: nsresult michael@0: nsHTMLDocument::Init() michael@0: { michael@0: nsresult rv = nsDocument::Init(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Now reset the compatibility mode of the CSSLoader michael@0: // to match our compat mode. michael@0: CSSLoader()->SetCompatibilityMode(mCompatMode); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: void michael@0: nsHTMLDocument::Reset(nsIChannel* aChannel, nsILoadGroup* aLoadGroup) michael@0: { michael@0: nsDocument::Reset(aChannel, aLoadGroup); michael@0: michael@0: if (aChannel) { michael@0: aChannel->GetLoadFlags(&mLoadFlags); michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::ResetToURI(nsIURI *aURI, nsILoadGroup *aLoadGroup, michael@0: nsIPrincipal* aPrincipal) michael@0: { michael@0: mLoadFlags = nsIRequest::LOAD_NORMAL; michael@0: michael@0: nsDocument::ResetToURI(aURI, aLoadGroup, aPrincipal); michael@0: michael@0: mImages = nullptr; michael@0: mApplets = nullptr; michael@0: mEmbeds = nullptr; michael@0: mLinks = nullptr; michael@0: mAnchors = nullptr; michael@0: mScripts = nullptr; michael@0: michael@0: mForms = nullptr; michael@0: michael@0: NS_ASSERTION(!mWyciwygChannel, michael@0: "nsHTMLDocument::Reset() - Wyciwyg Channel still exists!"); michael@0: michael@0: mWyciwygChannel = nullptr; michael@0: michael@0: // Make the content type default to "text/html", we are a HTML michael@0: // document, after all. Once we start getting data, this may be michael@0: // changed. michael@0: SetContentTypeInternal(nsDependentCString("text/html")); michael@0: } michael@0: michael@0: already_AddRefed michael@0: nsHTMLDocument::CreateShell(nsPresContext* aContext, michael@0: nsViewManager* aViewManager, michael@0: nsStyleSet* aStyleSet) michael@0: { michael@0: return doCreateShell(aContext, aViewManager, aStyleSet, mCompatMode); michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::TryHintCharset(nsIMarkupDocumentViewer* aMarkupDV, michael@0: int32_t& aCharsetSource, nsACString& aCharset) michael@0: { michael@0: if (aMarkupDV) { michael@0: int32_t requestCharsetSource; michael@0: nsresult rv = aMarkupDV->GetHintCharacterSetSource(&requestCharsetSource); michael@0: michael@0: if(NS_SUCCEEDED(rv) && kCharsetUninitialized != requestCharsetSource) { michael@0: nsAutoCString requestCharset; michael@0: rv = aMarkupDV->GetHintCharacterSet(requestCharset); michael@0: aMarkupDV->SetHintCharacterSetSource((int32_t)(kCharsetUninitialized)); michael@0: michael@0: if(requestCharsetSource <= aCharsetSource) michael@0: return; michael@0: michael@0: if(NS_SUCCEEDED(rv) && EncodingUtils::IsAsciiCompatible(requestCharset)) { michael@0: aCharsetSource = requestCharsetSource; michael@0: aCharset = requestCharset; michael@0: michael@0: return; michael@0: } michael@0: } michael@0: } michael@0: return; michael@0: } michael@0: michael@0: michael@0: void michael@0: nsHTMLDocument::TryUserForcedCharset(nsIMarkupDocumentViewer* aMarkupDV, michael@0: nsIDocShell* aDocShell, michael@0: int32_t& aCharsetSource, michael@0: nsACString& aCharset) michael@0: { michael@0: nsresult rv = NS_OK; michael@0: michael@0: if(kCharsetFromUserForced <= aCharsetSource) michael@0: return; michael@0: michael@0: // mCharacterSet not updated yet for channel, so check aCharset, too. michael@0: if (WillIgnoreCharsetOverride() || !EncodingUtils::IsAsciiCompatible(aCharset)) { michael@0: return; michael@0: } michael@0: michael@0: nsAutoCString forceCharsetFromDocShell; michael@0: if (aMarkupDV) { michael@0: // XXX mailnews-only michael@0: rv = aMarkupDV->GetForceCharacterSet(forceCharsetFromDocShell); michael@0: } michael@0: michael@0: if(NS_SUCCEEDED(rv) && michael@0: !forceCharsetFromDocShell.IsEmpty() && michael@0: EncodingUtils::IsAsciiCompatible(forceCharsetFromDocShell)) { michael@0: aCharset = forceCharsetFromDocShell; michael@0: aCharsetSource = kCharsetFromUserForced; michael@0: return; michael@0: } michael@0: michael@0: if (aDocShell) { michael@0: // This is the Character Encoding menu code path in Firefox michael@0: nsAutoCString charset; michael@0: rv = aDocShell->GetForcedCharset(charset); michael@0: michael@0: if (NS_SUCCEEDED(rv) && !charset.IsEmpty()) { michael@0: if (!EncodingUtils::IsAsciiCompatible(charset)) { michael@0: return; michael@0: } michael@0: aCharset = charset; michael@0: aCharsetSource = kCharsetFromUserForced; michael@0: aDocShell->SetForcedCharset(NS_LITERAL_CSTRING("")); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::TryCacheCharset(nsICachingChannel* aCachingChannel, michael@0: int32_t& aCharsetSource, michael@0: nsACString& aCharset) michael@0: { michael@0: nsresult rv; michael@0: michael@0: if (kCharsetFromCache <= aCharsetSource) { michael@0: return; michael@0: } michael@0: michael@0: nsCString cachedCharset; michael@0: rv = aCachingChannel->GetCacheTokenCachedCharset(cachedCharset); michael@0: // Check EncodingUtils::IsAsciiCompatible() even in the cache case, because the value michael@0: // might be stale and in the case of a stale charset that is not a rough michael@0: // ASCII superset, the parser has no way to recover. michael@0: if (NS_SUCCEEDED(rv) && michael@0: !cachedCharset.IsEmpty() && michael@0: EncodingUtils::IsAsciiCompatible(cachedCharset)) michael@0: { michael@0: aCharset = cachedCharset; michael@0: aCharsetSource = kCharsetFromCache; michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::TryParentCharset(nsIDocShell* aDocShell, michael@0: int32_t& aCharsetSource, michael@0: nsACString& aCharset) michael@0: { michael@0: if (!aDocShell) { michael@0: return; michael@0: } michael@0: if (aCharsetSource >= kCharsetFromParentForced) { michael@0: return; michael@0: } michael@0: michael@0: int32_t parentSource; michael@0: nsAutoCString parentCharset; michael@0: nsCOMPtr parentPrincipal; michael@0: aDocShell->GetParentCharset(parentCharset, michael@0: &parentSource, michael@0: getter_AddRefs(parentPrincipal)); michael@0: if (parentCharset.IsEmpty()) { michael@0: return; michael@0: } michael@0: if (kCharsetFromParentForced == parentSource || michael@0: kCharsetFromUserForced == parentSource) { michael@0: if (WillIgnoreCharsetOverride() || michael@0: !EncodingUtils::IsAsciiCompatible(aCharset) || // if channel said UTF-16 michael@0: !EncodingUtils::IsAsciiCompatible(parentCharset)) { michael@0: return; michael@0: } michael@0: aCharset.Assign(parentCharset); michael@0: aCharsetSource = kCharsetFromParentForced; michael@0: return; michael@0: } michael@0: michael@0: if (aCharsetSource >= kCharsetFromParentFrame) { michael@0: return; michael@0: } michael@0: michael@0: if (kCharsetFromCache <= parentSource) { michael@0: // Make sure that's OK michael@0: if (!NodePrincipal()->Equals(parentPrincipal) || michael@0: !EncodingUtils::IsAsciiCompatible(parentCharset)) { michael@0: return; michael@0: } michael@0: michael@0: aCharset.Assign(parentCharset); michael@0: aCharsetSource = kCharsetFromParentFrame; michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::TryTLD(int32_t& aCharsetSource, nsACString& aCharset) michael@0: { michael@0: if (aCharsetSource >= kCharsetFromTopLevelDomain) { michael@0: return; michael@0: } michael@0: if (!FallbackEncoding::sGuessFallbackFromTopLevelDomain) { michael@0: return; michael@0: } michael@0: if (!mDocumentURI) { michael@0: return; michael@0: } michael@0: nsAutoCString host; michael@0: mDocumentURI->GetAsciiHost(host); michael@0: if (host.IsEmpty()) { michael@0: return; michael@0: } michael@0: // First let's see if the host is DNS-absolute and ends with a dot and michael@0: // get rid of that one. michael@0: if (host.Last() == '.') { michael@0: host.SetLength(host.Length() - 1); michael@0: if (host.IsEmpty()) { michael@0: return; michael@0: } michael@0: } michael@0: // If we still have a dot, the host is weird, so let's continue only michael@0: // if we have something other than a dot now. michael@0: if (host.Last() == '.') { michael@0: return; michael@0: } michael@0: int32_t index = host.RFindChar('.'); michael@0: if (index == kNotFound) { michael@0: // We have an intranet host, Gecko-internal URL or an IPv6 address. michael@0: return; michael@0: } michael@0: // Since the string didn't end with a dot and we found a dot, michael@0: // there is at least one character between the dot and the end of michael@0: // the string, so taking the substring below is safe. michael@0: nsAutoCString tld; michael@0: ToLowerCase(Substring(host, index + 1, host.Length() - (index + 1)), tld); michael@0: // Reject generic TLDs and country TLDs that need more research michael@0: if (!FallbackEncoding::IsParticipatingTopLevelDomain(tld)) { michael@0: return; michael@0: } michael@0: // Check if we have an IPv4 address michael@0: bool seenNonDigit = false; michael@0: for (size_t i = 0; i < tld.Length(); ++i) { michael@0: char c = tld.CharAt(i); michael@0: if (c < '0' || c > '9') { michael@0: seenNonDigit = true; michael@0: break; michael@0: } michael@0: } michael@0: if (!seenNonDigit) { michael@0: return; michael@0: } michael@0: aCharsetSource = kCharsetFromTopLevelDomain; michael@0: FallbackEncoding::FromTopLevelDomain(tld, aCharset); michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::TryFallback(int32_t& aCharsetSource, nsACString& aCharset) michael@0: { michael@0: if (kCharsetFromFallback <= aCharsetSource) michael@0: return; michael@0: michael@0: aCharsetSource = kCharsetFromFallback; michael@0: FallbackEncoding::FromLocale(aCharset); michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::SetDocumentCharacterSet(const nsACString& aCharSetID) michael@0: { michael@0: nsDocument::SetDocumentCharacterSet(aCharSetID); michael@0: // Make sure to stash this charset on our channel as needed if it's a wyciwyg michael@0: // channel. michael@0: nsCOMPtr wyciwygChannel = do_QueryInterface(mChannel); michael@0: if (wyciwygChannel) { michael@0: wyciwygChannel->SetCharsetAndSource(GetDocumentCharacterSetSource(), michael@0: aCharSetID); michael@0: } michael@0: } michael@0: michael@0: nsresult michael@0: nsHTMLDocument::StartDocumentLoad(const char* aCommand, michael@0: nsIChannel* aChannel, michael@0: nsILoadGroup* aLoadGroup, michael@0: nsISupports* aContainer, michael@0: nsIStreamListener **aDocListener, michael@0: bool aReset, michael@0: nsIContentSink* aSink) michael@0: { michael@0: if (!aCommand) { michael@0: MOZ_ASSERT(false, "Command is mandatory"); michael@0: return NS_ERROR_INVALID_POINTER; michael@0: } michael@0: if (aSink) { michael@0: MOZ_ASSERT(false, "Got a sink override. Should not happen for HTML doc."); michael@0: return NS_ERROR_INVALID_ARG; michael@0: } michael@0: if (!mIsRegularHTML) { michael@0: MOZ_ASSERT(false, "Must not set HTML doc to XHTML mode before load start."); michael@0: return NS_ERROR_DOM_INVALID_STATE_ERR; michael@0: } michael@0: michael@0: nsAutoCString contentType; michael@0: aChannel->GetContentType(contentType); michael@0: michael@0: bool view = !strcmp(aCommand, "view") || michael@0: !strcmp(aCommand, "external-resource"); michael@0: bool viewSource = !strcmp(aCommand, "view-source"); michael@0: bool asData = !strcmp(aCommand, kLoadAsData); michael@0: if(!(view || viewSource || asData)) { michael@0: MOZ_ASSERT(false, "Bad parser command"); michael@0: return NS_ERROR_INVALID_ARG; michael@0: } michael@0: michael@0: bool html = contentType.EqualsLiteral(TEXT_HTML); michael@0: bool xhtml = !html && contentType.EqualsLiteral(APPLICATION_XHTML_XML); michael@0: bool plainText = !html && !xhtml && nsContentUtils::IsPlainTextType(contentType); michael@0: if (!(html || xhtml || plainText || viewSource)) { michael@0: MOZ_ASSERT(false, "Channel with bad content type."); michael@0: return NS_ERROR_INVALID_ARG; michael@0: } michael@0: michael@0: bool loadAsHtml5 = true; michael@0: michael@0: if (!viewSource && xhtml) { michael@0: // We're parsing XHTML as XML, remember that. michael@0: mIsRegularHTML = false; michael@0: mCompatMode = eCompatibility_FullStandards; michael@0: loadAsHtml5 = false; michael@0: } michael@0: michael@0: // TODO: Proper about:blank treatment is bug 543435 michael@0: if (loadAsHtml5 && view) { michael@0: // mDocumentURI hasn't been set, yet, so get the URI from the channel michael@0: nsCOMPtr uri; michael@0: aChannel->GetOriginalURI(getter_AddRefs(uri)); michael@0: // Adapted from nsDocShell: michael@0: // GetSpec can be expensive for some URIs, so check the scheme first. michael@0: bool isAbout = false; michael@0: if (uri && NS_SUCCEEDED(uri->SchemeIs("about", &isAbout)) && isAbout) { michael@0: nsAutoCString str; michael@0: uri->GetSpec(str); michael@0: if (str.EqualsLiteral("about:blank")) { michael@0: loadAsHtml5 = false; michael@0: } michael@0: } michael@0: } michael@0: michael@0: CSSLoader()->SetCompatibilityMode(mCompatMode); michael@0: michael@0: nsresult rv = nsDocument::StartDocumentLoad(aCommand, michael@0: aChannel, aLoadGroup, michael@0: aContainer, michael@0: aDocListener, aReset); michael@0: if (NS_FAILED(rv)) { michael@0: return rv; michael@0: } michael@0: michael@0: // Store the security info for future use with wyciwyg channels. michael@0: aChannel->GetSecurityInfo(getter_AddRefs(mSecurityInfo)); michael@0: michael@0: nsCOMPtr uri; michael@0: rv = aChannel->GetURI(getter_AddRefs(uri)); michael@0: if (NS_FAILED(rv)) { michael@0: return rv; michael@0: } michael@0: michael@0: nsCOMPtr cachingChan = do_QueryInterface(aChannel); michael@0: michael@0: if (loadAsHtml5) { michael@0: mParser = nsHtml5Module::NewHtml5Parser(); michael@0: if (plainText) { michael@0: if (viewSource) { michael@0: mParser->MarkAsNotScriptCreated("view-source-plain"); michael@0: } else { michael@0: mParser->MarkAsNotScriptCreated("plain-text"); michael@0: } michael@0: } else if (viewSource && !html) { michael@0: mParser->MarkAsNotScriptCreated("view-source-xml"); michael@0: } else { michael@0: mParser->MarkAsNotScriptCreated(aCommand); michael@0: } michael@0: } else { michael@0: mParser = do_CreateInstance(kCParserCID, &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: // Look for the parent document. Note that at this point we don't have our michael@0: // content viewer set up yet, and therefore do not have a useful michael@0: // mParentDocument. michael@0: michael@0: // in this block of code, if we get an error result, we return it michael@0: // but if we get a null pointer, that's perfectly legal for parent michael@0: // and parentContentViewer michael@0: nsCOMPtr docShell(do_QueryInterface(aContainer)); michael@0: nsCOMPtr parentAsItem; michael@0: if (docShell) { michael@0: docShell->GetSameTypeParent(getter_AddRefs(parentAsItem)); michael@0: } michael@0: michael@0: nsCOMPtr parent(do_QueryInterface(parentAsItem)); michael@0: nsCOMPtr parentContentViewer; michael@0: if (parent) { michael@0: rv = parent->GetContentViewer(getter_AddRefs(parentContentViewer)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: nsCOMPtr muCV; michael@0: nsCOMPtr cv; michael@0: if (docShell) { michael@0: docShell->GetContentViewer(getter_AddRefs(cv)); michael@0: } michael@0: if (cv) { michael@0: muCV = do_QueryInterface(cv); michael@0: } else { michael@0: muCV = do_QueryInterface(parentContentViewer); michael@0: } michael@0: michael@0: nsAutoCString urlSpec; michael@0: uri->GetSpec(urlSpec); michael@0: #ifdef DEBUG_charset michael@0: printf("Determining charset for %s\n", urlSpec.get()); michael@0: #endif michael@0: michael@0: // These are the charset source and charset for our document michael@0: int32_t charsetSource; michael@0: nsAutoCString charset; michael@0: michael@0: // These are the charset source and charset for the parser. This can differ michael@0: // from that for the document if the channel is a wyciwyg channel. michael@0: int32_t parserCharsetSource; michael@0: nsAutoCString parserCharset; michael@0: michael@0: nsCOMPtr wyciwygChannel; michael@0: michael@0: // For error reporting michael@0: nsHtml5TreeOpExecutor* executor = nullptr; michael@0: if (loadAsHtml5) { michael@0: executor = static_cast (mParser->GetContentSink()); michael@0: } michael@0: michael@0: if (!IsHTML() || !docShell) { // no docshell for text/html XHR michael@0: charsetSource = IsHTML() ? kCharsetFromFallback michael@0: : kCharsetFromDocTypeDefault; michael@0: charset.AssignLiteral("UTF-8"); michael@0: TryChannelCharset(aChannel, charsetSource, charset, executor); michael@0: parserCharsetSource = charsetSource; michael@0: parserCharset = charset; michael@0: } else { michael@0: NS_ASSERTION(docShell, "Unexpected null value"); michael@0: michael@0: charsetSource = kCharsetUninitialized; michael@0: wyciwygChannel = do_QueryInterface(aChannel); michael@0: michael@0: // The following will try to get the character encoding from various michael@0: // sources. Each Try* function will return early if the source is already michael@0: // at least as large as any of the sources it might look at. Some of michael@0: // these functions (like TryHintCharset and TryParentCharset) can set michael@0: // charsetSource to various values depending on where the charset they michael@0: // end up finding originally comes from. michael@0: michael@0: // Don't actually get the charset from the channel if this is a michael@0: // wyciwyg channel; it'll always be UTF-16 michael@0: if (!wyciwygChannel) { michael@0: // Otherwise, try the channel's charset (e.g., charset from HTTP michael@0: // "Content-Type" header) first. This way, we get to reject overrides in michael@0: // TryParentCharset and TryUserForcedCharset if the channel said UTF-16. michael@0: // This is to avoid socially engineered XSS by adding user-supplied michael@0: // content to a UTF-16 site such that the byte have a dangerous michael@0: // interpretation as ASCII and the user can be lured to using the michael@0: // charset menu. michael@0: TryChannelCharset(aChannel, charsetSource, charset, executor); michael@0: } michael@0: michael@0: TryUserForcedCharset(muCV, docShell, charsetSource, charset); michael@0: michael@0: TryHintCharset(muCV, charsetSource, charset); // XXX mailnews-only michael@0: TryParentCharset(docShell, charsetSource, charset); michael@0: michael@0: if (cachingChan && !urlSpec.IsEmpty()) { michael@0: TryCacheCharset(cachingChan, charsetSource, charset); michael@0: } michael@0: michael@0: TryTLD(charsetSource, charset); michael@0: TryFallback(charsetSource, charset); michael@0: michael@0: if (wyciwygChannel) { michael@0: // We know for sure that the parser needs to be using UTF16. michael@0: parserCharset = "UTF-16"; michael@0: parserCharsetSource = charsetSource < kCharsetFromChannel ? michael@0: kCharsetFromChannel : charsetSource; michael@0: michael@0: nsAutoCString cachedCharset; michael@0: int32_t cachedSource; michael@0: rv = wyciwygChannel->GetCharsetAndSource(&cachedSource, cachedCharset); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: if (cachedSource > charsetSource) { michael@0: charsetSource = cachedSource; michael@0: charset = cachedCharset; michael@0: } michael@0: } else { michael@0: // Don't propagate this error. michael@0: rv = NS_OK; michael@0: } michael@0: michael@0: } else { michael@0: parserCharset = charset; michael@0: parserCharsetSource = charsetSource; michael@0: } michael@0: } michael@0: michael@0: SetDocumentCharacterSetSource(charsetSource); michael@0: SetDocumentCharacterSet(charset); michael@0: michael@0: if (cachingChan) { michael@0: NS_ASSERTION(charset == parserCharset, michael@0: "How did those end up different here? wyciwyg channels are " michael@0: "not nsICachingChannel"); michael@0: rv = cachingChan->SetCacheTokenCachedCharset(charset); michael@0: NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "cannot SetMetaDataElement"); michael@0: rv = NS_OK; // don't propagate error michael@0: } michael@0: michael@0: // Set the parser as the stream listener for the document loader... michael@0: rv = NS_OK; michael@0: nsCOMPtr listener = mParser->GetStreamListener(); michael@0: listener.forget(aDocListener); michael@0: michael@0: #ifdef DEBUG_charset michael@0: printf(" charset = %s source %d\n", michael@0: charset.get(), charsetSource); michael@0: #endif michael@0: mParser->SetDocumentCharset(parserCharset, parserCharsetSource); michael@0: mParser->SetCommand(aCommand); michael@0: michael@0: if (!IsHTML()) { michael@0: MOZ_ASSERT(!loadAsHtml5); michael@0: nsCOMPtr xmlsink; michael@0: NS_NewXMLContentSink(getter_AddRefs(xmlsink), this, uri, michael@0: docShell, aChannel); michael@0: mParser->SetContentSink(xmlsink); michael@0: } else { michael@0: if (loadAsHtml5) { michael@0: nsHtml5Module::Initialize(mParser, this, uri, docShell, aChannel); michael@0: } else { michael@0: // about:blank *only* michael@0: nsCOMPtr htmlsink; michael@0: NS_NewHTMLContentSink(getter_AddRefs(htmlsink), this, uri, michael@0: docShell, aChannel); michael@0: mParser->SetContentSink(htmlsink); michael@0: } michael@0: } michael@0: michael@0: if (plainText && !nsContentUtils::IsChildOfSameType(this) && michael@0: Preferences::GetBool("plain_text.wrap_long_lines")) { michael@0: nsCOMPtr bundleService = do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv); michael@0: NS_ASSERTION(NS_SUCCEEDED(rv) && bundleService, "The bundle service could not be loaded"); michael@0: nsCOMPtr bundle; michael@0: rv = bundleService->CreateBundle("chrome://global/locale/browser.properties", michael@0: getter_AddRefs(bundle)); michael@0: NS_ASSERTION(NS_SUCCEEDED(rv) && bundle, "chrome://global/locale/browser.properties could not be loaded"); michael@0: nsXPIDLString title; michael@0: if (bundle) { michael@0: bundle->GetStringFromName(MOZ_UTF16("plainText.wordWrap"), getter_Copies(title)); michael@0: } michael@0: SetSelectedStyleSheetSet(title); michael@0: } michael@0: michael@0: // parser the content of the URI michael@0: mParser->Parse(uri, nullptr, (void *)this); michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::StopDocumentLoad() michael@0: { michael@0: BlockOnload(); michael@0: michael@0: // Remove the wyciwyg channel request from the document load group michael@0: // that we added in Open() if Open() was called on this doc. michael@0: RemoveWyciwygChannel(); michael@0: NS_ASSERTION(!mWyciwygChannel, "nsHTMLDocument::StopDocumentLoad(): " michael@0: "nsIWyciwygChannel could not be removed!"); michael@0: michael@0: nsDocument::StopDocumentLoad(); michael@0: UnblockOnload(false); michael@0: return; michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::BeginLoad() michael@0: { michael@0: if (IsEditingOn()) { michael@0: // Reset() blows away all event listeners in the document, and our michael@0: // editor relies heavily on those. Midas is turned on, to make it michael@0: // work, re-initialize it to give it a chance to add its event michael@0: // listeners again. michael@0: michael@0: TurnEditingOff(); michael@0: EditingStateChanged(); michael@0: } michael@0: nsDocument::BeginLoad(); michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::EndLoad() michael@0: { michael@0: bool turnOnEditing = michael@0: mParser && (HasFlag(NODE_IS_EDITABLE) || mContentEditableCount > 0); michael@0: // Note: nsDocument::EndLoad nulls out mParser. michael@0: nsDocument::EndLoad(); michael@0: if (turnOnEditing) { michael@0: EditingStateChanged(); michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::SetCompatibilityMode(nsCompatibility aMode) michael@0: { michael@0: NS_ASSERTION(IsHTML() || aMode == eCompatibility_FullStandards, michael@0: "Bad compat mode for XHTML document!"); michael@0: michael@0: mCompatMode = aMode; michael@0: CSSLoader()->SetCompatibilityMode(mCompatMode); michael@0: nsCOMPtr shell = GetShell(); michael@0: if (shell) { michael@0: nsPresContext *pc = shell->GetPresContext(); michael@0: if (pc) { michael@0: pc->CompatibilityModeChanged(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // michael@0: // nsIDOMHTMLDocument interface implementation michael@0: // michael@0: already_AddRefed michael@0: nsHTMLDocument::GetDomainURI() michael@0: { michael@0: nsIPrincipal* principal = NodePrincipal(); michael@0: michael@0: nsCOMPtr uri; michael@0: principal->GetDomain(getter_AddRefs(uri)); michael@0: if (uri) { michael@0: return uri.forget(); michael@0: } michael@0: michael@0: principal->GetURI(getter_AddRefs(uri)); michael@0: return uri.forget(); michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::GetDomain(nsAString& aDomain) michael@0: { michael@0: ErrorResult rv; michael@0: GetDomain(aDomain, rv); michael@0: return rv.ErrorCode(); michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::GetDomain(nsAString& aDomain, ErrorResult& rv) michael@0: { michael@0: nsCOMPtr uri = GetDomainURI(); michael@0: michael@0: if (!uri) { michael@0: rv.Throw(NS_ERROR_FAILURE); michael@0: return; michael@0: } michael@0: michael@0: nsAutoCString hostName; michael@0: michael@0: if (NS_SUCCEEDED(uri->GetHost(hostName))) { michael@0: CopyUTF8toUTF16(hostName, aDomain); michael@0: } else { michael@0: // If we can't get the host from the URI (e.g. about:, javascript:, michael@0: // etc), just return an null string. michael@0: SetDOMStringToNull(aDomain); michael@0: } michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::SetDomain(const nsAString& aDomain) michael@0: { michael@0: ErrorResult rv; michael@0: SetDomain(aDomain, rv); michael@0: return rv.ErrorCode(); michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::SetDomain(const nsAString& aDomain, ErrorResult& rv) michael@0: { michael@0: if (mSandboxFlags & SANDBOXED_DOMAIN) { michael@0: // We're sandboxed; disallow setting domain michael@0: rv.Throw(NS_ERROR_DOM_SECURITY_ERR); michael@0: return; michael@0: } michael@0: michael@0: if (aDomain.IsEmpty()) { michael@0: rv.Throw(NS_ERROR_DOM_BAD_DOCUMENT_DOMAIN); michael@0: return; michael@0: } michael@0: michael@0: // Create new URI michael@0: nsCOMPtr uri = GetDomainURI(); michael@0: michael@0: if (!uri) { michael@0: rv.Throw(NS_ERROR_FAILURE); michael@0: return; michael@0: } michael@0: michael@0: nsAutoCString newURIString; michael@0: if (NS_FAILED(uri->GetScheme(newURIString))) { michael@0: rv.Throw(NS_ERROR_FAILURE); michael@0: return; michael@0: } michael@0: nsAutoCString path; michael@0: if (NS_FAILED(uri->GetPath(path))) { michael@0: rv.Throw(NS_ERROR_FAILURE); michael@0: return; michael@0: } michael@0: newURIString.AppendLiteral("://"); michael@0: AppendUTF16toUTF8(aDomain, newURIString); michael@0: newURIString.Append(path); michael@0: michael@0: nsCOMPtr newURI; michael@0: if (NS_FAILED(NS_NewURI(getter_AddRefs(newURI), newURIString))) { michael@0: rv.Throw(NS_ERROR_FAILURE); michael@0: return; michael@0: } michael@0: michael@0: // Check new domain - must be a superdomain of the current host michael@0: // For example, a page from foo.bar.com may set domain to bar.com, michael@0: // but not to ar.com, baz.com, or fi.foo.bar.com. michael@0: nsAutoCString current, domain; michael@0: if (NS_FAILED(uri->GetAsciiHost(current))) michael@0: current.Truncate(); michael@0: if (NS_FAILED(newURI->GetAsciiHost(domain))) michael@0: domain.Truncate(); michael@0: michael@0: bool ok = current.Equals(domain); michael@0: if (current.Length() > domain.Length() && michael@0: StringEndsWith(current, domain) && michael@0: current.CharAt(current.Length() - domain.Length() - 1) == '.') { michael@0: // We're golden if the new domain is the current page's base domain or a michael@0: // subdomain of it. michael@0: nsCOMPtr tldService = michael@0: do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID); michael@0: if (!tldService) { michael@0: rv.Throw(NS_ERROR_NOT_AVAILABLE); michael@0: return; michael@0: } michael@0: michael@0: nsAutoCString currentBaseDomain; michael@0: ok = NS_SUCCEEDED(tldService->GetBaseDomain(uri, 0, currentBaseDomain)); michael@0: NS_ASSERTION(StringEndsWith(domain, currentBaseDomain) == michael@0: (domain.Length() >= currentBaseDomain.Length()), michael@0: "uh-oh! slight optimization wasn't valid somehow!"); michael@0: ok = ok && domain.Length() >= currentBaseDomain.Length(); michael@0: } michael@0: if (!ok) { michael@0: // Error: illegal domain michael@0: rv.Throw(NS_ERROR_DOM_BAD_DOCUMENT_DOMAIN); michael@0: return; michael@0: } michael@0: michael@0: rv = NodePrincipal()->SetDomain(newURI); michael@0: } michael@0: michael@0: nsGenericHTMLElement* michael@0: nsHTMLDocument::GetBody() michael@0: { michael@0: Element* html = GetHtmlElement(); michael@0: if (!html) { michael@0: return nullptr; michael@0: } michael@0: michael@0: for (nsIContent* child = html->GetFirstChild(); michael@0: child; michael@0: child = child->GetNextSibling()) { michael@0: if (child->IsHTML(nsGkAtoms::body) || child->IsHTML(nsGkAtoms::frameset)) { michael@0: return static_cast(child); michael@0: } michael@0: } michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::GetBody(nsIDOMHTMLElement** aBody) michael@0: { michael@0: *aBody = nullptr; michael@0: michael@0: nsIContent *body = GetBody(); michael@0: michael@0: return body ? CallQueryInterface(body, aBody) : NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::SetBody(nsIDOMHTMLElement* aBody) michael@0: { michael@0: nsCOMPtr newBody = do_QueryInterface(aBody); michael@0: MOZ_ASSERT(!newBody || newBody->IsHTML(), michael@0: "How could we be an nsIContent but not actually HTML here?"); michael@0: ErrorResult rv; michael@0: SetBody(static_cast(newBody.get()), rv); michael@0: return rv.ErrorCode(); michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::SetBody(nsGenericHTMLElement* newBody, ErrorResult& rv) michael@0: { michael@0: Element* root = GetRootElement(); michael@0: michael@0: // The body element must be either a body tag or a frameset tag. And we must michael@0: // have a html root tag, otherwise GetBody will not return the newly set michael@0: // body. michael@0: if (!newBody || !(newBody->Tag() == nsGkAtoms::body || michael@0: newBody->Tag() == nsGkAtoms::frameset) || michael@0: !root || !root->IsHTML() || michael@0: root->Tag() != nsGkAtoms::html) { michael@0: rv.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); michael@0: return; michael@0: } michael@0: michael@0: // Use DOM methods so that we pass through the appropriate security checks. michael@0: nsCOMPtr currentBody = GetBodyElement(); michael@0: if (currentBody) { michael@0: root->ReplaceChild(*newBody, *currentBody, rv); michael@0: } else { michael@0: root->AppendChild(*newBody, rv); michael@0: } michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::GetHead(nsIDOMHTMLHeadElement** aHead) michael@0: { michael@0: *aHead = nullptr; michael@0: michael@0: Element* head = GetHeadElement(); michael@0: michael@0: return head ? CallQueryInterface(head, aHead) : NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::GetImages(nsIDOMHTMLCollection** aImages) michael@0: { michael@0: NS_ADDREF(*aImages = Images()); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsIHTMLCollection* michael@0: nsHTMLDocument::Images() michael@0: { michael@0: if (!mImages) { michael@0: mImages = new nsContentList(this, kNameSpaceID_XHTML, nsGkAtoms::img, nsGkAtoms::img); michael@0: } michael@0: return mImages; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::GetApplets(nsIDOMHTMLCollection** aApplets) michael@0: { michael@0: NS_ADDREF(*aApplets = Applets()); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsIHTMLCollection* michael@0: nsHTMLDocument::Applets() michael@0: { michael@0: if (!mApplets) { michael@0: mApplets = new nsContentList(this, kNameSpaceID_XHTML, nsGkAtoms::applet, nsGkAtoms::applet); michael@0: } michael@0: return mApplets; michael@0: } michael@0: michael@0: bool michael@0: nsHTMLDocument::MatchLinks(nsIContent *aContent, int32_t aNamespaceID, michael@0: nsIAtom* aAtom, void* aData) michael@0: { michael@0: nsIDocument* doc = aContent->GetCurrentDoc(); michael@0: michael@0: if (doc) { michael@0: NS_ASSERTION(aContent->IsInDoc(), michael@0: "This method should never be called on content nodes that " michael@0: "are not in a document!"); michael@0: #ifdef DEBUG michael@0: { michael@0: nsCOMPtr htmldoc = michael@0: do_QueryInterface(aContent->GetCurrentDoc()); michael@0: NS_ASSERTION(htmldoc, michael@0: "Huh, how did this happen? This should only be used with " michael@0: "HTML documents!"); michael@0: } michael@0: #endif michael@0: michael@0: nsINodeInfo *ni = aContent->NodeInfo(); michael@0: michael@0: nsIAtom *localName = ni->NameAtom(); michael@0: if (ni->NamespaceID() == kNameSpaceID_XHTML && michael@0: (localName == nsGkAtoms::a || localName == nsGkAtoms::area)) { michael@0: return aContent->HasAttr(kNameSpaceID_None, nsGkAtoms::href); michael@0: } michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::GetLinks(nsIDOMHTMLCollection** aLinks) michael@0: { michael@0: NS_ADDREF(*aLinks = Links()); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsIHTMLCollection* michael@0: nsHTMLDocument::Links() michael@0: { michael@0: if (!mLinks) { michael@0: mLinks = new nsContentList(this, MatchLinks, nullptr, nullptr); michael@0: } michael@0: return mLinks; michael@0: } michael@0: michael@0: bool michael@0: nsHTMLDocument::MatchAnchors(nsIContent *aContent, int32_t aNamespaceID, michael@0: nsIAtom* aAtom, void* aData) michael@0: { michael@0: NS_ASSERTION(aContent->IsInDoc(), michael@0: "This method should never be called on content nodes that " michael@0: "are not in a document!"); michael@0: #ifdef DEBUG michael@0: { michael@0: nsCOMPtr htmldoc = michael@0: do_QueryInterface(aContent->GetCurrentDoc()); michael@0: NS_ASSERTION(htmldoc, michael@0: "Huh, how did this happen? This should only be used with " michael@0: "HTML documents!"); michael@0: } michael@0: #endif michael@0: michael@0: if (aContent->NodeInfo()->Equals(nsGkAtoms::a, kNameSpaceID_XHTML)) { michael@0: return aContent->HasAttr(kNameSpaceID_None, nsGkAtoms::name); michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::GetAnchors(nsIDOMHTMLCollection** aAnchors) michael@0: { michael@0: NS_ADDREF(*aAnchors = Anchors()); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsIHTMLCollection* michael@0: nsHTMLDocument::Anchors() michael@0: { michael@0: if (!mAnchors) { michael@0: mAnchors = new nsContentList(this, MatchAnchors, nullptr, nullptr); michael@0: } michael@0: return mAnchors; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::GetScripts(nsIDOMHTMLCollection** aScripts) michael@0: { michael@0: NS_ADDREF(*aScripts = Scripts()); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsIHTMLCollection* michael@0: nsHTMLDocument::Scripts() michael@0: { michael@0: if (!mScripts) { michael@0: mScripts = new nsContentList(this, kNameSpaceID_XHTML, nsGkAtoms::script, nsGkAtoms::script); michael@0: } michael@0: return mScripts; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::GetCookie(nsAString& aCookie) michael@0: { michael@0: ErrorResult rv; michael@0: GetCookie(aCookie, rv); michael@0: return rv.ErrorCode(); michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::GetCookie(nsAString& aCookie, ErrorResult& rv) michael@0: { michael@0: aCookie.Truncate(); // clear current cookie in case service fails; michael@0: // no cookie isn't an error condition. michael@0: michael@0: if (mDisableCookieAccess) { michael@0: return; michael@0: } michael@0: michael@0: // If the document's sandboxed origin flag is set, access to read cookies michael@0: // is prohibited. michael@0: if (mSandboxFlags & SANDBOXED_ORIGIN) { michael@0: rv.Throw(NS_ERROR_DOM_SECURITY_ERR); michael@0: return; michael@0: } michael@0: michael@0: // not having a cookie service isn't an error michael@0: nsCOMPtr service = do_GetService(NS_COOKIESERVICE_CONTRACTID); michael@0: if (service) { michael@0: // Get a URI from the document principal. We use the original michael@0: // codebase in case the codebase was changed by SetDomain michael@0: nsCOMPtr codebaseURI; michael@0: NodePrincipal()->GetURI(getter_AddRefs(codebaseURI)); michael@0: michael@0: if (!codebaseURI) { michael@0: // Document's principal is not a codebase (may be system), so michael@0: // can't set cookies michael@0: michael@0: return; michael@0: } michael@0: michael@0: nsXPIDLCString cookie; michael@0: service->GetCookieString(codebaseURI, mChannel, getter_Copies(cookie)); michael@0: // CopyUTF8toUTF16 doesn't handle error michael@0: // because it assumes that the input is valid. michael@0: nsContentUtils::ConvertStringFromEncoding(NS_LITERAL_CSTRING("UTF-8"), michael@0: cookie, aCookie); michael@0: } michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::SetCookie(const nsAString& aCookie) michael@0: { michael@0: ErrorResult rv; michael@0: SetCookie(aCookie, rv); michael@0: return rv.ErrorCode(); michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::SetCookie(const nsAString& aCookie, ErrorResult& rv) michael@0: { michael@0: if (mDisableCookieAccess) { michael@0: return; michael@0: } michael@0: michael@0: // If the document's sandboxed origin flag is set, access to write cookies michael@0: // is prohibited. michael@0: if (mSandboxFlags & SANDBOXED_ORIGIN) { michael@0: rv.Throw(NS_ERROR_DOM_SECURITY_ERR); michael@0: return; michael@0: } michael@0: michael@0: // not having a cookie service isn't an error michael@0: nsCOMPtr service = do_GetService(NS_COOKIESERVICE_CONTRACTID); michael@0: if (service && mDocumentURI) { michael@0: // The for getting the URI matches nsNavigator::GetCookieEnabled michael@0: nsCOMPtr codebaseURI; michael@0: NodePrincipal()->GetURI(getter_AddRefs(codebaseURI)); michael@0: michael@0: if (!codebaseURI) { michael@0: // Document's principal is not a codebase (may be system), so michael@0: // can't set cookies michael@0: michael@0: return; michael@0: } michael@0: michael@0: NS_ConvertUTF16toUTF8 cookie(aCookie); michael@0: service->SetCookieString(codebaseURI, nullptr, cookie.get(), mChannel); michael@0: } michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::Open(const nsAString& aContentTypeOrUrl, michael@0: const nsAString& aReplaceOrName, michael@0: const nsAString& aFeatures, michael@0: JSContext* cx, uint8_t aOptionalArgCount, michael@0: nsISupports** aReturn) michael@0: { michael@0: // When called with 3 or more arguments, document.open() calls window.open(). michael@0: if (aOptionalArgCount > 2) { michael@0: ErrorResult rv; michael@0: *aReturn = Open(cx, aContentTypeOrUrl, aReplaceOrName, aFeatures, michael@0: false, rv).take(); michael@0: return rv.ErrorCode(); michael@0: } michael@0: michael@0: nsString type; michael@0: if (aOptionalArgCount > 0) { michael@0: type = aContentTypeOrUrl; michael@0: } else { michael@0: type.AssignLiteral("text/html"); michael@0: } michael@0: nsString replace; michael@0: if (aOptionalArgCount > 1) { michael@0: replace = aReplaceOrName; michael@0: } michael@0: ErrorResult rv; michael@0: *aReturn = Open(cx, type, replace, rv).take(); michael@0: return rv.ErrorCode(); michael@0: } michael@0: michael@0: already_AddRefed michael@0: nsHTMLDocument::Open(JSContext* /* unused */, michael@0: const nsAString& aURL, michael@0: const nsAString& aName, michael@0: const nsAString& aFeatures, michael@0: bool aReplace, michael@0: ErrorResult& rv) michael@0: { michael@0: NS_ASSERTION(nsContentUtils::CanCallerAccess(static_cast(this)), michael@0: "XOW should have caught this!"); michael@0: michael@0: nsCOMPtr window = GetInnerWindow(); michael@0: if (!window) { michael@0: rv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR); michael@0: return nullptr; michael@0: } michael@0: nsCOMPtr win = do_QueryInterface(window); michael@0: nsCOMPtr newWindow; michael@0: // XXXbz We ignore aReplace for now. michael@0: rv = win->OpenJS(aURL, aName, aFeatures, getter_AddRefs(newWindow)); michael@0: return newWindow.forget(); michael@0: } michael@0: michael@0: already_AddRefed michael@0: nsHTMLDocument::Open(JSContext* cx, michael@0: const nsAString& aType, michael@0: const nsAString& aReplace, michael@0: ErrorResult& rv) michael@0: { michael@0: NS_ASSERTION(nsContentUtils::CanCallerAccess(static_cast(this)), michael@0: "XOW should have caught this!"); michael@0: if (!IsHTML() || mDisableDocWrite) { michael@0: // No calling document.open() on XHTML michael@0: rv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); michael@0: return nullptr; michael@0: } michael@0: michael@0: nsAutoCString contentType; michael@0: contentType.AssignLiteral("text/html"); michael@0: michael@0: nsAutoString type; michael@0: nsContentUtils::ASCIIToLower(aType, type); michael@0: nsAutoCString actualType, dummy; michael@0: NS_ParseContentType(NS_ConvertUTF16toUTF8(type), actualType, dummy); michael@0: if (!actualType.EqualsLiteral("text/html") && michael@0: !type.EqualsLiteral("replace")) { michael@0: contentType.AssignLiteral("text/plain"); michael@0: } michael@0: michael@0: // If we already have a parser we ignore the document.open call. michael@0: if (mParser || mParserAborted) { michael@0: // The WHATWG spec says: "If the document has an active parser that isn't michael@0: // a script-created parser, and the insertion point associated with that michael@0: // parser's input stream is not undefined (that is, it does point to michael@0: // somewhere in the input stream), then the method does nothing. Abort michael@0: // these steps and return the Document object on which the method was michael@0: // invoked." michael@0: // Note that aborting a parser leaves the parser "active" with its michael@0: // insertion point "not undefined". We track this using mParserAborted, michael@0: // because aborting a parser nulls out mParser. michael@0: nsCOMPtr ret = this; michael@0: return ret.forget(); michael@0: } michael@0: michael@0: // No calling document.open() without a script global object michael@0: if (!mScriptGlobalObject) { michael@0: nsCOMPtr ret = this; michael@0: return ret.forget(); michael@0: } michael@0: michael@0: nsPIDOMWindow* outer = GetWindow(); michael@0: if (!outer || (GetInnerWindow() != outer->GetCurrentInnerWindow())) { michael@0: nsCOMPtr ret = this; michael@0: return ret.forget(); michael@0: } michael@0: michael@0: // check whether we're in the middle of unload. If so, ignore this call. michael@0: nsCOMPtr shell(mDocumentContainer); michael@0: if (!shell) { michael@0: // We won't be able to create a parser anyway. michael@0: nsCOMPtr ret = this; michael@0: return ret.forget(); michael@0: } michael@0: michael@0: bool inUnload; michael@0: shell->GetIsInUnload(&inUnload); michael@0: if (inUnload) { michael@0: nsCOMPtr ret = this; michael@0: return ret.forget(); michael@0: } michael@0: michael@0: // Note: We want to use GetDocumentFromContext here because this document michael@0: // should inherit the security information of the document that's opening us, michael@0: // (since if it's secure, then it's presumably trusted). michael@0: nsCOMPtr callerDoc = nsContentUtils::GetDocumentFromContext(); michael@0: if (!callerDoc) { michael@0: // If we're called from C++ or in some other way without an originating michael@0: // document we can't do a document.open w/o changing the principal of the michael@0: // document to something like about:blank (as that's the only sane thing to michael@0: // do when we don't know the origin of this call), and since we can't michael@0: // change the principals of a document for security reasons we'll have to michael@0: // refuse to go ahead with this call. michael@0: michael@0: rv.Throw(NS_ERROR_DOM_SECURITY_ERR); michael@0: return nullptr; michael@0: } michael@0: michael@0: // Grab a reference to the calling documents security info (if any) michael@0: // and URIs as they may be lost in the call to Reset(). michael@0: nsCOMPtr securityInfo = callerDoc->GetSecurityInfo(); michael@0: nsCOMPtr uri = callerDoc->GetDocumentURI(); michael@0: nsCOMPtr baseURI = callerDoc->GetBaseURI(); michael@0: nsCOMPtr callerPrincipal = callerDoc->NodePrincipal(); michael@0: nsCOMPtr callerChannel = callerDoc->GetChannel(); michael@0: michael@0: // We're called from script. Make sure the script is from the same michael@0: // origin, not just that the caller can access the document. This is michael@0: // needed to keep document principals from ever changing, which is michael@0: // needed because of the way we use our XOW code, and is a sane michael@0: // thing to do anyways. michael@0: michael@0: bool equals = false; michael@0: if (NS_FAILED(callerPrincipal->Equals(NodePrincipal(), &equals)) || michael@0: !equals) { michael@0: michael@0: #ifdef DEBUG michael@0: nsCOMPtr callerDocURI = callerDoc->GetDocumentURI(); michael@0: nsCOMPtr thisURI = nsIDocument::GetDocumentURI(); michael@0: nsAutoCString callerSpec; michael@0: nsAutoCString thisSpec; michael@0: if (callerDocURI) { michael@0: callerDocURI->GetSpec(callerSpec); michael@0: } michael@0: if (thisURI) { michael@0: thisURI->GetSpec(thisSpec); michael@0: } michael@0: printf("nsHTMLDocument::Open callerDoc %s this %s\n", callerSpec.get(), thisSpec.get()); michael@0: #endif michael@0: michael@0: rv.Throw(NS_ERROR_DOM_SECURITY_ERR); michael@0: return nullptr; michael@0: } michael@0: michael@0: // Stop current loads targeted at the window this document is in. michael@0: if (mScriptGlobalObject) { michael@0: nsCOMPtr cv; michael@0: shell->GetContentViewer(getter_AddRefs(cv)); michael@0: michael@0: if (cv) { michael@0: bool okToUnload; michael@0: if (NS_SUCCEEDED(cv->PermitUnload(false, &okToUnload)) && !okToUnload) { michael@0: // We don't want to unload, so stop here, but don't throw an michael@0: // exception. michael@0: nsCOMPtr ret = this; michael@0: return ret.forget(); michael@0: } michael@0: } michael@0: michael@0: nsCOMPtr webnav(do_QueryInterface(shell)); michael@0: webnav->Stop(nsIWebNavigation::STOP_NETWORK); michael@0: michael@0: // The Stop call may have cancelled the onload blocker request or prevented michael@0: // it from getting added, so we need to make sure it gets added to the michael@0: // document again otherwise the document could have a non-zero onload block michael@0: // count without the onload blocker request being in the loadgroup. michael@0: EnsureOnloadBlocker(); michael@0: } michael@0: michael@0: // The open occurred after the document finished loading. michael@0: // So we reset the document and create a new one. michael@0: nsCOMPtr channel; michael@0: nsCOMPtr group = do_QueryReferent(mDocumentLoadGroup); michael@0: michael@0: rv = NS_NewChannel(getter_AddRefs(channel), uri, nullptr, group); michael@0: michael@0: if (rv.Failed()) { michael@0: return nullptr; michael@0: } michael@0: michael@0: // We can't depend on channels implementing property bags, so do our michael@0: // base URI manually after reset. michael@0: michael@0: // Set the caller principal, if any, on the channel so that we'll michael@0: // make sure to use it when we reset. michael@0: rv = channel->SetOwner(callerPrincipal); michael@0: if (rv.Failed()) { michael@0: return nullptr; michael@0: } michael@0: michael@0: if (callerChannel) { michael@0: nsLoadFlags callerLoadFlags; michael@0: rv = callerChannel->GetLoadFlags(&callerLoadFlags); michael@0: if (rv.Failed()) { michael@0: return nullptr; michael@0: } michael@0: michael@0: nsLoadFlags loadFlags; michael@0: rv = channel->GetLoadFlags(&loadFlags); michael@0: if (rv.Failed()) { michael@0: return nullptr; michael@0: } michael@0: michael@0: loadFlags |= callerLoadFlags & nsIRequest::INHIBIT_PERSISTENT_CACHING; michael@0: michael@0: rv = channel->SetLoadFlags(loadFlags); michael@0: if (rv.Failed()) { michael@0: return nullptr; michael@0: } michael@0: michael@0: // If the user has allowed mixed content on the rootDoc, then we should propogate it michael@0: // down to the new document channel. michael@0: bool rootHasSecureConnection = false; michael@0: bool allowMixedContent = false; michael@0: bool isDocShellRoot = false; michael@0: nsresult rvalue = shell->GetAllowMixedContentAndConnectionData(&rootHasSecureConnection, &allowMixedContent, &isDocShellRoot); michael@0: if (NS_SUCCEEDED(rvalue) && allowMixedContent && isDocShellRoot) { michael@0: shell->SetMixedContentChannel(channel); michael@0: } michael@0: } michael@0: michael@0: // Before we reset the doc notify the globalwindow of the change, michael@0: // but only if we still have a window (i.e. our window object the michael@0: // current inner window in our outer window). michael@0: michael@0: // Hold onto ourselves on the offchance that we're down to one ref michael@0: nsCOMPtr kungFuDeathGrip = this; michael@0: michael@0: nsPIDOMWindow *window = GetInnerWindow(); michael@0: if (window) { michael@0: // Remember the old scope in case the call to SetNewDocument changes it. michael@0: nsCOMPtr oldScope(do_QueryReferent(mScopeObject)); michael@0: michael@0: #ifdef DEBUG michael@0: bool willReparent = mWillReparent; michael@0: mWillReparent = true; michael@0: #endif michael@0: michael@0: // Should this pass true for aForceReuseInnerWindow? michael@0: rv = window->SetNewDocument(this, nullptr, false); michael@0: if (rv.Failed()) { michael@0: return nullptr; michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: mWillReparent = willReparent; michael@0: #endif michael@0: michael@0: // Now make sure we're not flagged as the initial document anymore, now michael@0: // that we've had stuff done to us. From now on, if anyone tries to michael@0: // document.open() us, they get a new inner window. michael@0: SetIsInitialDocument(false); michael@0: michael@0: nsCOMPtr newScope(do_QueryReferent(mScopeObject)); michael@0: JS::Rooted wrapper(cx, GetWrapper()); michael@0: if (oldScope && newScope != oldScope && wrapper) { michael@0: JSAutoCompartment ac(cx, wrapper); michael@0: rv = mozilla::dom::ReparentWrapper(cx, wrapper); michael@0: if (rv.Failed()) { michael@0: return nullptr; michael@0: } michael@0: nsIXPConnect *xpc = nsContentUtils::XPConnect(); michael@0: rv = xpc->RescueOrphansInScope(cx, oldScope->GetGlobalJSObject()); michael@0: if (rv.Failed()) { michael@0: return nullptr; michael@0: } michael@0: } michael@0: } michael@0: michael@0: mDidDocumentOpen = true; michael@0: michael@0: // Call Reset(), this will now do the full reset michael@0: Reset(channel, group); michael@0: if (baseURI) { michael@0: mDocumentBaseURI = baseURI; michael@0: } michael@0: michael@0: // Store the security info of the caller now that we're done michael@0: // resetting the document. michael@0: mSecurityInfo = securityInfo; michael@0: michael@0: mParserAborted = false; michael@0: mParser = nsHtml5Module::NewHtml5Parser(); michael@0: nsHtml5Module::Initialize(mParser, this, uri, shell, channel); michael@0: michael@0: // This will be propagated to the parser when someone actually calls write() michael@0: SetContentTypeInternal(contentType); michael@0: michael@0: // Prepare the docshell and the document viewer for the impending michael@0: // out of band document.write() michael@0: shell->PrepareForNewContentModel(); michael@0: michael@0: // Now check whether we were opened with a "replace" argument. If michael@0: // so, we need to tell the docshell to not create a new history michael@0: // entry for this load. Otherwise, make sure that we're doing a normal load, michael@0: // not whatever type of load was previously done on this docshell. michael@0: shell->SetLoadType(aReplace.LowerCaseEqualsLiteral("replace") ? michael@0: LOAD_NORMAL_REPLACE : LOAD_NORMAL); michael@0: michael@0: nsCOMPtr cv; michael@0: shell->GetContentViewer(getter_AddRefs(cv)); michael@0: if (cv) { michael@0: cv->LoadStart(this); michael@0: } michael@0: michael@0: // Add a wyciwyg channel request into the document load group michael@0: NS_ASSERTION(!mWyciwygChannel, "nsHTMLDocument::Open(): wyciwyg " michael@0: "channel already exists!"); michael@0: michael@0: // In case the editor is listening and will see the new channel michael@0: // being added, make sure mWriteLevel is non-zero so that the editor michael@0: // knows that document.open/write/close() is being called on this michael@0: // document. michael@0: ++mWriteLevel; michael@0: michael@0: CreateAndAddWyciwygChannel(); michael@0: michael@0: --mWriteLevel; michael@0: michael@0: SetReadyStateInternal(nsIDocument::READYSTATE_LOADING); michael@0: michael@0: // After changing everything around, make sure that the principal on the michael@0: // document's compartment exactly matches NodePrincipal(). michael@0: DebugOnly wrapper = GetWrapperPreserveColor(); michael@0: MOZ_ASSERT_IF(wrapper, michael@0: JS_GetCompartmentPrincipals(js::GetObjectCompartment(wrapper)) == michael@0: nsJSPrincipals::get(NodePrincipal())); michael@0: michael@0: return kungFuDeathGrip.forget(); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::Clear() michael@0: { michael@0: // This method has been deprecated michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::Close() michael@0: { michael@0: ErrorResult rv; michael@0: Close(rv); michael@0: return rv.ErrorCode(); michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::Close(ErrorResult& rv) michael@0: { michael@0: if (!IsHTML()) { michael@0: // No calling document.close() on XHTML! michael@0: michael@0: rv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); michael@0: return; michael@0: } michael@0: michael@0: if (!mParser || !mParser->IsScriptCreated()) { michael@0: return; michael@0: } michael@0: michael@0: ++mWriteLevel; michael@0: rv = (static_cast(mParser.get()))->Parse( michael@0: EmptyString(), nullptr, GetContentTypeInternal(), true); michael@0: --mWriteLevel; michael@0: michael@0: // Even if that Parse() call failed, do the rest of this method michael@0: michael@0: // XXX Make sure that all the document.written content is michael@0: // reflowed. We should remove this call once we change michael@0: // nsHTMLDocument::OpenCommon() so that it completely destroys the michael@0: // earlier document's content and frame hierarchy. Right now, it michael@0: // re-uses the earlier document's root content object and michael@0: // corresponding frame objects. These re-used frame objects think michael@0: // that they have already been reflowed, so they drop initial michael@0: // reflows. For certain cases of document.written content, like a michael@0: // frameset document, the dropping of the initial reflow means michael@0: // that we end up in document.close() without appended any reflow michael@0: // commands to the reflow queue and, consequently, without adding michael@0: // the dummy layout request to the load group. Since the dummy michael@0: // layout request is not added to the load group, the onload michael@0: // handler of the frameset fires before the frames get reflowed michael@0: // and loaded. That is the long explanation for why we need this michael@0: // one line of code here! michael@0: // XXXbz as far as I can tell this may not be needed anymore; all michael@0: // the testcases in bug 57636 pass without this line... Leaving michael@0: // it be for now, though. In any case, there's no reason to do michael@0: // this if we have no presshell, since in that case none of the michael@0: // above about reusing frames applies. michael@0: // michael@0: // XXXhsivonen keeping this around for bug 577508 / 253951 still :-( michael@0: if (GetShell()) { michael@0: FlushPendingNotifications(Flush_Layout); michael@0: } michael@0: michael@0: // Removing the wyciwygChannel here is wrong when document.close() is michael@0: // called from within the document itself. However, legacy requires the michael@0: // channel to be removed here. Otherwise, the load event never fires. michael@0: NS_ASSERTION(mWyciwygChannel, "nsHTMLDocument::Close(): Trying to remove " michael@0: "nonexistent wyciwyg channel!"); michael@0: RemoveWyciwygChannel(); michael@0: NS_ASSERTION(!mWyciwygChannel, "nsHTMLDocument::Close(): " michael@0: "nsIWyciwygChannel could not be removed!"); michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::WriteCommon(JSContext *cx, michael@0: const Sequence& aText, michael@0: bool aNewlineTerminate, michael@0: mozilla::ErrorResult& rv) michael@0: { michael@0: // Fast path the common case michael@0: if (aText.Length() == 1) { michael@0: rv = WriteCommon(cx, aText[0], aNewlineTerminate); michael@0: } else { michael@0: // XXXbz it would be nice if we could pass all the strings to the parser michael@0: // without having to do all this copying and then ask it to start michael@0: // parsing.... michael@0: nsString text; michael@0: for (uint32_t i = 0; i < aText.Length(); ++i) { michael@0: text.Append(aText[i]); michael@0: } michael@0: rv = WriteCommon(cx, text, aNewlineTerminate); michael@0: } michael@0: } michael@0: michael@0: nsresult michael@0: nsHTMLDocument::WriteCommon(JSContext *cx, michael@0: const nsAString& aText, michael@0: bool aNewlineTerminate) michael@0: { michael@0: mTooDeepWriteRecursion = michael@0: (mWriteLevel > NS_MAX_DOCUMENT_WRITE_DEPTH || mTooDeepWriteRecursion); michael@0: NS_ENSURE_STATE(!mTooDeepWriteRecursion); michael@0: michael@0: if (!IsHTML() || mDisableDocWrite) { michael@0: // No calling document.write*() on XHTML! michael@0: michael@0: return NS_ERROR_DOM_INVALID_STATE_ERR; michael@0: } michael@0: michael@0: if (mParserAborted) { michael@0: // Hixie says aborting the parser doesn't undefine the insertion point. michael@0: // However, since we null out mParser in that case, we track the michael@0: // theoretically defined insertion point using mParserAborted. michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult rv = NS_OK; michael@0: michael@0: void *key = GenerateParserKey(); michael@0: if (mParser && !mParser->IsInsertionPointDefined()) { michael@0: if (mExternalScriptsBeingEvaluated) { michael@0: // Instead of implying a call to document.open(), ignore the call. michael@0: nsContentUtils::ReportToConsole(nsIScriptError::warningFlag, michael@0: NS_LITERAL_CSTRING("DOM Events"), this, michael@0: nsContentUtils::eDOM_PROPERTIES, michael@0: "DocumentWriteIgnored", michael@0: nullptr, 0, michael@0: mDocumentURI); michael@0: return NS_OK; michael@0: } michael@0: mParser->Terminate(); michael@0: NS_ASSERTION(!mParser, "mParser should have been null'd out"); michael@0: } michael@0: michael@0: if (!mParser) { michael@0: if (mExternalScriptsBeingEvaluated) { michael@0: // Instead of implying a call to document.open(), ignore the call. michael@0: nsContentUtils::ReportToConsole(nsIScriptError::warningFlag, michael@0: NS_LITERAL_CSTRING("DOM Events"), this, michael@0: nsContentUtils::eDOM_PROPERTIES, michael@0: "DocumentWriteIgnored", michael@0: nullptr, 0, michael@0: mDocumentURI); michael@0: return NS_OK; michael@0: } michael@0: nsCOMPtr ignored; michael@0: rv = Open(NS_LITERAL_STRING("text/html"), EmptyString(), EmptyString(), cx, michael@0: 1, getter_AddRefs(ignored)); michael@0: michael@0: // If Open() fails, or if it didn't create a parser (as it won't michael@0: // if the user chose to not discard the current document through michael@0: // onbeforeunload), don't write anything. michael@0: if (NS_FAILED(rv) || !mParser) { michael@0: return rv; michael@0: } michael@0: NS_ABORT_IF_FALSE(!JS_IsExceptionPending(cx), michael@0: "Open() succeeded but JS exception is pending"); michael@0: } michael@0: michael@0: static NS_NAMED_LITERAL_STRING(new_line, "\n"); michael@0: michael@0: // Save the data in cache if the write isn't from within the doc michael@0: if (mWyciwygChannel && !key) { michael@0: if (!aText.IsEmpty()) { michael@0: mWyciwygChannel->WriteToCacheEntry(aText); michael@0: } michael@0: michael@0: if (aNewlineTerminate) { michael@0: mWyciwygChannel->WriteToCacheEntry(new_line); michael@0: } michael@0: } michael@0: michael@0: ++mWriteLevel; michael@0: michael@0: // This could be done with less code, but for performance reasons it michael@0: // makes sense to have the code for two separate Parse() calls here michael@0: // since the concatenation of strings costs more than we like. And michael@0: // why pay that price when we don't need to? michael@0: if (aNewlineTerminate) { michael@0: rv = (static_cast(mParser.get()))->Parse( michael@0: aText + new_line, key, GetContentTypeInternal(), false); michael@0: } else { michael@0: rv = (static_cast(mParser.get()))->Parse( michael@0: aText, key, GetContentTypeInternal(), false); michael@0: } michael@0: michael@0: --mWriteLevel; michael@0: michael@0: mTooDeepWriteRecursion = (mWriteLevel != 0 && mTooDeepWriteRecursion); michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::Write(const nsAString& aText, JSContext *cx) michael@0: { michael@0: return WriteCommon(cx, aText, false); michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::Write(JSContext* cx, const Sequence& aText, michael@0: ErrorResult& rv) michael@0: { michael@0: WriteCommon(cx, aText, false, rv); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::Writeln(const nsAString& aText, JSContext *cx) michael@0: { michael@0: return WriteCommon(cx, aText, true); michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::Writeln(JSContext* cx, const Sequence& aText, michael@0: ErrorResult& rv) michael@0: { michael@0: WriteCommon(cx, aText, true, rv); michael@0: } michael@0: michael@0: bool michael@0: nsHTMLDocument::MatchNameAttribute(nsIContent* aContent, int32_t aNamespaceID, michael@0: nsIAtom* aAtom, void* aData) michael@0: { michael@0: NS_PRECONDITION(aContent, "Must have content node to work with!"); michael@0: nsString* elementName = static_cast(aData); michael@0: return michael@0: aContent->GetNameSpaceID() == kNameSpaceID_XHTML && michael@0: aContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::name, michael@0: *elementName, eCaseMatters); michael@0: } michael@0: michael@0: /* static */ michael@0: void* michael@0: nsHTMLDocument::UseExistingNameString(nsINode* aRootNode, const nsString* aName) michael@0: { michael@0: return const_cast(aName); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::GetElementsByName(const nsAString& aElementName, michael@0: nsIDOMNodeList** aReturn) michael@0: { michael@0: *aReturn = GetElementsByName(aElementName).take(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: static bool MatchItems(nsIContent* aContent, int32_t aNameSpaceID, michael@0: nsIAtom* aAtom, void* aData) michael@0: { michael@0: if (!(aContent->IsElement() && aContent->AsElement()->IsHTML())) { michael@0: return false; michael@0: } michael@0: michael@0: nsGenericHTMLElement* elem = static_cast(aContent); michael@0: if (!elem->HasAttr(kNameSpaceID_None, nsGkAtoms::itemscope) || michael@0: elem->HasAttr(kNameSpaceID_None, nsGkAtoms::itemprop)) { michael@0: return false; michael@0: } michael@0: michael@0: nsTArray >* tokens = static_cast >*>(aData); michael@0: if (tokens->IsEmpty()) { michael@0: return true; michael@0: } michael@0: michael@0: const nsAttrValue* attr = elem->GetParsedAttr(nsGkAtoms::itemtype); michael@0: if (!attr) michael@0: return false; michael@0: michael@0: for (uint32_t i = 0; i < tokens->Length(); i++) { michael@0: if (!attr->Contains(tokens->ElementAt(i), eCaseMatters)) { michael@0: return false; michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: static void DestroyTokens(void* aData) michael@0: { michael@0: nsTArray >* tokens = static_cast >*>(aData); michael@0: delete tokens; michael@0: } michael@0: michael@0: static void* CreateTokens(nsINode* aRootNode, const nsString* types) michael@0: { michael@0: nsTArray >* tokens = new nsTArray >(); michael@0: nsAString::const_iterator iter, end; michael@0: types->BeginReading(iter); michael@0: types->EndReading(end); michael@0: michael@0: // skip initial whitespace michael@0: while (iter != end && nsContentUtils::IsHTMLWhitespace(*iter)) { michael@0: ++iter; michael@0: } michael@0: michael@0: // parse the tokens michael@0: while (iter != end) { michael@0: nsAString::const_iterator start(iter); michael@0: michael@0: do { michael@0: ++iter; michael@0: } while (iter != end && !nsContentUtils::IsHTMLWhitespace(*iter)); michael@0: michael@0: nsCOMPtr token = do_GetAtom(Substring(start, iter)); michael@0: tokens->AppendElement(token); michael@0: michael@0: // skip whitespace michael@0: while (iter != end && nsContentUtils::IsHTMLWhitespace(*iter)) { michael@0: ++iter; michael@0: } michael@0: } michael@0: return tokens; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::GetItems(const nsAString& types, nsIDOMNodeList** aReturn) michael@0: { michael@0: *aReturn = GetItems(types).take(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: already_AddRefed michael@0: nsHTMLDocument::GetItems(const nsAString& aTypeNames) michael@0: { michael@0: return NS_GetFuncStringNodeList(this, MatchItems, DestroyTokens, CreateTokens, michael@0: aTypeNames); michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::AddedForm() michael@0: { michael@0: ++mNumForms; michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::RemovedForm() michael@0: { michael@0: --mNumForms; michael@0: } michael@0: michael@0: int32_t michael@0: nsHTMLDocument::GetNumFormsSynchronous() michael@0: { michael@0: return mNumForms; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::GetAlinkColor(nsAString& aAlinkColor) michael@0: { michael@0: aAlinkColor.Truncate(); michael@0: michael@0: HTMLBodyElement* body = GetBodyElement(); michael@0: if (body) { michael@0: body->GetALink(aAlinkColor); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::SetAlinkColor(const nsAString& aAlinkColor) michael@0: { michael@0: HTMLBodyElement* body = GetBodyElement(); michael@0: if (body) { michael@0: body->SetALink(aAlinkColor); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::GetLinkColor(nsAString& aLinkColor) michael@0: { michael@0: aLinkColor.Truncate(); michael@0: michael@0: HTMLBodyElement* body = GetBodyElement(); michael@0: if (body) { michael@0: body->GetLink(aLinkColor); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::SetLinkColor(const nsAString& aLinkColor) michael@0: { michael@0: HTMLBodyElement* body = GetBodyElement(); michael@0: if (body) { michael@0: body->SetLink(aLinkColor); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::GetVlinkColor(nsAString& aVlinkColor) michael@0: { michael@0: aVlinkColor.Truncate(); michael@0: michael@0: HTMLBodyElement* body = GetBodyElement(); michael@0: if (body) { michael@0: body->GetVLink(aVlinkColor); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::SetVlinkColor(const nsAString& aVlinkColor) michael@0: { michael@0: HTMLBodyElement* body = GetBodyElement(); michael@0: if (body) { michael@0: body->SetVLink(aVlinkColor); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::GetBgColor(nsAString& aBgColor) michael@0: { michael@0: aBgColor.Truncate(); michael@0: michael@0: HTMLBodyElement* body = GetBodyElement(); michael@0: if (body) { michael@0: body->GetBgColor(aBgColor); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::SetBgColor(const nsAString& aBgColor) michael@0: { michael@0: HTMLBodyElement* body = GetBodyElement(); michael@0: if (body) { michael@0: body->SetBgColor(aBgColor); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::GetFgColor(nsAString& aFgColor) michael@0: { michael@0: aFgColor.Truncate(); michael@0: michael@0: HTMLBodyElement* body = GetBodyElement(); michael@0: if (body) { michael@0: body->GetText(aFgColor); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::SetFgColor(const nsAString& aFgColor) michael@0: { michael@0: HTMLBodyElement* body = GetBodyElement(); michael@0: if (body) { michael@0: body->SetText(aFgColor); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::GetEmbeds(nsIDOMHTMLCollection** aEmbeds) michael@0: { michael@0: NS_ADDREF(*aEmbeds = Embeds()); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsIHTMLCollection* michael@0: nsHTMLDocument::Embeds() michael@0: { michael@0: if (!mEmbeds) { michael@0: mEmbeds = new nsContentList(this, kNameSpaceID_XHTML, nsGkAtoms::embed, nsGkAtoms::embed); michael@0: } michael@0: return mEmbeds; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::GetSelection(nsISelection** aReturn) michael@0: { michael@0: ErrorResult rv; michael@0: NS_IF_ADDREF(*aReturn = GetSelection(rv)); michael@0: return rv.ErrorCode(); michael@0: } michael@0: michael@0: Selection* michael@0: nsHTMLDocument::GetSelection(ErrorResult& aRv) michael@0: { michael@0: nsCOMPtr window = do_QueryInterface(GetScopeObject()); michael@0: if (!window) { michael@0: return nullptr; michael@0: } michael@0: michael@0: NS_ASSERTION(window->IsInnerWindow(), "Should have inner window here!"); michael@0: if (!window->IsCurrentInnerWindow()) { michael@0: return nullptr; michael@0: } michael@0: michael@0: return static_cast(window.get())->GetSelection(aRv); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::CaptureEvents() michael@0: { michael@0: WarnOnceAbout(nsIDocument::eUseOfCaptureEvents); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::ReleaseEvents() michael@0: { michael@0: WarnOnceAbout(nsIDocument::eUseOfReleaseEvents); michael@0: return NS_OK; michael@0: } michael@0: michael@0: // Mapped to document.embeds for NS4 compatibility michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::GetPlugins(nsIDOMHTMLCollection** aPlugins) michael@0: { michael@0: *aPlugins = nullptr; michael@0: michael@0: return GetEmbeds(aPlugins); michael@0: } michael@0: michael@0: nsIHTMLCollection* michael@0: nsHTMLDocument::Plugins() michael@0: { michael@0: return Embeds(); michael@0: } michael@0: michael@0: nsISupports* michael@0: nsHTMLDocument::ResolveName(const nsAString& aName, nsWrapperCache **aCache) michael@0: { michael@0: nsIdentifierMapEntry *entry = mIdentifierMap.GetEntry(aName); michael@0: if (!entry) { michael@0: *aCache = nullptr; michael@0: return nullptr; michael@0: } michael@0: michael@0: nsBaseContentList *list = entry->GetNameContentList(); michael@0: uint32_t length = list ? list->Length() : 0; michael@0: michael@0: if (length > 0) { michael@0: if (length == 1) { michael@0: // Only one element in the list, return the element instead of returning michael@0: // the list. michael@0: nsIContent *node = list->Item(0); michael@0: *aCache = node; michael@0: return node; michael@0: } michael@0: michael@0: // The list contains more than one element, return the whole list. michael@0: *aCache = list; michael@0: return list; michael@0: } michael@0: michael@0: // No named items were found, see if there's one registerd by id for aName. michael@0: Element *e = entry->GetIdElement(); michael@0: michael@0: if (e && nsGenericHTMLElement::ShouldExposeIdAsHTMLDocumentProperty(e)) { michael@0: *aCache = e; michael@0: return e; michael@0: } michael@0: michael@0: *aCache = nullptr; michael@0: return nullptr; michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::NamedGetter(JSContext* cx, const nsAString& aName, bool& aFound, michael@0: JS::MutableHandle aRetval, michael@0: ErrorResult& rv) michael@0: { michael@0: nsWrapperCache* cache; michael@0: nsISupports* supp = ResolveName(aName, &cache); michael@0: if (!supp) { michael@0: aFound = false; michael@0: aRetval.set(nullptr); michael@0: return; michael@0: } michael@0: michael@0: JS::Rooted val(cx); michael@0: // XXXbz Should we call the (slightly misnamed, really) WrapNativeParent michael@0: // here? michael@0: if (!dom::WrapObject(cx, supp, cache, nullptr, &val)) { michael@0: rv.Throw(NS_ERROR_OUT_OF_MEMORY); michael@0: return; michael@0: } michael@0: aFound = true; michael@0: aRetval.set(&val.toObject()); michael@0: } michael@0: michael@0: bool michael@0: nsHTMLDocument::NameIsEnumerable(const nsAString& aName) michael@0: { michael@0: return true; michael@0: } michael@0: michael@0: static PLDHashOperator michael@0: IdentifierMapEntryAddNames(nsIdentifierMapEntry* aEntry, void* aArg) michael@0: { michael@0: nsTArray* aNames = static_cast*>(aArg); michael@0: if (aEntry->HasNameElement() || michael@0: aEntry->HasIdElementExposedAsHTMLDocumentProperty()) { michael@0: aNames->AppendElement(aEntry->GetKey()); michael@0: } michael@0: return PL_DHASH_NEXT; michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::GetSupportedNames(unsigned, nsTArray& aNames) michael@0: { michael@0: mIdentifierMap.EnumerateEntries(IdentifierMapEntryAddNames, &aNames); michael@0: } michael@0: michael@0: //---------------------------- michael@0: michael@0: // forms related stuff michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::GetForms(nsIDOMHTMLCollection** aForms) michael@0: { michael@0: NS_ADDREF(*aForms = nsHTMLDocument::GetForms()); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsContentList* michael@0: nsHTMLDocument::GetForms() michael@0: { michael@0: if (!mForms) { michael@0: mForms = new nsContentList(this, kNameSpaceID_XHTML, nsGkAtoms::form, nsGkAtoms::form); michael@0: } michael@0: michael@0: return mForms; michael@0: } michael@0: michael@0: static bool MatchFormControls(nsIContent* aContent, int32_t aNamespaceID, michael@0: nsIAtom* aAtom, void* aData) michael@0: { michael@0: return aContent->IsNodeOfType(nsIContent::eHTML_FORM_CONTROL); michael@0: } michael@0: michael@0: nsContentList* michael@0: nsHTMLDocument::GetFormControls() michael@0: { michael@0: if (!mFormControls) { michael@0: mFormControls = new nsContentList(this, MatchFormControls, nullptr, nullptr); michael@0: } michael@0: michael@0: return mFormControls; michael@0: } michael@0: michael@0: nsresult michael@0: nsHTMLDocument::CreateAndAddWyciwygChannel(void) michael@0: { michael@0: nsresult rv = NS_OK; michael@0: nsAutoCString url, originalSpec; michael@0: michael@0: mDocumentURI->GetSpec(originalSpec); michael@0: michael@0: // Generate the wyciwyg url michael@0: url = NS_LITERAL_CSTRING("wyciwyg://") michael@0: + nsPrintfCString("%d", gWyciwygSessionCnt++) michael@0: + NS_LITERAL_CSTRING("/") michael@0: + originalSpec; michael@0: michael@0: nsCOMPtr wcwgURI; michael@0: NS_NewURI(getter_AddRefs(wcwgURI), url); michael@0: michael@0: // Create the nsIWyciwygChannel to store out-of-band michael@0: // document.write() script to cache michael@0: nsCOMPtr channel; michael@0: // Create a wyciwyg Channel michael@0: rv = NS_NewChannel(getter_AddRefs(channel), wcwgURI); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: mWyciwygChannel = do_QueryInterface(channel); michael@0: michael@0: mWyciwygChannel->SetSecurityInfo(mSecurityInfo); michael@0: michael@0: // Note: we want to treat this like a "previous document" hint so that, michael@0: // e.g. a tag in the document.write content can override it. michael@0: SetDocumentCharacterSetSource(kCharsetFromHintPrevDoc); michael@0: mWyciwygChannel->SetCharsetAndSource(kCharsetFromHintPrevDoc, michael@0: GetDocumentCharacterSet()); michael@0: michael@0: // Use our new principal michael@0: channel->SetOwner(NodePrincipal()); michael@0: michael@0: // Inherit load flags from the original document's channel michael@0: channel->SetLoadFlags(mLoadFlags); michael@0: michael@0: nsCOMPtr loadGroup = GetDocumentLoadGroup(); michael@0: michael@0: // Use the Parent document's loadgroup to trigger load notifications michael@0: if (loadGroup && channel) { michael@0: rv = channel->SetLoadGroup(loadGroup); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsLoadFlags loadFlags = 0; michael@0: channel->GetLoadFlags(&loadFlags); michael@0: loadFlags |= nsIChannel::LOAD_DOCUMENT_URI; michael@0: channel->SetLoadFlags(loadFlags); michael@0: michael@0: channel->SetOriginalURI(wcwgURI); michael@0: michael@0: rv = loadGroup->AddRequest(mWyciwygChannel, nullptr); michael@0: NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to add request to load group."); michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: nsresult michael@0: nsHTMLDocument::RemoveWyciwygChannel(void) michael@0: { michael@0: nsCOMPtr loadGroup = GetDocumentLoadGroup(); michael@0: michael@0: // note there can be a write request without a load group if michael@0: // this is a synchronously constructed about:blank document michael@0: if (loadGroup && mWyciwygChannel) { michael@0: mWyciwygChannel->CloseCacheEntry(NS_OK); michael@0: loadGroup->RemoveRequest(mWyciwygChannel, nullptr, NS_OK); michael@0: } michael@0: michael@0: mWyciwygChannel = nullptr; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void * michael@0: nsHTMLDocument::GenerateParserKey(void) michael@0: { michael@0: if (!mScriptLoader) { michael@0: // If we don't have a script loader, then the parser probably isn't parsing michael@0: // anything anyway, so just return null. michael@0: return nullptr; michael@0: } michael@0: michael@0: // The script loader provides us with the currently executing script element, michael@0: // which is guaranteed to be unique per script. michael@0: nsIScriptElement* script = mScriptLoader->GetCurrentParserInsertedScript(); michael@0: if (script && mParser && mParser->IsScriptCreated()) { michael@0: nsCOMPtr creatorParser = script->GetCreatorParser(); michael@0: if (creatorParser != mParser) { michael@0: // Make scripts that aren't inserted by the active parser of this document michael@0: // participate in the context of the script that document.open()ed michael@0: // this document. michael@0: return nullptr; michael@0: } michael@0: } michael@0: return script; michael@0: } michael@0: michael@0: /* attribute DOMString designMode; */ michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::GetDesignMode(nsAString & aDesignMode) michael@0: { michael@0: if (HasFlag(NODE_IS_EDITABLE)) { michael@0: aDesignMode.AssignLiteral("on"); michael@0: } michael@0: else { michael@0: aDesignMode.AssignLiteral("off"); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::MaybeEditingStateChanged() michael@0: { michael@0: if (!mPendingMaybeEditingStateChanged && michael@0: mUpdateNestLevel == 0 && (mContentEditableCount > 0) != IsEditingOn()) { michael@0: if (nsContentUtils::IsSafeToRunScript()) { michael@0: EditingStateChanged(); michael@0: } else if (!mInDestructor) { michael@0: nsContentUtils::AddScriptRunner( michael@0: NS_NewRunnableMethod(this, &nsHTMLDocument::MaybeEditingStateChanged)); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::EndUpdate(nsUpdateType aUpdateType) michael@0: { michael@0: const bool reset = !mPendingMaybeEditingStateChanged; michael@0: mPendingMaybeEditingStateChanged = true; michael@0: nsDocument::EndUpdate(aUpdateType); michael@0: if (reset) { michael@0: mPendingMaybeEditingStateChanged = false; michael@0: } michael@0: MaybeEditingStateChanged(); michael@0: } michael@0: michael@0: michael@0: // Helper class, used below in ChangeContentEditableCount(). michael@0: class DeferredContentEditableCountChangeEvent : public nsRunnable michael@0: { michael@0: public: michael@0: DeferredContentEditableCountChangeEvent(nsHTMLDocument *aDoc, michael@0: nsIContent *aElement) michael@0: : mDoc(aDoc) michael@0: , mElement(aElement) michael@0: { michael@0: } michael@0: michael@0: NS_IMETHOD Run() { michael@0: if (mElement && mElement->OwnerDoc() == mDoc) { michael@0: mDoc->DeferredContentEditableCountChange(mElement); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: private: michael@0: nsRefPtr mDoc; michael@0: nsCOMPtr mElement; michael@0: }; michael@0: michael@0: nsresult michael@0: nsHTMLDocument::ChangeContentEditableCount(nsIContent *aElement, michael@0: int32_t aChange) michael@0: { michael@0: NS_ASSERTION(int32_t(mContentEditableCount) + aChange >= 0, michael@0: "Trying to decrement too much."); michael@0: michael@0: mContentEditableCount += aChange; michael@0: michael@0: nsContentUtils::AddScriptRunner( michael@0: new DeferredContentEditableCountChangeEvent(this, aElement)); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::DeferredContentEditableCountChange(nsIContent *aElement) michael@0: { michael@0: if (mParser || michael@0: (mUpdateNestLevel > 0 && (mContentEditableCount > 0) != IsEditingOn())) { michael@0: return; michael@0: } michael@0: michael@0: EditingState oldState = mEditingState; michael@0: michael@0: nsresult rv = EditingStateChanged(); michael@0: NS_ENSURE_SUCCESS_VOID(rv); michael@0: michael@0: if (oldState == mEditingState && mEditingState == eContentEditable) { michael@0: // We just changed the contentEditable state of a node, we need to reset michael@0: // the spellchecking state of that node. michael@0: nsCOMPtr node = do_QueryInterface(aElement); michael@0: if (node) { michael@0: nsPIDOMWindow *window = GetWindow(); michael@0: if (!window) michael@0: return; michael@0: michael@0: nsIDocShell *docshell = window->GetDocShell(); michael@0: if (!docshell) michael@0: return; michael@0: michael@0: nsCOMPtr editor; michael@0: docshell->GetEditor(getter_AddRefs(editor)); michael@0: if (editor) { michael@0: nsRefPtr range = new nsRange(aElement); michael@0: rv = range->SelectNode(node); michael@0: if (NS_FAILED(rv)) { michael@0: // The node might be detached from the document at this point, michael@0: // which would cause this call to fail. In this case, we can michael@0: // safely ignore the contenteditable count change. michael@0: return; michael@0: } michael@0: michael@0: nsCOMPtr spellChecker; michael@0: rv = editor->GetInlineSpellChecker(false, michael@0: getter_AddRefs(spellChecker)); michael@0: NS_ENSURE_SUCCESS_VOID(rv); michael@0: michael@0: if (spellChecker) { michael@0: rv = spellChecker->SpellCheckRange(range); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: HTMLAllCollection* michael@0: nsHTMLDocument::All() michael@0: { michael@0: if (!mAll) { michael@0: mAll = new HTMLAllCollection(this); michael@0: } michael@0: return mAll; michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::GetAll(JSContext* aCx, JS::MutableHandle aRetval, michael@0: ErrorResult& aRv) michael@0: { michael@0: aRetval.set(All()->GetObject(aCx, aRv)); michael@0: } michael@0: michael@0: static void michael@0: NotifyEditableStateChange(nsINode *aNode, nsIDocument *aDocument) michael@0: { michael@0: for (nsIContent* child = aNode->GetFirstChild(); michael@0: child; michael@0: child = child->GetNextSibling()) { michael@0: if (child->IsElement()) { michael@0: child->AsElement()->UpdateState(true); michael@0: } michael@0: NotifyEditableStateChange(child, aDocument); michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::TearingDownEditor(nsIEditor *aEditor) michael@0: { michael@0: if (IsEditingOn()) { michael@0: EditingState oldState = mEditingState; michael@0: mEditingState = eTearingDown; michael@0: michael@0: nsCOMPtr presShell = GetShell(); michael@0: if (!presShell) michael@0: return; michael@0: michael@0: nsCOMArray agentSheets; michael@0: presShell->GetAgentStyleSheets(agentSheets); michael@0: michael@0: RemoveFromAgentSheets(agentSheets, NS_LITERAL_STRING("resource://gre/res/contenteditable.css")); michael@0: if (oldState == eDesignMode) michael@0: RemoveFromAgentSheets(agentSheets, NS_LITERAL_STRING("resource://gre/res/designmode.css")); michael@0: michael@0: presShell->SetAgentStyleSheets(agentSheets); michael@0: michael@0: presShell->ReconstructStyleData(); michael@0: } michael@0: } michael@0: michael@0: nsresult michael@0: nsHTMLDocument::TurnEditingOff() michael@0: { michael@0: NS_ASSERTION(mEditingState != eOff, "Editing is already off."); michael@0: michael@0: nsPIDOMWindow *window = GetWindow(); michael@0: if (!window) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: nsIDocShell *docshell = window->GetDocShell(); michael@0: if (!docshell) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: nsresult rv; michael@0: nsCOMPtr editSession = do_GetInterface(docshell, &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // turn editing off michael@0: rv = editSession->TearDownEditorOnWindow(window); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: mEditingState = eOff; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: static bool HasPresShell(nsPIDOMWindow *aWindow) michael@0: { michael@0: nsIDocShell *docShell = aWindow->GetDocShell(); michael@0: if (!docShell) michael@0: return false; michael@0: return docShell->GetPresShell() != nullptr; michael@0: } michael@0: michael@0: nsresult michael@0: nsHTMLDocument::SetEditingState(EditingState aState) michael@0: { michael@0: mEditingState = aState; michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsHTMLDocument::EditingStateChanged() michael@0: { michael@0: if (mRemovedFromDocShell) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: if (mEditingState == eSettingUp || mEditingState == eTearingDown) { michael@0: // XXX We shouldn't recurse. michael@0: return NS_OK; michael@0: } michael@0: michael@0: bool designMode = HasFlag(NODE_IS_EDITABLE); michael@0: EditingState newState = designMode ? eDesignMode : michael@0: (mContentEditableCount > 0 ? eContentEditable : eOff); michael@0: if (mEditingState == newState) { michael@0: // No changes in editing mode. michael@0: return NS_OK; michael@0: } michael@0: michael@0: if (newState == eOff) { michael@0: // Editing is being turned off. michael@0: nsAutoScriptBlocker scriptBlocker; michael@0: NotifyEditableStateChange(this, this); michael@0: return TurnEditingOff(); michael@0: } michael@0: michael@0: // Flush out style changes on our _parent_ document, if any, so that michael@0: // our check for a presshell won't get stale information. michael@0: if (mParentDocument) { michael@0: mParentDocument->FlushPendingNotifications(Flush_Style); michael@0: } michael@0: michael@0: // get editing session, make sure this is a strong reference so the michael@0: // window can't get deleted during the rest of this call. michael@0: nsCOMPtr window = GetWindow(); michael@0: if (!window) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: nsIDocShell *docshell = window->GetDocShell(); michael@0: if (!docshell) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: nsresult rv; michael@0: nsCOMPtr editSession = do_GetInterface(docshell, &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsCOMPtr existingEditor; michael@0: editSession->GetEditorForWindow(window, getter_AddRefs(existingEditor)); michael@0: if (existingEditor) { michael@0: // We might already have an editor if it was set up for mail, let's see michael@0: // if this is actually the case. michael@0: nsCOMPtr htmlEditor = do_QueryInterface(existingEditor); michael@0: NS_ABORT_IF_FALSE(htmlEditor, "If we have an editor, it must be an HTML editor"); michael@0: uint32_t flags = 0; michael@0: existingEditor->GetFlags(&flags); michael@0: if (flags & nsIPlaintextEditor::eEditorMailMask) { michael@0: // We already have a mail editor, then we should not attempt to create michael@0: // another one. michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: if (!HasPresShell(window)) { michael@0: // We should not make the window editable or setup its editor. michael@0: // It's probably style=display:none. michael@0: return NS_OK; michael@0: } michael@0: michael@0: bool makeWindowEditable = mEditingState == eOff; michael@0: bool updateState = false; michael@0: bool spellRecheckAll = false; michael@0: nsCOMPtr editor; michael@0: michael@0: { michael@0: EditingState oldState = mEditingState; michael@0: nsAutoEditingState push(this, eSettingUp); michael@0: michael@0: if (makeWindowEditable) { michael@0: // Editing is being turned on (through designMode or contentEditable) michael@0: // Turn on editor. michael@0: // XXX This can cause flushing which can change the editing state, so make michael@0: // sure to avoid recursing. michael@0: rv = editSession->MakeWindowEditable(window, "html", false, false, michael@0: true); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: // XXX Need to call TearDownEditorOnWindow for all failures. michael@0: docshell->GetEditor(getter_AddRefs(editor)); michael@0: if (!editor) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: nsCOMPtr presShell = GetShell(); michael@0: NS_ENSURE_TRUE(presShell, NS_ERROR_FAILURE); michael@0: michael@0: // If we're entering the design mode, put the selection at the beginning of michael@0: // the document for compatibility reasons. michael@0: if (designMode && oldState == eOff) { michael@0: editor->BeginningOfDocument(); michael@0: } michael@0: michael@0: nsCOMArray agentSheets; michael@0: rv = presShell->GetAgentStyleSheets(agentSheets); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsCOMPtr uri; michael@0: rv = NS_NewURI(getter_AddRefs(uri), NS_LITERAL_STRING("resource://gre/res/contenteditable.css")); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsRefPtr sheet; michael@0: rv = LoadChromeSheetSync(uri, true, getter_AddRefs(sheet)); michael@0: NS_ENSURE_TRUE(sheet, rv); michael@0: michael@0: bool result = agentSheets.AppendObject(sheet); michael@0: NS_ENSURE_TRUE(result, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: // Should we update the editable state of all the nodes in the document? We michael@0: // need to do this when the designMode value changes, as that overrides michael@0: // specific states on the elements. michael@0: if (designMode) { michael@0: // designMode is being turned on (overrides contentEditable). michael@0: rv = NS_NewURI(getter_AddRefs(uri), NS_LITERAL_STRING("resource://gre/res/designmode.css")); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = LoadChromeSheetSync(uri, true, getter_AddRefs(sheet)); michael@0: NS_ENSURE_TRUE(sheet, rv); michael@0: michael@0: result = agentSheets.AppendObject(sheet); michael@0: NS_ENSURE_TRUE(result, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: updateState = true; michael@0: spellRecheckAll = oldState == eContentEditable; michael@0: } michael@0: else if (oldState == eDesignMode) { michael@0: // designMode is being turned off (contentEditable is still on). michael@0: RemoveFromAgentSheets(agentSheets, NS_LITERAL_STRING("resource://gre/res/designmode.css")); michael@0: michael@0: updateState = true; michael@0: } michael@0: michael@0: rv = presShell->SetAgentStyleSheets(agentSheets); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: presShell->ReconstructStyleData(); michael@0: } michael@0: michael@0: mEditingState = newState; michael@0: michael@0: if (makeWindowEditable) { michael@0: // Set the editor to not insert br's on return when in p michael@0: // elements by default. michael@0: // XXX Do we only want to do this for designMode? michael@0: bool unused; michael@0: rv = ExecCommand(NS_LITERAL_STRING("insertBrOnReturn"), false, michael@0: NS_LITERAL_STRING("false"), &unused); michael@0: michael@0: if (NS_FAILED(rv)) { michael@0: // Editor setup failed. Editing is not on after all. michael@0: // XXX Should we reset the editable flag on nodes? michael@0: editSession->TearDownEditorOnWindow(window); michael@0: mEditingState = eOff; michael@0: michael@0: return rv; michael@0: } michael@0: } michael@0: michael@0: if (updateState) { michael@0: nsAutoScriptBlocker scriptBlocker; michael@0: NotifyEditableStateChange(this, this); michael@0: } michael@0: michael@0: // Resync the editor's spellcheck state. michael@0: if (spellRecheckAll) { michael@0: nsCOMPtr selcon; michael@0: nsresult rv = editor->GetSelectionController(getter_AddRefs(selcon)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsCOMPtr spellCheckSelection; michael@0: rv = selcon->GetSelection(nsISelectionController::SELECTION_SPELLCHECK, michael@0: getter_AddRefs(spellCheckSelection)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: spellCheckSelection->RemoveAllRanges(); michael@0: } michael@0: } michael@0: editor->SyncRealTimeSpell(); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::SetDesignMode(const nsAString & aDesignMode) michael@0: { michael@0: ErrorResult rv; michael@0: SetDesignMode(aDesignMode, rv); michael@0: return rv.ErrorCode(); michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::SetDesignMode(const nsAString& aDesignMode, ErrorResult& rv) michael@0: { michael@0: if (!nsContentUtils::IsCallerChrome()) { michael@0: nsCOMPtr subject; michael@0: nsIScriptSecurityManager *secMan = nsContentUtils::GetSecurityManager(); michael@0: rv = secMan->GetSubjectPrincipal(getter_AddRefs(subject)); michael@0: if (rv.Failed()) { michael@0: return; michael@0: } michael@0: if (subject) { michael@0: bool subsumes; michael@0: rv = subject->Subsumes(NodePrincipal(), &subsumes); michael@0: if (rv.Failed()) { michael@0: return; michael@0: } michael@0: michael@0: if (!subsumes) { michael@0: rv.Throw(NS_ERROR_DOM_PROP_ACCESS_DENIED); michael@0: return; michael@0: } michael@0: } michael@0: } michael@0: michael@0: bool editableMode = HasFlag(NODE_IS_EDITABLE); michael@0: if (aDesignMode.LowerCaseEqualsASCII(editableMode ? "off" : "on")) { michael@0: SetEditableFlag(!editableMode); michael@0: michael@0: rv = EditingStateChanged(); michael@0: } michael@0: } michael@0: michael@0: nsresult michael@0: nsHTMLDocument::GetMidasCommandManager(nsICommandManager** aCmdMgr) michael@0: { michael@0: // initialize return value michael@0: NS_ENSURE_ARG_POINTER(aCmdMgr); michael@0: michael@0: // check if we have it cached michael@0: if (mMidasCommandManager) { michael@0: NS_ADDREF(*aCmdMgr = mMidasCommandManager); michael@0: return NS_OK; michael@0: } michael@0: michael@0: *aCmdMgr = nullptr; michael@0: michael@0: nsPIDOMWindow *window = GetWindow(); michael@0: if (!window) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: nsIDocShell *docshell = window->GetDocShell(); michael@0: if (!docshell) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: mMidasCommandManager = do_GetInterface(docshell); michael@0: if (!mMidasCommandManager) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: NS_ADDREF(*aCmdMgr = mMidasCommandManager); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: struct MidasCommand { michael@0: const char* incomingCommandString; michael@0: const char* internalCommandString; michael@0: const char* internalParamString; michael@0: bool useNewParam; michael@0: bool convertToBoolean; michael@0: }; michael@0: michael@0: static const struct MidasCommand gMidasCommandTable[] = { michael@0: { "bold", "cmd_bold", "", true, false }, michael@0: { "italic", "cmd_italic", "", true, false }, michael@0: { "underline", "cmd_underline", "", true, false }, michael@0: { "strikethrough", "cmd_strikethrough", "", true, false }, michael@0: { "subscript", "cmd_subscript", "", true, false }, michael@0: { "superscript", "cmd_superscript", "", true, false }, michael@0: { "cut", "cmd_cut", "", true, false }, michael@0: { "copy", "cmd_copy", "", true, false }, michael@0: { "paste", "cmd_paste", "", true, false }, michael@0: { "delete", "cmd_deleteCharBackward", "", true, false }, michael@0: { "forwarddelete", "cmd_deleteCharForward", "", true, false }, michael@0: { "selectall", "cmd_selectAll", "", true, false }, michael@0: { "undo", "cmd_undo", "", true, false }, michael@0: { "redo", "cmd_redo", "", true, false }, michael@0: { "indent", "cmd_indent", "", true, false }, michael@0: { "outdent", "cmd_outdent", "", true, false }, michael@0: { "backcolor", "cmd_highlight", "", false, false }, michael@0: { "forecolor", "cmd_fontColor", "", false, false }, michael@0: { "hilitecolor", "cmd_highlight", "", false, false }, michael@0: { "fontname", "cmd_fontFace", "", false, false }, michael@0: { "fontsize", "cmd_fontSize", "", false, false }, michael@0: { "increasefontsize", "cmd_increaseFont", "", false, false }, michael@0: { "decreasefontsize", "cmd_decreaseFont", "", false, false }, michael@0: { "inserthorizontalrule", "cmd_insertHR", "", true, false }, michael@0: { "createlink", "cmd_insertLinkNoUI", "", false, false }, michael@0: { "insertimage", "cmd_insertImageNoUI", "", false, false }, michael@0: { "inserthtml", "cmd_insertHTML", "", false, false }, michael@0: { "inserttext", "cmd_insertText", "", false, false }, michael@0: { "gethtml", "cmd_getContents", "", false, false }, michael@0: { "justifyleft", "cmd_align", "left", true, false }, michael@0: { "justifyright", "cmd_align", "right", true, false }, michael@0: { "justifycenter", "cmd_align", "center", true, false }, michael@0: { "justifyfull", "cmd_align", "justify", true, false }, michael@0: { "removeformat", "cmd_removeStyles", "", true, false }, michael@0: { "unlink", "cmd_removeLinks", "", true, false }, michael@0: { "insertorderedlist", "cmd_ol", "", true, false }, michael@0: { "insertunorderedlist", "cmd_ul", "", true, false }, michael@0: { "insertparagraph", "cmd_paragraphState", "p", true, false }, michael@0: { "formatblock", "cmd_paragraphState", "", false, false }, michael@0: { "heading", "cmd_paragraphState", "", false, false }, michael@0: { "styleWithCSS", "cmd_setDocumentUseCSS", "", false, true }, michael@0: { "contentReadOnly", "cmd_setDocumentReadOnly", "", false, true }, michael@0: { "insertBrOnReturn", "cmd_insertBrOnReturn", "", false, true }, michael@0: { "enableObjectResizing", "cmd_enableObjectResizing", "", false, true }, michael@0: { "enableInlineTableEditing", "cmd_enableInlineTableEditing", "", false, true }, michael@0: #if 0 michael@0: // no editor support to remove alignments right now michael@0: { "justifynone", "cmd_align", "", true, false }, michael@0: michael@0: // the following will need special review before being turned on michael@0: { "saveas", "cmd_saveAs", "", true, false }, michael@0: { "print", "cmd_print", "", true, false }, michael@0: #endif michael@0: { nullptr, nullptr, nullptr, false, false } michael@0: }; michael@0: michael@0: #define MidasCommandCount ((sizeof(gMidasCommandTable) / sizeof(struct MidasCommand)) - 1) michael@0: michael@0: static const char* const gBlocks[] = { michael@0: "ADDRESS", michael@0: "BLOCKQUOTE", michael@0: "DD", michael@0: "DIV", michael@0: "DL", michael@0: "DT", michael@0: "H1", michael@0: "H2", michael@0: "H3", michael@0: "H4", michael@0: "H5", michael@0: "H6", michael@0: "P", michael@0: "PRE" michael@0: }; michael@0: michael@0: static bool michael@0: ConvertToMidasInternalCommandInner(const nsAString& inCommandID, michael@0: const nsAString& inParam, michael@0: nsACString& outCommandID, michael@0: nsACString& outParam, michael@0: bool& outIsBoolean, michael@0: bool& outBooleanValue, michael@0: bool aIgnoreParams) michael@0: { michael@0: NS_ConvertUTF16toUTF8 convertedCommandID(inCommandID); michael@0: michael@0: // Hack to support old boolean commands that were backwards (see bug 301490). michael@0: bool invertBool = false; michael@0: if (convertedCommandID.LowerCaseEqualsLiteral("usecss")) { michael@0: convertedCommandID.Assign("styleWithCSS"); michael@0: invertBool = true; michael@0: } else if (convertedCommandID.LowerCaseEqualsLiteral("readonly")) { michael@0: convertedCommandID.Assign("contentReadOnly"); michael@0: invertBool = true; michael@0: } michael@0: michael@0: uint32_t i; michael@0: bool found = false; michael@0: for (i = 0; i < MidasCommandCount; ++i) { michael@0: if (convertedCommandID.Equals(gMidasCommandTable[i].incomingCommandString, michael@0: nsCaseInsensitiveCStringComparator())) { michael@0: found = true; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (!found) { michael@0: // reset results if the command is not found in our table michael@0: outCommandID.SetLength(0); michael@0: outParam.SetLength(0); michael@0: outIsBoolean = false; michael@0: return false; michael@0: } michael@0: michael@0: // set outCommandID (what we use internally) michael@0: outCommandID.Assign(gMidasCommandTable[i].internalCommandString); michael@0: michael@0: // set outParam & outIsBoolean based on flags from the table michael@0: outIsBoolean = gMidasCommandTable[i].convertToBoolean; michael@0: michael@0: if (aIgnoreParams) { michael@0: // No further work to do michael@0: return true; michael@0: } michael@0: michael@0: if (gMidasCommandTable[i].useNewParam) { michael@0: // Just have to copy it, no checking michael@0: outParam.Assign(gMidasCommandTable[i].internalParamString); michael@0: return true; michael@0: } michael@0: michael@0: // handle checking of param passed in michael@0: if (outIsBoolean) { michael@0: // If this is a boolean value and it's not explicitly false (e.g. no value) michael@0: // we default to "true". For old backwards commands we invert the check (see michael@0: // bug 301490). michael@0: if (invertBool) { michael@0: outBooleanValue = inParam.LowerCaseEqualsLiteral("false"); michael@0: } else { michael@0: outBooleanValue = !inParam.LowerCaseEqualsLiteral("false"); michael@0: } michael@0: outParam.Truncate(); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // String parameter -- see if we need to convert it (necessary for michael@0: // cmd_paragraphState and cmd_fontSize) michael@0: if (outCommandID.EqualsLiteral("cmd_paragraphState")) { michael@0: const char16_t* start = inParam.BeginReading(); michael@0: const char16_t* end = inParam.EndReading(); michael@0: if (start != end && *start == '<' && *(end - 1) == '>') { michael@0: ++start; michael@0: --end; michael@0: } michael@0: michael@0: NS_ConvertUTF16toUTF8 convertedParam(Substring(start, end)); michael@0: uint32_t j; michael@0: for (j = 0; j < ArrayLength(gBlocks); ++j) { michael@0: if (convertedParam.Equals(gBlocks[j], michael@0: nsCaseInsensitiveCStringComparator())) { michael@0: outParam.Assign(gBlocks[j]); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (j == ArrayLength(gBlocks)) { michael@0: outParam.Truncate(); michael@0: } michael@0: } else if (outCommandID.EqualsLiteral("cmd_fontSize")) { michael@0: // Per editing spec as of April 23, 2012, we need to reject the value if michael@0: // it's not a valid floating-point number surrounded by optional whitespace. michael@0: // Otherwise, we parse it as a legacy font size. For now, we just parse as michael@0: // a legacy font size regardless (matching WebKit) -- bug 747879. michael@0: outParam.Truncate(); michael@0: int32_t size = nsContentUtils::ParseLegacyFontSize(inParam); michael@0: if (size) { michael@0: outParam.AppendInt(size); michael@0: } michael@0: } else { michael@0: CopyUTF16toUTF8(inParam, outParam); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: static bool michael@0: ConvertToMidasInternalCommand(const nsAString & inCommandID, michael@0: const nsAString & inParam, michael@0: nsACString& outCommandID, michael@0: nsACString& outParam, michael@0: bool& outIsBoolean, michael@0: bool& outBooleanValue) michael@0: { michael@0: return ConvertToMidasInternalCommandInner(inCommandID, inParam, outCommandID, michael@0: outParam, outIsBoolean, michael@0: outBooleanValue, false); michael@0: } michael@0: michael@0: static bool michael@0: ConvertToMidasInternalCommand(const nsAString & inCommandID, michael@0: nsACString& outCommandID) michael@0: { michael@0: nsAutoCString dummyCString; michael@0: nsAutoString dummyString; michael@0: bool dummyBool; michael@0: return ConvertToMidasInternalCommandInner(inCommandID, dummyString, michael@0: outCommandID, dummyCString, michael@0: dummyBool, dummyBool, true); michael@0: } michael@0: michael@0: /* TODO: don't let this call do anything if the page is not done loading */ michael@0: /* boolean execCommand(in DOMString commandID, in boolean doShowUI, michael@0: in DOMString value); */ michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::ExecCommand(const nsAString& commandID, michael@0: bool doShowUI, michael@0: const nsAString& value, michael@0: bool* _retval) michael@0: { michael@0: ErrorResult rv; michael@0: *_retval = ExecCommand(commandID, doShowUI, value, rv); michael@0: return rv.ErrorCode(); michael@0: } michael@0: michael@0: bool michael@0: nsHTMLDocument::ExecCommand(const nsAString& commandID, michael@0: bool doShowUI, michael@0: const nsAString& value, michael@0: ErrorResult& rv) michael@0: { michael@0: // for optional parameters see dom/src/base/nsHistory.cpp: HistoryImpl::Go() michael@0: // this might add some ugly JS dependencies? michael@0: michael@0: nsAutoCString cmdToDispatch, paramStr; michael@0: bool isBool, boolVal; michael@0: if (!ConvertToMidasInternalCommand(commandID, value, michael@0: cmdToDispatch, paramStr, michael@0: isBool, boolVal)) { michael@0: return false; michael@0: } michael@0: michael@0: // if editing is not on, bail michael@0: if (!IsEditingOnAfterFlush()) { michael@0: rv.Throw(NS_ERROR_FAILURE); michael@0: return false; michael@0: } michael@0: michael@0: // if they are requesting UI from us, let's fail since we have no UI michael@0: if (doShowUI) { michael@0: return false; michael@0: } michael@0: michael@0: if (commandID.LowerCaseEqualsLiteral("gethtml")) { michael@0: rv.Throw(NS_ERROR_FAILURE); michael@0: return false; michael@0: } michael@0: michael@0: bool restricted = commandID.LowerCaseEqualsLiteral("cut") || michael@0: commandID.LowerCaseEqualsLiteral("copy")|| michael@0: commandID.LowerCaseEqualsLiteral("paste"); michael@0: if (restricted && !nsContentUtils::IsCallerChrome()) { michael@0: rv = NS_ERROR_DOM_SECURITY_ERR; michael@0: return false; michael@0: } michael@0: michael@0: // get command manager and dispatch command to our window if it's acceptable michael@0: nsCOMPtr cmdMgr; michael@0: GetMidasCommandManager(getter_AddRefs(cmdMgr)); michael@0: if (!cmdMgr) { michael@0: rv.Throw(NS_ERROR_FAILURE); michael@0: return false; michael@0: } michael@0: michael@0: nsIDOMWindow* window = GetWindow(); michael@0: if (!window) { michael@0: rv.Throw(NS_ERROR_FAILURE); michael@0: return false; michael@0: } michael@0: michael@0: if ((cmdToDispatch.EqualsLiteral("cmd_fontSize") || michael@0: cmdToDispatch.EqualsLiteral("cmd_insertImageNoUI") || michael@0: cmdToDispatch.EqualsLiteral("cmd_insertLinkNoUI") || michael@0: cmdToDispatch.EqualsLiteral("cmd_paragraphState")) && michael@0: paramStr.IsEmpty()) { michael@0: // Invalid value, return false michael@0: return false; michael@0: } michael@0: michael@0: // Return false for disabled commands (bug 760052) michael@0: bool enabled = false; michael@0: cmdMgr->IsCommandEnabled(cmdToDispatch.get(), window, &enabled); michael@0: if (!enabled) { michael@0: return false; michael@0: } michael@0: michael@0: if (!isBool && paramStr.IsEmpty()) { michael@0: rv = cmdMgr->DoCommand(cmdToDispatch.get(), nullptr, window); michael@0: } else { michael@0: // we have a command that requires a parameter, create params michael@0: nsCOMPtr cmdParams = do_CreateInstance( michael@0: NS_COMMAND_PARAMS_CONTRACTID); michael@0: if (!cmdParams) { michael@0: rv.Throw(NS_ERROR_OUT_OF_MEMORY); michael@0: return false; michael@0: } michael@0: michael@0: if (isBool) { michael@0: rv = cmdParams->SetBooleanValue("state_attribute", boolVal); michael@0: } else if (cmdToDispatch.EqualsLiteral("cmd_fontFace")) { michael@0: rv = cmdParams->SetStringValue("state_attribute", value); michael@0: } else if (cmdToDispatch.EqualsLiteral("cmd_insertHTML") || michael@0: cmdToDispatch.EqualsLiteral("cmd_insertText")) { michael@0: rv = cmdParams->SetStringValue("state_data", value); michael@0: } else { michael@0: rv = cmdParams->SetCStringValue("state_attribute", paramStr.get()); michael@0: } michael@0: if (rv.Failed()) { michael@0: return false; michael@0: } michael@0: rv = cmdMgr->DoCommand(cmdToDispatch.get(), cmdParams, window); michael@0: } michael@0: michael@0: return !rv.Failed(); michael@0: } michael@0: michael@0: /* boolean queryCommandEnabled(in DOMString commandID); */ michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::QueryCommandEnabled(const nsAString& commandID, michael@0: bool* _retval) michael@0: { michael@0: ErrorResult rv; michael@0: *_retval = QueryCommandEnabled(commandID, rv); michael@0: return rv.ErrorCode(); michael@0: } michael@0: michael@0: bool michael@0: nsHTMLDocument::QueryCommandEnabled(const nsAString& commandID, ErrorResult& rv) michael@0: { michael@0: nsAutoCString cmdToDispatch; michael@0: if (!ConvertToMidasInternalCommand(commandID, cmdToDispatch)) { michael@0: return false; michael@0: } michael@0: michael@0: // if editing is not on, bail michael@0: if (!IsEditingOnAfterFlush()) { michael@0: rv.Throw(NS_ERROR_FAILURE); michael@0: return false; michael@0: } michael@0: michael@0: // get command manager and dispatch command to our window if it's acceptable michael@0: nsCOMPtr cmdMgr; michael@0: GetMidasCommandManager(getter_AddRefs(cmdMgr)); michael@0: if (!cmdMgr) { michael@0: rv.Throw(NS_ERROR_FAILURE); michael@0: return false; michael@0: } michael@0: michael@0: nsIDOMWindow* window = GetWindow(); michael@0: if (!window) { michael@0: rv.Throw(NS_ERROR_FAILURE); michael@0: return false; michael@0: } michael@0: michael@0: bool retval; michael@0: rv = cmdMgr->IsCommandEnabled(cmdToDispatch.get(), window, &retval); michael@0: return retval; michael@0: } michael@0: michael@0: /* boolean queryCommandIndeterm (in DOMString commandID); */ michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::QueryCommandIndeterm(const nsAString & commandID, michael@0: bool *_retval) michael@0: { michael@0: ErrorResult rv; michael@0: *_retval = QueryCommandIndeterm(commandID, rv); michael@0: return rv.ErrorCode(); michael@0: } michael@0: michael@0: bool michael@0: nsHTMLDocument::QueryCommandIndeterm(const nsAString& commandID, ErrorResult& rv) michael@0: { michael@0: nsAutoCString cmdToDispatch; michael@0: if (!ConvertToMidasInternalCommand(commandID, cmdToDispatch)) { michael@0: return false; michael@0: } michael@0: michael@0: // if editing is not on, bail michael@0: if (!IsEditingOnAfterFlush()) { michael@0: rv.Throw(NS_ERROR_FAILURE); michael@0: return false; michael@0: } michael@0: michael@0: // get command manager and dispatch command to our window if it's acceptable michael@0: nsCOMPtr cmdMgr; michael@0: GetMidasCommandManager(getter_AddRefs(cmdMgr)); michael@0: if (!cmdMgr) { michael@0: rv.Throw(NS_ERROR_FAILURE); michael@0: return false; michael@0: } michael@0: michael@0: nsIDOMWindow* window = GetWindow(); michael@0: if (!window) { michael@0: rv.Throw(NS_ERROR_FAILURE); michael@0: return false; michael@0: } michael@0: michael@0: nsresult res; michael@0: nsCOMPtr cmdParams = do_CreateInstance( michael@0: NS_COMMAND_PARAMS_CONTRACTID, &res); michael@0: if (NS_FAILED(res)) { michael@0: rv.Throw(res); michael@0: return false; michael@0: } michael@0: michael@0: rv = cmdMgr->GetCommandState(cmdToDispatch.get(), window, cmdParams); michael@0: if (rv.Failed()) { michael@0: return false; michael@0: } michael@0: michael@0: // If command does not have a state_mixed value, this call fails and sets michael@0: // retval to false. This is fine -- we want to return false in that case michael@0: // anyway (bug 738385), so we just don't throw regardless. michael@0: bool retval = false; michael@0: cmdParams->GetBooleanValue("state_mixed", &retval); michael@0: return retval; michael@0: } michael@0: michael@0: /* boolean queryCommandState(in DOMString commandID); */ michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::QueryCommandState(const nsAString & commandID, bool *_retval) michael@0: { michael@0: ErrorResult rv; michael@0: *_retval = QueryCommandState(commandID, rv); michael@0: return rv.ErrorCode(); michael@0: } michael@0: michael@0: bool michael@0: nsHTMLDocument::QueryCommandState(const nsAString& commandID, ErrorResult& rv) michael@0: { michael@0: nsAutoCString cmdToDispatch, paramToCheck; michael@0: bool dummy, dummy2; michael@0: if (!ConvertToMidasInternalCommand(commandID, commandID, michael@0: cmdToDispatch, paramToCheck, michael@0: dummy, dummy2)) { michael@0: return false; michael@0: } michael@0: michael@0: // if editing is not on, bail michael@0: if (!IsEditingOnAfterFlush()) { michael@0: rv.Throw(NS_ERROR_FAILURE); michael@0: return false; michael@0: } michael@0: michael@0: // get command manager and dispatch command to our window if it's acceptable michael@0: nsCOMPtr cmdMgr; michael@0: GetMidasCommandManager(getter_AddRefs(cmdMgr)); michael@0: if (!cmdMgr) { michael@0: rv.Throw(NS_ERROR_FAILURE); michael@0: return false; michael@0: } michael@0: michael@0: nsIDOMWindow* window = GetWindow(); michael@0: if (!window) { michael@0: rv.Throw(NS_ERROR_FAILURE); michael@0: return false; michael@0: } michael@0: michael@0: if (commandID.LowerCaseEqualsLiteral("usecss")) { michael@0: // Per spec, state is supported for styleWithCSS but not useCSS, so we just michael@0: // return false always. michael@0: return false; michael@0: } michael@0: michael@0: nsCOMPtr cmdParams = do_CreateInstance( michael@0: NS_COMMAND_PARAMS_CONTRACTID); michael@0: if (!cmdParams) { michael@0: rv.Throw(NS_ERROR_OUT_OF_MEMORY); michael@0: return false; michael@0: } michael@0: michael@0: rv = cmdMgr->GetCommandState(cmdToDispatch.get(), window, cmdParams); michael@0: if (rv.Failed()) { michael@0: return false; michael@0: } michael@0: michael@0: // handle alignment as a special case (possibly other commands too?) michael@0: // Alignment is special because the external api is individual michael@0: // commands but internally we use cmd_align with different michael@0: // parameters. When getting the state of this command, we need to michael@0: // return the boolean for this particular alignment rather than the michael@0: // string of 'which alignment is this?' michael@0: if (cmdToDispatch.EqualsLiteral("cmd_align")) { michael@0: char * actualAlignmentType = nullptr; michael@0: rv = cmdParams->GetCStringValue("state_attribute", &actualAlignmentType); michael@0: bool retval = false; michael@0: if (!rv.Failed() && actualAlignmentType && actualAlignmentType[0]) { michael@0: retval = paramToCheck.Equals(actualAlignmentType); michael@0: } michael@0: if (actualAlignmentType) { michael@0: nsMemory::Free(actualAlignmentType); michael@0: } michael@0: return retval; michael@0: } michael@0: michael@0: // If command does not have a state_all value, this call fails and sets michael@0: // retval to false. This is fine -- we want to return false in that case michael@0: // anyway (bug 738385), so we just succeed and return false regardless. michael@0: bool retval = false; michael@0: cmdParams->GetBooleanValue("state_all", &retval); michael@0: return retval; michael@0: } michael@0: michael@0: /* boolean queryCommandSupported(in DOMString commandID); */ michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::QueryCommandSupported(const nsAString & commandID, michael@0: bool *_retval) michael@0: { michael@0: *_retval = QueryCommandSupported(commandID); michael@0: return NS_OK; michael@0: } michael@0: michael@0: bool michael@0: nsHTMLDocument::QueryCommandSupported(const nsAString& commandID) michael@0: { michael@0: // commandID is supported if it can be converted to a Midas command michael@0: nsAutoCString cmdToDispatch; michael@0: return ConvertToMidasInternalCommand(commandID, cmdToDispatch); michael@0: } michael@0: michael@0: /* DOMString queryCommandValue(in DOMString commandID); */ michael@0: NS_IMETHODIMP michael@0: nsHTMLDocument::QueryCommandValue(const nsAString & commandID, michael@0: nsAString &_retval) michael@0: { michael@0: ErrorResult rv; michael@0: QueryCommandValue(commandID, _retval, rv); michael@0: return rv.ErrorCode(); michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::QueryCommandValue(const nsAString& commandID, michael@0: nsAString& aValue, michael@0: ErrorResult& rv) michael@0: { michael@0: aValue.Truncate(); michael@0: michael@0: nsAutoCString cmdToDispatch, paramStr; michael@0: if (!ConvertToMidasInternalCommand(commandID, cmdToDispatch)) { michael@0: // Return empty string michael@0: return; michael@0: } michael@0: michael@0: // if editing is not on, bail michael@0: if (!IsEditingOnAfterFlush()) { michael@0: rv.Throw(NS_ERROR_FAILURE); michael@0: return; michael@0: } michael@0: michael@0: // get command manager and dispatch command to our window if it's acceptable michael@0: nsCOMPtr cmdMgr; michael@0: GetMidasCommandManager(getter_AddRefs(cmdMgr)); michael@0: if (!cmdMgr) { michael@0: rv.Throw(NS_ERROR_FAILURE); michael@0: return; michael@0: } michael@0: michael@0: nsIDOMWindow* window = GetWindow(); michael@0: if (!window) { michael@0: rv.Throw(NS_ERROR_FAILURE); michael@0: return; michael@0: } michael@0: michael@0: // create params michael@0: nsCOMPtr cmdParams = do_CreateInstance( michael@0: NS_COMMAND_PARAMS_CONTRACTID); michael@0: if (!cmdParams) { michael@0: rv.Throw(NS_ERROR_OUT_OF_MEMORY); michael@0: return; michael@0: } michael@0: michael@0: // this is a special command since we are calling DoCommand rather than michael@0: // GetCommandState like the other commands michael@0: if (cmdToDispatch.EqualsLiteral("cmd_getContents")) { michael@0: rv = cmdParams->SetBooleanValue("selection_only", true); michael@0: if (rv.Failed()) { michael@0: return; michael@0: } michael@0: rv = cmdParams->SetCStringValue("format", "text/html"); michael@0: if (rv.Failed()) { michael@0: return; michael@0: } michael@0: rv = cmdMgr->DoCommand(cmdToDispatch.get(), cmdParams, window); michael@0: if (rv.Failed()) { michael@0: return; michael@0: } michael@0: rv = cmdParams->GetStringValue("result", aValue); michael@0: return; michael@0: } michael@0: michael@0: rv = cmdParams->SetCStringValue("state_attribute", paramStr.get()); michael@0: if (rv.Failed()) { michael@0: return; michael@0: } michael@0: michael@0: rv = cmdMgr->GetCommandState(cmdToDispatch.get(), window, cmdParams); michael@0: if (rv.Failed()) { michael@0: return; michael@0: } michael@0: michael@0: // If command does not have a state_attribute value, this call fails, and michael@0: // aValue will wind up being the empty string. This is fine -- we want to michael@0: // return "" in that case anyway (bug 738385), so we just return NS_OK michael@0: // regardless. michael@0: nsXPIDLCString cStringResult; michael@0: cmdParams->GetCStringValue("state_attribute", michael@0: getter_Copies(cStringResult)); michael@0: CopyUTF8toUTF16(cStringResult, aValue); michael@0: } michael@0: michael@0: nsresult michael@0: nsHTMLDocument::Clone(nsINodeInfo *aNodeInfo, nsINode **aResult) const michael@0: { michael@0: NS_ASSERTION(aNodeInfo->NodeInfoManager() == mNodeInfoManager, michael@0: "Can't import this document into another document!"); michael@0: michael@0: nsRefPtr clone = new nsHTMLDocument(); michael@0: nsresult rv = CloneDocHelper(clone.get()); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // State from nsHTMLDocument michael@0: clone->mLoadFlags = mLoadFlags; michael@0: michael@0: return CallQueryInterface(clone.get(), aResult); michael@0: } michael@0: michael@0: bool michael@0: nsHTMLDocument::IsEditingOnAfterFlush() michael@0: { michael@0: nsIDocument* doc = GetParentDocument(); michael@0: if (doc) { michael@0: // Make sure frames are up to date, since that can affect whether michael@0: // we're editable. michael@0: doc->FlushPendingNotifications(Flush_Frames); michael@0: } michael@0: michael@0: return IsEditingOn(); michael@0: } michael@0: michael@0: void michael@0: nsHTMLDocument::RemovedFromDocShell() michael@0: { michael@0: mEditingState = eOff; michael@0: nsDocument::RemovedFromDocShell(); michael@0: } michael@0: michael@0: /* virtual */ void michael@0: nsHTMLDocument::DocAddSizeOfExcludingThis(nsWindowSizes* aWindowSizes) const michael@0: { michael@0: nsDocument::DocAddSizeOfExcludingThis(aWindowSizes); michael@0: michael@0: // Measurement of the following members may be added later if DMD finds it is michael@0: // worthwhile: michael@0: // - mImages michael@0: // - mApplets michael@0: // - mEmbeds michael@0: // - mLinks michael@0: // - mAnchors michael@0: // - mScripts michael@0: // - mForms michael@0: // - mFormControls michael@0: // - mWyciwygChannel michael@0: // - mMidasCommandManager michael@0: } michael@0: michael@0: bool michael@0: nsHTMLDocument::WillIgnoreCharsetOverride() michael@0: { michael@0: if (!mIsRegularHTML) { michael@0: return true; michael@0: } michael@0: if (mCharacterSetSource == kCharsetFromByteOrderMark) { michael@0: return true; michael@0: } michael@0: if (!EncodingUtils::IsAsciiCompatible(mCharacterSet)) { michael@0: return true; michael@0: } michael@0: nsCOMPtr wyciwyg = do_QueryInterface(mChannel); michael@0: if (wyciwyg) { michael@0: return true; michael@0: } michael@0: nsIURI* uri = GetOriginalURI(); michael@0: if (uri) { michael@0: bool schemeIs = false; michael@0: uri->SchemeIs("about", &schemeIs); michael@0: if (schemeIs) { michael@0: return true; michael@0: } michael@0: bool isResource; michael@0: nsresult rv = NS_URIChainHasFlags(uri, michael@0: nsIProtocolHandler::URI_IS_UI_RESOURCE, michael@0: &isResource); michael@0: if (NS_FAILED(rv) || isResource) { michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: }