Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / widget / src / xpwidgets / nsHTMLFormatConverter.cpp
blob7c4e09c76ef5ed0093900d1f08e5040335ba3dcb
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsHTMLFormatConverter.h"
41 #include "nsCRT.h"
42 #include "nsISupportsArray.h"
43 #include "nsIComponentManager.h"
44 #include "nsCOMPtr.h"
45 #include "nsXPCOM.h"
46 #include "nsISupportsPrimitives.h"
48 #include "nsITransferable.h" // for mime defs, this is BAD
50 // HTML convertor stuff
51 #include "nsIParser.h"
52 #include "nsIDTD.h"
53 #include "nsParserCIID.h"
54 #include "nsIContentSink.h"
55 #include "nsPrimitiveHelpers.h"
56 #include "nsIDocumentEncoder.h"
57 #include "nsIHTMLToTextSink.h"
59 static NS_DEFINE_CID(kCParserCID, NS_PARSER_CID);
61 nsHTMLFormatConverter::nsHTMLFormatConverter()
65 nsHTMLFormatConverter::~nsHTMLFormatConverter()
69 NS_IMPL_ISUPPORTS1(nsHTMLFormatConverter, nsIFormatConverter)
72 // GetInputDataFlavors
74 // Creates a new list and returns the list of all the flavors this converter
75 // knows how to import. In this case, it's just HTML.
77 // Flavors (strings) are wrapped in a primitive object so that JavaScript can
78 // access them easily via XPConnect.
80 NS_IMETHODIMP
81 nsHTMLFormatConverter::GetInputDataFlavors(nsISupportsArray **_retval)
83 if ( !_retval )
84 return NS_ERROR_INVALID_ARG;
86 nsresult rv = NS_NewISupportsArray ( _retval ); // addrefs for us
87 if ( NS_SUCCEEDED(rv) )
88 rv = AddFlavorToList ( *_retval, kHTMLMime );
90 return rv;
92 } // GetInputDataFlavors
96 // GetOutputDataFlavors
98 // Creates a new list and returns the list of all the flavors this converter
99 // knows how to export (convert). In this case, it's all sorts of things that HTML can be
100 // converted to.
102 // Flavors (strings) are wrapped in a primitive object so that JavaScript can
103 // access them easily via XPConnect.
105 NS_IMETHODIMP
106 nsHTMLFormatConverter::GetOutputDataFlavors(nsISupportsArray **_retval)
108 if ( !_retval )
109 return NS_ERROR_INVALID_ARG;
111 nsresult rv = NS_NewISupportsArray ( _retval ); // addrefs for us
112 if ( NS_SUCCEEDED(rv) ) {
113 rv = AddFlavorToList ( *_retval, kHTMLMime );
114 if ( NS_FAILED(rv) )
115 return rv;
116 #if NOT_NOW
117 // pinkerton
118 // no one uses this flavor right now, so it's just slowing things down. If anyone cares I
119 // can put it back in.
120 rv = AddFlavorToList ( *_retval, kAOLMailMime );
121 if ( NS_FAILED(rv) )
122 return rv;
123 #endif
124 rv = AddFlavorToList ( *_retval, kUnicodeMime );
125 if ( NS_FAILED(rv) )
126 return rv;
128 return rv;
130 } // GetOutputDataFlavors
134 // AddFlavorToList
136 // Convenience routine for adding a flavor wrapped in an nsISupportsCString object
137 // to a list
139 nsresult
140 nsHTMLFormatConverter :: AddFlavorToList ( nsISupportsArray* inList, const char* inFlavor )
142 nsresult rv;
144 nsCOMPtr<nsISupportsCString> dataFlavor =
145 do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
146 if ( dataFlavor ) {
147 dataFlavor->SetData ( nsDependentCString(inFlavor) );
148 // add to list as an nsISupports so the correct interface gets the addref
149 // in AppendElement()
150 nsCOMPtr<nsISupports> genericFlavor ( do_QueryInterface(dataFlavor) );
151 inList->AppendElement ( genericFlavor);
153 return rv;
155 } // AddFlavorToList
159 // CanConvert
161 // Determines if we support the given conversion. Currently, this method only
162 // converts from HTML to others.
164 NS_IMETHODIMP
165 nsHTMLFormatConverter::CanConvert(const char *aFromDataFlavor, const char *aToDataFlavor, PRBool *_retval)
167 if ( !_retval )
168 return NS_ERROR_INVALID_ARG;
170 // STRING USE WARNING: reduce conversions here?
172 *_retval = PR_FALSE;
173 nsAutoString fromFlavor; fromFlavor.AssignWithConversion( aFromDataFlavor );
174 if ( !nsCRT::strcmp(aFromDataFlavor, kHTMLMime) ) {
175 if ( !nsCRT::strcmp(aToDataFlavor, kHTMLMime) )
176 *_retval = PR_TRUE;
177 else if ( !nsCRT::strcmp(aToDataFlavor, kUnicodeMime) )
178 *_retval = PR_TRUE;
179 #if NOT_NOW
180 // pinkerton
181 // no one uses this flavor right now, so it's just slowing things down. If anyone cares I
182 // can put it back in.
183 else if ( toFlavor.Equals(kAOLMailMime) )
184 *_retval = PR_TRUE;
185 #endif
187 return NS_OK;
189 } // CanConvert
194 // Convert
196 // Convert data from one flavor to another. The data is wrapped in primitive objects so that it is
197 // accessible from JS. Currently, this only accepts HTML input, so anything else is invalid.
199 //XXX This method copies the data WAAAAY too many time for my liking. Grrrrrr. Mostly it's because
200 //XXX we _must_ put things into nsStrings so that the parser will accept it. Lame lame lame lame. We
201 //XXX also can't just get raw unicode out of the nsString, so we have to allocate heap to get
202 //XXX unicode out of the string. Lame lame lame.
204 NS_IMETHODIMP
205 nsHTMLFormatConverter::Convert(const char *aFromDataFlavor, nsISupports *aFromData, PRUint32 aDataLen,
206 const char *aToDataFlavor, nsISupports **aToData, PRUint32 *aDataToLen)
208 if ( !aToData || !aDataToLen )
209 return NS_ERROR_INVALID_ARG;
211 nsresult rv = NS_OK;
213 if ( !nsCRT::strcmp(aFromDataFlavor, kHTMLMime) ) {
214 nsCAutoString toFlavor ( aToDataFlavor );
216 // HTML on clipboard is going to always be double byte so it will be in a primitive
217 // class of nsISupportsString. Also, since the data is in two byte chunks the
218 // length represents the length in 1-byte chars, so we need to divide by two.
219 nsCOMPtr<nsISupportsString> dataWrapper0 ( do_QueryInterface(aFromData) );
220 if (!dataWrapper0) {
221 return NS_ERROR_INVALID_ARG;
224 nsAutoString dataStr;
225 dataWrapper0->GetData ( dataStr ); //¥¥¥ COPY #1
226 // note: conversion to text/plain is done inside the clipboard. we do not need to worry
227 // about it here.
228 if ( toFlavor.Equals(kHTMLMime) || toFlavor.Equals(kUnicodeMime) ) {
229 nsresult res;
230 if (toFlavor.Equals(kHTMLMime)) {
231 PRInt32 dataLen = dataStr.Length() * 2;
232 nsPrimitiveHelpers::CreatePrimitiveForData ( toFlavor.get(), (void*)dataStr.get(), dataLen, aToData );
233 if ( *aToData )
234 *aDataToLen = dataLen;
235 } else {
236 nsAutoString outStr;
237 res = ConvertFromHTMLToUnicode(dataStr, outStr);
238 if (NS_SUCCEEDED(res)) {
239 PRInt32 dataLen = outStr.Length() * 2;
240 nsPrimitiveHelpers::CreatePrimitiveForData ( toFlavor.get(), (void*)outStr.get(), dataLen, aToData );
241 if ( *aToData )
242 *aDataToLen = dataLen;
245 } // else if HTML or Unicode
246 else if ( toFlavor.Equals(kAOLMailMime) ) {
247 nsAutoString outStr;
248 if ( NS_SUCCEEDED(ConvertFromHTMLToAOLMail(dataStr, outStr)) ) {
249 PRInt32 dataLen = outStr.Length() * 2;
250 nsPrimitiveHelpers::CreatePrimitiveForData ( toFlavor.get(), (void*)outStr.get(), dataLen, aToData );
251 if ( *aToData )
252 *aDataToLen = dataLen;
254 } // else if AOL mail
255 else {
256 *aToData = nsnull;
257 *aDataToLen = 0;
258 rv = NS_ERROR_FAILURE;
260 } // if we got html mime
261 else
262 rv = NS_ERROR_FAILURE;
264 return rv;
266 } // Convert
270 // ConvertFromHTMLToUnicode
272 // Takes HTML and converts it to plain text but in unicode.
274 NS_IMETHODIMP
275 nsHTMLFormatConverter::ConvertFromHTMLToUnicode(const nsAutoString & aFromStr, nsAutoString & aToStr)
277 // create the parser to do the conversion.
278 aToStr.SetLength(0);
279 nsresult rv;
280 nsCOMPtr<nsIParser> parser = do_CreateInstance(kCParserCID, &rv);
281 if ( !parser )
282 return rv;
284 // convert it!
285 nsCOMPtr<nsIContentSink> sink;
287 sink = do_CreateInstance(NS_PLAINTEXTSINK_CONTRACTID);
288 NS_ENSURE_TRUE(sink, NS_ERROR_FAILURE);
290 nsCOMPtr<nsIHTMLToTextSink> textSink(do_QueryInterface(sink));
291 NS_ENSURE_TRUE(textSink, NS_ERROR_FAILURE);
293 textSink->Initialize(&aToStr, nsIDocumentEncoder::OutputSelectionOnly
294 | nsIDocumentEncoder::OutputAbsoluteLinks, 0);
296 parser->SetContentSink(sink);
298 parser->Parse(aFromStr, 0, NS_LITERAL_CSTRING("text/html"), PR_TRUE, eDTDMode_fragment);
300 return NS_OK;
301 } // ConvertFromHTMLToUnicode
304 NS_IMETHODIMP
305 nsHTMLFormatConverter::ConvertFromHTMLToAOLMail(const nsAutoString & aFromStr,
306 nsAutoString & aToStr)
308 aToStr.AssignLiteral("<HTML>");
309 aToStr.Append(aFromStr);
310 aToStr.AppendLiteral("</HTML>");
312 return NS_OK;