nsHTMLFormatConverter.cpp 7.2 KB

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