2001-09-29 00:14:13 +04:00
|
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-19 02:01:16 +04:00
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2000-10-07 10:58:49 +04:00
|
|
|
|
*
|
2004-04-19 02:01:16 +04:00
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
|
* http://www.mozilla.org/MPL/
|
2000-10-07 10:58:49 +04:00
|
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
|
* License.
|
2000-10-07 10:58:49 +04:00
|
|
|
|
*
|
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
|
*
|
2004-04-19 02:01:16 +04:00
|
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-29 00:14:13 +04:00
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
2000-10-07 10:58:49 +04:00
|
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
|
* Contributor(s):
|
2000-10-07 10:58:49 +04:00
|
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
2010-06-06 18:25:48 +04:00
|
|
|
|
* Mats Palmgren <matpal@gmail.com>
|
2001-09-29 00:14:13 +04:00
|
|
|
|
*
|
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-19 02:01:16 +04:00
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
2001-09-29 00:14:13 +04:00
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-19 02:01:16 +04:00
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-29 00:14:13 +04:00
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-19 02:01:16 +04:00
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-29 00:14:13 +04:00
|
|
|
|
*
|
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2000-10-07 10:58:49 +04:00
|
|
|
|
|
2007-08-07 19:18:36 +04:00
|
|
|
|
#include "nsHTMLFormatConverter.h"
|
|
|
|
|
|
2002-05-15 22:55:21 +04:00
|
|
|
|
#include "nsCRT.h"
|
2000-10-07 10:58:49 +04:00
|
|
|
|
#include "nsISupportsArray.h"
|
2001-09-07 01:13:11 +04:00
|
|
|
|
#include "nsIComponentManager.h"
|
2000-10-07 10:58:49 +04:00
|
|
|
|
#include "nsCOMPtr.h"
|
2002-09-04 03:36:13 +04:00
|
|
|
|
#include "nsXPCOM.h"
|
2000-10-07 10:58:49 +04:00
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
|
|
|
|
|
|
#include "nsITransferable.h" // for mime defs, this is BAD
|
|
|
|
|
|
|
|
|
|
// HTML convertor stuff
|
|
|
|
|
#include "nsPrimitiveHelpers.h"
|
|
|
|
|
#include "nsIDocumentEncoder.h"
|
2012-02-27 15:57:48 +04:00
|
|
|
|
#include "nsContentUtils.h"
|
2000-10-07 10:58:49 +04:00
|
|
|
|
|
|
|
|
|
nsHTMLFormatConverter::nsHTMLFormatConverter()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsHTMLFormatConverter::~nsHTMLFormatConverter()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-07 19:18:36 +04:00
|
|
|
|
NS_IMPL_ISUPPORTS1(nsHTMLFormatConverter, nsIFormatConverter)
|
2000-10-07 10:58:49 +04:00
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// GetInputDataFlavors
|
|
|
|
|
//
|
|
|
|
|
// Creates a new list and returns the list of all the flavors this converter
|
|
|
|
|
// knows how to import. In this case, it's just HTML.
|
|
|
|
|
//
|
|
|
|
|
// Flavors (strings) are wrapped in a primitive object so that JavaScript can
|
|
|
|
|
// access them easily via XPConnect.
|
|
|
|
|
//
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
|
nsHTMLFormatConverter::GetInputDataFlavors(nsISupportsArray **_retval)
|
|
|
|
|
{
|
|
|
|
|
if ( !_retval )
|
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
|
|
nsresult rv = NS_NewISupportsArray ( _retval ); // addrefs for us
|
|
|
|
|
if ( NS_SUCCEEDED(rv) )
|
|
|
|
|
rv = AddFlavorToList ( *_retval, kHTMLMime );
|
|
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
|
|
} // GetInputDataFlavors
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// GetOutputDataFlavors
|
|
|
|
|
//
|
|
|
|
|
// Creates a new list and returns the list of all the flavors this converter
|
|
|
|
|
// knows how to export (convert). In this case, it's all sorts of things that HTML can be
|
|
|
|
|
// converted to.
|
|
|
|
|
//
|
|
|
|
|
// Flavors (strings) are wrapped in a primitive object so that JavaScript can
|
|
|
|
|
// access them easily via XPConnect.
|
|
|
|
|
//
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
|
nsHTMLFormatConverter::GetOutputDataFlavors(nsISupportsArray **_retval)
|
|
|
|
|
{
|
|
|
|
|
if ( !_retval )
|
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
|
|
nsresult rv = NS_NewISupportsArray ( _retval ); // addrefs for us
|
|
|
|
|
if ( NS_SUCCEEDED(rv) ) {
|
|
|
|
|
rv = AddFlavorToList ( *_retval, kHTMLMime );
|
|
|
|
|
if ( NS_FAILED(rv) )
|
|
|
|
|
return rv;
|
|
|
|
|
#if NOT_NOW
|
|
|
|
|
// pinkerton
|
|
|
|
|
// no one uses this flavor right now, so it's just slowing things down. If anyone cares I
|
|
|
|
|
// can put it back in.
|
|
|
|
|
rv = AddFlavorToList ( *_retval, kAOLMailMime );
|
|
|
|
|
if ( NS_FAILED(rv) )
|
|
|
|
|
return rv;
|
|
|
|
|
#endif
|
|
|
|
|
rv = AddFlavorToList ( *_retval, kUnicodeMime );
|
|
|
|
|
if ( NS_FAILED(rv) )
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
|
|
} // GetOutputDataFlavors
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// AddFlavorToList
|
|
|
|
|
//
|
2002-08-06 04:53:19 +04:00
|
|
|
|
// Convenience routine for adding a flavor wrapped in an nsISupportsCString object
|
2000-10-07 10:58:49 +04:00
|
|
|
|
// to a list
|
|
|
|
|
//
|
|
|
|
|
nsresult
|
|
|
|
|
nsHTMLFormatConverter :: AddFlavorToList ( nsISupportsArray* inList, const char* inFlavor )
|
|
|
|
|
{
|
2004-11-01 21:50:36 +03:00
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupportsCString> dataFlavor =
|
|
|
|
|
do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
|
2000-10-07 10:58:49 +04:00
|
|
|
|
if ( dataFlavor ) {
|
2002-08-27 01:20:34 +04:00
|
|
|
|
dataFlavor->SetData ( nsDependentCString(inFlavor) );
|
2000-10-07 10:58:49 +04:00
|
|
|
|
// add to list as an nsISupports so the correct interface gets the addref
|
|
|
|
|
// in AppendElement()
|
|
|
|
|
nsCOMPtr<nsISupports> genericFlavor ( do_QueryInterface(dataFlavor) );
|
|
|
|
|
inList->AppendElement ( genericFlavor);
|
|
|
|
|
}
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
|
|
} // AddFlavorToList
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// CanConvert
|
|
|
|
|
//
|
|
|
|
|
// Determines if we support the given conversion. Currently, this method only
|
|
|
|
|
// converts from HTML to others.
|
|
|
|
|
//
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
|
nsHTMLFormatConverter::CanConvert(const char *aFromDataFlavor, const char *aToDataFlavor, bool *_retval)
|
2000-10-07 10:58:49 +04:00
|
|
|
|
{
|
|
|
|
|
if ( !_retval )
|
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
|
*_retval = false;
|
2000-10-07 10:58:49 +04:00
|
|
|
|
if ( !nsCRT::strcmp(aFromDataFlavor, kHTMLMime) ) {
|
|
|
|
|
if ( !nsCRT::strcmp(aToDataFlavor, kHTMLMime) )
|
2011-10-17 18:59:28 +04:00
|
|
|
|
*_retval = true;
|
2000-10-07 10:58:49 +04:00
|
|
|
|
else if ( !nsCRT::strcmp(aToDataFlavor, kUnicodeMime) )
|
2011-10-17 18:59:28 +04:00
|
|
|
|
*_retval = true;
|
2000-10-07 10:58:49 +04:00
|
|
|
|
#if NOT_NOW
|
|
|
|
|
// pinkerton
|
|
|
|
|
// no one uses this flavor right now, so it's just slowing things down. If anyone cares I
|
|
|
|
|
// can put it back in.
|
|
|
|
|
else if ( toFlavor.Equals(kAOLMailMime) )
|
2011-10-17 18:59:28 +04:00
|
|
|
|
*_retval = true;
|
2000-10-07 10:58:49 +04:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
|
|
} // CanConvert
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Convert
|
|
|
|
|
//
|
|
|
|
|
// Convert data from one flavor to another. The data is wrapped in primitive objects so that it is
|
2005-11-21 09:30:34 +03:00
|
|
|
|
// accessible from JS. Currently, this only accepts HTML input, so anything else is invalid.
|
2000-10-07 10:58:49 +04:00
|
|
|
|
//
|
|
|
|
|
//XXX This method copies the data WAAAAY too many time for my liking. Grrrrrr. Mostly it's because
|
|
|
|
|
//XXX we _must_ put things into nsStrings so that the parser will accept it. Lame lame lame lame. We
|
|
|
|
|
//XXX also can't just get raw unicode out of the nsString, so we have to allocate heap to get
|
|
|
|
|
//XXX unicode out of the string. Lame lame lame.
|
|
|
|
|
//
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
|
nsHTMLFormatConverter::Convert(const char *aFromDataFlavor, nsISupports *aFromData, PRUint32 aDataLen,
|
|
|
|
|
const char *aToDataFlavor, nsISupports **aToData, PRUint32 *aDataToLen)
|
|
|
|
|
{
|
|
|
|
|
if ( !aToData || !aDataToLen )
|
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
2010-06-06 18:25:48 +04:00
|
|
|
|
*aToData = nsnull;
|
|
|
|
|
*aDataToLen = 0;
|
2000-10-07 10:58:49 +04:00
|
|
|
|
|
|
|
|
|
if ( !nsCRT::strcmp(aFromDataFlavor, kHTMLMime) ) {
|
|
|
|
|
nsCAutoString toFlavor ( aToDataFlavor );
|
|
|
|
|
|
|
|
|
|
// HTML on clipboard is going to always be double byte so it will be in a primitive
|
2002-08-06 04:53:19 +04:00
|
|
|
|
// class of nsISupportsString. Also, since the data is in two byte chunks the
|
2000-10-07 10:58:49 +04:00
|
|
|
|
// length represents the length in 1-byte chars, so we need to divide by two.
|
2002-08-06 04:53:19 +04:00
|
|
|
|
nsCOMPtr<nsISupportsString> dataWrapper0 ( do_QueryInterface(aFromData) );
|
2005-09-10 15:39:32 +04:00
|
|
|
|
if (!dataWrapper0) {
|
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
}
|
2000-10-07 10:58:49 +04:00
|
|
|
|
|
2005-09-10 15:39:32 +04:00
|
|
|
|
nsAutoString dataStr;
|
|
|
|
|
dataWrapper0->GetData ( dataStr ); //<2F><><EFBFBD> COPY #1
|
|
|
|
|
// note: conversion to text/plain is done inside the clipboard. we do not need to worry
|
|
|
|
|
// about it here.
|
|
|
|
|
if ( toFlavor.Equals(kHTMLMime) || toFlavor.Equals(kUnicodeMime) ) {
|
|
|
|
|
nsresult res;
|
|
|
|
|
if (toFlavor.Equals(kHTMLMime)) {
|
|
|
|
|
PRInt32 dataLen = dataStr.Length() * 2;
|
|
|
|
|
nsPrimitiveHelpers::CreatePrimitiveForData ( toFlavor.get(), (void*)dataStr.get(), dataLen, aToData );
|
|
|
|
|
if ( *aToData )
|
|
|
|
|
*aDataToLen = dataLen;
|
|
|
|
|
} else {
|
|
|
|
|
nsAutoString outStr;
|
|
|
|
|
res = ConvertFromHTMLToUnicode(dataStr, outStr);
|
|
|
|
|
if (NS_SUCCEEDED(res)) {
|
|
|
|
|
PRInt32 dataLen = outStr.Length() * 2;
|
|
|
|
|
nsPrimitiveHelpers::CreatePrimitiveForData ( toFlavor.get(), (void*)outStr.get(), dataLen, aToData );
|
|
|
|
|
if ( *aToData )
|
|
|
|
|
*aDataToLen = dataLen;
|
2000-10-07 10:58:49 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2005-09-10 15:39:32 +04:00
|
|
|
|
} // else if HTML or Unicode
|
|
|
|
|
else if ( toFlavor.Equals(kAOLMailMime) ) {
|
|
|
|
|
nsAutoString outStr;
|
|
|
|
|
if ( NS_SUCCEEDED(ConvertFromHTMLToAOLMail(dataStr, outStr)) ) {
|
|
|
|
|
PRInt32 dataLen = outStr.Length() * 2;
|
|
|
|
|
nsPrimitiveHelpers::CreatePrimitiveForData ( toFlavor.get(), (void*)outStr.get(), dataLen, aToData );
|
|
|
|
|
if ( *aToData )
|
|
|
|
|
*aDataToLen = dataLen;
|
|
|
|
|
}
|
|
|
|
|
} // else if AOL mail
|
|
|
|
|
else {
|
|
|
|
|
rv = NS_ERROR_FAILURE;
|
2000-10-07 10:58:49 +04:00
|
|
|
|
}
|
|
|
|
|
} // if we got html mime
|
|
|
|
|
else
|
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
|
|
} // Convert
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// ConvertFromHTMLToUnicode
|
|
|
|
|
//
|
|
|
|
|
// Takes HTML and converts it to plain text but in unicode.
|
|
|
|
|
//
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
|
nsHTMLFormatConverter::ConvertFromHTMLToUnicode(const nsAutoString & aFromStr, nsAutoString & aToStr)
|
|
|
|
|
{
|
2012-02-27 15:57:48 +04:00
|
|
|
|
return nsContentUtils::ConvertToPlainText(aFromStr,
|
|
|
|
|
aToStr,
|
2011-08-09 19:14:37 +04:00
|
|
|
|
nsIDocumentEncoder::OutputSelectionOnly |
|
|
|
|
|
nsIDocumentEncoder::OutputAbsoluteLinks |
|
|
|
|
|
nsIDocumentEncoder::OutputNoScriptContent |
|
2012-02-27 15:57:48 +04:00
|
|
|
|
nsIDocumentEncoder::OutputNoFramesContent,
|
|
|
|
|
0);
|
2000-10-07 10:58:49 +04:00
|
|
|
|
} // ConvertFromHTMLToUnicode
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
|
nsHTMLFormatConverter::ConvertFromHTMLToAOLMail(const nsAutoString & aFromStr,
|
|
|
|
|
nsAutoString & aToStr)
|
|
|
|
|
{
|
2004-06-17 04:13:25 +04:00
|
|
|
|
aToStr.AssignLiteral("<HTML>");
|
2000-10-07 10:58:49 +04:00
|
|
|
|
aToStr.Append(aFromStr);
|
2004-06-17 04:13:25 +04:00
|
|
|
|
aToStr.AppendLiteral("</HTML>");
|
2000-10-07 10:58:49 +04:00
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|