1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsParserUtils.h"
7 #include "mozilla/NullPrincipal.h"
8 #include "mozilla/UniquePtr.h"
9 #include "mozilla/dom/DocumentFragment.h"
10 #include "mozilla/dom/Element.h"
11 #include "mozilla/dom/ScriptLoader.h"
12 #include "nsAttrName.h"
14 #include "nsContentUtils.h"
16 #include "nsHTMLParts.h"
17 #include "nsHtml5Module.h"
18 #include "nsIContent.h"
19 #include "nsIContentSink.h"
21 #include "mozilla/dom/Document.h"
22 #include "nsIDocumentEncoder.h"
23 #include "nsIFragmentContentSink.h"
24 #include "nsIParser.h"
26 #include "nsNetUtil.h"
28 #include "nsTreeSanitizer.h"
31 #define XHTML_DIV_TAG u"div xmlns=\"http://www.w3.org/1999/xhtml\""
33 using namespace mozilla::dom
;
35 NS_IMPL_ISUPPORTS(nsParserUtils
, nsIParserUtils
)
38 nsParserUtils::ConvertToPlainText(const nsAString
& aFromStr
, uint32_t aFlags
,
39 uint32_t aWrapCol
, nsAString
& aToStr
) {
40 return nsContentUtils::ConvertToPlainText(aFromStr
, aToStr
, aFlags
, aWrapCol
);
43 template <typename Callable
>
44 static nsresult
SanitizeWith(const nsAString
& aInput
, nsAString
& aOutput
,
45 Callable aDoSanitize
) {
46 RefPtr
<Document
> document
= nsContentUtils::CreateInertHTMLDocument(nullptr);
48 return NS_ERROR_FAILURE
;
51 nsresult rv
= nsContentUtils::ParseDocumentHTML(aInput
, document
, false);
52 NS_ENSURE_SUCCESS(rv
, rv
);
54 aDoSanitize(document
.get());
56 nsCOMPtr
<nsIDocumentEncoder
> encoder
= do_createDocumentEncoder("text/html");
57 encoder
->NativeInit(document
, u
"text/html"_ns
,
58 nsIDocumentEncoder::OutputDontRewriteEncodingDeclaration
|
59 nsIDocumentEncoder::OutputNoScriptContent
|
60 nsIDocumentEncoder::OutputEncodeBasicEntities
|
61 nsIDocumentEncoder::OutputLFLineBreak
|
62 nsIDocumentEncoder::OutputRaw
);
63 return encoder
->EncodeToString(aOutput
);
67 nsParserUtils::Sanitize(const nsAString
& aFromStr
, uint32_t aFlags
,
69 return SanitizeWith(aFromStr
, aToStr
, [&](Document
* aDocument
) {
70 nsTreeSanitizer
sanitizer(aFlags
);
71 sanitizer
.Sanitize(aDocument
);
76 nsParserUtils::RemoveConditionalCSS(const nsAString
& aFromStr
,
78 return SanitizeWith(aFromStr
, aToStr
, [](Document
* aDocument
) {
79 nsTreeSanitizer::RemoveConditionalCSSFromSubtree(aDocument
);
84 nsParserUtils::ParseFragment(const nsAString
& aFragment
, uint32_t aFlags
,
85 bool aIsXML
, nsIURI
* aBaseURI
,
86 Element
* aContextElement
,
87 DocumentFragment
** aReturn
) {
88 NS_ENSURE_ARG(aContextElement
);
91 RefPtr
<Document
> document
= aContextElement
->OwnerDoc();
93 nsAutoScriptBlockerSuppressNodeRemoved autoBlocker
;
96 RefPtr
<ScriptLoader
> loader
= document
->ScriptLoader();
97 bool scripts_enabled
= loader
->GetEnabled();
98 if (scripts_enabled
) {
99 loader
->SetEnabled(false);
102 // Wrap things in a div or body for parsing, but it won't show up in
105 AutoTArray
<nsString
, 2> tagStack
;
106 RefPtr
<DocumentFragment
> fragment
;
109 tagStack
.AppendElement(nsLiteralString(XHTML_DIV_TAG
));
110 rv
= nsContentUtils::ParseFragmentXML(aFragment
, document
, tagStack
, true,
111 aFlags
, getter_AddRefs(fragment
));
113 fragment
= new (document
->NodeInfoManager())
114 DocumentFragment(document
->NodeInfoManager());
115 rv
= nsContentUtils::ParseFragmentHTML(aFragment
, fragment
, nsGkAtoms::body
,
116 kNameSpaceID_XHTML
, false, true,
120 if (scripts_enabled
) {
121 loader
->SetEnabled(true);
124 fragment
.forget(aReturn
);