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"
12 #include "nsISupportsPrimitives.h"
14 // HTML convertor stuff
15 #include "nsPrimitiveHelpers.h"
16 #include "nsIDocumentEncoder.h"
17 #include "nsContentUtils.h"
19 nsHTMLFormatConverter::nsHTMLFormatConverter() {}
21 nsHTMLFormatConverter::~nsHTMLFormatConverter() {}
23 NS_IMPL_ISUPPORTS(nsHTMLFormatConverter
, nsIFormatConverter
)
26 // GetInputDataFlavors
28 // Creates a new list and returns the list of all the flavors this converter
29 // knows how to import. In this case, it's just HTML.
32 nsHTMLFormatConverter::GetInputDataFlavors(nsTArray
<nsCString
>& aFlavors
) {
33 aFlavors
.AppendElement(NS_LITERAL_CSTRING(kHTMLMime
));
38 // GetOutputDataFlavors
40 // Creates a new list and returns the list of all the flavors this converter
41 // knows how to export (convert). In this case, it's all sorts of things that
42 // HTML can be converted to.
45 nsHTMLFormatConverter::GetOutputDataFlavors(nsTArray
<nsCString
>& aFlavors
) {
46 aFlavors
.AppendElement(NS_LITERAL_CSTRING(kHTMLMime
));
47 aFlavors
.AppendElement(NS_LITERAL_CSTRING(kUnicodeMime
));
54 // Determines if we support the given conversion. Currently, this method only
55 // converts from HTML to others.
58 nsHTMLFormatConverter::CanConvert(const char* aFromDataFlavor
,
59 const char* aToDataFlavor
, bool* _retval
) {
60 if (!_retval
) return NS_ERROR_INVALID_ARG
;
63 if (!nsCRT::strcmp(aFromDataFlavor
, kHTMLMime
)) {
64 if (!nsCRT::strcmp(aToDataFlavor
, kHTMLMime
))
66 else if (!nsCRT::strcmp(aToDataFlavor
, kUnicodeMime
))
70 // no one uses this flavor right now, so it's just slowing things down. If
71 // anyone cares I can put it back in.
72 else if (toFlavor
.Equals(kAOLMailMime
))
83 // Convert data from one flavor to another. The data is wrapped in primitive
84 // objects so that it is accessible from JS. Currently, this only accepts HTML
85 // input, so anything else is invalid.
87 // XXX This method copies the data WAAAAY too many time for my liking. Grrrrrr.
88 // Mostly it's because
89 // XXX we _must_ put things into nsStrings so that the parser will accept it.
90 // Lame lame lame lame. We
91 // XXX also can't just get raw unicode out of the nsString, so we have to
92 // allocate heap to get
93 // XXX unicode out of the string. Lame lame lame.
96 nsHTMLFormatConverter::Convert(const char* aFromDataFlavor
,
97 nsISupports
* aFromData
,
98 const char* aToDataFlavor
,
99 nsISupports
** aToData
) {
100 if (!aToData
) return NS_ERROR_INVALID_ARG
;
105 if (!nsCRT::strcmp(aFromDataFlavor
, kHTMLMime
)) {
106 nsAutoCString
toFlavor(aToDataFlavor
);
108 // HTML on clipboard is going to always be double byte so it will be in a
109 // primitive class of nsISupportsString. Also, since the data is in two byte
110 // chunks the length represents the length in 1-byte chars, so we need to
112 nsCOMPtr
<nsISupportsString
> dataWrapper0(do_QueryInterface(aFromData
));
114 return NS_ERROR_INVALID_ARG
;
117 nsAutoString dataStr
;
118 dataWrapper0
->GetData(dataStr
); // COPY #1
119 // note: conversion to text/plain is done inside the clipboard. we do not
120 // need to worry about it here.
121 if (toFlavor
.Equals(kHTMLMime
) || toFlavor
.Equals(kUnicodeMime
)) {
123 if (toFlavor
.Equals(kHTMLMime
)) {
124 int32_t dataLen
= dataStr
.Length() * 2;
125 nsPrimitiveHelpers::CreatePrimitiveForData(toFlavor
, dataStr
.get(),
129 res
= ConvertFromHTMLToUnicode(dataStr
, outStr
);
130 if (NS_SUCCEEDED(res
)) {
131 int32_t dataLen
= outStr
.Length() * 2;
132 nsPrimitiveHelpers::CreatePrimitiveForData(toFlavor
, outStr
.get(),
136 } // else if HTML or Unicode
137 else if (toFlavor
.Equals(kAOLMailMime
)) {
139 if (NS_SUCCEEDED(ConvertFromHTMLToAOLMail(dataStr
, outStr
))) {
140 int32_t dataLen
= outStr
.Length() * 2;
141 nsPrimitiveHelpers::CreatePrimitiveForData(toFlavor
, outStr
.get(),
144 } // else if AOL mail
146 rv
= NS_ERROR_FAILURE
;
148 } // if we got html mime
150 rv
= NS_ERROR_FAILURE
;
157 // ConvertFromHTMLToUnicode
159 // Takes HTML and converts it to plain text but in unicode.
162 nsHTMLFormatConverter::ConvertFromHTMLToUnicode(const nsAutoString
& aFromStr
,
163 nsAutoString
& aToStr
) {
164 return nsContentUtils::ConvertToPlainText(
166 nsIDocumentEncoder::OutputSelectionOnly
|
167 nsIDocumentEncoder::OutputAbsoluteLinks
|
168 nsIDocumentEncoder::OutputNoScriptContent
|
169 nsIDocumentEncoder::OutputNoFramesContent
,
171 } // ConvertFromHTMLToUnicode
174 nsHTMLFormatConverter::ConvertFromHTMLToAOLMail(const nsAutoString
& aFromStr
,
175 nsAutoString
& aToStr
) {
176 aToStr
.AssignLiteral("<HTML>");
177 aToStr
.Append(aFromStr
);
178 aToStr
.AppendLiteral("</HTML>");