Bug 1606095 [wpt PR 20924] - [LayoutInstability] Fix flaky tests, a=testonly
[gecko.git] / widget / nsHTMLFormatConverter.cpp
blobd7cabe853188c5b80a997afec192dc67927128d0
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 "nsXPCOM.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.
31 NS_IMETHODIMP
32 nsHTMLFormatConverter::GetInputDataFlavors(nsTArray<nsCString>& aFlavors) {
33 aFlavors.AppendElement(NS_LITERAL_CSTRING(kHTMLMime));
34 return NS_OK;
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.
44 NS_IMETHODIMP
45 nsHTMLFormatConverter::GetOutputDataFlavors(nsTArray<nsCString>& aFlavors) {
46 aFlavors.AppendElement(NS_LITERAL_CSTRING(kHTMLMime));
47 aFlavors.AppendElement(NS_LITERAL_CSTRING(kUnicodeMime));
48 return NS_OK;
52 // CanConvert
54 // Determines if we support the given conversion. Currently, this method only
55 // converts from HTML to others.
57 NS_IMETHODIMP
58 nsHTMLFormatConverter::CanConvert(const char* aFromDataFlavor,
59 const char* aToDataFlavor, bool* _retval) {
60 if (!_retval) return NS_ERROR_INVALID_ARG;
62 *_retval = false;
63 if (!nsCRT::strcmp(aFromDataFlavor, kHTMLMime)) {
64 if (!nsCRT::strcmp(aToDataFlavor, kHTMLMime))
65 *_retval = true;
66 else if (!nsCRT::strcmp(aToDataFlavor, kUnicodeMime))
67 *_retval = true;
68 #if NOT_NOW
69 // pinkerton
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))
73 *_retval = true;
74 #endif
76 return NS_OK;
78 } // CanConvert
81 // Convert
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.
95 NS_IMETHODIMP
96 nsHTMLFormatConverter::Convert(const char* aFromDataFlavor,
97 nsISupports* aFromData,
98 const char* aToDataFlavor,
99 nsISupports** aToData) {
100 if (!aToData) return NS_ERROR_INVALID_ARG;
102 nsresult rv = NS_OK;
103 *aToData = nullptr;
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
111 // divide by two.
112 nsCOMPtr<nsISupportsString> dataWrapper0(do_QueryInterface(aFromData));
113 if (!dataWrapper0) {
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)) {
122 nsresult res;
123 if (toFlavor.Equals(kHTMLMime)) {
124 int32_t dataLen = dataStr.Length() * 2;
125 nsPrimitiveHelpers::CreatePrimitiveForData(toFlavor, dataStr.get(),
126 dataLen, aToData);
127 } else {
128 nsAutoString outStr;
129 res = ConvertFromHTMLToUnicode(dataStr, outStr);
130 if (NS_SUCCEEDED(res)) {
131 int32_t dataLen = outStr.Length() * 2;
132 nsPrimitiveHelpers::CreatePrimitiveForData(toFlavor, outStr.get(),
133 dataLen, aToData);
136 } // else if HTML or Unicode
137 else if (toFlavor.Equals(kAOLMailMime)) {
138 nsAutoString outStr;
139 if (NS_SUCCEEDED(ConvertFromHTMLToAOLMail(dataStr, outStr))) {
140 int32_t dataLen = outStr.Length() * 2;
141 nsPrimitiveHelpers::CreatePrimitiveForData(toFlavor, outStr.get(),
142 dataLen, aToData);
144 } // else if AOL mail
145 else {
146 rv = NS_ERROR_FAILURE;
148 } // if we got html mime
149 else
150 rv = NS_ERROR_FAILURE;
152 return rv;
154 } // Convert
157 // ConvertFromHTMLToUnicode
159 // Takes HTML and converts it to plain text but in unicode.
161 NS_IMETHODIMP
162 nsHTMLFormatConverter::ConvertFromHTMLToUnicode(const nsAutoString& aFromStr,
163 nsAutoString& aToStr) {
164 return nsContentUtils::ConvertToPlainText(
165 aFromStr, aToStr,
166 nsIDocumentEncoder::OutputSelectionOnly |
167 nsIDocumentEncoder::OutputAbsoluteLinks |
168 nsIDocumentEncoder::OutputNoScriptContent |
169 nsIDocumentEncoder::OutputNoFramesContent,
171 } // ConvertFromHTMLToUnicode
173 NS_IMETHODIMP
174 nsHTMLFormatConverter::ConvertFromHTMLToAOLMail(const nsAutoString& aFromStr,
175 nsAutoString& aToStr) {
176 aToStr.AssignLiteral("<HTML>");
177 aToStr.Append(aFromStr);
178 aToStr.AppendLiteral("</HTML>");
180 return NS_OK;