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 "nsContentCID.h"
15 #include "nsContentUtils.h"
17 #include "nsHTMLParts.h"
18 #include "nsHtml5Module.h"
19 #include "nsIContent.h"
20 #include "nsIContentSink.h"
22 #include "mozilla/dom/Document.h"
23 #include "nsIDocumentEncoder.h"
24 #include "nsIFragmentContentSink.h"
25 #include "nsIParser.h"
27 #include "nsNetUtil.h"
29 #include "nsTreeSanitizer.h"
32 #define XHTML_DIV_TAG u"div xmlns=\"http://www.w3.org/1999/xhtml\""
34 using namespace mozilla::dom
;
36 NS_IMPL_ISUPPORTS(nsParserUtils
, nsIParserUtils
)
39 nsParserUtils::ConvertToPlainText(const nsAString
& aFromStr
, uint32_t aFlags
,
40 uint32_t aWrapCol
, nsAString
& aToStr
) {
41 return nsContentUtils::ConvertToPlainText(aFromStr
, aToStr
, aFlags
, aWrapCol
);
44 template <typename Callable
>
45 static nsresult
SanitizeWith(const nsAString
& aInput
, nsAString
& aOutput
,
46 Callable aDoSanitize
) {
47 RefPtr
<Document
> document
= nsContentUtils::CreateInertHTMLDocument(nullptr);
49 return NS_ERROR_FAILURE
;
52 nsresult rv
= nsContentUtils::ParseDocumentHTML(aInput
, document
, false);
53 NS_ENSURE_SUCCESS(rv
, rv
);
55 aDoSanitize(document
.get());
57 nsCOMPtr
<nsIDocumentEncoder
> encoder
= do_createDocumentEncoder("text/html");
58 encoder
->NativeInit(document
, u
"text/html"_ns
,
59 nsIDocumentEncoder::OutputDontRewriteEncodingDeclaration
|
60 nsIDocumentEncoder::OutputNoScriptContent
|
61 nsIDocumentEncoder::OutputEncodeBasicEntities
|
62 nsIDocumentEncoder::OutputLFLineBreak
|
63 nsIDocumentEncoder::OutputRaw
);
64 return encoder
->EncodeToString(aOutput
);
68 nsParserUtils::Sanitize(const nsAString
& aFromStr
, uint32_t aFlags
,
70 return SanitizeWith(aFromStr
, aToStr
, [&](Document
* aDocument
) {
71 nsTreeSanitizer
sanitizer(aFlags
);
72 sanitizer
.Sanitize(aDocument
);
77 nsParserUtils::RemoveConditionalCSS(const nsAString
& aFromStr
,
79 return SanitizeWith(aFromStr
, aToStr
, [](Document
* aDocument
) {
80 nsTreeSanitizer::RemoveConditionalCSSFromSubtree(aDocument
);
85 nsParserUtils::ParseFragment(const nsAString
& aFragment
, uint32_t aFlags
,
86 bool aIsXML
, nsIURI
* aBaseURI
,
87 Element
* aContextElement
,
88 DocumentFragment
** aReturn
) {
89 NS_ENSURE_ARG(aContextElement
);
92 RefPtr
<Document
> document
= aContextElement
->OwnerDoc();
94 nsAutoScriptBlockerSuppressNodeRemoved autoBlocker
;
97 RefPtr
<ScriptLoader
> loader
= document
->ScriptLoader();
98 bool scripts_enabled
= loader
->GetEnabled();
99 if (scripts_enabled
) {
100 loader
->SetEnabled(false);
103 // Wrap things in a div or body for parsing, but it won't show up in
106 AutoTArray
<nsString
, 2> tagStack
;
107 RefPtr
<DocumentFragment
> fragment
;
110 tagStack
.AppendElement(nsLiteralString(XHTML_DIV_TAG
));
111 rv
= nsContentUtils::ParseFragmentXML(aFragment
, document
, tagStack
, true,
112 aFlags
, getter_AddRefs(fragment
));
114 fragment
= new (document
->NodeInfoManager())
115 DocumentFragment(document
->NodeInfoManager());
116 rv
= nsContentUtils::ParseFragmentHTML(aFragment
, fragment
, nsGkAtoms::body
,
117 kNameSpaceID_XHTML
, false, true,
121 if (scripts_enabled
) {
122 loader
->SetEnabled(true);
125 fragment
.forget(aReturn
);