Bug 1487655 [wpt PR 12775] - [css-properties-values-api] Typify CSSStyleValue.parse...
[gecko.git] / widget / nsHTMLFormatConverter.cpp
blob656550d33b9b55a3adfb3b0d68b9ad0eb883c00d
1 /* -*- Mode: C++; tab-width: 2; 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 "nsHTMLFormatConverter.h"
8 #include "nsArray.h"
9 #include "nsCRT.h"
10 #include "nsIComponentManager.h"
11 #include "nsCOMPtr.h"
12 #include "nsXPCOM.h"
13 #include "nsISupportsPrimitives.h"
15 #include "nsITransferable.h" // for mime defs, this is BAD
17 // HTML convertor stuff
18 #include "nsPrimitiveHelpers.h"
19 #include "nsIDocumentEncoder.h"
20 #include "nsContentUtils.h"
22 nsHTMLFormatConverter::nsHTMLFormatConverter()
26 nsHTMLFormatConverter::~nsHTMLFormatConverter()
30 NS_IMPL_ISUPPORTS(nsHTMLFormatConverter, nsIFormatConverter)
33 // GetInputDataFlavors
35 // Creates a new list and returns the list of all the flavors this converter
36 // knows how to import. In this case, it's just HTML.
38 // Flavors (strings) are wrapped in a primitive object so that JavaScript can
39 // access them easily via XPConnect.
41 NS_IMETHODIMP
42 nsHTMLFormatConverter::GetInputDataFlavors(nsIArray **_retval)
44 if ( !_retval )
45 return NS_ERROR_INVALID_ARG;
47 nsCOMPtr<nsIMutableArray> array = nsArray::Create();
48 nsresult rv = AddFlavorToList ( array, kHTMLMime );
50 array.forget(_retval);
51 return rv;
53 } // GetInputDataFlavors
57 // GetOutputDataFlavors
59 // Creates a new list and returns the list of all the flavors this converter
60 // knows how to export (convert). In this case, it's all sorts of things that HTML can be
61 // converted to.
63 // Flavors (strings) are wrapped in a primitive object so that JavaScript can
64 // access them easily via XPConnect.
66 NS_IMETHODIMP
67 nsHTMLFormatConverter::GetOutputDataFlavors(nsIArray **_retval)
69 if ( !_retval )
70 return NS_ERROR_INVALID_ARG;
72 nsCOMPtr<nsIMutableArray> array = nsArray::Create();
73 nsresult rv = AddFlavorToList ( array, kHTMLMime );
74 if ( NS_FAILED(rv) )
75 return rv;
76 rv = AddFlavorToList ( array, kUnicodeMime );
77 if ( NS_FAILED(rv) )
78 return rv;
80 array.forget(_retval);
81 return rv;
83 } // GetOutputDataFlavors
87 // AddFlavorToList
89 // Convenience routine for adding a flavor wrapped in an nsISupportsCString object
90 // to a list
92 nsresult
93 nsHTMLFormatConverter :: AddFlavorToList ( nsCOMPtr<nsIMutableArray>& inList, const char* inFlavor )
95 nsresult rv;
97 nsCOMPtr<nsISupportsCString> dataFlavor =
98 do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
99 if ( dataFlavor ) {
100 dataFlavor->SetData ( nsDependentCString(inFlavor) );
101 // add to list as an nsISupports so the correct interface gets the addref
102 // in AppendElement()
103 nsCOMPtr<nsISupports> genericFlavor ( do_QueryInterface(dataFlavor) );
104 inList->AppendElement ( genericFlavor );
106 return rv;
108 } // AddFlavorToList
112 // CanConvert
114 // Determines if we support the given conversion. Currently, this method only
115 // converts from HTML to others.
117 NS_IMETHODIMP
118 nsHTMLFormatConverter::CanConvert(const char *aFromDataFlavor, const char *aToDataFlavor, bool *_retval)
120 if ( !_retval )
121 return NS_ERROR_INVALID_ARG;
123 *_retval = false;
124 if ( !nsCRT::strcmp(aFromDataFlavor, kHTMLMime) ) {
125 if ( !nsCRT::strcmp(aToDataFlavor, kHTMLMime) )
126 *_retval = true;
127 else if ( !nsCRT::strcmp(aToDataFlavor, kUnicodeMime) )
128 *_retval = true;
129 #if NOT_NOW
130 // pinkerton
131 // no one uses this flavor right now, so it's just slowing things down. If anyone cares I
132 // can put it back in.
133 else if ( toFlavor.Equals(kAOLMailMime) )
134 *_retval = true;
135 #endif
137 return NS_OK;
139 } // CanConvert
144 // Convert
146 // Convert data from one flavor to another. The data is wrapped in primitive objects so that it is
147 // accessible from JS. Currently, this only accepts HTML input, so anything else is invalid.
149 //XXX This method copies the data WAAAAY too many time for my liking. Grrrrrr. Mostly it's because
150 //XXX we _must_ put things into nsStrings so that the parser will accept it. Lame lame lame lame. We
151 //XXX also can't just get raw unicode out of the nsString, so we have to allocate heap to get
152 //XXX unicode out of the string. Lame lame lame.
154 NS_IMETHODIMP
155 nsHTMLFormatConverter::Convert(const char *aFromDataFlavor, nsISupports *aFromData, uint32_t aDataLen,
156 const char *aToDataFlavor, nsISupports **aToData, uint32_t *aDataToLen)
158 if ( !aToData || !aDataToLen )
159 return NS_ERROR_INVALID_ARG;
161 nsresult rv = NS_OK;
162 *aToData = nullptr;
163 *aDataToLen = 0;
165 if ( !nsCRT::strcmp(aFromDataFlavor, kHTMLMime) ) {
166 nsAutoCString toFlavor ( aToDataFlavor );
168 // HTML on clipboard is going to always be double byte so it will be in a primitive
169 // class of nsISupportsString. Also, since the data is in two byte chunks the
170 // length represents the length in 1-byte chars, so we need to divide by two.
171 nsCOMPtr<nsISupportsString> dataWrapper0 ( do_QueryInterface(aFromData) );
172 if (!dataWrapper0) {
173 return NS_ERROR_INVALID_ARG;
176 nsAutoString dataStr;
177 dataWrapper0->GetData ( dataStr ); // COPY #1
178 // note: conversion to text/plain is done inside the clipboard. we do not need to worry
179 // about it here.
180 if ( toFlavor.Equals(kHTMLMime) || toFlavor.Equals(kUnicodeMime) ) {
181 nsresult res;
182 if (toFlavor.Equals(kHTMLMime)) {
183 int32_t dataLen = dataStr.Length() * 2;
184 nsPrimitiveHelpers::CreatePrimitiveForData ( toFlavor, dataStr.get(), dataLen, aToData );
185 if ( *aToData )
186 *aDataToLen = dataLen;
187 } else {
188 nsAutoString outStr;
189 res = ConvertFromHTMLToUnicode(dataStr, outStr);
190 if (NS_SUCCEEDED(res)) {
191 int32_t dataLen = outStr.Length() * 2;
192 nsPrimitiveHelpers::CreatePrimitiveForData ( toFlavor, outStr.get(), dataLen, aToData );
193 if ( *aToData )
194 *aDataToLen = dataLen;
197 } // else if HTML or Unicode
198 else if ( toFlavor.Equals(kAOLMailMime) ) {
199 nsAutoString outStr;
200 if ( NS_SUCCEEDED(ConvertFromHTMLToAOLMail(dataStr, outStr)) ) {
201 int32_t dataLen = outStr.Length() * 2;
202 nsPrimitiveHelpers::CreatePrimitiveForData ( toFlavor, outStr.get(), dataLen, aToData );
203 if ( *aToData )
204 *aDataToLen = dataLen;
206 } // else if AOL mail
207 else {
208 rv = NS_ERROR_FAILURE;
210 } // if we got html mime
211 else
212 rv = NS_ERROR_FAILURE;
214 return rv;
216 } // Convert
220 // ConvertFromHTMLToUnicode
222 // Takes HTML and converts it to plain text but in unicode.
224 NS_IMETHODIMP
225 nsHTMLFormatConverter::ConvertFromHTMLToUnicode(const nsAutoString & aFromStr, nsAutoString & aToStr)
227 return nsContentUtils::ConvertToPlainText(aFromStr,
228 aToStr,
229 nsIDocumentEncoder::OutputSelectionOnly |
230 nsIDocumentEncoder::OutputAbsoluteLinks |
231 nsIDocumentEncoder::OutputNoScriptContent |
232 nsIDocumentEncoder::OutputNoFramesContent,
234 } // ConvertFromHTMLToUnicode
237 NS_IMETHODIMP
238 nsHTMLFormatConverter::ConvertFromHTMLToAOLMail(const nsAutoString & aFromStr,
239 nsAutoString & aToStr)
241 aToStr.AssignLiteral("<HTML>");
242 aToStr.Append(aFromStr);
243 aToStr.AppendLiteral("</HTML>");
245 return NS_OK;