Bug 1874535 - [wpt-sync] Update web-platform-tests to 2fb9eb91e48870dd8f9bbb7adae728d...
[gecko.git] / parser / html / nsParserUtils.cpp
blob74b86febe8b2fea479d8a09def321839ec6dc476
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"
13 #include "nsCOMPtr.h"
14 #include "nsContentCID.h"
15 #include "nsContentUtils.h"
16 #include "nsEscape.h"
17 #include "nsHTMLParts.h"
18 #include "nsHtml5Module.h"
19 #include "nsIContent.h"
20 #include "nsIContentSink.h"
21 #include "nsIDTD.h"
22 #include "mozilla/dom/Document.h"
23 #include "nsIDocumentEncoder.h"
24 #include "nsIFragmentContentSink.h"
25 #include "nsIParser.h"
26 #include "nsNetCID.h"
27 #include "nsNetUtil.h"
28 #include "nsString.h"
29 #include "nsTreeSanitizer.h"
30 #include "nsXPCOM.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)
38 NS_IMETHODIMP
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);
48 if (!document) {
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);
67 NS_IMETHODIMP
68 nsParserUtils::Sanitize(const nsAString& aFromStr, uint32_t aFlags,
69 nsAString& aToStr) {
70 return SanitizeWith(aFromStr, aToStr, [&](Document* aDocument) {
71 nsTreeSanitizer sanitizer(aFlags);
72 sanitizer.Sanitize(aDocument);
73 });
76 NS_IMETHODIMP
77 nsParserUtils::RemoveConditionalCSS(const nsAString& aFromStr,
78 nsAString& aToStr) {
79 return SanitizeWith(aFromStr, aToStr, [](Document* aDocument) {
80 nsTreeSanitizer::RemoveConditionalCSSFromSubtree(aDocument);
81 });
84 NS_IMETHODIMP
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);
90 *aReturn = nullptr;
92 RefPtr<Document> document = aContextElement->OwnerDoc();
94 nsAutoScriptBlockerSuppressNodeRemoved autoBlocker;
96 // stop scripts
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
104 // the fragment.
105 nsresult rv = NS_OK;
106 AutoTArray<nsString, 2> tagStack;
107 RefPtr<DocumentFragment> fragment;
108 if (aIsXML) {
109 // XHTML
110 tagStack.AppendElement(nsLiteralString(XHTML_DIV_TAG));
111 rv = nsContentUtils::ParseFragmentXML(aFragment, document, tagStack, true,
112 aFlags, getter_AddRefs(fragment));
113 } else {
114 fragment = new (document->NodeInfoManager())
115 DocumentFragment(document->NodeInfoManager());
116 rv = nsContentUtils::ParseFragmentHTML(aFragment, fragment, nsGkAtoms::body,
117 kNameSpaceID_XHTML, false, true,
118 aFlags);
121 if (scripts_enabled) {
122 loader->SetEnabled(true);
125 fragment.forget(aReturn);
126 return rv;