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
|
|
|
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsXPIDLString.h"
|
2006-11-04 08:45:02 +03:00
|
|
|
#include "nsScriptLoader.h"
|
2006-11-23 10:18:59 +03:00
|
|
|
#include "nsEscape.h"
|
2006-05-06 08:13:20 +04:00
|
|
|
#include "nsIParser.h"
|
|
|
|
#include "nsIDTD.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsParserCIID.h"
|
2011-08-11 17:29:50 +04:00
|
|
|
#include "nsContentUtils.h"
|
2006-05-06 08:13:20 +04:00
|
|
|
#include "nsIContentSink.h"
|
|
|
|
#include "nsIDocumentEncoder.h"
|
2006-08-12 08:46:43 +04:00
|
|
|
#include "nsIDOMDocumentFragment.h"
|
|
|
|
#include "nsIFragmentContentSink.h"
|
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIDOMNodeList.h"
|
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsIDOMElement.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsAttrName.h"
|
|
|
|
#include "nsHTMLParts.h"
|
|
|
|
#include "nsContentCID.h"
|
2006-05-06 08:13:20 +04:00
|
|
|
#include "nsIScriptableUnescapeHTML.h"
|
2012-02-27 15:57:48 +04:00
|
|
|
#include "nsParserUtils.h"
|
2006-11-04 08:45:02 +03:00
|
|
|
#include "nsAutoPtr.h"
|
2011-07-29 15:48:04 +04:00
|
|
|
#include "nsTreeSanitizer.h"
|
|
|
|
#include "nsHtml5Module.h"
|
2013-04-10 18:15:54 +04:00
|
|
|
#include "mozilla/dom/DocumentFragment.h"
|
2006-05-06 08:13:20 +04:00
|
|
|
|
2006-08-12 08:46:43 +04:00
|
|
|
#define XHTML_DIV_TAG "div xmlns=\"http://www.w3.org/1999/xhtml\""
|
|
|
|
|
2013-04-10 18:15:54 +04:00
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsParserUtils,
|
|
|
|
nsIScriptableUnescapeHTML,
|
|
|
|
nsIParserUtils)
|
2006-05-06 08:13:20 +04:00
|
|
|
|
|
|
|
static NS_DEFINE_CID(kCParserCID, NS_PARSER_CID);
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-27 15:57:48 +04:00
|
|
|
NS_IMETHODIMP
|
2012-03-19 12:16:20 +04:00
|
|
|
nsParserUtils::ConvertToPlainText(const nsAString& aFromStr,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aFlags,
|
|
|
|
uint32_t aWrapCol,
|
2012-03-20 19:28:42 +04:00
|
|
|
nsAString& aToStr)
|
2012-02-27 15:57:48 +04:00
|
|
|
{
|
|
|
|
return nsContentUtils::ConvertToPlainText(aFromStr,
|
|
|
|
aToStr,
|
|
|
|
aFlags,
|
|
|
|
aWrapCol);
|
|
|
|
}
|
2012-01-13 08:57:12 +04:00
|
|
|
|
2012-02-27 15:57:48 +04:00
|
|
|
NS_IMETHODIMP
|
2012-03-19 12:16:20 +04:00
|
|
|
nsParserUtils::Unescape(const nsAString& aFromStr,
|
|
|
|
nsAString& aToStr)
|
2012-02-27 15:57:48 +04:00
|
|
|
{
|
|
|
|
return nsContentUtils::ConvertToPlainText(aFromStr,
|
|
|
|
aToStr,
|
|
|
|
nsIDocumentEncoder::OutputSelectionOnly |
|
|
|
|
nsIDocumentEncoder::OutputAbsoluteLinks,
|
|
|
|
0);
|
2006-05-06 08:13:20 +04:00
|
|
|
}
|
|
|
|
|
2012-03-19 12:16:20 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsParserUtils::Sanitize(const nsAString& aFromStr,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aFlags,
|
2012-03-19 12:16:20 +04:00
|
|
|
nsAString& aToStr)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
NS_NewURI(getter_AddRefs(uri), "about:blank");
|
|
|
|
nsCOMPtr<nsIPrincipal> principal =
|
|
|
|
do_CreateInstance("@mozilla.org/nullprincipal;1");
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDocument;
|
2012-12-10 18:05:33 +04:00
|
|
|
nsresult rv = NS_NewDOMDocument(getter_AddRefs(domDocument),
|
|
|
|
EmptyString(),
|
|
|
|
EmptyString(),
|
|
|
|
nullptr,
|
|
|
|
uri,
|
|
|
|
uri,
|
|
|
|
principal,
|
|
|
|
true,
|
|
|
|
nullptr,
|
|
|
|
DocumentFlavorHTML);
|
2012-03-19 12:16:20 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> document = do_QueryInterface(domDocument);
|
|
|
|
rv = nsContentUtils::ParseDocumentHTML(aFromStr, document, false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsTreeSanitizer sanitizer(aFlags);
|
|
|
|
sanitizer.Sanitize(document);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocumentEncoder> encoder =
|
|
|
|
do_CreateInstance(NS_DOC_ENCODER_CONTRACTID_BASE "text/html");
|
|
|
|
|
|
|
|
encoder->NativeInit(document,
|
|
|
|
NS_LITERAL_STRING("text/html"),
|
|
|
|
nsIDocumentEncoder::OutputDontRewriteEncodingDeclaration |
|
|
|
|
nsIDocumentEncoder::OutputNoScriptContent |
|
|
|
|
nsIDocumentEncoder::OutputEncodeBasicEntities |
|
|
|
|
nsIDocumentEncoder::OutputLFLineBreak |
|
|
|
|
nsIDocumentEncoder::OutputRaw);
|
|
|
|
|
|
|
|
return encoder->EncodeToString(aToStr);
|
|
|
|
}
|
|
|
|
|
2006-08-12 08:46:43 +04:00
|
|
|
NS_IMETHODIMP
|
2012-03-19 12:16:20 +04:00
|
|
|
nsParserUtils::ParseFragment(const nsAString& aFragment,
|
2012-03-20 19:28:42 +04:00
|
|
|
bool aIsXML,
|
|
|
|
nsIURI* aBaseURI,
|
|
|
|
nsIDOMElement* aContextElement,
|
|
|
|
nsIDOMDocumentFragment** aReturn)
|
|
|
|
{
|
|
|
|
return nsParserUtils::ParseFragment(aFragment,
|
|
|
|
0,
|
|
|
|
aIsXML,
|
|
|
|
aBaseURI,
|
|
|
|
aContextElement,
|
|
|
|
aReturn);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsParserUtils::ParseFragment(const nsAString& aFragment,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aFlags,
|
2012-03-20 19:28:42 +04:00
|
|
|
bool aIsXML,
|
|
|
|
nsIURI* aBaseURI,
|
|
|
|
nsIDOMElement* aContextElement,
|
|
|
|
nsIDOMDocumentFragment** 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
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> document;
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDocument;
|
|
|
|
nsCOMPtr<nsIDOMNode> contextNode;
|
|
|
|
contextNode = do_QueryInterface(aContextElement);
|
|
|
|
contextNode->GetOwnerDocument(getter_AddRefs(domDocument));
|
|
|
|
document = do_QueryInterface(domDocument);
|
|
|
|
NS_ENSURE_TRUE(document, NS_ERROR_NOT_AVAILABLE);
|
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
|
2006-11-04 08:45:02 +03:00
|
|
|
nsRefPtr<nsScriptLoader> loader;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool scripts_enabled = false;
|
2006-08-12 08:46:43 +04:00
|
|
|
if (document) {
|
2007-05-31 00:43:41 +04:00
|
|
|
loader = document->ScriptLoader();
|
|
|
|
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.
|
2008-04-11 08:38:25 +04:00
|
|
|
nsAutoTArray<nsString, 2> tagStack;
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString base, spec;
|
2006-08-12 08:46:43 +04:00
|
|
|
if (aIsXML) {
|
|
|
|
// XHTML
|
|
|
|
if (aBaseURI) {
|
2014-05-22 07:48:50 +04:00
|
|
|
base.AppendLiteral(XHTML_DIV_TAG);
|
|
|
|
base.AppendLiteral(" xml:base=\"");
|
2006-08-12 08:46:43 +04:00
|
|
|
aBaseURI->GetSpec(spec);
|
2006-11-23 10:18:59 +03:00
|
|
|
// nsEscapeHTML is good enough, because we only need to get
|
|
|
|
// quotes, ampersands, and angle brackets
|
|
|
|
char* escapedSpec = nsEscapeHTML(spec.get());
|
|
|
|
if (escapedSpec)
|
|
|
|
base += escapedSpec;
|
|
|
|
NS_Free(escapedSpec);
|
2014-05-22 07:48:50 +04:00
|
|
|
base.Append('"');
|
2007-11-13 05:01:13 +03:00
|
|
|
tagStack.AppendElement(NS_ConvertUTF8toUTF16(base));
|
2006-08-12 08:46:43 +04:00
|
|
|
} else {
|
2007-11-13 05:01:13 +03:00
|
|
|
tagStack.AppendElement(NS_LITERAL_STRING(XHTML_DIV_TAG));
|
2006-08-12 08:46:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-15 16:28:28 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsIContent> fragment;
|
|
|
|
if (aIsXML) {
|
|
|
|
rv = nsContentUtils::ParseFragmentXML(aFragment,
|
|
|
|
document,
|
|
|
|
tagStack,
|
|
|
|
true,
|
|
|
|
aReturn);
|
|
|
|
fragment = do_QueryInterface(*aReturn);
|
|
|
|
} else {
|
2013-04-10 18:15:54 +04:00
|
|
|
NS_ADDREF(*aReturn = new DocumentFragment(document->NodeInfoManager()));
|
2012-06-15 16:28:28 +04:00
|
|
|
fragment = do_QueryInterface(*aReturn);
|
|
|
|
rv = nsContentUtils::ParseFragmentHTML(aFragment,
|
|
|
|
fragment,
|
|
|
|
nsGkAtoms::body,
|
|
|
|
kNameSpaceID_XHTML,
|
|
|
|
false,
|
|
|
|
true);
|
|
|
|
// Now, set the base URI on all subtree roots.
|
|
|
|
if (aBaseURI) {
|
|
|
|
aBaseURI->GetSpec(spec);
|
|
|
|
nsAutoString spec16;
|
|
|
|
CopyUTF8toUTF16(spec, spec16);
|
|
|
|
nsIContent* node = fragment->GetFirstChild();
|
|
|
|
while (node) {
|
|
|
|
if (node->IsElement()) {
|
|
|
|
node->SetAttr(kNameSpaceID_XML,
|
|
|
|
nsGkAtoms::base,
|
|
|
|
nsGkAtoms::xml,
|
|
|
|
spec16,
|
|
|
|
false);
|
2011-07-29 15:48:04 +04:00
|
|
|
}
|
2012-06-15 16:28:28 +04:00
|
|
|
node = node->GetNextSibling();
|
2011-07-29 15:48:04 +04:00
|
|
|
}
|
2006-08-12 08:46:43 +04:00
|
|
|
}
|
2012-06-15 16:28:28 +04:00
|
|
|
}
|
|
|
|
if (fragment) {
|
|
|
|
nsTreeSanitizer sanitizer(aFlags);
|
|
|
|
sanitizer.Sanitize(fragment);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scripts_enabled) {
|
|
|
|
loader->SetEnabled(true);
|
2006-08-12 08:46:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|