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"
|
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"
|
2017-05-08 09:24:22 +03:00
|
|
|
#include "mozilla/dom/ScriptLoader.h"
|
2017-03-22 13:38:40 +03:00
|
|
|
#include "NullPrincipal.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
|
|
|
|
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");
|
2017-03-22 13:38:40 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> principal = NullPrincipal::Create();
|
2012-03-19 12:16:20 +04:00
|
|
|
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,
|
Bug 1389300 - Inherit style backend into NS_NewDOMDocument. r=smaug,r=heycam
Our current machinery for enabling stylo requires a docshell - if there isn't
one, we default to the Gecko style system.
When getComputedStyle operates on an element without a presshell, it uses the
caller's presshell instead. If the element has previously been styled with
one style system (but no longer has a presshell), and the caller uses a
different style backend, using the caller's style system can cause crashes when
we pull bits of cached data off the DOM (like cached style attributes).
So we want to throw when window.getComputedStyle(element) is called for a
(window, element) pair with different style backends (which is what the next
patch in this bug does).
However, that causes a few failures where stylo-backed documents try to do
getComputedStyle on an XHR document (which, without a docshell, will use the
gecko style system).
So this patch does some work to propagate the creator's style backend into
various docshell-less documents. This should allow both chrome (which uses gecko)
and content (which uses stylo) to use getComputedStyle on the response document
for XHRs they create.
Note that the second patch in this bug will make
chromeWin.getComputedStyle(contentObj) throw. If we discover code that does
that, we can just make it invoke the content's getComputedStyle method over Xrays.
MozReview-Commit-ID: 5OsmHJKq5Ui
2017-08-15 05:50:28 +03:00
|
|
|
DocumentFlavorHTML,
|
|
|
|
mozilla::StyleBackendType::None);
|
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
|
2017-05-08 09:24:22 +03:00
|
|
|
RefPtr<ScriptLoader> 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.
|
2016-08-31 05:13:59 +03:00
|
|
|
nsresult rv = NS_OK;
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<nsString, 2> tagStack;
|
2012-06-15 16:28:28 +04:00
|
|
|
nsCOMPtr<nsIContent> fragment;
|
|
|
|
if (aIsXML) {
|
2017-02-20 10:26:27 +03:00
|
|
|
// XHTML
|
|
|
|
tagStack.AppendElement(NS_LITERAL_STRING(XHTML_DIV_TAG));
|
2012-06-15 16:28:28 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
if (fragment) {
|
|
|
|
nsTreeSanitizer sanitizer(aFlags);
|
|
|
|
sanitizer.Sanitize(fragment);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scripts_enabled) {
|
|
|
|
loader->SetEnabled(true);
|
2006-08-12 08:46:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|