LCOV - code coverage report
Current view: directory - widget/xpwidgets - nsHTMLFormatConverter.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 84 28 33.3 %
Date: 2012-06-02 Functions: 13 8 61.5 %

       1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2                 : /* ***** BEGIN LICENSE BLOCK *****
       3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       4                 :  *
       5                 :  * The contents of this file are subject to the Mozilla Public License Version
       6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       7                 :  * the License. You may obtain a copy of the License at
       8                 :  * http://www.mozilla.org/MPL/
       9                 :  *
      10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12                 :  * for the specific language governing rights and limitations under the
      13                 :  * License.
      14                 :  *
      15                 :  * The Original Code is mozilla.org code.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is
      18                 :  * Netscape Communications Corporation.
      19                 :  * Portions created by the Initial Developer are Copyright (C) 1998
      20                 :  * the Initial Developer. All Rights Reserved.
      21                 :  *
      22                 :  * Contributor(s):
      23                 :  *   Pierre Phaneuf <pp@ludusdesign.com>
      24                 :  *   Mats Palmgren <matpal@gmail.com>
      25                 :  *
      26                 :  * Alternatively, the contents of this file may be used under the terms of
      27                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      28                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      29                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      30                 :  * of those above. If you wish to allow use of your version of this file only
      31                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      32                 :  * use your version of this file under the terms of the MPL, indicate your
      33                 :  * decision by deleting the provisions above and replace them with the notice
      34                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      35                 :  * the provisions above, a recipient may use your version of this file under
      36                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      37                 :  *
      38                 :  * ***** END LICENSE BLOCK ***** */
      39                 : 
      40                 : #include "nsHTMLFormatConverter.h"
      41                 : 
      42                 : #include "nsCRT.h"
      43                 : #include "nsISupportsArray.h"
      44                 : #include "nsIComponentManager.h"
      45                 : #include "nsCOMPtr.h"
      46                 : #include "nsXPCOM.h"
      47                 : #include "nsISupportsPrimitives.h"
      48                 : 
      49                 : #include "nsITransferable.h" // for mime defs, this is BAD
      50                 : 
      51                 : // HTML convertor stuff
      52                 : #include "nsPrimitiveHelpers.h"
      53                 : #include "nsIDocumentEncoder.h"
      54                 : #include "nsContentUtils.h"
      55                 : 
      56             216 : nsHTMLFormatConverter::nsHTMLFormatConverter()
      57                 : {
      58             216 : }
      59                 : 
      60             432 : nsHTMLFormatConverter::~nsHTMLFormatConverter()
      61                 : {
      62             864 : }
      63                 : 
      64            3672 : NS_IMPL_ISUPPORTS1(nsHTMLFormatConverter, nsIFormatConverter)
      65                 : 
      66                 : //
      67                 : // GetInputDataFlavors
      68                 : //
      69                 : // Creates a new list and returns the list of all the flavors this converter
      70                 : // knows how to import. In this case, it's just HTML.
      71                 : //
      72                 : // Flavors (strings) are wrapped in a primitive object so that JavaScript can
      73                 : // access them easily via XPConnect.
      74                 : //
      75                 : NS_IMETHODIMP
      76               0 : nsHTMLFormatConverter::GetInputDataFlavors(nsISupportsArray **_retval)
      77                 : {
      78               0 :   if ( !_retval )
      79               0 :     return NS_ERROR_INVALID_ARG;
      80                 :   
      81               0 :   nsresult rv = NS_NewISupportsArray ( _retval );  // addrefs for us
      82               0 :   if ( NS_SUCCEEDED(rv) )
      83               0 :     rv = AddFlavorToList ( *_retval, kHTMLMime );
      84                 :   
      85               0 :   return rv;
      86                 :   
      87                 : } // GetInputDataFlavors
      88                 : 
      89                 : 
      90                 : //
      91                 : // GetOutputDataFlavors
      92                 : //
      93                 : // Creates a new list and returns the list of all the flavors this converter
      94                 : // knows how to export (convert). In this case, it's all sorts of things that HTML can be
      95                 : // converted to.
      96                 : //
      97                 : // Flavors (strings) are wrapped in a primitive object so that JavaScript can
      98                 : // access them easily via XPConnect.
      99                 : //
     100                 : NS_IMETHODIMP
     101               0 : nsHTMLFormatConverter::GetOutputDataFlavors(nsISupportsArray **_retval)
     102                 : {
     103               0 :   if ( !_retval )
     104               0 :     return NS_ERROR_INVALID_ARG;
     105                 :   
     106               0 :   nsresult rv = NS_NewISupportsArray ( _retval );  // addrefs for us
     107               0 :   if ( NS_SUCCEEDED(rv) ) {
     108               0 :     rv = AddFlavorToList ( *_retval, kHTMLMime );
     109               0 :     if ( NS_FAILED(rv) )
     110               0 :       return rv;
     111                 : #if NOT_NOW
     112                 : // pinkerton
     113                 : // no one uses this flavor right now, so it's just slowing things down. If anyone cares I
     114                 : // can put it back in.
     115                 :     rv = AddFlavorToList ( *_retval, kAOLMailMime );
     116                 :     if ( NS_FAILED(rv) )
     117                 :       return rv;
     118                 : #endif
     119               0 :     rv = AddFlavorToList ( *_retval, kUnicodeMime );
     120               0 :     if ( NS_FAILED(rv) )
     121               0 :       return rv;
     122                 :   }
     123               0 :   return rv;
     124                 : 
     125                 : } // GetOutputDataFlavors
     126                 : 
     127                 : 
     128                 : //
     129                 : // AddFlavorToList
     130                 : //
     131                 : // Convenience routine for adding a flavor wrapped in an nsISupportsCString object
     132                 : // to a list
     133                 : //
     134                 : nsresult
     135               0 : nsHTMLFormatConverter :: AddFlavorToList ( nsISupportsArray* inList, const char* inFlavor )
     136                 : {
     137                 :   nsresult rv;
     138                 :   
     139                 :   nsCOMPtr<nsISupportsCString> dataFlavor =
     140               0 :       do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
     141               0 :   if ( dataFlavor ) {
     142               0 :     dataFlavor->SetData ( nsDependentCString(inFlavor) );
     143                 :     // add to list as an nsISupports so the correct interface gets the addref
     144                 :     // in AppendElement()
     145               0 :     nsCOMPtr<nsISupports> genericFlavor ( do_QueryInterface(dataFlavor) );
     146               0 :     inList->AppendElement ( genericFlavor);
     147                 :   }
     148               0 :   return rv;
     149                 : 
     150                 : } // AddFlavorToList
     151                 : 
     152                 : 
     153                 : //
     154                 : // CanConvert
     155                 : //
     156                 : // Determines if we support the given conversion. Currently, this method only
     157                 : // converts from HTML to others.
     158                 : //
     159                 : NS_IMETHODIMP
     160               0 : nsHTMLFormatConverter::CanConvert(const char *aFromDataFlavor, const char *aToDataFlavor, bool *_retval)
     161                 : {
     162               0 :   if ( !_retval )
     163               0 :     return NS_ERROR_INVALID_ARG;
     164                 : 
     165               0 :   *_retval = false;
     166               0 :   if ( !nsCRT::strcmp(aFromDataFlavor, kHTMLMime) ) {
     167               0 :     if ( !nsCRT::strcmp(aToDataFlavor, kHTMLMime) )
     168               0 :       *_retval = true;
     169               0 :     else if ( !nsCRT::strcmp(aToDataFlavor, kUnicodeMime) )
     170               0 :       *_retval = true;
     171                 : #if NOT_NOW
     172                 : // pinkerton
     173                 : // no one uses this flavor right now, so it's just slowing things down. If anyone cares I
     174                 : // can put it back in.
     175                 :     else if ( toFlavor.Equals(kAOLMailMime) )
     176                 :       *_retval = true;
     177                 : #endif
     178                 :   }
     179               0 :   return NS_OK;
     180                 : 
     181                 : } // CanConvert
     182                 : 
     183                 : 
     184                 : 
     185                 : //
     186                 : // Convert
     187                 : //
     188                 : // Convert data from one flavor to another. The data is wrapped in primitive objects so that it is
     189                 : // accessible from JS. Currently, this only accepts HTML input, so anything else is invalid.
     190                 : //
     191                 : //XXX This method copies the data WAAAAY too many time for my liking. Grrrrrr. Mostly it's because
     192                 : //XXX we _must_ put things into nsStrings so that the parser will accept it. Lame lame lame lame. We
     193                 : //XXX also can't just get raw unicode out of the nsString, so we have to allocate heap to get
     194                 : //XXX unicode out of the string. Lame lame lame.
     195                 : //
     196                 : NS_IMETHODIMP
     197             216 : nsHTMLFormatConverter::Convert(const char *aFromDataFlavor, nsISupports *aFromData, PRUint32 aDataLen, 
     198                 :                                const char *aToDataFlavor, nsISupports **aToData, PRUint32 *aDataToLen)
     199                 : {
     200             216 :   if ( !aToData || !aDataToLen )
     201               0 :     return NS_ERROR_INVALID_ARG;
     202                 : 
     203             216 :   nsresult rv = NS_OK;
     204             216 :   *aToData = nsnull;
     205             216 :   *aDataToLen = 0;
     206                 : 
     207             216 :   if ( !nsCRT::strcmp(aFromDataFlavor, kHTMLMime) ) {
     208             432 :     nsCAutoString toFlavor ( aToDataFlavor );
     209                 : 
     210                 :     // HTML on clipboard is going to always be double byte so it will be in a primitive
     211                 :     // class of nsISupportsString. Also, since the data is in two byte chunks the 
     212                 :     // length represents the length in 1-byte chars, so we need to divide by two.
     213             432 :     nsCOMPtr<nsISupportsString> dataWrapper0 ( do_QueryInterface(aFromData) );
     214             216 :     if (!dataWrapper0) {
     215               0 :       return NS_ERROR_INVALID_ARG;
     216                 :     }
     217                 : 
     218             648 :     nsAutoString dataStr;
     219             216 :     dataWrapper0->GetData ( dataStr );  //еее COPY #1
     220                 :     // note: conversion to text/plain is done inside the clipboard. we do not need to worry 
     221                 :     // about it here.
     222             216 :     if ( toFlavor.Equals(kHTMLMime) || toFlavor.Equals(kUnicodeMime) ) {
     223                 :       nsresult res;
     224             216 :       if (toFlavor.Equals(kHTMLMime)) {
     225               0 :         PRInt32 dataLen = dataStr.Length() * 2;
     226               0 :         nsPrimitiveHelpers::CreatePrimitiveForData ( toFlavor.get(), (void*)dataStr.get(), dataLen, aToData );
     227               0 :         if ( *aToData )
     228               0 :           *aDataToLen = dataLen;
     229                 :       } else {
     230             432 :         nsAutoString outStr;
     231             216 :         res = ConvertFromHTMLToUnicode(dataStr, outStr);
     232             216 :         if (NS_SUCCEEDED(res)) {
     233             216 :           PRInt32 dataLen = outStr.Length() * 2;
     234             216 :           nsPrimitiveHelpers::CreatePrimitiveForData ( toFlavor.get(), (void*)outStr.get(), dataLen, aToData );
     235             216 :           if ( *aToData ) 
     236             216 :             *aDataToLen = dataLen;
     237                 :         }
     238                 :       }
     239                 :     } // else if HTML or Unicode
     240               0 :     else if ( toFlavor.Equals(kAOLMailMime) ) {
     241               0 :       nsAutoString outStr;
     242               0 :       if ( NS_SUCCEEDED(ConvertFromHTMLToAOLMail(dataStr, outStr)) ) {
     243               0 :         PRInt32 dataLen = outStr.Length() * 2;
     244               0 :         nsPrimitiveHelpers::CreatePrimitiveForData ( toFlavor.get(), (void*)outStr.get(), dataLen, aToData );
     245               0 :         if ( *aToData ) 
     246               0 :           *aDataToLen = dataLen;
     247                 :       }
     248                 :     } // else if AOL mail
     249                 :     else {
     250               0 :       rv = NS_ERROR_FAILURE;
     251                 :     }
     252                 :   } // if we got html mime
     253                 :   else
     254               0 :     rv = NS_ERROR_FAILURE;      
     255                 :     
     256             216 :   return rv;
     257                 :   
     258                 : } // Convert
     259                 : 
     260                 : 
     261                 : //
     262                 : // ConvertFromHTMLToUnicode
     263                 : //
     264                 : // Takes HTML and converts it to plain text but in unicode.
     265                 : //
     266                 : NS_IMETHODIMP
     267             216 : nsHTMLFormatConverter::ConvertFromHTMLToUnicode(const nsAutoString & aFromStr, nsAutoString & aToStr)
     268                 : {
     269                 :   return nsContentUtils::ConvertToPlainText(aFromStr,
     270                 :     aToStr,
     271                 :     nsIDocumentEncoder::OutputSelectionOnly |
     272                 :     nsIDocumentEncoder::OutputAbsoluteLinks |
     273                 :     nsIDocumentEncoder::OutputNoScriptContent |
     274                 :     nsIDocumentEncoder::OutputNoFramesContent,
     275             216 :     0);
     276                 : } // ConvertFromHTMLToUnicode
     277                 : 
     278                 : 
     279                 : NS_IMETHODIMP
     280               0 : nsHTMLFormatConverter::ConvertFromHTMLToAOLMail(const nsAutoString & aFromStr,
     281                 :                                                 nsAutoString & aToStr)
     282                 : {
     283               0 :   aToStr.AssignLiteral("<HTML>");
     284               0 :   aToStr.Append(aFromStr);
     285               0 :   aToStr.AppendLiteral("</HTML>");
     286                 : 
     287               0 :   return NS_OK;
     288                 : }
     289                 : 

Generated by: LCOV version 1.7