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