2006-05-06 08:13:20 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-29 19:52:43 +04: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/. */
|
2006-05-06 08:13:20 +04:00
|
|
|
|
2018-03-16 18:26:06 +03:00
|
|
|
#include "nsParserUtils.h"
|
2018-07-17 22:37:48 +03:00
|
|
|
#include "mozilla/NullPrincipal.h"
|
2020-02-28 23:49:06 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2018-03-16 18:26:06 +03:00
|
|
|
#include "mozilla/dom/DocumentFragment.h"
|
2018-04-27 06:37:29 +03:00
|
|
|
#include "mozilla/dom/Element.h"
|
2018-03-16 18:26:06 +03:00
|
|
|
#include "mozilla/dom/ScriptLoader.h"
|
|
|
|
#include "nsAttrName.h"
|
2006-05-06 08:13:20 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2018-03-16 18:26:06 +03:00
|
|
|
#include "nsContentCID.h"
|
2011-08-11 17:29:50 +04:00
|
|
|
#include "nsContentUtils.h"
|
2018-03-16 18:26:06 +03:00
|
|
|
#include "nsEscape.h"
|
|
|
|
#include "nsHTMLParts.h"
|
|
|
|
#include "nsHtml5Module.h"
|
|
|
|
#include "nsIContent.h"
|
2006-05-06 08:13:20 +04:00
|
|
|
#include "nsIContentSink.h"
|
2018-03-16 18:26:06 +03:00
|
|
|
#include "nsIDTD.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2018-03-16 18:26:06 +03:00
|
|
|
#include "nsIDocumentEncoder.h"
|
|
|
|
#include "nsIFragmentContentSink.h"
|
|
|
|
#include "nsIParser.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsString.h"
|
2011-07-29 15:48:04 +04:00
|
|
|
#include "nsTreeSanitizer.h"
|
2018-03-16 18:26:06 +03:00
|
|
|
#include "nsXPCOM.h"
|
2006-05-06 08:13:20 +04:00
|
|
|
|
2006-08-12 08:46:43 +04:00
|
|
|
#define XHTML_DIV_TAG u"div xmlns=\"http://www.w3.org/1999/xhtml\""
|
|
|
|
|
2013-04-10 18:15:54 +04:00
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2018-12-17 12:33:28 +03:00
|
|
|
NS_IMPL_ISUPPORTS(nsParserUtils, nsIParserUtils)
|
2006-05-06 08:13:20 +04:00
|
|
|
|
2012-02-27 15:57:48 +04:00
|
|
|
NS_IMETHODIMP
|
2012-03-19 12:16:20 +04:00
|
|
|
nsParserUtils::ConvertToPlainText(const nsAString& aFromStr, uint32_t aFlags,
|
2012-03-20 19:28:42 +04:00
|
|
|
uint32_t aWrapCol, nsAString& aToStr) {
|
2018-03-16 18:26:06 +03:00
|
|
|
return nsContentUtils::ConvertToPlainText(aFromStr, aToStr, aFlags, aWrapCol);
|
2012-02-27 15:57:48 +04:00
|
|
|
}
|
2012-01-13 08:57:12 +04:00
|
|
|
|
2022-07-21 11:18:46 +03:00
|
|
|
template <typename Callable>
|
|
|
|
static nsresult SanitizeWith(const nsAString& aInput, nsAString& aOutput,
|
|
|
|
Callable aDoSanitize) {
|
2020-10-23 15:48:52 +03:00
|
|
|
RefPtr<Document> document = nsContentUtils::CreateInertHTMLDocument(nullptr);
|
|
|
|
if (!document) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-03-19 12:16:20 +04:00
|
|
|
|
2022-07-21 11:18:46 +03:00
|
|
|
nsresult rv = nsContentUtils::ParseDocumentHTML(aInput, document, false);
|
2012-03-19 12:16:20 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2022-07-21 11:18:46 +03:00
|
|
|
aDoSanitize(document.get());
|
2012-03-19 12:16:20 +04:00
|
|
|
|
2018-11-05 03:41:05 +03:00
|
|
|
nsCOMPtr<nsIDocumentEncoder> encoder = do_createDocumentEncoder("text/html");
|
2012-03-19 12:16:20 +04:00
|
|
|
encoder->NativeInit(document, u"text/html"_ns,
|
|
|
|
nsIDocumentEncoder::OutputDontRewriteEncodingDeclaration |
|
2018-03-16 18:26:06 +03:00
|
|
|
nsIDocumentEncoder::OutputNoScriptContent |
|
|
|
|
nsIDocumentEncoder::OutputEncodeBasicEntities |
|
|
|
|
nsIDocumentEncoder::OutputLFLineBreak |
|
|
|
|
nsIDocumentEncoder::OutputRaw);
|
2022-07-21 11:18:46 +03:00
|
|
|
return encoder->EncodeToString(aOutput);
|
|
|
|
}
|
2012-03-19 12:16:20 +04:00
|
|
|
|
2022-07-21 11:18:46 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsParserUtils::Sanitize(const nsAString& aFromStr, uint32_t aFlags,
|
|
|
|
nsAString& aToStr) {
|
|
|
|
return SanitizeWith(aFromStr, aToStr, [&](Document* aDocument) {
|
|
|
|
nsTreeSanitizer sanitizer(aFlags);
|
|
|
|
sanitizer.Sanitize(aDocument);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsParserUtils::RemoveConditionalCSS(const nsAString& aFromStr,
|
|
|
|
nsAString& aToStr) {
|
|
|
|
return SanitizeWith(aFromStr, aToStr, [](Document* aDocument) {
|
|
|
|
nsTreeSanitizer::RemoveConditionalCSSFromSubtree(aDocument);
|
|
|
|
});
|
2012-03-19 12:16:20 +04:00
|
|
|
}
|
|
|
|
|
2012-03-20 19:28:42 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsParserUtils::ParseFragment(const nsAString& aFragment, uint32_t aFlags,
|
|
|
|
bool aIsXML, nsIURI* aBaseURI,
|
2018-04-27 06:37:29 +03:00
|
|
|
Element* aContextElement,
|
2018-04-18 04:13:03 +03:00
|
|
|
DocumentFragment** aReturn) {
|
2006-08-12 08:46:43 +04:00
|
|
|
NS_ENSURE_ARG(aContextElement);
|
2012-07-30 18:20:58 +04:00
|
|
|
*aReturn = nullptr;
|
2006-08-12 08:46:43 +04:00
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
RefPtr<Document> document = aContextElement->OwnerDoc();
|
2006-08-15 02:08:14 +04:00
|
|
|
|
2012-06-14 10:14:47 +04:00
|
|
|
nsAutoScriptBlockerSuppressNodeRemoved autoBlocker;
|
|
|
|
|
2006-08-12 08:46:43 +04:00
|
|
|
// stop scripts
|
2019-01-02 16:05:23 +03:00
|
|
|
RefPtr<ScriptLoader> loader = document->ScriptLoader();
|
|
|
|
bool scripts_enabled = loader->GetEnabled();
|
2006-08-12 08:46:43 +04:00
|
|
|
if (scripts_enabled) {
|
2011-10-17 18:59:28 +04:00
|
|
|
loader->SetEnabled(false);
|
2006-08-12 08:46:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap things in a div or body for parsing, but it won't show up in
|
|
|
|
// the fragment.
|
2016-08-31 05:13:59 +03:00
|
|
|
nsresult rv = NS_OK;
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<nsString, 2> tagStack;
|
2018-04-18 04:13:03 +03:00
|
|
|
RefPtr<DocumentFragment> fragment;
|
2012-06-15 16:28:28 +04:00
|
|
|
if (aIsXML) {
|
2017-02-20 10:26:27 +03:00
|
|
|
// XHTML
|
|
|
|
tagStack.AppendElement(nsLiteralString(XHTML_DIV_TAG));
|
2018-04-18 04:13:03 +03:00
|
|
|
rv = nsContentUtils::ParseFragmentXML(aFragment, document, tagStack, true,
|
2020-01-12 22:50:13 +03:00
|
|
|
aFlags, getter_AddRefs(fragment));
|
2012-06-15 16:28:28 +04:00
|
|
|
} else {
|
2020-03-17 17:53:46 +03:00
|
|
|
fragment = new (document->NodeInfoManager())
|
|
|
|
DocumentFragment(document->NodeInfoManager());
|
2018-03-16 18:26:06 +03:00
|
|
|
rv = nsContentUtils::ParseFragmentHTML(aFragment, fragment, nsGkAtoms::body,
|
2020-01-12 22:50:13 +03:00
|
|
|
kNameSpaceID_XHTML, false, true,
|
|
|
|
aFlags);
|
2012-06-15 16:28:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (scripts_enabled) {
|
|
|
|
loader->SetEnabled(true);
|
2006-08-12 08:46:43 +04:00
|
|
|
}
|
|
|
|
|
2018-04-18 04:13:03 +03:00
|
|
|
fragment.forget(aReturn);
|
2006-08-12 08:46:43 +04:00
|
|
|
return rv;
|
|
|
|
}
|