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