Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsHTMLFormatConverter.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "nsCRT.h" |
michael@0 | 9 | #include "nsISupportsArray.h" |
michael@0 | 10 | #include "nsIComponentManager.h" |
michael@0 | 11 | #include "nsCOMPtr.h" |
michael@0 | 12 | #include "nsXPCOM.h" |
michael@0 | 13 | #include "nsISupportsPrimitives.h" |
michael@0 | 14 | |
michael@0 | 15 | #include "nsITransferable.h" // for mime defs, this is BAD |
michael@0 | 16 | |
michael@0 | 17 | // HTML convertor stuff |
michael@0 | 18 | #include "nsPrimitiveHelpers.h" |
michael@0 | 19 | #include "nsIDocumentEncoder.h" |
michael@0 | 20 | #include "nsContentUtils.h" |
michael@0 | 21 | |
michael@0 | 22 | nsHTMLFormatConverter::nsHTMLFormatConverter() |
michael@0 | 23 | { |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | nsHTMLFormatConverter::~nsHTMLFormatConverter() |
michael@0 | 27 | { |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | NS_IMPL_ISUPPORTS(nsHTMLFormatConverter, nsIFormatConverter) |
michael@0 | 31 | |
michael@0 | 32 | // |
michael@0 | 33 | // GetInputDataFlavors |
michael@0 | 34 | // |
michael@0 | 35 | // Creates a new list and returns the list of all the flavors this converter |
michael@0 | 36 | // knows how to import. In this case, it's just HTML. |
michael@0 | 37 | // |
michael@0 | 38 | // Flavors (strings) are wrapped in a primitive object so that JavaScript can |
michael@0 | 39 | // access them easily via XPConnect. |
michael@0 | 40 | // |
michael@0 | 41 | NS_IMETHODIMP |
michael@0 | 42 | nsHTMLFormatConverter::GetInputDataFlavors(nsISupportsArray **_retval) |
michael@0 | 43 | { |
michael@0 | 44 | if ( !_retval ) |
michael@0 | 45 | return NS_ERROR_INVALID_ARG; |
michael@0 | 46 | |
michael@0 | 47 | nsresult rv = NS_NewISupportsArray ( _retval ); // addrefs for us |
michael@0 | 48 | if ( NS_SUCCEEDED(rv) ) |
michael@0 | 49 | rv = AddFlavorToList ( *_retval, kHTMLMime ); |
michael@0 | 50 | |
michael@0 | 51 | return rv; |
michael@0 | 52 | |
michael@0 | 53 | } // GetInputDataFlavors |
michael@0 | 54 | |
michael@0 | 55 | |
michael@0 | 56 | // |
michael@0 | 57 | // GetOutputDataFlavors |
michael@0 | 58 | // |
michael@0 | 59 | // Creates a new list and returns the list of all the flavors this converter |
michael@0 | 60 | // knows how to export (convert). In this case, it's all sorts of things that HTML can be |
michael@0 | 61 | // converted to. |
michael@0 | 62 | // |
michael@0 | 63 | // Flavors (strings) are wrapped in a primitive object so that JavaScript can |
michael@0 | 64 | // access them easily via XPConnect. |
michael@0 | 65 | // |
michael@0 | 66 | NS_IMETHODIMP |
michael@0 | 67 | nsHTMLFormatConverter::GetOutputDataFlavors(nsISupportsArray **_retval) |
michael@0 | 68 | { |
michael@0 | 69 | if ( !_retval ) |
michael@0 | 70 | return NS_ERROR_INVALID_ARG; |
michael@0 | 71 | |
michael@0 | 72 | nsresult rv = NS_NewISupportsArray ( _retval ); // addrefs for us |
michael@0 | 73 | if ( NS_SUCCEEDED(rv) ) { |
michael@0 | 74 | rv = AddFlavorToList ( *_retval, kHTMLMime ); |
michael@0 | 75 | if ( NS_FAILED(rv) ) |
michael@0 | 76 | return rv; |
michael@0 | 77 | #if NOT_NOW |
michael@0 | 78 | // pinkerton |
michael@0 | 79 | // no one uses this flavor right now, so it's just slowing things down. If anyone cares I |
michael@0 | 80 | // can put it back in. |
michael@0 | 81 | rv = AddFlavorToList ( *_retval, kAOLMailMime ); |
michael@0 | 82 | if ( NS_FAILED(rv) ) |
michael@0 | 83 | return rv; |
michael@0 | 84 | #endif |
michael@0 | 85 | rv = AddFlavorToList ( *_retval, kUnicodeMime ); |
michael@0 | 86 | if ( NS_FAILED(rv) ) |
michael@0 | 87 | return rv; |
michael@0 | 88 | } |
michael@0 | 89 | return rv; |
michael@0 | 90 | |
michael@0 | 91 | } // GetOutputDataFlavors |
michael@0 | 92 | |
michael@0 | 93 | |
michael@0 | 94 | // |
michael@0 | 95 | // AddFlavorToList |
michael@0 | 96 | // |
michael@0 | 97 | // Convenience routine for adding a flavor wrapped in an nsISupportsCString object |
michael@0 | 98 | // to a list |
michael@0 | 99 | // |
michael@0 | 100 | nsresult |
michael@0 | 101 | nsHTMLFormatConverter :: AddFlavorToList ( nsISupportsArray* inList, const char* inFlavor ) |
michael@0 | 102 | { |
michael@0 | 103 | nsresult rv; |
michael@0 | 104 | |
michael@0 | 105 | nsCOMPtr<nsISupportsCString> dataFlavor = |
michael@0 | 106 | do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv); |
michael@0 | 107 | if ( dataFlavor ) { |
michael@0 | 108 | dataFlavor->SetData ( nsDependentCString(inFlavor) ); |
michael@0 | 109 | // add to list as an nsISupports so the correct interface gets the addref |
michael@0 | 110 | // in AppendElement() |
michael@0 | 111 | nsCOMPtr<nsISupports> genericFlavor ( do_QueryInterface(dataFlavor) ); |
michael@0 | 112 | inList->AppendElement ( genericFlavor); |
michael@0 | 113 | } |
michael@0 | 114 | return rv; |
michael@0 | 115 | |
michael@0 | 116 | } // AddFlavorToList |
michael@0 | 117 | |
michael@0 | 118 | |
michael@0 | 119 | // |
michael@0 | 120 | // CanConvert |
michael@0 | 121 | // |
michael@0 | 122 | // Determines if we support the given conversion. Currently, this method only |
michael@0 | 123 | // converts from HTML to others. |
michael@0 | 124 | // |
michael@0 | 125 | NS_IMETHODIMP |
michael@0 | 126 | nsHTMLFormatConverter::CanConvert(const char *aFromDataFlavor, const char *aToDataFlavor, bool *_retval) |
michael@0 | 127 | { |
michael@0 | 128 | if ( !_retval ) |
michael@0 | 129 | return NS_ERROR_INVALID_ARG; |
michael@0 | 130 | |
michael@0 | 131 | *_retval = false; |
michael@0 | 132 | if ( !nsCRT::strcmp(aFromDataFlavor, kHTMLMime) ) { |
michael@0 | 133 | if ( !nsCRT::strcmp(aToDataFlavor, kHTMLMime) ) |
michael@0 | 134 | *_retval = true; |
michael@0 | 135 | else if ( !nsCRT::strcmp(aToDataFlavor, kUnicodeMime) ) |
michael@0 | 136 | *_retval = true; |
michael@0 | 137 | #if NOT_NOW |
michael@0 | 138 | // pinkerton |
michael@0 | 139 | // no one uses this flavor right now, so it's just slowing things down. If anyone cares I |
michael@0 | 140 | // can put it back in. |
michael@0 | 141 | else if ( toFlavor.Equals(kAOLMailMime) ) |
michael@0 | 142 | *_retval = true; |
michael@0 | 143 | #endif |
michael@0 | 144 | } |
michael@0 | 145 | return NS_OK; |
michael@0 | 146 | |
michael@0 | 147 | } // CanConvert |
michael@0 | 148 | |
michael@0 | 149 | |
michael@0 | 150 | |
michael@0 | 151 | // |
michael@0 | 152 | // Convert |
michael@0 | 153 | // |
michael@0 | 154 | // Convert data from one flavor to another. The data is wrapped in primitive objects so that it is |
michael@0 | 155 | // accessible from JS. Currently, this only accepts HTML input, so anything else is invalid. |
michael@0 | 156 | // |
michael@0 | 157 | //XXX This method copies the data WAAAAY too many time for my liking. Grrrrrr. Mostly it's because |
michael@0 | 158 | //XXX we _must_ put things into nsStrings so that the parser will accept it. Lame lame lame lame. We |
michael@0 | 159 | //XXX also can't just get raw unicode out of the nsString, so we have to allocate heap to get |
michael@0 | 160 | //XXX unicode out of the string. Lame lame lame. |
michael@0 | 161 | // |
michael@0 | 162 | NS_IMETHODIMP |
michael@0 | 163 | nsHTMLFormatConverter::Convert(const char *aFromDataFlavor, nsISupports *aFromData, uint32_t aDataLen, |
michael@0 | 164 | const char *aToDataFlavor, nsISupports **aToData, uint32_t *aDataToLen) |
michael@0 | 165 | { |
michael@0 | 166 | if ( !aToData || !aDataToLen ) |
michael@0 | 167 | return NS_ERROR_INVALID_ARG; |
michael@0 | 168 | |
michael@0 | 169 | nsresult rv = NS_OK; |
michael@0 | 170 | *aToData = nullptr; |
michael@0 | 171 | *aDataToLen = 0; |
michael@0 | 172 | |
michael@0 | 173 | if ( !nsCRT::strcmp(aFromDataFlavor, kHTMLMime) ) { |
michael@0 | 174 | nsAutoCString toFlavor ( aToDataFlavor ); |
michael@0 | 175 | |
michael@0 | 176 | // HTML on clipboard is going to always be double byte so it will be in a primitive |
michael@0 | 177 | // class of nsISupportsString. Also, since the data is in two byte chunks the |
michael@0 | 178 | // length represents the length in 1-byte chars, so we need to divide by two. |
michael@0 | 179 | nsCOMPtr<nsISupportsString> dataWrapper0 ( do_QueryInterface(aFromData) ); |
michael@0 | 180 | if (!dataWrapper0) { |
michael@0 | 181 | return NS_ERROR_INVALID_ARG; |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | nsAutoString dataStr; |
michael@0 | 185 | dataWrapper0->GetData ( dataStr ); //еее COPY #1 |
michael@0 | 186 | // note: conversion to text/plain is done inside the clipboard. we do not need to worry |
michael@0 | 187 | // about it here. |
michael@0 | 188 | if ( toFlavor.Equals(kHTMLMime) || toFlavor.Equals(kUnicodeMime) ) { |
michael@0 | 189 | nsresult res; |
michael@0 | 190 | if (toFlavor.Equals(kHTMLMime)) { |
michael@0 | 191 | int32_t dataLen = dataStr.Length() * 2; |
michael@0 | 192 | nsPrimitiveHelpers::CreatePrimitiveForData ( toFlavor.get(), dataStr.get(), dataLen, aToData ); |
michael@0 | 193 | if ( *aToData ) |
michael@0 | 194 | *aDataToLen = dataLen; |
michael@0 | 195 | } else { |
michael@0 | 196 | nsAutoString outStr; |
michael@0 | 197 | res = ConvertFromHTMLToUnicode(dataStr, outStr); |
michael@0 | 198 | if (NS_SUCCEEDED(res)) { |
michael@0 | 199 | int32_t dataLen = outStr.Length() * 2; |
michael@0 | 200 | nsPrimitiveHelpers::CreatePrimitiveForData ( toFlavor.get(), outStr.get(), dataLen, aToData ); |
michael@0 | 201 | if ( *aToData ) |
michael@0 | 202 | *aDataToLen = dataLen; |
michael@0 | 203 | } |
michael@0 | 204 | } |
michael@0 | 205 | } // else if HTML or Unicode |
michael@0 | 206 | else if ( toFlavor.Equals(kAOLMailMime) ) { |
michael@0 | 207 | nsAutoString outStr; |
michael@0 | 208 | if ( NS_SUCCEEDED(ConvertFromHTMLToAOLMail(dataStr, outStr)) ) { |
michael@0 | 209 | int32_t dataLen = outStr.Length() * 2; |
michael@0 | 210 | nsPrimitiveHelpers::CreatePrimitiveForData ( toFlavor.get(), outStr.get(), dataLen, aToData ); |
michael@0 | 211 | if ( *aToData ) |
michael@0 | 212 | *aDataToLen = dataLen; |
michael@0 | 213 | } |
michael@0 | 214 | } // else if AOL mail |
michael@0 | 215 | else { |
michael@0 | 216 | rv = NS_ERROR_FAILURE; |
michael@0 | 217 | } |
michael@0 | 218 | } // if we got html mime |
michael@0 | 219 | else |
michael@0 | 220 | rv = NS_ERROR_FAILURE; |
michael@0 | 221 | |
michael@0 | 222 | return rv; |
michael@0 | 223 | |
michael@0 | 224 | } // Convert |
michael@0 | 225 | |
michael@0 | 226 | |
michael@0 | 227 | // |
michael@0 | 228 | // ConvertFromHTMLToUnicode |
michael@0 | 229 | // |
michael@0 | 230 | // Takes HTML and converts it to plain text but in unicode. |
michael@0 | 231 | // |
michael@0 | 232 | NS_IMETHODIMP |
michael@0 | 233 | nsHTMLFormatConverter::ConvertFromHTMLToUnicode(const nsAutoString & aFromStr, nsAutoString & aToStr) |
michael@0 | 234 | { |
michael@0 | 235 | return nsContentUtils::ConvertToPlainText(aFromStr, |
michael@0 | 236 | aToStr, |
michael@0 | 237 | nsIDocumentEncoder::OutputSelectionOnly | |
michael@0 | 238 | nsIDocumentEncoder::OutputAbsoluteLinks | |
michael@0 | 239 | nsIDocumentEncoder::OutputNoScriptContent | |
michael@0 | 240 | nsIDocumentEncoder::OutputNoFramesContent, |
michael@0 | 241 | 0); |
michael@0 | 242 | } // ConvertFromHTMLToUnicode |
michael@0 | 243 | |
michael@0 | 244 | |
michael@0 | 245 | NS_IMETHODIMP |
michael@0 | 246 | nsHTMLFormatConverter::ConvertFromHTMLToAOLMail(const nsAutoString & aFromStr, |
michael@0 | 247 | nsAutoString & aToStr) |
michael@0 | 248 | { |
michael@0 | 249 | aToStr.AssignLiteral("<HTML>"); |
michael@0 | 250 | aToStr.Append(aFromStr); |
michael@0 | 251 | aToStr.AppendLiteral("</HTML>"); |
michael@0 | 252 | |
michael@0 | 253 | return NS_OK; |
michael@0 | 254 | } |
michael@0 | 255 |