2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2000-10-07 10:58:49 +04:00
|
|
|
|
2007-08-07 19:18:36 +04:00
|
|
|
#include "nsHTMLFormatConverter.h"
|
|
|
|
|
2016-10-16 22:43:56 +03:00
|
|
|
#include "nsArray.h"
|
2002-05-15 22:55:21 +04:00
|
|
|
#include "nsCRT.h"
|
2000-10-07 10:58:49 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2020-05-01 06:02:13 +03:00
|
|
|
#include "nsITransferable.h"
|
|
|
|
#include "nsLiteralString.h"
|
2002-09-04 03:36:13 +04:00
|
|
|
#include "nsXPCOM.h"
|
2000-10-07 10:58:49 +04:00
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2020-03-16 13:56:57 +03:00
|
|
|
nsHTMLFormatConverter::nsHTMLFormatConverter() = default;
|
2000-10-07 10:58:49 +04:00
|
|
|
|
2020-03-16 13:56:57 +03:00
|
|
|
nsHTMLFormatConverter::~nsHTMLFormatConverter() = default;
|
2000-10-07 10:58:49 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(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.
|
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
2018-10-10 23:26:06 +03:00
|
|
|
nsHTMLFormatConverter::GetInputDataFlavors(nsTArray<nsCString>& aFlavors) {
|
2020-07-01 11:29:29 +03:00
|
|
|
aFlavors.AppendElement(nsLiteralCString(kHTMLMime));
|
2018-10-10 23:26:06 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-10-07 10:58:49 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
2018-10-10 23:26:06 +03:00
|
|
|
nsHTMLFormatConverter::GetOutputDataFlavors(nsTArray<nsCString>& aFlavors) {
|
2020-07-01 11:29:29 +03:00
|
|
|
aFlavors.AppendElement(nsLiteralCString(kHTMLMime));
|
|
|
|
aFlavors.AppendElement(nsLiteralCString(kUnicodeMime));
|
2018-10-10 23:26:06 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-10-07 10:58:49 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// 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
|
2005-11-21 09:30:34 +03:00
|
|
|
// objects so that it is 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
|
2018-11-20 20:01:46 +03:00
|
|
|
nsHTMLFormatConverter::Convert(const char* aFromDataFlavor,
|
|
|
|
nsISupports* aFromData,
|
|
|
|
const char* aToDataFlavor,
|
|
|
|
nsISupports** aToData) {
|
2000-10-07 10:58:49 +04:00
|
|
|
if (!aToData) return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
2012-07-30 18:20:58 +04:00
|
|
|
*aToData = nullptr;
|
2000-10-07 10:58:49 +04:00
|
|
|
|
|
|
|
if (!nsCRT::strcmp(aFromDataFlavor, kHTMLMime)) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString toFlavor(aToDataFlavor);
|
2000-10-07 10:58:49 +04:00
|
|
|
|
|
|
|
// HTML on clipboard is going to always be double byte so it will be in a
|
2016-11-02 14:46:53 +03:00
|
|
|
// primitive class of nsISupportsString. Also, since the data is in two byte
|
2000-10-07 10:58:49 +04:00
|
|
|
// chunks the 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;
|
2016-11-02 14:46:53 +03:00
|
|
|
dataWrapper0->GetData(dataStr); // COPY #1
|
|
|
|
// note: conversion to text/plain is done inside the clipboard. we do not
|
2005-09-10 15:39:32 +04:00
|
|
|
// need to worry about it here.
|
|
|
|
if (toFlavor.Equals(kHTMLMime) || toFlavor.Equals(kUnicodeMime)) {
|
|
|
|
nsresult res;
|
|
|
|
if (toFlavor.Equals(kHTMLMime)) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t dataLen = dataStr.Length() * 2;
|
2017-08-21 13:01:27 +03:00
|
|
|
nsPrimitiveHelpers::CreatePrimitiveForData(toFlavor, dataStr.get(),
|
|
|
|
dataLen, aToData);
|
2005-09-10 15:39:32 +04:00
|
|
|
} else {
|
|
|
|
nsAutoString outStr;
|
|
|
|
res = ConvertFromHTMLToUnicode(dataStr, outStr);
|
|
|
|
if (NS_SUCCEEDED(res)) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t dataLen = outStr.Length() * 2;
|
2017-08-21 13:01:27 +03:00
|
|
|
nsPrimitiveHelpers::CreatePrimitiveForData(toFlavor, outStr.get(),
|
|
|
|
dataLen, aToData);
|
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))) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t dataLen = outStr.Length() * 2;
|
2017-08-21 13:01:27 +03:00
|
|
|
nsPrimitiveHelpers::CreatePrimitiveForData(toFlavor, outStr.get(),
|
|
|
|
dataLen, aToData);
|
2005-09-10 15:39:32 +04:00
|
|
|
}
|
|
|
|
} // else if AOL mail
|
|
|
|
else {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
2000-10-07 10:58:49 +04:00
|
|
|
}
|
|
|
|
} // if we got html mime
|
|
|
|
else
|
2016-11-02 14:46:53 +03:00
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
|
2000-10-07 10:58:49 +04:00
|
|
|
return rv;
|
2016-11-02 14:46:53 +03:00
|
|
|
|
2000-10-07 10:58:49 +04:00
|
|
|
} // 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;
|
|
|
|
}
|