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
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.
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"
42 #include "nsISupportsArray.h"
43 #include "nsIComponentManager.h"
46 #include "nsISupportsPrimitives.h"
48 #include "nsITransferable.h" // for mime defs, this is BAD
50 // HTML convertor stuff
51 #include "nsIParser.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.
81 nsHTMLFormatConverter::GetInputDataFlavors(nsISupportsArray
**_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
);
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
102 // Flavors (strings) are wrapped in a primitive object so that JavaScript can
103 // access them easily via XPConnect.
106 nsHTMLFormatConverter::GetOutputDataFlavors(nsISupportsArray
**_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
);
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
);
124 rv
= AddFlavorToList ( *_retval
, kUnicodeMime
);
130 } // GetOutputDataFlavors
136 // Convenience routine for adding a flavor wrapped in an nsISupportsCString object
140 nsHTMLFormatConverter :: AddFlavorToList ( nsISupportsArray
* inList
, const char* inFlavor
)
144 nsCOMPtr
<nsISupportsCString
> dataFlavor
=
145 do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID
, &rv
);
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
);
161 // Determines if we support the given conversion. Currently, this method only
162 // converts from HTML to others.
165 nsHTMLFormatConverter::CanConvert(const char *aFromDataFlavor
, const char *aToDataFlavor
, PRBool
*_retval
)
168 return NS_ERROR_INVALID_ARG
;
170 // STRING USE WARNING: reduce conversions here?
173 nsAutoString fromFlavor
; fromFlavor
.AssignWithConversion( aFromDataFlavor
);
174 if ( !nsCRT::strcmp(aFromDataFlavor
, kHTMLMime
) ) {
175 if ( !nsCRT::strcmp(aToDataFlavor
, kHTMLMime
) )
177 else if ( !nsCRT::strcmp(aToDataFlavor
, kUnicodeMime
) )
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
) )
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.
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
;
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
) );
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
228 if ( toFlavor
.Equals(kHTMLMime
) || toFlavor
.Equals(kUnicodeMime
) ) {
230 if (toFlavor
.Equals(kHTMLMime
)) {
231 PRInt32 dataLen
= dataStr
.Length() * 2;
232 nsPrimitiveHelpers::CreatePrimitiveForData ( toFlavor
.get(), (void*)dataStr
.get(), dataLen
, aToData
);
234 *aDataToLen
= dataLen
;
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
);
242 *aDataToLen
= dataLen
;
245 } // else if HTML or Unicode
246 else if ( toFlavor
.Equals(kAOLMailMime
) ) {
248 if ( NS_SUCCEEDED(ConvertFromHTMLToAOLMail(dataStr
, outStr
)) ) {
249 PRInt32 dataLen
= outStr
.Length() * 2;
250 nsPrimitiveHelpers::CreatePrimitiveForData ( toFlavor
.get(), (void*)outStr
.get(), dataLen
, aToData
);
252 *aDataToLen
= dataLen
;
254 } // else if AOL mail
258 rv
= NS_ERROR_FAILURE
;
260 } // if we got html mime
262 rv
= NS_ERROR_FAILURE
;
270 // ConvertFromHTMLToUnicode
272 // Takes HTML and converts it to plain text but in unicode.
275 nsHTMLFormatConverter::ConvertFromHTMLToUnicode(const nsAutoString
& aFromStr
, nsAutoString
& aToStr
)
277 // create the parser to do the conversion.
280 nsCOMPtr
<nsIParser
> parser
= do_CreateInstance(kCParserCID
, &rv
);
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
);
301 } // ConvertFromHTMLToUnicode
305 nsHTMLFormatConverter::ConvertFromHTMLToAOLMail(const nsAutoString
& aFromStr
,
306 nsAutoString
& aToStr
)
308 aToStr
.AssignLiteral("<HTML>");
309 aToStr
.Append(aFromStr
);
310 aToStr
.AppendLiteral("</HTML>");