Line data Source code
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/. */
5 :
6 : #include "nsString.h"
7 : #include "nsIComponentManager.h"
8 : #include "nsCOMPtr.h"
9 : #include "nsXPCOM.h"
10 : #include "nsISupportsPrimitives.h"
11 : #include "nsXPIDLString.h"
12 : #include "nsEscape.h"
13 : #include "nsIParser.h"
14 : #include "nsIDTD.h"
15 : #include "nsNetCID.h"
16 : #include "nsNetUtil.h"
17 : #include "nsParserCIID.h"
18 : #include "nsContentUtils.h"
19 : #include "nsIContentSink.h"
20 : #include "nsIDocumentEncoder.h"
21 : #include "nsIDOMDocumentFragment.h"
22 : #include "nsIFragmentContentSink.h"
23 : #include "nsIDOMDocument.h"
24 : #include "nsIDOMNodeList.h"
25 : #include "nsIDOMNode.h"
26 : #include "nsIDOMElement.h"
27 : #include "nsIDocument.h"
28 : #include "nsIContent.h"
29 : #include "nsAttrName.h"
30 : #include "nsHTMLParts.h"
31 : #include "nsContentCID.h"
32 : #include "nsIScriptableUnescapeHTML.h"
33 : #include "nsParserUtils.h"
34 : #include "nsAutoPtr.h"
35 : #include "nsTreeSanitizer.h"
36 : #include "nsHtml5Module.h"
37 : #include "mozilla/dom/DocumentFragment.h"
38 : #include "mozilla/dom/ScriptLoader.h"
39 : #include "NullPrincipal.h"
40 :
41 : #define XHTML_DIV_TAG "div xmlns=\"http://www.w3.org/1999/xhtml\""
42 :
43 : using namespace mozilla::dom;
44 :
45 0 : NS_IMPL_ISUPPORTS(nsParserUtils,
46 : nsIScriptableUnescapeHTML,
47 : nsIParserUtils)
48 :
49 : NS_IMETHODIMP
50 0 : nsParserUtils::ConvertToPlainText(const nsAString& aFromStr,
51 : uint32_t aFlags,
52 : uint32_t aWrapCol,
53 : nsAString& aToStr)
54 : {
55 : return nsContentUtils::ConvertToPlainText(aFromStr,
56 : aToStr,
57 : aFlags,
58 0 : aWrapCol);
59 : }
60 :
61 : NS_IMETHODIMP
62 0 : nsParserUtils::Unescape(const nsAString& aFromStr,
63 : nsAString& aToStr)
64 : {
65 : return nsContentUtils::ConvertToPlainText(aFromStr,
66 : aToStr,
67 : nsIDocumentEncoder::OutputSelectionOnly |
68 : nsIDocumentEncoder::OutputAbsoluteLinks,
69 0 : 0);
70 : }
71 :
72 : NS_IMETHODIMP
73 0 : nsParserUtils::Sanitize(const nsAString& aFromStr,
74 : uint32_t aFlags,
75 : nsAString& aToStr)
76 : {
77 0 : nsCOMPtr<nsIURI> uri;
78 0 : NS_NewURI(getter_AddRefs(uri), "about:blank");
79 0 : nsCOMPtr<nsIPrincipal> principal = NullPrincipal::Create();
80 0 : nsCOMPtr<nsIDOMDocument> domDocument;
81 0 : nsresult rv = NS_NewDOMDocument(getter_AddRefs(domDocument),
82 0 : EmptyString(),
83 0 : EmptyString(),
84 : nullptr,
85 : uri,
86 : uri,
87 : principal,
88 : true,
89 : nullptr,
90 0 : DocumentFlavorHTML);
91 0 : NS_ENSURE_SUCCESS(rv, rv);
92 :
93 0 : nsCOMPtr<nsIDocument> document = do_QueryInterface(domDocument);
94 0 : rv = nsContentUtils::ParseDocumentHTML(aFromStr, document, false);
95 0 : NS_ENSURE_SUCCESS(rv, rv);
96 :
97 0 : nsTreeSanitizer sanitizer(aFlags);
98 0 : sanitizer.Sanitize(document);
99 :
100 : nsCOMPtr<nsIDocumentEncoder> encoder =
101 0 : do_CreateInstance(NS_DOC_ENCODER_CONTRACTID_BASE "text/html");
102 :
103 0 : encoder->NativeInit(document,
104 0 : NS_LITERAL_STRING("text/html"),
105 : nsIDocumentEncoder::OutputDontRewriteEncodingDeclaration |
106 : nsIDocumentEncoder::OutputNoScriptContent |
107 : nsIDocumentEncoder::OutputEncodeBasicEntities |
108 : nsIDocumentEncoder::OutputLFLineBreak |
109 0 : nsIDocumentEncoder::OutputRaw);
110 :
111 0 : return encoder->EncodeToString(aToStr);
112 : }
113 :
114 : NS_IMETHODIMP
115 0 : nsParserUtils::ParseFragment(const nsAString& aFragment,
116 : bool aIsXML,
117 : nsIURI* aBaseURI,
118 : nsIDOMElement* aContextElement,
119 : nsIDOMDocumentFragment** aReturn)
120 : {
121 0 : return nsParserUtils::ParseFragment(aFragment,
122 : 0,
123 : aIsXML,
124 : aBaseURI,
125 : aContextElement,
126 0 : aReturn);
127 : }
128 :
129 : NS_IMETHODIMP
130 0 : nsParserUtils::ParseFragment(const nsAString& aFragment,
131 : uint32_t aFlags,
132 : bool aIsXML,
133 : nsIURI* aBaseURI,
134 : nsIDOMElement* aContextElement,
135 : nsIDOMDocumentFragment** aReturn)
136 : {
137 0 : NS_ENSURE_ARG(aContextElement);
138 0 : *aReturn = nullptr;
139 :
140 0 : nsCOMPtr<nsIDocument> document;
141 0 : nsCOMPtr<nsIDOMDocument> domDocument;
142 0 : nsCOMPtr<nsIDOMNode> contextNode;
143 0 : contextNode = do_QueryInterface(aContextElement);
144 0 : contextNode->GetOwnerDocument(getter_AddRefs(domDocument));
145 0 : document = do_QueryInterface(domDocument);
146 0 : NS_ENSURE_TRUE(document, NS_ERROR_NOT_AVAILABLE);
147 :
148 0 : nsAutoScriptBlockerSuppressNodeRemoved autoBlocker;
149 :
150 : // stop scripts
151 0 : RefPtr<ScriptLoader> loader;
152 0 : bool scripts_enabled = false;
153 0 : if (document) {
154 0 : loader = document->ScriptLoader();
155 0 : scripts_enabled = loader->GetEnabled();
156 : }
157 0 : if (scripts_enabled) {
158 0 : loader->SetEnabled(false);
159 : }
160 :
161 : // Wrap things in a div or body for parsing, but it won't show up in
162 : // the fragment.
163 0 : nsresult rv = NS_OK;
164 0 : AutoTArray<nsString, 2> tagStack;
165 0 : nsCOMPtr<nsIContent> fragment;
166 0 : if (aIsXML) {
167 : // XHTML
168 0 : tagStack.AppendElement(NS_LITERAL_STRING(XHTML_DIV_TAG));
169 0 : rv = nsContentUtils::ParseFragmentXML(aFragment,
170 : document,
171 : tagStack,
172 : true,
173 0 : aReturn);
174 0 : fragment = do_QueryInterface(*aReturn);
175 : } else {
176 0 : NS_ADDREF(*aReturn = new DocumentFragment(document->NodeInfoManager()));
177 0 : fragment = do_QueryInterface(*aReturn);
178 0 : rv = nsContentUtils::ParseFragmentHTML(aFragment,
179 : fragment,
180 : nsGkAtoms::body,
181 : kNameSpaceID_XHTML,
182 : false,
183 0 : true);
184 : }
185 0 : if (fragment) {
186 0 : nsTreeSanitizer sanitizer(aFlags);
187 0 : sanitizer.Sanitize(fragment);
188 : }
189 :
190 0 : if (scripts_enabled) {
191 0 : loader->SetEnabled(true);
192 : }
193 :
194 0 : return rv;
195 : }
|