Bug 1884032 [wpt PR 44942] - [css-color] add missing colorscheme-aware tests, a=testonly
[gecko.git] / widget / nsHTMLFormatConverter.cpp
blob4898fd2c8b59d34a10f42282c5d29fe2c5ce96b4
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 "nsCOMPtr.h"
11 #include "nsITransferable.h"
12 #include "nsLiteralString.h"
13 #include "nsXPCOM.h"
14 #include "nsISupportsPrimitives.h"
16 // HTML convertor stuff
17 #include "nsPrimitiveHelpers.h"
18 #include "nsIDocumentEncoder.h"
19 #include "nsContentUtils.h"
21 nsHTMLFormatConverter::nsHTMLFormatConverter() = default;
23 nsHTMLFormatConverter::~nsHTMLFormatConverter() = default;
25 NS_IMPL_ISUPPORTS(nsHTMLFormatConverter, nsIFormatConverter)
28 // GetInputDataFlavors
30 // Creates a new list and returns the list of all the flavors this converter
31 // knows how to import. In this case, it's just HTML.
33 NS_IMETHODIMP
34 nsHTMLFormatConverter::GetInputDataFlavors(nsTArray<nsCString>& aFlavors) {
35 aFlavors.AppendElement(nsLiteralCString(kHTMLMime));
36 return NS_OK;
40 // GetOutputDataFlavors
42 // Creates a new list and returns the list of all the flavors this converter
43 // knows how to export (convert). In this case, it's all sorts of things that
44 // HTML can be converted to.
46 NS_IMETHODIMP
47 nsHTMLFormatConverter::GetOutputDataFlavors(nsTArray<nsCString>& aFlavors) {
48 aFlavors.AppendElement(nsLiteralCString(kHTMLMime));
49 aFlavors.AppendElement(nsLiteralCString(kTextMime));
50 return NS_OK;
54 // CanConvert
56 // Determines if we support the given conversion. Currently, this method only
57 // converts from HTML to others.
59 NS_IMETHODIMP
60 nsHTMLFormatConverter::CanConvert(const char* aFromDataFlavor,
61 const char* aToDataFlavor, bool* _retval) {
62 if (!_retval) return NS_ERROR_INVALID_ARG;
64 *_retval = false;
65 if (!nsCRT::strcmp(aFromDataFlavor, kHTMLMime)) {
66 if (!nsCRT::strcmp(aToDataFlavor, kHTMLMime)) {
67 *_retval = true;
68 } else if (!nsCRT::strcmp(aToDataFlavor, kTextMime)) {
69 *_retval = true;
71 #if NOT_NOW
72 // pinkerton
73 // no one uses this flavor right now, so it's just slowing things down. If
74 // anyone cares I can put it back in.
75 else if (toFlavor.Equals(kAOLMailMime))
76 *_retval = true;
77 #endif
79 return NS_OK;
81 } // CanConvert
84 // Convert
86 // Convert data from one flavor to another. The data is wrapped in primitive
87 // objects so that it is accessible from JS. Currently, this only accepts HTML
88 // input, so anything else is invalid.
90 // XXX This method copies the data WAAAAY too many time for my liking. Grrrrrr.
91 // Mostly it's because
92 // XXX we _must_ put things into nsStrings so that the parser will accept it.
93 // Lame lame lame lame. We
94 // XXX also can't just get raw unicode out of the nsString, so we have to
95 // allocate heap to get
96 // XXX unicode out of the string. Lame lame lame.
98 NS_IMETHODIMP
99 nsHTMLFormatConverter::Convert(const char* aFromDataFlavor,
100 nsISupports* aFromData,
101 const char* aToDataFlavor,
102 nsISupports** aToData) {
103 if (!aToData) return NS_ERROR_INVALID_ARG;
105 nsresult rv = NS_OK;
106 *aToData = nullptr;
108 if (!nsCRT::strcmp(aFromDataFlavor, kHTMLMime)) {
109 nsAutoCString toFlavor(aToDataFlavor);
111 // HTML on clipboard is going to always be double byte so it will be in a
112 // primitive class of nsISupportsString. Also, since the data is in two byte
113 // chunks the length represents the length in 1-byte chars, so we need to
114 // divide by two.
115 nsCOMPtr<nsISupportsString> dataWrapper0(do_QueryInterface(aFromData));
116 if (!dataWrapper0) {
117 return NS_ERROR_INVALID_ARG;
120 nsAutoString dataStr;
121 dataWrapper0->GetData(dataStr); // COPY #1
122 // note: conversion to text/plain is done inside the clipboard. we do not
123 // need to worry about it here.
124 if (toFlavor.Equals(kHTMLMime) || toFlavor.Equals(kTextMime)) {
125 nsresult res;
126 if (toFlavor.Equals(kHTMLMime)) {
127 int32_t dataLen = dataStr.Length() * 2;
128 nsPrimitiveHelpers::CreatePrimitiveForData(toFlavor, dataStr.get(),
129 dataLen, aToData);
130 } else {
131 nsAutoString outStr;
132 res = ConvertFromHTMLToUnicode(dataStr, outStr);
133 if (NS_SUCCEEDED(res)) {
134 int32_t dataLen = outStr.Length() * 2;
135 nsPrimitiveHelpers::CreatePrimitiveForData(toFlavor, outStr.get(),
136 dataLen, aToData);
139 } // else if HTML or Unicode
140 else if (toFlavor.Equals(kAOLMailMime)) {
141 nsAutoString outStr;
142 if (NS_SUCCEEDED(ConvertFromHTMLToAOLMail(dataStr, outStr))) {
143 int32_t dataLen = outStr.Length() * 2;
144 nsPrimitiveHelpers::CreatePrimitiveForData(toFlavor, outStr.get(),
145 dataLen, aToData);
147 } // else if AOL mail
148 else {
149 rv = NS_ERROR_FAILURE;
151 } // if we got html mime
152 else
153 rv = NS_ERROR_FAILURE;
155 return rv;
157 } // Convert
160 // ConvertFromHTMLToUnicode
162 // Takes HTML and converts it to plain text but in unicode.
164 NS_IMETHODIMP
165 nsHTMLFormatConverter::ConvertFromHTMLToUnicode(const nsAutoString& aFromStr,
166 nsAutoString& aToStr) {
167 return nsContentUtils::ConvertToPlainText(
168 aFromStr, aToStr,
169 nsIDocumentEncoder::OutputSelectionOnly |
170 nsIDocumentEncoder::OutputAbsoluteLinks |
171 nsIDocumentEncoder::OutputNoScriptContent |
172 nsIDocumentEncoder::OutputNoFramesContent,
174 } // ConvertFromHTMLToUnicode
176 NS_IMETHODIMP
177 nsHTMLFormatConverter::ConvertFromHTMLToAOLMail(const nsAutoString& aFromStr,
178 nsAutoString& aToStr) {
179 aToStr.AssignLiteral("<HTML>");
180 aToStr.Append(aFromStr);
181 aToStr.AppendLiteral("</HTML>");
183 return NS_OK;