2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2015-08-06 00:25:39 +03:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "WebBrowserPersistLocalDocument.h"
|
|
|
|
#include "WebBrowserPersistDocumentParent.h"
|
|
|
|
|
2018-01-21 02:26:41 +03:00
|
|
|
#include "mozilla/dom/Attr.h"
|
2018-01-30 07:10:52 +03:00
|
|
|
#include "mozilla/dom/Comment.h"
|
2018-01-21 02:26:41 +03:00
|
|
|
#include "mozilla/dom/Element.h"
|
2017-09-23 00:56:51 +03:00
|
|
|
#include "mozilla/dom/HTMLAnchorElement.h"
|
2017-09-22 03:52:28 +03:00
|
|
|
#include "mozilla/dom/HTMLAreaElement.h"
|
2015-08-06 00:25:39 +03:00
|
|
|
#include "mozilla/dom/HTMLInputElement.h"
|
2017-10-07 00:15:03 +03:00
|
|
|
#include "mozilla/dom/HTMLLinkElement.h"
|
2017-09-23 03:05:12 +03:00
|
|
|
#include "mozilla/dom/HTMLObjectElement.h"
|
2017-11-09 04:43:09 +03:00
|
|
|
#include "mozilla/dom/HTMLOptionElement.h"
|
2015-08-06 00:25:39 +03:00
|
|
|
#include "mozilla/dom/HTMLSharedElement.h"
|
2017-10-31 02:14:51 +03:00
|
|
|
#include "mozilla/dom/HTMLTextAreaElement.h"
|
2018-02-01 22:26:12 +03:00
|
|
|
#include "mozilla/dom/NodeFilterBinding.h"
|
2018-03-19 22:15:39 +03:00
|
|
|
#include "mozilla/dom/ProcessingInstruction.h"
|
2015-08-06 00:25:39 +03:00
|
|
|
#include "mozilla/dom/TabParent.h"
|
2018-01-31 23:18:10 +03:00
|
|
|
#include "mozilla/dom/TreeWalker.h"
|
2018-02-26 22:43:46 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-08-06 00:25:39 +03:00
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsContentCID.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2018-01-21 02:26:41 +03:00
|
|
|
#include "nsDOMAttributeMap.h"
|
2015-08-06 00:25:39 +03:00
|
|
|
#include "nsFrameLoader.h"
|
2018-07-25 02:47:43 +03:00
|
|
|
#include "nsGlobalWindowOuter.h"
|
2015-08-06 00:25:39 +03:00
|
|
|
#include "nsIComponentRegistrar.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDOMWindowUtils.h"
|
|
|
|
#include "nsIDocShell.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2015-08-06 00:25:39 +03:00
|
|
|
#include "nsIDocumentEncoder.h"
|
|
|
|
#include "nsILoadContext.h"
|
|
|
|
#include "nsIProtocolHandler.h"
|
|
|
|
#include "nsISHEntry.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsITabParent.h"
|
2018-02-26 22:43:46 +03:00
|
|
|
#include "nsIURIMutator.h"
|
2015-08-06 00:25:39 +03:00
|
|
|
#include "nsIWebBrowserPersist.h"
|
|
|
|
#include "nsIWebNavigation.h"
|
|
|
|
#include "nsIWebPageDescriptor.h"
|
|
|
|
#include "nsNetUtil.h"
|
2019-02-16 01:20:53 +03:00
|
|
|
#include "nsQueryObject.h"
|
2015-08-06 00:25:39 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(WebBrowserPersistLocalDocument)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(WebBrowserPersistLocalDocument)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(WebBrowserPersistLocalDocument)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIWebBrowserPersistDocument)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION(WebBrowserPersistLocalDocument, mDocument)
|
|
|
|
|
|
|
|
WebBrowserPersistLocalDocument::WebBrowserPersistLocalDocument(
|
2019-01-02 16:05:23 +03:00
|
|
|
dom::Document* aDocument)
|
2015-08-06 00:25:39 +03:00
|
|
|
: mDocument(aDocument), mPersistFlags(0) {
|
|
|
|
MOZ_ASSERT(mDocument);
|
|
|
|
}
|
|
|
|
|
2016-11-22 15:56:19 +03:00
|
|
|
WebBrowserPersistLocalDocument::~WebBrowserPersistLocalDocument() = default;
|
2015-08-06 00:25:39 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebBrowserPersistLocalDocument::SetPersistFlags(uint32_t aFlags) {
|
|
|
|
mPersistFlags = aFlags;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebBrowserPersistLocalDocument::GetPersistFlags(uint32_t* aFlags) {
|
|
|
|
*aFlags = mPersistFlags;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebBrowserPersistLocalDocument::GetIsPrivate(bool* aIsPrivate) {
|
|
|
|
nsCOMPtr<nsILoadContext> privacyContext = mDocument->GetLoadContext();
|
|
|
|
*aIsPrivate = privacyContext && privacyContext->UsePrivateBrowsing();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebBrowserPersistLocalDocument::GetDocumentURI(nsACString& aURISpec) {
|
|
|
|
nsCOMPtr<nsIURI> uri = mDocument->GetDocumentURI();
|
|
|
|
if (!uri) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
return uri->GetSpec(aURISpec);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebBrowserPersistLocalDocument::GetBaseURI(nsACString& aURISpec) {
|
|
|
|
nsCOMPtr<nsIURI> uri = GetBaseURI();
|
|
|
|
if (!uri) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
return uri->GetSpec(aURISpec);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebBrowserPersistLocalDocument::GetContentType(nsACString& aContentType) {
|
|
|
|
nsAutoString utf16Type;
|
2018-01-31 23:18:11 +03:00
|
|
|
mDocument->GetContentType(utf16Type);
|
|
|
|
CopyUTF16toUTF8(utf16Type, aContentType);
|
2015-08-06 00:25:39 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebBrowserPersistLocalDocument::GetCharacterSet(nsACString& aCharSet) {
|
2017-06-18 14:37:50 +03:00
|
|
|
GetCharacterSet()->Name(aCharSet);
|
2015-08-06 00:25:39 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebBrowserPersistLocalDocument::GetTitle(nsAString& aTitle) {
|
|
|
|
nsAutoString titleBuffer;
|
|
|
|
mDocument->GetTitle(titleBuffer);
|
|
|
|
aTitle = titleBuffer;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebBrowserPersistLocalDocument::GetReferrer(nsAString& aReferrer) {
|
|
|
|
mDocument->GetReferrer(aReferrer);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebBrowserPersistLocalDocument::GetContentDisposition(nsAString& aCD) {
|
2019-01-02 16:26:56 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> window = mDocument->GetWindow();
|
2015-08-14 04:08:09 +03:00
|
|
|
if (NS_WARN_IF(!window)) {
|
|
|
|
aCD.SetIsVoid(true);
|
|
|
|
return NS_OK;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-07-25 02:47:43 +03:00
|
|
|
nsCOMPtr<nsIDOMWindowUtils> utils =
|
|
|
|
nsGlobalWindowOuter::Cast(window)->WindowUtils();
|
2015-08-14 04:08:09 +03:00
|
|
|
nsresult rv =
|
2015-08-06 00:25:39 +03:00
|
|
|
utils->GetDocumentMetadata(NS_LITERAL_STRING("content-disposition"), aCD);
|
2015-08-14 04:08:09 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aCD.SetIsVoid(true);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-14 04:08:09 +03:00
|
|
|
return NS_OK;
|
2015-08-06 00:25:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebBrowserPersistLocalDocument::GetCacheKey(uint32_t* aKey) {
|
2018-03-06 06:19:42 +03:00
|
|
|
*aKey = 0;
|
2015-08-14 04:08:09 +03:00
|
|
|
nsCOMPtr<nsISHEntry> history = GetHistory();
|
2018-03-06 06:19:42 +03:00
|
|
|
if (history) {
|
|
|
|
history->GetCacheKey(aKey);
|
2015-08-06 00:25:39 +03:00
|
|
|
}
|
2018-03-06 06:19:42 +03:00
|
|
|
return NS_OK;
|
2015-08-06 00:25:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebBrowserPersistLocalDocument::GetPostData(nsIInputStream** aStream) {
|
2015-08-14 04:08:09 +03:00
|
|
|
nsCOMPtr<nsISHEntry> history = GetHistory();
|
|
|
|
if (!history) {
|
|
|
|
*aStream = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
return history->GetPostData(aStream);
|
|
|
|
}
|
|
|
|
|
2018-06-22 17:41:39 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
WebBrowserPersistLocalDocument::GetPrincipal(nsIPrincipal** aPrincipal) {
|
|
|
|
nsCOMPtr<nsIPrincipal> nodePrincipal = mDocument->NodePrincipal();
|
|
|
|
nodePrincipal.forget(aPrincipal);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-14 04:08:09 +03:00
|
|
|
already_AddRefed<nsISHEntry> WebBrowserPersistLocalDocument::GetHistory() {
|
2019-01-02 16:26:56 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> window = mDocument->GetWindow();
|
2015-08-14 04:08:09 +03:00
|
|
|
if (NS_WARN_IF(!window)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
nsCOMPtr<nsIWebNavigation> webNav = do_GetInterface(window);
|
2015-08-14 04:08:09 +03:00
|
|
|
if (NS_WARN_IF(!webNav)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
nsCOMPtr<nsIWebPageDescriptor> desc = do_QueryInterface(webNav);
|
2015-08-14 04:08:09 +03:00
|
|
|
if (NS_WARN_IF(!desc)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
nsCOMPtr<nsISupports> curDesc;
|
|
|
|
nsresult rv = desc->GetCurrentDescriptor(getter_AddRefs(curDesc));
|
2015-08-14 04:08:09 +03:00
|
|
|
// This can fail if, e.g., the document is a Print Preview.
|
|
|
|
if (NS_FAILED(rv) || NS_WARN_IF(!curDesc)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
nsCOMPtr<nsISHEntry> history = do_QueryInterface(curDesc);
|
2015-08-14 04:08:09 +03:00
|
|
|
return history.forget();
|
2015-08-06 00:25:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NotNull<const Encoding*> WebBrowserPersistLocalDocument::GetCharacterSet()
|
|
|
|
const {
|
|
|
|
return mDocument->GetDocumentCharacterSet();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t WebBrowserPersistLocalDocument::GetPersistFlags() const {
|
|
|
|
return mPersistFlags;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIURI> WebBrowserPersistLocalDocument::GetBaseURI() const {
|
|
|
|
return mDocument->GetBaseURI();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Helper class for ReadResources().
|
|
|
|
class ResourceReader final : public nsIWebBrowserPersistDocumentReceiver {
|
|
|
|
public:
|
|
|
|
ResourceReader(WebBrowserPersistLocalDocument* aParent,
|
|
|
|
nsIWebBrowserPersistResourceVisitor* aVisitor);
|
2018-05-30 05:58:49 +03:00
|
|
|
nsresult OnWalkDOMNode(nsINode* aNode);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
// This is called both to indicate the end of the document walk
|
|
|
|
// and when a subdocument is (maybe asynchronously) sent to the
|
|
|
|
// visitor. The call to EndVisit needs to happen after both of
|
|
|
|
// those have finished.
|
|
|
|
void DocumentDone(nsresult aStatus);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
NS_DECL_NSIWEBBROWSERPERSISTDOCUMENTRECEIVER
|
|
|
|
NS_DECL_ISUPPORTS
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WebBrowserPersistLocalDocument> mParent;
|
2015-08-06 00:25:39 +03:00
|
|
|
nsCOMPtr<nsIWebBrowserPersistResourceVisitor> mVisitor;
|
|
|
|
nsCOMPtr<nsIURI> mCurrentBaseURI;
|
|
|
|
uint32_t mPersistFlags;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
// The number of DocumentDone calls after which EndVisit will be
|
|
|
|
// called on the visitor. Counts the main document if it's still
|
|
|
|
// being walked and any outstanding asynchronous subdocument
|
|
|
|
// StartPersistence calls.
|
|
|
|
size_t mOutstandingDocuments;
|
|
|
|
// Collects the status parameters to DocumentDone calls.
|
|
|
|
nsresult mEndStatus;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-11-09 17:48:11 +03:00
|
|
|
nsresult OnWalkURI(const nsACString& aURISpec,
|
|
|
|
nsContentPolicyType aContentPolicyType);
|
|
|
|
nsresult OnWalkURI(nsIURI* aURI, nsContentPolicyType aContentPolicyType);
|
2018-05-30 05:58:49 +03:00
|
|
|
nsresult OnWalkAttribute(Element* aElement,
|
2018-11-09 17:48:11 +03:00
|
|
|
nsContentPolicyType aContentPolicyType,
|
2015-08-06 00:25:39 +03:00
|
|
|
const char* aAttribute,
|
|
|
|
const char* aNamespaceURI = "");
|
2018-05-30 05:58:49 +03:00
|
|
|
nsresult OnWalkSubframe(nsINode* aNode);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
~ResourceReader();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
using IWBP = nsIWebBrowserPersist;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(ResourceReader, nsIWebBrowserPersistDocumentReceiver)
|
|
|
|
|
|
|
|
ResourceReader::ResourceReader(WebBrowserPersistLocalDocument* aParent,
|
|
|
|
nsIWebBrowserPersistResourceVisitor* aVisitor)
|
|
|
|
: mParent(aParent),
|
|
|
|
mVisitor(aVisitor),
|
|
|
|
mCurrentBaseURI(aParent->GetBaseURI()),
|
|
|
|
mPersistFlags(aParent->GetPersistFlags()),
|
|
|
|
mOutstandingDocuments(1),
|
|
|
|
mEndStatus(NS_OK) {
|
|
|
|
MOZ_ASSERT(mCurrentBaseURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
ResourceReader::~ResourceReader() { MOZ_ASSERT(mOutstandingDocuments == 0); }
|
|
|
|
|
|
|
|
void ResourceReader::DocumentDone(nsresult aStatus) {
|
|
|
|
MOZ_ASSERT(mOutstandingDocuments > 0);
|
|
|
|
if (NS_SUCCEEDED(mEndStatus)) {
|
|
|
|
mEndStatus = aStatus;
|
|
|
|
}
|
|
|
|
if (--mOutstandingDocuments == 0) {
|
|
|
|
mVisitor->EndVisit(mParent, mEndStatus);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 05:58:49 +03:00
|
|
|
nsresult ResourceReader::OnWalkSubframe(nsINode* aNode) {
|
2019-02-16 01:20:53 +03:00
|
|
|
RefPtr<nsFrameLoaderOwner> loaderOwner = do_QueryObject(aNode);
|
2015-08-06 00:25:39 +03:00
|
|
|
NS_ENSURE_STATE(loaderOwner);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsFrameLoader> loader = loaderOwner->GetFrameLoader();
|
2015-08-06 00:25:39 +03:00
|
|
|
NS_ENSURE_STATE(loader);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
++mOutstandingDocuments;
|
2015-08-06 17:44:16 +03:00
|
|
|
// Pass in 0 as the outer window ID so that we start
|
|
|
|
// persisting the root of this subframe, and not some other
|
|
|
|
// subframe child of this subframe.
|
2018-04-09 23:30:33 +03:00
|
|
|
ErrorResult err;
|
|
|
|
loader->StartPersistence(0, this, err);
|
|
|
|
nsresult rv = err.StealNSResult();
|
2015-08-06 00:25:39 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (rv == NS_ERROR_NO_CONTENT) {
|
|
|
|
// Just ignore frames with no content document.
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
// StartPersistence won't eventually call this if it failed,
|
|
|
|
// so this does so (to keep mOutstandingDocuments correct).
|
|
|
|
DocumentDone(rv);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ResourceReader::OnDocumentReady(nsIWebBrowserPersistDocument* aDocument) {
|
|
|
|
mVisitor->VisitDocument(mParent, aDocument);
|
|
|
|
DocumentDone(NS_OK);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ResourceReader::OnError(nsresult aFailure) {
|
|
|
|
DocumentDone(aFailure);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-11-09 17:48:11 +03:00
|
|
|
nsresult ResourceReader::OnWalkURI(nsIURI* aURI,
|
|
|
|
nsContentPolicyType aContentPolicyType) {
|
2015-08-06 00:25:39 +03:00
|
|
|
// Test if this URI should be persisted. By default
|
|
|
|
// we should assume the URI is persistable.
|
|
|
|
bool doNotPersistURI;
|
|
|
|
nsresult rv = NS_URIChainHasFlags(
|
|
|
|
aURI, nsIProtocolHandler::URI_NON_PERSISTABLE, &doNotPersistURI);
|
|
|
|
if (NS_SUCCEEDED(rv) && doNotPersistURI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString stringURI;
|
|
|
|
rv = aURI->GetSpec(stringURI);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-11-09 17:48:11 +03:00
|
|
|
return mVisitor->VisitResource(mParent, stringURI, aContentPolicyType);
|
2015-08-06 00:25:39 +03:00
|
|
|
}
|
|
|
|
|
2018-11-09 17:48:11 +03:00
|
|
|
nsresult ResourceReader::OnWalkURI(const nsACString& aURISpec,
|
|
|
|
nsContentPolicyType aContentPolicyType) {
|
2015-08-06 00:25:39 +03:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
|
2018-05-30 05:58:49 +03:00
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), aURISpec, mParent->GetCharacterSet(),
|
2015-08-06 00:25:39 +03:00
|
|
|
mCurrentBaseURI);
|
2019-04-03 16:56:05 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// We don't want to break saving a page in case of a malformed URI.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-01-21 02:33:06 +03:00
|
|
|
return OnWalkURI(uri, aContentPolicyType);
|
2015-08-06 00:25:39 +03:00
|
|
|
}
|
|
|
|
|
2018-05-30 05:58:49 +03:00
|
|
|
static void ExtractAttribute(Element* aElement, const char* aAttribute,
|
|
|
|
const char* aNamespaceURI, nsCString& aValue) {
|
2015-08-06 00:25:39 +03:00
|
|
|
// Find the named URI attribute on the (element) node and store
|
2018-05-30 05:58:49 +03:00
|
|
|
// a reference to the URI that maps onto a local file name
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-21 02:33:06 +03:00
|
|
|
RefPtr<nsDOMAttributeMap> attrMap = aElement->Attributes();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-05-30 05:58:49 +03:00
|
|
|
NS_ConvertASCIItoUTF16 namespaceURI(aNamespaceURI);
|
2015-08-06 00:25:39 +03:00
|
|
|
NS_ConvertASCIItoUTF16 attribute(aAttribute);
|
2018-11-09 17:48:11 +03:00
|
|
|
RefPtr<dom::Attr> attr = attrMap->GetNamedItemNS(namespaceURI, attribute);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (attr) {
|
|
|
|
nsAutoString value;
|
2018-01-21 20:07:31 +03:00
|
|
|
attr->GetValue(value);
|
2018-11-09 17:48:11 +03:00
|
|
|
CopyUTF16toUTF8(value, aValue);
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2015-08-06 00:25:39 +03:00
|
|
|
aValue.Truncate();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
}
|
|
|
|
|
2018-03-19 22:15:39 +03:00
|
|
|
nsresult ResourceReader::OnWalkAttribute(Element* aElement,
|
|
|
|
nsContentPolicyType aContentPolicyType,
|
|
|
|
const char* aAttribute,
|
2015-08-06 00:25:39 +03:00
|
|
|
const char* aNamespaceURI) {
|
|
|
|
nsAutoCString uriSpec;
|
|
|
|
ExtractAttribute(aElement, aAttribute, aNamespaceURI, uriSpec);
|
|
|
|
if (uriSpec.IsEmpty()) {
|
|
|
|
return NS_OK;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-11-09 17:48:11 +03:00
|
|
|
return OnWalkURI(uriSpec, aContentPolicyType);
|
2015-08-06 00:25:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult GetXMLStyleSheetLink(dom::ProcessingInstruction* aPI,
|
2018-05-30 05:58:49 +03:00
|
|
|
nsAString& aHref) {
|
2015-08-06 00:25:39 +03:00
|
|
|
nsAutoString data;
|
2018-11-09 17:48:11 +03:00
|
|
|
aPI->GetData(data);
|
2015-08-06 00:25:39 +03:00
|
|
|
|
2018-11-09 17:48:11 +03:00
|
|
|
nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::href, aHref);
|
|
|
|
return NS_OK;
|
2015-08-06 00:25:39 +03:00
|
|
|
}
|
|
|
|
|
2018-05-30 05:58:49 +03:00
|
|
|
nsresult ResourceReader::OnWalkDOMNode(nsINode* aNode) {
|
|
|
|
// Fixup xml-stylesheet processing instructions
|
|
|
|
if (auto nodeAsPI = dom::ProcessingInstruction::FromNode(aNode)) {
|
2018-11-09 17:48:11 +03:00
|
|
|
nsAutoString target;
|
|
|
|
nodeAsPI->GetTarget(target);
|
|
|
|
if (target.EqualsLiteral("xml-stylesheet")) {
|
2015-08-06 00:25:39 +03:00
|
|
|
nsAutoString href;
|
2018-11-09 17:48:11 +03:00
|
|
|
GetXMLStyleSheetLink(nodeAsPI, href);
|
|
|
|
if (!href.IsEmpty()) {
|
|
|
|
return OnWalkURI(NS_ConvertUTF16toUTF8(href),
|
|
|
|
nsIContentPolicy::TYPE_STYLESHEET);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
// Test the node to see if it's an image, frame, iframe, css, js
|
2018-05-30 05:58:49 +03:00
|
|
|
if (aNode->IsHTMLElement(nsGkAtoms::img)) {
|
2018-11-09 17:48:11 +03:00
|
|
|
return OnWalkAttribute(aNode->AsElement(), nsIContentPolicy::TYPE_IMAGE,
|
2018-11-30 13:46:48 +03:00
|
|
|
"src");
|
|
|
|
}
|
|
|
|
|
2018-05-30 05:58:49 +03:00
|
|
|
if (aNode->IsSVGElement(nsGkAtoms::img)) {
|
2018-11-09 17:48:11 +03:00
|
|
|
return OnWalkAttribute(aNode->AsElement(), nsIContentPolicy::TYPE_IMAGE,
|
2015-08-06 00:25:39 +03:00
|
|
|
"href", "http://www.w3.org/1999/xlink");
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-05-30 05:58:49 +03:00
|
|
|
if (aNode->IsAnyOfHTMLElements(nsGkAtoms::audio, nsGkAtoms::video)) {
|
2018-11-09 17:48:11 +03:00
|
|
|
return OnWalkAttribute(aNode->AsElement(), nsIContentPolicy::TYPE_MEDIA,
|
2018-11-30 13:46:48 +03:00
|
|
|
"src");
|
|
|
|
}
|
|
|
|
|
2018-05-30 05:58:49 +03:00
|
|
|
if (aNode->IsHTMLElement(nsGkAtoms::source)) {
|
2018-11-09 17:48:11 +03:00
|
|
|
return OnWalkAttribute(aNode->AsElement(), nsIContentPolicy::TYPE_MEDIA,
|
2018-11-30 13:46:48 +03:00
|
|
|
"src");
|
|
|
|
}
|
|
|
|
|
2018-05-30 05:58:49 +03:00
|
|
|
if (aNode->IsHTMLElement(nsGkAtoms::body)) {
|
2018-11-09 17:48:11 +03:00
|
|
|
return OnWalkAttribute(aNode->AsElement(), nsIContentPolicy::TYPE_IMAGE,
|
|
|
|
"background");
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-05-30 05:58:49 +03:00
|
|
|
if (aNode->IsHTMLElement(nsGkAtoms::table)) {
|
2018-11-09 17:48:11 +03:00
|
|
|
return OnWalkAttribute(aNode->AsElement(), nsIContentPolicy::TYPE_IMAGE,
|
|
|
|
"background");
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-05-30 05:58:49 +03:00
|
|
|
if (aNode->IsHTMLElement(nsGkAtoms::tr)) {
|
2018-11-09 17:48:11 +03:00
|
|
|
return OnWalkAttribute(aNode->AsElement(), nsIContentPolicy::TYPE_IMAGE,
|
|
|
|
"background");
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-05-30 05:58:49 +03:00
|
|
|
if (aNode->IsAnyOfHTMLElements(nsGkAtoms::td, nsGkAtoms::th)) {
|
2018-11-09 17:48:11 +03:00
|
|
|
return OnWalkAttribute(aNode->AsElement(), nsIContentPolicy::TYPE_IMAGE,
|
|
|
|
"background");
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-05-30 05:58:49 +03:00
|
|
|
if (aNode->IsHTMLElement(nsGkAtoms::script)) {
|
2018-11-09 17:48:11 +03:00
|
|
|
return OnWalkAttribute(aNode->AsElement(), nsIContentPolicy::TYPE_SCRIPT,
|
2018-11-30 13:46:48 +03:00
|
|
|
"src");
|
|
|
|
}
|
|
|
|
|
2018-05-30 05:58:49 +03:00
|
|
|
if (aNode->IsSVGElement(nsGkAtoms::script)) {
|
2018-11-09 17:48:11 +03:00
|
|
|
return OnWalkAttribute(aNode->AsElement(), nsIContentPolicy::TYPE_SCRIPT,
|
2015-08-06 00:25:39 +03:00
|
|
|
"href", "http://www.w3.org/1999/xlink");
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-05-30 05:58:49 +03:00
|
|
|
if (aNode->IsHTMLElement(nsGkAtoms::embed)) {
|
2018-11-09 17:48:11 +03:00
|
|
|
return OnWalkAttribute(aNode->AsElement(), nsIContentPolicy::TYPE_OBJECT,
|
2018-11-30 13:46:48 +03:00
|
|
|
"src");
|
|
|
|
}
|
|
|
|
|
2018-05-30 05:58:49 +03:00
|
|
|
if (aNode->IsHTMLElement(nsGkAtoms::object)) {
|
2018-11-09 17:48:11 +03:00
|
|
|
return OnWalkAttribute(aNode->AsElement(), nsIContentPolicy::TYPE_OBJECT,
|
2018-11-30 13:46:48 +03:00
|
|
|
"data");
|
|
|
|
}
|
|
|
|
|
2018-05-30 05:58:49 +03:00
|
|
|
if (auto nodeAsLink = dom::HTMLLinkElement::FromNode(aNode)) {
|
2015-08-06 00:25:39 +03:00
|
|
|
// Test if the link has a rel value indicating it to be a stylesheet
|
|
|
|
nsAutoString linkRel;
|
2017-10-07 00:15:03 +03:00
|
|
|
nodeAsLink->GetRel(linkRel);
|
|
|
|
if (!linkRel.IsEmpty()) {
|
2015-08-06 00:25:39 +03:00
|
|
|
nsReadingIterator<char16_t> start;
|
|
|
|
nsReadingIterator<char16_t> end;
|
|
|
|
nsReadingIterator<char16_t> current;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
linkRel.BeginReading(start);
|
|
|
|
linkRel.EndReading(end);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
// Walk through space delimited string looking for "stylesheet"
|
|
|
|
for (current = start; current != end; ++current) {
|
|
|
|
// Ignore whitespace
|
|
|
|
if (nsCRT::IsAsciiSpace(*current)) {
|
|
|
|
continue;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
|
|
|
// Grab the next space delimited word
|
|
|
|
nsReadingIterator<char16_t> startWord = current;
|
|
|
|
do {
|
|
|
|
++current;
|
|
|
|
} while (current != end && !nsCRT::IsAsciiSpace(*current));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
// Store the link for fix up if it says "stylesheet"
|
|
|
|
if (Substring(startWord, current)
|
|
|
|
.LowerCaseEqualsLiteral("stylesheet")) {
|
2018-11-09 17:48:11 +03:00
|
|
|
OnWalkAttribute(aNode->AsElement(), nsIContentPolicy::TYPE_STYLESHEET,
|
|
|
|
"href");
|
2015-08-06 00:25:39 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (current == end) {
|
|
|
|
break;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
2018-05-30 05:58:49 +03:00
|
|
|
if (aNode->IsHTMLElement(nsGkAtoms::frame)) {
|
2015-08-06 00:25:39 +03:00
|
|
|
return OnWalkSubframe(aNode);
|
|
|
|
}
|
|
|
|
|
2018-05-30 05:58:49 +03:00
|
|
|
if (aNode->IsHTMLElement(nsGkAtoms::iframe) &&
|
2017-10-05 06:41:24 +03:00
|
|
|
!(mPersistFlags & IWBP::PERSIST_FLAGS_IGNORE_IFRAMES)) {
|
2015-08-06 00:25:39 +03:00
|
|
|
return OnWalkSubframe(aNode);
|
|
|
|
}
|
|
|
|
|
2018-05-30 05:58:49 +03:00
|
|
|
auto nodeAsInput = dom::HTMLInputElement::FromNode(aNode);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (nodeAsInput) {
|
2018-11-09 17:48:11 +03:00
|
|
|
return OnWalkAttribute(aNode->AsElement(), nsIContentPolicy::TYPE_IMAGE,
|
|
|
|
"src");
|
2015-08-06 00:25:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper class for node rewriting in writeContent().
|
|
|
|
class PersistNodeFixup final : public nsIDocumentEncoderNodeFixup {
|
|
|
|
public:
|
|
|
|
PersistNodeFixup(WebBrowserPersistLocalDocument* aParent,
|
|
|
|
nsIWebBrowserPersistURIMap* aMap, nsIURI* aTargetURI);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIDOCUMENTENCODERNODEFIXUP
|
|
|
|
private:
|
2016-11-22 15:56:19 +03:00
|
|
|
virtual ~PersistNodeFixup() = default;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WebBrowserPersistLocalDocument> mParent;
|
2015-08-06 00:25:39 +03:00
|
|
|
nsClassHashtable<nsCStringHashKey, nsCString> mMap;
|
|
|
|
nsCOMPtr<nsIURI> mCurrentBaseURI;
|
|
|
|
nsCOMPtr<nsIURI> mTargetBaseURI;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
bool IsFlagSet(uint32_t aFlag) const {
|
|
|
|
return mParent->GetPersistFlags() & aFlag;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult GetNodeToFixup(nsINode* aNodeIn, nsINode** aNodeOut);
|
2015-08-06 00:25:39 +03:00
|
|
|
nsresult FixupURI(nsAString& aURI);
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult FixupAttribute(nsINode* aNode, const char* aAttribute,
|
2015-08-06 00:25:39 +03:00
|
|
|
const char* aNamespaceURI = "");
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult FixupAnchor(nsINode* aNode);
|
2018-03-19 22:15:39 +03:00
|
|
|
nsresult FixupXMLStyleSheetLink(dom::ProcessingInstruction* aPI,
|
2015-08-06 00:25:39 +03:00
|
|
|
const nsAString& aHref);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
using IWBP = nsIWebBrowserPersist;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(PersistNodeFixup, nsIDocumentEncoderNodeFixup)
|
|
|
|
|
|
|
|
PersistNodeFixup::PersistNodeFixup(WebBrowserPersistLocalDocument* aParent,
|
|
|
|
nsIWebBrowserPersistURIMap* aMap,
|
|
|
|
nsIURI* aTargetURI)
|
|
|
|
: mParent(aParent),
|
|
|
|
mCurrentBaseURI(aParent->GetBaseURI()),
|
|
|
|
mTargetBaseURI(aTargetURI) {
|
|
|
|
if (aMap) {
|
|
|
|
uint32_t mapSize;
|
|
|
|
nsresult rv = aMap->GetNumMappedURIs(&mapSize);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
for (uint32_t i = 0; i < mapSize; ++i) {
|
|
|
|
nsAutoCString urlFrom;
|
2016-11-22 15:55:44 +03:00
|
|
|
auto* urlTo = new nsCString();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
rv = aMap->GetURIMapping(i, urlFrom, *urlTo);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mMap.Put(urlFrom, urlTo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult PersistNodeFixup::GetNodeToFixup(nsINode* aNodeIn,
|
|
|
|
nsINode** aNodeOut) {
|
2015-08-06 00:25:39 +03:00
|
|
|
// Avoid mixups in FixupNode that could leak objects; this goes
|
|
|
|
// against the usual out parameter convention, but it's a private
|
|
|
|
// method so shouldn't be a problem.
|
|
|
|
MOZ_ASSERT(!*aNodeOut);
|
2018-01-30 07:10:50 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
if (!IsFlagSet(IWBP::PERSIST_FLAGS_FIXUP_ORIGINAL_DOM)) {
|
2018-01-30 07:10:50 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
*aNodeOut = aNodeIn->CloneNode(false, rv).take();
|
|
|
|
return rv.StealNSResult();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-01-30 07:10:50 +03:00
|
|
|
NS_ADDREF(*aNodeOut = aNodeIn);
|
2015-08-06 00:25:39 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult PersistNodeFixup::FixupURI(nsAString& aURI) {
|
|
|
|
// get the current location of the file (absolutized)
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri), aURI, mParent->GetCharacterSet(),
|
2017-06-18 14:37:50 +03:00
|
|
|
mCurrentBaseURI);
|
2015-08-06 00:25:39 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsAutoCString spec;
|
|
|
|
rv = uri->GetSpec(spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
const nsCString* replacement = mMap.Get(spec);
|
|
|
|
if (!replacement) {
|
|
|
|
// Note that most callers ignore this "failure".
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
if (!replacement->IsEmpty()) {
|
|
|
|
aURI = NS_ConvertUTF8toUTF16(*replacement);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult PersistNodeFixup::FixupAttribute(nsINode* aNode,
|
2015-08-06 00:25:39 +03:00
|
|
|
const char* aAttribute,
|
|
|
|
const char* aNamespaceURI) {
|
2018-01-30 07:10:50 +03:00
|
|
|
MOZ_ASSERT(aNode->IsElement());
|
|
|
|
dom::Element* element = aNode->AsElement();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-21 02:33:06 +03:00
|
|
|
RefPtr<nsDOMAttributeMap> attrMap = element->Attributes();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
NS_ConvertASCIItoUTF16 attribute(aAttribute);
|
|
|
|
NS_ConvertASCIItoUTF16 namespaceURI(aNamespaceURI);
|
2018-01-21 02:33:06 +03:00
|
|
|
RefPtr<dom::Attr> attr = attrMap->GetNamedItemNS(namespaceURI, attribute);
|
|
|
|
nsresult rv = NS_OK;
|
2015-08-06 00:25:39 +03:00
|
|
|
if (attr) {
|
|
|
|
nsString uri;
|
|
|
|
attr->GetValue(uri);
|
|
|
|
rv = FixupURI(uri);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2018-02-01 22:21:14 +03:00
|
|
|
attr->SetValue(uri, IgnoreErrors());
|
2015-08-06 00:25:39 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult PersistNodeFixup::FixupAnchor(nsINode* aNode) {
|
2015-08-06 00:25:39 +03:00
|
|
|
if (IsFlagSet(IWBP::PERSIST_FLAGS_DONT_FIXUP_LINKS)) {
|
|
|
|
return NS_OK;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-01-30 07:10:50 +03:00
|
|
|
MOZ_ASSERT(aNode->IsElement());
|
|
|
|
dom::Element* element = aNode->AsElement();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-21 02:26:41 +03:00
|
|
|
RefPtr<nsDOMAttributeMap> attrMap = element->Attributes();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
// Make all anchor links absolute so they point off onto the Internet
|
|
|
|
nsString attribute(NS_LITERAL_STRING("href"));
|
2018-01-21 02:26:41 +03:00
|
|
|
RefPtr<dom::Attr> attr = attrMap->GetNamedItem(attribute);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (attr) {
|
|
|
|
nsString oldValue;
|
|
|
|
attr->GetValue(oldValue);
|
|
|
|
NS_ConvertUTF16toUTF8 oldCValue(oldValue);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
// Skip empty values and self-referencing bookmarks
|
|
|
|
if (oldCValue.IsEmpty() || oldCValue.CharAt(0) == '#') {
|
|
|
|
return NS_OK;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
// if saving file to same location, we don't need to do any fixup
|
|
|
|
bool isEqual;
|
|
|
|
if (mTargetBaseURI &&
|
|
|
|
NS_SUCCEEDED(mCurrentBaseURI->Equals(mTargetBaseURI, &isEqual)) &&
|
|
|
|
isEqual) {
|
|
|
|
return NS_OK;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
nsCOMPtr<nsIURI> relativeURI;
|
|
|
|
relativeURI = IsFlagSet(IWBP::PERSIST_FLAGS_FIXUP_LINKS_TO_DESTINATION)
|
|
|
|
? mTargetBaseURI
|
|
|
|
: mCurrentBaseURI;
|
|
|
|
// Make a new URI to replace the current one
|
|
|
|
nsCOMPtr<nsIURI> newURI;
|
2018-01-21 02:26:41 +03:00
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(newURI), oldCValue,
|
|
|
|
mParent->GetCharacterSet(), relativeURI);
|
2016-08-31 02:08:52 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && newURI) {
|
2018-11-30 13:46:48 +03:00
|
|
|
Unused
|
2018-02-26 22:43:46 +03:00
|
|
|
<< NS_MutateURI(newURI).SetUserPass(EmptyCString()).Finalize(newURI);
|
2015-08-06 00:25:39 +03:00
|
|
|
nsAutoCString uriSpec;
|
2016-08-31 02:08:52 +03:00
|
|
|
rv = newURI->GetSpec(uriSpec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-02-01 22:21:14 +03:00
|
|
|
attr->SetValue(NS_ConvertUTF8toUTF16(uriSpec), IgnoreErrors());
|
2015-08-06 00:25:39 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
2018-01-30 07:10:50 +03:00
|
|
|
return NS_OK;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
2018-01-21 02:26:41 +03:00
|
|
|
static void AppendXMLAttr(const nsAString& key, const nsAString& aValue,
|
|
|
|
nsAString& aBuffer) {
|
|
|
|
if (!aBuffer.IsEmpty()) {
|
2015-08-06 00:25:39 +03:00
|
|
|
aBuffer.Append(' ');
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
aBuffer.Append(key);
|
2018-01-21 02:26:41 +03:00
|
|
|
aBuffer.AppendLiteral(R"(=")");
|
|
|
|
for (size_t i = 0; i < aValue.Length(); ++i) {
|
2015-08-06 00:25:39 +03:00
|
|
|
switch (aValue[i]) {
|
|
|
|
case '&':
|
2018-01-21 02:26:41 +03:00
|
|
|
aBuffer.AppendLiteral("&");
|
|
|
|
break;
|
2015-08-06 00:25:39 +03:00
|
|
|
case '<':
|
|
|
|
aBuffer.AppendLiteral("<");
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
2015-08-06 00:25:39 +03:00
|
|
|
case '>':
|
|
|
|
aBuffer.AppendLiteral(">");
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
2015-08-06 00:25:39 +03:00
|
|
|
case '"':
|
|
|
|
aBuffer.AppendLiteral(""");
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
2018-01-21 02:26:41 +03:00
|
|
|
default:
|
2015-08-06 00:25:39 +03:00
|
|
|
aBuffer.Append(aValue[i]);
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
aBuffer.Append('"');
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
|
|
|
nsresult PersistNodeFixup::FixupXMLStyleSheetLink(
|
|
|
|
dom::ProcessingInstruction* aPI, const nsAString& aHref) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aPI);
|
|
|
|
|
|
|
|
nsAutoString data;
|
|
|
|
aPI->GetData(data);
|
|
|
|
|
|
|
|
nsAutoString href;
|
|
|
|
nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::href, href);
|
|
|
|
|
|
|
|
// Construct and set a new data value for the xml-stylesheet
|
2016-08-31 02:08:52 +03:00
|
|
|
if (!aHref.IsEmpty() && !href.IsEmpty()) {
|
2018-02-26 22:43:46 +03:00
|
|
|
nsAutoString alternate;
|
|
|
|
nsAutoString charset;
|
|
|
|
nsAutoString title;
|
2015-08-06 00:25:39 +03:00
|
|
|
nsAutoString type;
|
2018-02-01 22:21:14 +03:00
|
|
|
nsAutoString media;
|
2015-08-06 00:25:39 +03:00
|
|
|
|
|
|
|
nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::alternate,
|
|
|
|
alternate);
|
|
|
|
nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::charset, charset);
|
|
|
|
nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::title, title);
|
|
|
|
nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::type, type);
|
|
|
|
nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::media, media);
|
|
|
|
|
|
|
|
nsAutoString newData;
|
|
|
|
AppendXMLAttr(NS_LITERAL_STRING("href"), aHref, newData);
|
|
|
|
if (!title.IsEmpty()) {
|
|
|
|
AppendXMLAttr(NS_LITERAL_STRING("title"), title, newData);
|
|
|
|
}
|
|
|
|
if (!media.IsEmpty()) {
|
|
|
|
AppendXMLAttr(NS_LITERAL_STRING("media"), media, newData);
|
|
|
|
}
|
2018-03-19 22:15:39 +03:00
|
|
|
if (!type.IsEmpty()) {
|
2015-08-06 00:25:39 +03:00
|
|
|
AppendXMLAttr(NS_LITERAL_STRING("type"), type, newData);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
if (!charset.IsEmpty()) {
|
|
|
|
AppendXMLAttr(NS_LITERAL_STRING("charset"), charset, newData);
|
|
|
|
}
|
|
|
|
if (!alternate.IsEmpty()) {
|
|
|
|
AppendXMLAttr(NS_LITERAL_STRING("alternate"), alternate, newData);
|
|
|
|
}
|
2018-03-19 22:18:07 +03:00
|
|
|
aPI->SetData(newData, IgnoreErrors());
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
2018-03-19 22:18:07 +03:00
|
|
|
return NS_OK;
|
2015-08-06 00:25:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2018-01-30 07:10:50 +03:00
|
|
|
PersistNodeFixup::FixupNode(nsINode* aNodeIn, bool* aSerializeCloneKids,
|
|
|
|
nsINode** aNodeOut) {
|
2015-08-06 00:25:39 +03:00
|
|
|
*aNodeOut = nullptr;
|
|
|
|
*aSerializeCloneKids = false;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-30 07:10:50 +03:00
|
|
|
uint16_t type = aNodeIn->NodeType();
|
2018-01-30 07:10:53 +03:00
|
|
|
if (type != nsINode::ELEMENT_NODE &&
|
|
|
|
type != nsINode::PROCESSING_INSTRUCTION_NODE) {
|
2015-08-06 00:25:39 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-19 22:15:39 +03:00
|
|
|
MOZ_ASSERT(aNodeIn->IsContent());
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
// Fixup xml-stylesheet processing instructions
|
2018-03-22 00:39:04 +03:00
|
|
|
if (auto nodeAsPI = dom::ProcessingInstruction::FromNode(aNodeIn)) {
|
2015-08-06 00:25:39 +03:00
|
|
|
nsAutoString target;
|
|
|
|
nodeAsPI->GetTarget(target);
|
|
|
|
if (target.EqualsLiteral("xml-stylesheet")) {
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult rv = GetNodeToFixup(aNodeIn, aNodeOut);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && *aNodeOut) {
|
2018-03-19 22:15:39 +03:00
|
|
|
MOZ_ASSERT((*aNodeOut)->IsProcessingInstruction());
|
|
|
|
auto nodeAsPI = static_cast<dom::ProcessingInstruction*>(*aNodeOut);
|
2015-08-06 00:25:39 +03:00
|
|
|
nsAutoString href;
|
|
|
|
GetXMLStyleSheetLink(nodeAsPI, href);
|
|
|
|
if (!href.IsEmpty()) {
|
|
|
|
FixupURI(href);
|
2018-03-19 22:15:39 +03:00
|
|
|
FixupXMLStyleSheetLink(nodeAsPI, href);
|
2015-08-06 00:25:39 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
2017-11-10 05:22:44 +03:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aNodeIn);
|
|
|
|
if (!content) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
// BASE elements are replaced by a comment so relative links are not hosed.
|
2017-11-10 05:22:44 +03:00
|
|
|
if (!IsFlagSet(IWBP::PERSIST_FLAGS_NO_BASE_TAG_MODIFICATIONS) &&
|
|
|
|
content->IsHTMLElement(nsGkAtoms::base)) {
|
|
|
|
// Base uses HTMLSharedElement, which would be awkward to implement
|
|
|
|
// FromContent on, since it represents multiple elements. Since we've
|
|
|
|
// already checked IsHTMLElement here, just cast as we were doing.
|
|
|
|
auto* base = static_cast<dom::HTMLSharedElement*>(content.get());
|
2019-01-02 16:05:23 +03:00
|
|
|
dom::Document* ownerDoc = base->OwnerDoc();
|
2018-01-30 07:10:52 +03:00
|
|
|
|
|
|
|
nsAutoString href;
|
|
|
|
base->GetHref(href); // Doesn't matter if this fails
|
|
|
|
nsAutoString commentText;
|
|
|
|
commentText.AssignLiteral(" base ");
|
|
|
|
if (!href.IsEmpty()) {
|
|
|
|
commentText +=
|
|
|
|
NS_LITERAL_STRING("href=\"") + href + NS_LITERAL_STRING("\" ");
|
2015-08-06 00:25:39 +03:00
|
|
|
}
|
2018-01-30 07:10:52 +03:00
|
|
|
*aNodeOut = ownerDoc->CreateComment(commentText).take();
|
2015-08-06 00:25:39 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix up href and file links in the elements
|
2018-03-22 00:39:04 +03:00
|
|
|
RefPtr<dom::HTMLAnchorElement> nodeAsAnchor =
|
|
|
|
dom::HTMLAnchorElement::FromNode(content);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (nodeAsAnchor) {
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult rv = GetNodeToFixup(aNodeIn, aNodeOut);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && *aNodeOut) {
|
|
|
|
FixupAnchor(*aNodeOut);
|
|
|
|
}
|
|
|
|
return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
2018-03-22 00:39:04 +03:00
|
|
|
RefPtr<dom::HTMLAreaElement> nodeAsArea =
|
|
|
|
dom::HTMLAreaElement::FromNode(content);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (nodeAsArea) {
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult rv = GetNodeToFixup(aNodeIn, aNodeOut);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && *aNodeOut) {
|
|
|
|
FixupAnchor(*aNodeOut);
|
|
|
|
}
|
|
|
|
return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
|
|
|
if (content->IsHTMLElement(nsGkAtoms::body)) {
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult rv = GetNodeToFixup(aNodeIn, aNodeOut);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && *aNodeOut) {
|
|
|
|
FixupAttribute(*aNodeOut, "background");
|
|
|
|
}
|
|
|
|
return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
|
|
|
if (content->IsHTMLElement(nsGkAtoms::table)) {
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult rv = GetNodeToFixup(aNodeIn, aNodeOut);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && *aNodeOut) {
|
|
|
|
FixupAttribute(*aNodeOut, "background");
|
|
|
|
}
|
|
|
|
return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
|
|
|
if (content->IsHTMLElement(nsGkAtoms::tr)) {
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult rv = GetNodeToFixup(aNodeIn, aNodeOut);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && *aNodeOut) {
|
|
|
|
FixupAttribute(*aNodeOut, "background");
|
|
|
|
}
|
|
|
|
return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
|
|
|
if (content->IsAnyOfHTMLElements(nsGkAtoms::td, nsGkAtoms::th)) {
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult rv = GetNodeToFixup(aNodeIn, aNodeOut);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && *aNodeOut) {
|
|
|
|
FixupAttribute(*aNodeOut, "background");
|
|
|
|
}
|
|
|
|
return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2017-10-06 23:27:23 +03:00
|
|
|
if (content->IsHTMLElement(nsGkAtoms::img)) {
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult rv = GetNodeToFixup(aNodeIn, aNodeOut);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && *aNodeOut) {
|
|
|
|
// Disable image loads
|
|
|
|
nsCOMPtr<nsIImageLoadingContent> imgCon = do_QueryInterface(*aNodeOut);
|
|
|
|
if (imgCon) {
|
|
|
|
imgCon->SetLoadingEnabled(false);
|
|
|
|
}
|
|
|
|
FixupAnchor(*aNodeOut);
|
|
|
|
FixupAttribute(*aNodeOut, "src");
|
|
|
|
}
|
|
|
|
return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
2018-02-15 08:14:06 +03:00
|
|
|
if (content->IsAnyOfHTMLElements(nsGkAtoms::audio, nsGkAtoms::video)) {
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult rv = GetNodeToFixup(aNodeIn, aNodeOut);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && *aNodeOut) {
|
|
|
|
FixupAttribute(*aNodeOut, "src");
|
|
|
|
}
|
|
|
|
return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
2017-10-28 01:00:42 +03:00
|
|
|
if (content->IsHTMLElement(nsGkAtoms::source)) {
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult rv = GetNodeToFixup(aNodeIn, aNodeOut);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && *aNodeOut) {
|
|
|
|
FixupAttribute(*aNodeOut, "src");
|
|
|
|
}
|
|
|
|
return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
|
|
|
if (content->IsSVGElement(nsGkAtoms::img)) {
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult rv = GetNodeToFixup(aNodeIn, aNodeOut);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && *aNodeOut) {
|
|
|
|
// Disable image loads
|
|
|
|
nsCOMPtr<nsIImageLoadingContent> imgCon = do_QueryInterface(*aNodeOut);
|
|
|
|
if (imgCon) imgCon->SetLoadingEnabled(false);
|
|
|
|
|
2018-01-30 07:10:50 +03:00
|
|
|
// FixupAnchor(*aNodeOut); // XXXjwatt: is this line needed?
|
2015-08-06 00:25:39 +03:00
|
|
|
FixupAttribute(*aNodeOut, "href", "http://www.w3.org/1999/xlink");
|
|
|
|
}
|
|
|
|
return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
|
|
|
if (content->IsHTMLElement(nsGkAtoms::script)) {
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult rv = GetNodeToFixup(aNodeIn, aNodeOut);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && *aNodeOut) {
|
|
|
|
FixupAttribute(*aNodeOut, "src");
|
|
|
|
}
|
|
|
|
return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
2017-08-09 22:05:08 +03:00
|
|
|
if (content->IsSVGElement(nsGkAtoms::script)) {
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult rv = GetNodeToFixup(aNodeIn, aNodeOut);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && *aNodeOut) {
|
|
|
|
FixupAttribute(*aNodeOut, "href", "http://www.w3.org/1999/xlink");
|
|
|
|
}
|
|
|
|
return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
2017-09-23 03:05:12 +03:00
|
|
|
if (content->IsHTMLElement(nsGkAtoms::embed)) {
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult rv = GetNodeToFixup(aNodeIn, aNodeOut);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && *aNodeOut) {
|
|
|
|
FixupAttribute(*aNodeOut, "src");
|
|
|
|
}
|
|
|
|
return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
2017-10-07 00:15:03 +03:00
|
|
|
if (content->IsHTMLElement(nsGkAtoms::object)) {
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult rv = GetNodeToFixup(aNodeIn, aNodeOut);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && *aNodeOut) {
|
|
|
|
FixupAttribute(*aNodeOut, "data");
|
|
|
|
}
|
|
|
|
return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2017-09-29 05:06:14 +03:00
|
|
|
if (content->IsHTMLElement(nsGkAtoms::link)) {
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult rv = GetNodeToFixup(aNodeIn, aNodeOut);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && *aNodeOut) {
|
|
|
|
// First see if the link represents linked content
|
|
|
|
rv = FixupAttribute(*aNodeOut, "href");
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Perhaps this link is actually an anchor to related content
|
|
|
|
FixupAnchor(*aNodeOut);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
// TODO if "type" attribute == "text/css"
|
|
|
|
// fixup stylesheet
|
|
|
|
}
|
|
|
|
return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
2017-10-05 06:41:24 +03:00
|
|
|
if (content->IsHTMLElement(nsGkAtoms::frame)) {
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult rv = GetNodeToFixup(aNodeIn, aNodeOut);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && *aNodeOut) {
|
|
|
|
FixupAttribute(*aNodeOut, "src");
|
|
|
|
}
|
|
|
|
return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
2018-03-22 00:39:04 +03:00
|
|
|
if (content->IsHTMLElement(nsGkAtoms::iframe)) {
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult rv = GetNodeToFixup(aNodeIn, aNodeOut);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && *aNodeOut) {
|
|
|
|
FixupAttribute(*aNodeOut, "src");
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2016-11-15 20:46:32 +03:00
|
|
|
RefPtr<dom::HTMLInputElement> nodeAsInput =
|
2018-03-22 00:39:04 +03:00
|
|
|
dom::HTMLInputElement::FromNodeOrNull(content);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (nodeAsInput) {
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult rv = GetNodeToFixup(aNodeIn, aNodeOut);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && *aNodeOut) {
|
|
|
|
// Disable image loads
|
|
|
|
nsCOMPtr<nsIImageLoadingContent> imgCon = do_QueryInterface(*aNodeOut);
|
|
|
|
if (imgCon) {
|
|
|
|
imgCon->SetLoadingEnabled(false);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
FixupAttribute(*aNodeOut, "src");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
nsAutoString valueStr;
|
|
|
|
NS_NAMED_LITERAL_STRING(valueAttr, "value");
|
|
|
|
// Update element node attributes with user-entered form state
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<dom::HTMLInputElement> outElt =
|
2018-03-22 00:39:04 +03:00
|
|
|
dom::HTMLInputElement::FromNode((*aNodeOut)->AsContent());
|
2015-08-06 00:25:39 +03:00
|
|
|
nsCOMPtr<nsIFormControl> formControl = do_QueryInterface(*aNodeOut);
|
2017-04-01 05:49:00 +03:00
|
|
|
switch (formControl->ControlType()) {
|
2015-08-06 00:25:39 +03:00
|
|
|
case NS_FORM_INPUT_EMAIL:
|
|
|
|
case NS_FORM_INPUT_SEARCH:
|
|
|
|
case NS_FORM_INPUT_TEXT:
|
|
|
|
case NS_FORM_INPUT_TEL:
|
|
|
|
case NS_FORM_INPUT_URL:
|
|
|
|
case NS_FORM_INPUT_NUMBER:
|
|
|
|
case NS_FORM_INPUT_RANGE:
|
|
|
|
case NS_FORM_INPUT_DATE:
|
|
|
|
case NS_FORM_INPUT_TIME:
|
|
|
|
case NS_FORM_INPUT_COLOR:
|
2016-11-15 20:46:32 +03:00
|
|
|
nodeAsInput->GetValue(valueStr, dom::CallerType::System);
|
2015-08-06 00:25:39 +03:00
|
|
|
// Avoid superfluous value="" serialization
|
2017-02-02 18:32:58 +03:00
|
|
|
if (valueStr.IsEmpty()) {
|
2018-02-01 22:21:14 +03:00
|
|
|
outElt->RemoveAttribute(valueAttr, IgnoreErrors());
|
2017-02-02 18:32:58 +03:00
|
|
|
} else {
|
2018-02-01 22:21:14 +03:00
|
|
|
outElt->SetAttribute(valueAttr, valueStr, IgnoreErrors());
|
2017-02-02 18:32:58 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
break;
|
|
|
|
case NS_FORM_INPUT_CHECKBOX:
|
|
|
|
case NS_FORM_INPUT_RADIO: {
|
2016-11-15 20:46:32 +03:00
|
|
|
bool checked = nodeAsInput->Checked();
|
2018-02-01 22:21:14 +03:00
|
|
|
outElt->SetDefaultChecked(checked, IgnoreErrors());
|
2015-08-06 00:25:39 +03:00
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
2018-03-22 00:39:04 +03:00
|
|
|
dom::HTMLTextAreaElement* nodeAsTextArea =
|
|
|
|
dom::HTMLTextAreaElement::FromNode(content);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (nodeAsTextArea) {
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult rv = GetNodeToFixup(aNodeIn, aNodeOut);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && *aNodeOut) {
|
|
|
|
// Tell the document encoder to serialize the text child we create below
|
|
|
|
*aSerializeCloneKids = true;
|
|
|
|
|
|
|
|
nsAutoString valueStr;
|
|
|
|
nodeAsTextArea->GetValue(valueStr);
|
|
|
|
|
2018-02-01 22:21:14 +03:00
|
|
|
(*aNodeOut)->SetTextContent(valueStr, IgnoreErrors());
|
2015-08-06 00:25:39 +03:00
|
|
|
}
|
|
|
|
return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-03-22 00:39:04 +03:00
|
|
|
dom::HTMLOptionElement* nodeAsOption =
|
|
|
|
dom::HTMLOptionElement::FromNode(content);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (nodeAsOption) {
|
2018-01-30 07:10:50 +03:00
|
|
|
nsresult rv = GetNodeToFixup(aNodeIn, aNodeOut);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && *aNodeOut) {
|
2018-01-30 07:10:50 +03:00
|
|
|
dom::HTMLOptionElement* outElt =
|
2018-03-22 00:39:04 +03:00
|
|
|
dom::HTMLOptionElement::FromNode((*aNodeOut)->AsContent());
|
2017-11-09 04:43:09 +03:00
|
|
|
bool selected = nodeAsOption->Selected();
|
2018-02-01 22:21:14 +03:00
|
|
|
outElt->SetDefaultSelected(selected, IgnoreErrors());
|
2015-08-06 00:25:39 +03:00
|
|
|
}
|
|
|
|
return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // unnamed namespace
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebBrowserPersistLocalDocument::ReadResources(
|
|
|
|
nsIWebBrowserPersistResourceVisitor* aVisitor) {
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsIWebBrowserPersistResourceVisitor> visitor = aVisitor;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-31 23:18:10 +03:00
|
|
|
NS_ENSURE_TRUE(mDocument, NS_ERROR_FAILURE);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-31 23:18:10 +03:00
|
|
|
ErrorResult err;
|
2018-02-01 22:26:12 +03:00
|
|
|
RefPtr<dom::TreeWalker> walker = mDocument->CreateTreeWalker(
|
2018-01-31 23:18:10 +03:00
|
|
|
*mDocument,
|
2018-06-26 00:20:54 +03:00
|
|
|
dom::NodeFilter_Binding::SHOW_ELEMENT |
|
|
|
|
dom::NodeFilter_Binding::SHOW_DOCUMENT |
|
|
|
|
dom::NodeFilter_Binding::SHOW_PROCESSING_INSTRUCTION,
|
2018-01-31 23:18:10 +03:00
|
|
|
nullptr, err);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-31 23:18:10 +03:00
|
|
|
if (NS_WARN_IF(err.Failed())) {
|
|
|
|
return err.StealNSResult();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
MOZ_ASSERT(walker);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ResourceReader> reader = new ResourceReader(this, aVisitor);
|
2018-02-01 22:26:12 +03:00
|
|
|
nsCOMPtr<nsINode> currentNode = walker->CurrentNode();
|
2018-11-30 13:46:48 +03:00
|
|
|
do {
|
2018-05-30 05:58:49 +03:00
|
|
|
rv = reader->OnWalkDOMNode(currentNode);
|
2015-08-06 00:25:39 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
|
2018-01-31 23:18:10 +03:00
|
|
|
ErrorResult err;
|
|
|
|
currentNode = walker->NextNode(err);
|
|
|
|
if (NS_WARN_IF(err.Failed())) {
|
|
|
|
err.SuppressException();
|
2015-08-06 00:25:39 +03:00
|
|
|
break;
|
|
|
|
}
|
2018-02-01 22:26:12 +03:00
|
|
|
} while (currentNode);
|
2015-08-06 00:25:39 +03:00
|
|
|
reader->DocumentDone(rv);
|
2015-09-21 15:54:00 +03:00
|
|
|
// If NS_FAILED(rv), it was / will be reported by an EndVisit call
|
|
|
|
// via DocumentDone. This method must return a failure if and
|
|
|
|
// only if visitor won't be invoked.
|
|
|
|
return NS_OK;
|
2015-08-06 00:25:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t ConvertEncoderFlags(uint32_t aEncoderFlags) {
|
|
|
|
uint32_t encoderFlags = 0;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
if (aEncoderFlags & nsIWebBrowserPersist::ENCODE_FLAGS_SELECTION_ONLY)
|
|
|
|
encoderFlags |= nsIDocumentEncoder::OutputSelectionOnly;
|
|
|
|
if (aEncoderFlags & nsIWebBrowserPersist::ENCODE_FLAGS_FORMATTED)
|
|
|
|
encoderFlags |= nsIDocumentEncoder::OutputFormatted;
|
|
|
|
if (aEncoderFlags & nsIWebBrowserPersist::ENCODE_FLAGS_RAW)
|
|
|
|
encoderFlags |= nsIDocumentEncoder::OutputRaw;
|
|
|
|
if (aEncoderFlags & nsIWebBrowserPersist::ENCODE_FLAGS_BODY_ONLY)
|
|
|
|
encoderFlags |= nsIDocumentEncoder::OutputBodyOnly;
|
|
|
|
if (aEncoderFlags & nsIWebBrowserPersist::ENCODE_FLAGS_PREFORMATTED)
|
|
|
|
encoderFlags |= nsIDocumentEncoder::OutputPreformatted;
|
|
|
|
if (aEncoderFlags & nsIWebBrowserPersist::ENCODE_FLAGS_WRAP)
|
|
|
|
encoderFlags |= nsIDocumentEncoder::OutputWrap;
|
|
|
|
if (aEncoderFlags & nsIWebBrowserPersist::ENCODE_FLAGS_FORMAT_FLOWED)
|
|
|
|
encoderFlags |= nsIDocumentEncoder::OutputFormatFlowed;
|
|
|
|
if (aEncoderFlags & nsIWebBrowserPersist::ENCODE_FLAGS_ABSOLUTE_LINKS)
|
|
|
|
encoderFlags |= nsIDocumentEncoder::OutputAbsoluteLinks;
|
|
|
|
if (aEncoderFlags & nsIWebBrowserPersist::ENCODE_FLAGS_ENCODE_BASIC_ENTITIES)
|
|
|
|
encoderFlags |= nsIDocumentEncoder::OutputEncodeBasicEntities;
|
|
|
|
if (aEncoderFlags & nsIWebBrowserPersist::ENCODE_FLAGS_CR_LINEBREAKS)
|
|
|
|
encoderFlags |= nsIDocumentEncoder::OutputCRLineBreak;
|
|
|
|
if (aEncoderFlags & nsIWebBrowserPersist::ENCODE_FLAGS_LF_LINEBREAKS)
|
|
|
|
encoderFlags |= nsIDocumentEncoder::OutputLFLineBreak;
|
|
|
|
if (aEncoderFlags & nsIWebBrowserPersist::ENCODE_FLAGS_NOSCRIPT_CONTENT)
|
|
|
|
encoderFlags |= nsIDocumentEncoder::OutputNoScriptContent;
|
|
|
|
if (aEncoderFlags & nsIWebBrowserPersist::ENCODE_FLAGS_NOFRAMES_CONTENT)
|
|
|
|
encoderFlags |= nsIDocumentEncoder::OutputNoFramesContent;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
return encoderFlags;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ContentTypeEncoderExists(const nsACString& aType) {
|
2018-11-05 03:41:05 +03:00
|
|
|
return do_getDocumentTypeSupportedForEncoding(
|
|
|
|
PromiseFlatCString(aType).get());
|
2015-08-06 00:25:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebBrowserPersistLocalDocument::DecideContentType(
|
|
|
|
nsACString& aContentType) {
|
|
|
|
if (aContentType.IsEmpty()) {
|
|
|
|
if (NS_WARN_IF(NS_FAILED(GetContentType(aContentType)))) {
|
|
|
|
aContentType.Truncate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!aContentType.IsEmpty() && !ContentTypeEncoderExists(aContentType)) {
|
|
|
|
aContentType.Truncate();
|
|
|
|
}
|
|
|
|
if (aContentType.IsEmpty()) {
|
|
|
|
aContentType.AssignLiteral("text/html");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult WebBrowserPersistLocalDocument::GetDocEncoder(
|
|
|
|
const nsACString& aContentType, uint32_t aEncoderFlags,
|
|
|
|
nsIDocumentEncoder** aEncoder) {
|
|
|
|
nsCOMPtr<nsIDocumentEncoder> encoder =
|
2018-11-05 03:41:05 +03:00
|
|
|
do_createDocumentEncoder(PromiseFlatCString(aContentType).get());
|
|
|
|
NS_ENSURE_TRUE(encoder, NS_ERROR_FAILURE);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-11-05 03:41:05 +03:00
|
|
|
nsresult rv =
|
|
|
|
encoder->NativeInit(mDocument, NS_ConvertASCIItoUTF16(aContentType),
|
|
|
|
ConvertEncoderFlags(aEncoderFlags));
|
2015-08-06 00:25:39 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
nsAutoCString charSet;
|
|
|
|
rv = GetCharacterSet(charSet);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
|
|
|
|
rv = encoder->SetCharset(charSet);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
encoder.forget(aEncoder);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebBrowserPersistLocalDocument::WriteContent(
|
|
|
|
nsIOutputStream* aStream, nsIWebBrowserPersistURIMap* aMap,
|
|
|
|
const nsACString& aRequestedContentType, uint32_t aEncoderFlags,
|
|
|
|
uint32_t aWrapColumn, nsIWebBrowserPersistWriteCompletion* aCompletion) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aStream);
|
|
|
|
NS_ENSURE_ARG_POINTER(aCompletion);
|
|
|
|
nsAutoCString contentType(aRequestedContentType);
|
|
|
|
DecideContentType(contentType);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
nsCOMPtr<nsIDocumentEncoder> encoder;
|
|
|
|
nsresult rv =
|
|
|
|
GetDocEncoder(contentType, aEncoderFlags, getter_AddRefs(encoder));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
if (aWrapColumn != 0 &&
|
|
|
|
(aEncoderFlags & nsIWebBrowserPersist::ENCODE_FLAGS_WRAP)) {
|
|
|
|
encoder->SetWrapColumn(aWrapColumn);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
nsCOMPtr<nsIURI> targetURI;
|
|
|
|
if (aMap) {
|
|
|
|
nsAutoCString targetURISpec;
|
|
|
|
rv = aMap->GetTargetBaseURI(targetURISpec);
|
|
|
|
if (NS_SUCCEEDED(rv) && !targetURISpec.IsEmpty()) {
|
|
|
|
rv = NS_NewURI(getter_AddRefs(targetURI), targetURISpec,
|
|
|
|
/* charset: */ nullptr, /* base: */ nullptr);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_UNEXPECTED);
|
|
|
|
} else if (mPersistFlags &
|
|
|
|
nsIWebBrowserPersist::PERSIST_FLAGS_FIXUP_LINKS_TO_DESTINATION) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-08-06 00:25:39 +03:00
|
|
|
}
|
|
|
|
rv = encoder->SetNodeFixup(new PersistNodeFixup(this, aMap, targetURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
rv = encoder->EncodeToStream(aStream);
|
|
|
|
aCompletion->OnFinish(this, aStream, contentType, rv);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|