home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libmime / mimethtm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.1 KB  |  155 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /* mimethtm.c --- definition of the MimeInlineTextHTML class (see mimei.h)
  20.    Created: Jamie Zawinski <jwz@netscape.com>, 15-May-96.
  21.  */
  22.  
  23. #include "mimethtm.h"
  24.  
  25. #define MIME_SUPERCLASS mimeInlineTextClass
  26. MimeDefClass(MimeInlineTextHTML, MimeInlineTextHTMLClass,
  27.              mimeInlineTextHTMLClass, &MIME_SUPERCLASS);
  28.  
  29. static int MimeInlineTextHTML_parse_line (char *, int32, MimeObject *);
  30. static int MimeInlineTextHTML_parse_eof (MimeObject *, XP_Bool);
  31. static int MimeInlineTextHTML_parse_begin (MimeObject *obj);
  32.  
  33. static int
  34. MimeInlineTextHTMLClassInitialize(MimeInlineTextHTMLClass *class)
  35. {
  36.   MimeObjectClass *oclass = (MimeObjectClass *) class;
  37.   XP_ASSERT(!oclass->class_initialized);
  38.   oclass->parse_begin = MimeInlineTextHTML_parse_begin;
  39.   oclass->parse_line  = MimeInlineTextHTML_parse_line;
  40.   oclass->parse_eof   = MimeInlineTextHTML_parse_eof;
  41.  
  42.   return 0;
  43. }
  44.  
  45. static int
  46. MimeInlineTextHTML_parse_begin (MimeObject *obj)
  47. {
  48.   int status = ((MimeObjectClass*)&mimeLeafClass)->parse_begin(obj);
  49.   if (status < 0) return status;
  50.  
  51.   if (!obj->output_p) return 0;
  52.  
  53.   /* If this HTML part has a Content-Base header, and if we're displaying
  54.      to the screen (that is, not writing this part "raw") then translate
  55.      that Content-Base header into a <BASE> tag in the HTML.
  56.    */
  57.   if (obj->options &&
  58.       obj->options->write_html_p &&
  59.       obj->options->output_fn)
  60.     {
  61.       char *base_hdr = MimeHeaders_get (obj->headers, HEADER_CONTENT_BASE,
  62.                                         FALSE, FALSE);
  63.  
  64.       /* Encapsulate the entire text/html part inside an in-flow
  65.          layer.  This will provide it a private coordinate system and
  66.          prevent it from escaping the bounds of its clipping so that
  67.          it might, for example, spoof a mail header. */
  68.       if (obj->options->set_html_state_fn)
  69.         {
  70.           status = obj->options->set_html_state_fn(obj->options->stream_closure,
  71.                                                    TRUE,   /* layer_encapulate_p */
  72.                                                    TRUE,   /* start_p */
  73.                                                    FALSE); /* abort_p */
  74.           if (status < 0) return status;
  75.         }
  76.  
  77.       if (base_hdr)
  78.         {
  79.           char *buf = (char *) XP_ALLOC(XP_STRLEN(base_hdr) + 20);
  80.           const char *in;
  81.           char *out;
  82.           if (!buf)
  83.             return MK_OUT_OF_MEMORY;
  84.  
  85.           /* The value of the Content-Base header is a number of "words".
  86.              Whitespace in this header is not significant -- it is assumed
  87.              that any real whitespace in the URL has already been encoded,
  88.              and whitespace has been inserted to allow the lines in the
  89.              mail header to be wrapped reasonably.  Creators are supposed
  90.              to insert whitespace every 40 characters or less.
  91.            */
  92.           XP_STRCPY(buf, "<BASE HREF=\"");
  93.           out = buf + XP_STRLEN(buf);
  94.  
  95.           for (in = base_hdr; *in; in++)
  96.             /* ignore whitespace and quotes */
  97.             if (!XP_IS_SPACE(*in) && *in != '"')
  98.               *out++ = *in;
  99.  
  100.           /* Close the tag and argument. */
  101.           *out++ = '"';
  102.           *out++ = '>';
  103.           *out++ = 0;
  104.  
  105.           XP_FREE(base_hdr);
  106.  
  107.           status = MimeObject_write(obj, buf, XP_STRLEN(buf), FALSE);
  108.           XP_FREE(buf);
  109.           if (status < 0) return status;
  110.         }
  111.     }
  112.  
  113.   return 0;
  114. }
  115.  
  116.  
  117. static int
  118. MimeInlineTextHTML_parse_line (char *line, int32 length, MimeObject *obj)
  119. {
  120.   if (!obj->output_p) return 0;
  121.  
  122.   if (obj->options && obj->options->output_fn)
  123.     return MimeObject_write(obj, line, length, TRUE);
  124.   else
  125.     return 0;
  126. }
  127.  
  128. /* This method is the same as that of MimeInlineTextRichtext (and thus
  129.    MimeInlineTextEnriched); maybe that means that MimeInlineTextHTML
  130.    should share a common parent with them which is not also shared by
  131.    MimeInlineTextPlain?
  132.  */
  133. static int
  134. MimeInlineTextHTML_parse_eof (MimeObject *obj, XP_Bool abort_p)
  135. {
  136.   int status;
  137.   if (obj->closed_p) return 0;
  138.  
  139.   /* Run parent method first, to flush out any buffered data. */
  140.   status = ((MimeObjectClass*)&MIME_SUPERCLASS)->parse_eof(obj, abort_p);
  141.   if (status < 0) return status;
  142.  
  143.   if (obj->output_p &&
  144.       obj->options &&
  145.       obj->options->write_html_p &&
  146.       obj->options->set_html_state_fn)
  147.     {
  148.       return obj->options->set_html_state_fn(obj->options->stream_closure,
  149.                                              TRUE,  /* layer_encapulate_p */
  150.                                              FALSE, /* start_p */
  151.                                              abort_p);
  152.     }
  153.   return 0;
  154. }
  155.