Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes
nsPlainTextSerializer Class Reference

#include <nsPlainTextSerializer.h>

Inheritance diagram for nsPlainTextSerializer:
Inheritance graph
[legend]
Collaboration diagram for nsPlainTextSerializer:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 nsPlainTextSerializer ()
virtual ~nsPlainTextSerializer ()
NS_DECL_ISUPPORTS NS_IMETHOD Init (PRUint32 flags, PRUint32 aWrapColumn, const char *aCharSet, PRBool aIsCopying)
NS_IMETHOD AppendText (nsIDOMText *aText, PRInt32 aStartOffset, PRInt32 aEndOffset, nsAString &aStr)
NS_IMETHOD AppendCDATASection (nsIDOMCDATASection *aCDATASection, PRInt32 aStartOffset, PRInt32 aEndOffset, nsAString &aStr)
NS_IMETHOD AppendProcessingInstruction (nsIDOMProcessingInstruction *aPI, PRInt32 aStartOffset, PRInt32 aEndOffset, nsAString &aStr)
NS_IMETHOD AppendComment (nsIDOMComment *aComment, PRInt32 aStartOffset, PRInt32 aEndOffset, nsAString &aStr)
NS_IMETHOD AppendDoctype (nsIDOMDocumentType *aDoctype, nsAString &aStr)
NS_IMETHOD AppendElementStart (nsIDOMElement *aElement, PRBool aHasChildren, nsAString &aStr)
NS_IMETHOD AppendElementEnd (nsIDOMElement *aElement, nsAString &aStr)
NS_IMETHOD Flush (nsAString &aStr)
NS_IMETHOD AppendDocumentStart (nsIDOMDocument *aDocument, nsAString &aStr)
 Append any items in the beginning of the document that won't be serialized by other methods.
NS_IMETHOD WillBuildModel (void)
 This method gets called when the parser begins the process of building the content model via the content sink.
NS_IMETHOD DidBuildModel (void)
 This method gets called when the parser concludes the process of building the content model via the content sink.
NS_IMETHOD WillInterrupt (void)
 This method gets called when the parser gets i/o blocked, and wants to notify the sink that it may be a while before more data is available.
NS_IMETHOD WillResume (void)
 This method gets called when the parser i/o gets unblocked, and we're about to start dumping content again to the sink.
NS_IMETHOD SetParser (nsIParser *aParser)
 This method gets called by the parser so that the content sink can retain a reference to the parser.
NS_IMETHOD OpenContainer (const nsIParserNode &aNode)
 This method is used to open a generic container in the sink.
NS_IMETHOD CloseContainer (const nsHTMLTag aTag)
 This method gets called by the parser when a close container tag has been consumed and needs to be closed.
NS_IMETHOD AddHeadContent (const nsIParserNode &aNode)
 This gets called by the parser to contents to the head container.
NS_IMETHOD AddLeaf (const nsIParserNode &aNode)
 This gets called by the parser when you want to add a leaf node to the current container in the content model.
NS_IMETHOD AddComment (const nsIParserNode &aNode)
 This gets called by the parser when you want to add a leaf node to the current container in the content model.
NS_IMETHOD AddProcessingInstruction (const nsIParserNode &aNode)
 This gets called by the parser when you want to add a leaf node to the current container in the content model.
NS_IMETHOD AddDocTypeDecl (const nsIParserNode &aNode)
 This method is called by the parser when it encounters a document type declaration.
virtual void FlushPendingNotifications (mozFlushType aType)
 Flush content so that the content model is in sync with the state of the sink.
NS_IMETHOD SetDocumentCharset (nsACString &aCharset)
 Set the document character set.
virtual nsISupports * GetTarget ()
 Returns the target object (often a document object) into which the content built by this content sink is being added, if any (IOW, may return null).
NS_IMETHOD SetTitle (const nsString &aValue)
 This method gets called by the parser when it encounters a title tag and wants to set the document title in the sink.
NS_IMETHOD OpenHTML (const nsIParserNode &aNode)
 This method is used to open the outer HTML container.
NS_IMETHOD CloseHTML ()
 This method is used to close the outer HTML container.
NS_IMETHOD OpenHead (const nsIParserNode &aNode)
 This method is used to open the only HEAD container.
NS_IMETHOD CloseHead ()
 This method is used to close the only HEAD container.
NS_IMETHOD OpenBody (const nsIParserNode &aNode)
 This method is used to open the main BODY container.
NS_IMETHOD CloseBody ()
 This method is used to close the main BODY container.
NS_IMETHOD OpenForm (const nsIParserNode &aNode)
 This method is used to open a new FORM container.
NS_IMETHOD CloseForm ()
 This method is used to close the outer FORM container.
NS_IMETHOD OpenMap (const nsIParserNode &aNode)
 This method is used to open a new MAP container.
NS_IMETHOD CloseMap ()
 This method is used to close the MAP container.
NS_IMETHOD OpenFrameset (const nsIParserNode &aNode)
 This method is used to open the FRAMESET container.
NS_IMETHOD CloseFrameset ()
 This method is used to close the FRAMESET container.
NS_IMETHOD IsEnabled (PRInt32 aTag, PRBool *aReturn)
 01/09/2003 harishd
NS_IMETHOD NotifyTagObservers (nsIParserNode *aNode)
 This gets called by the parser to notify observers of the tag.
 NS_IMETHOD_ (PRBool) IsFormOnStack()
 Call this method to determnine if a FORM is on the sink's stack.
NS_IMETHOD BeginContext (PRInt32 aPosition)
 This gets called when handling illegal contents, especially in dealing with tables.
NS_IMETHOD EndContext (PRInt32 aPosition)
 This method terminates any new context that got created by BeginContext and switches back to the main context.
NS_IMETHOD WillProcessTokens (void)
 This method is called when parser is about to begin synchronously processing a chunk of tokens.
NS_IMETHOD DidProcessTokens (void)
 This method is called when parser has completed processing a chunk of tokens.
NS_IMETHOD WillProcessAToken (void)
 This method is called when parser is about to process a single token.
NS_IMETHOD DidProcessAToken (void)
 This method is called when parser has completed the processing for a single token.
NS_IMETHOD Initialize (nsAString *aOutString, PRUint32 aFlags, PRUint32 aWrapCol)

Protected Member Functions

nsresult GetAttributeValue (const nsIParserNode *node, nsIAtom *aName, nsString &aValueRet)
 Gets the value of an attribute in a string.
void AddToLine (const PRUnichar *aStringToAdd, PRInt32 aLength)
 This function adds a piece of text to the current stored line.
void EndLine (PRBool softlinebreak)
 Outputs the contents of mCurrentLine, and resets line specific variables.
void EnsureVerticalSpace (PRInt32 noOfRows)
 Adds as many newline as necessary to get |noOfRows| empty lines.
void FlushLine ()
 This empties the current line cache without adding a NEWLINE.
void OutputQuotesAndIndent (PRBool stripTrailingSpaces=PR_FALSE)
 Outputs the calculated and stored indent and text in the indentation.
void Output (nsString &aString)
 Prints the text to output to our current output device (the string mOutputString).
void Write (const nsAString &aString)
 Write a string.
PRBool IsBlockLevel (PRInt32 aId)
 Returns true if the id represents an element of block type.
PRBool IsContainer (PRInt32 aId)
 Returns true if the id represents a container.
PRBool IsInPre ()
 Returns true if we currently are inside a.
PRBool IsInOL ()
 This method is required only to indentify LI's inside OL.
PRBool IsCurrentNodeConverted (const nsIParserNode *aNode)
 Returns true, if the element was inserted by Moz' TXT->HTML converter.
nsresult DoOpenContainer (const nsIParserNode *aNode, PRInt32 aTag)
 aNode may be null when we're working with the DOM, but then mContent is useable instead.
nsresult DoCloseContainer (PRInt32 aTag)
nsresult DoAddLeaf (const nsIParserNode *aNode, PRInt32 aTag, const nsAString &aText)
 aNode may be null when we're working with the DOM, but then mContent is useable instead.
PRBool MayWrap ()
PRBool DoOutput ()
PRBool GetLastBool (const nsVoidArray &aStack)
void SetLastBool (nsVoidArray &aStack, PRBool aValue)
void PushBool (nsVoidArray &aStack, PRBool aValue)
PRBool PopBool (nsVoidArray &aStack)

Static Protected Member Functions

static PRInt32 GetIdForContent (nsIContent *aContent)

Protected Attributes

nsString mCurrentLine
PRPackedBool mInHead
PRPackedBool mAtFirstColumn
PRPackedBool mQuotesPreformatted
PRPackedBool mDontWrapAnyQuotes
PRPackedBool mStructs
PRPackedBool mHasWrittenCiteBlockquote
PRInt32 mIndent
nsString mInIndentString
PRInt32 mCiteQuoteLevel
PRInt32 mFlags
PRInt32 mFloatingLines
PRUint32 mWrapColumn
PRUint32 mCurrentLineWidth
PRInt32 mSpanLevel
PRInt32 mEmptyLines
PRPackedBool mInWhitespace
PRPackedBool mPreFormatted
PRPackedBool mStartedOutput
PRPackedBool mLineBreakDue
nsString mURL
PRInt32 mHeaderStrategy
PRInt32 mHeaderCounter [7]
nsCOMPtr< nsIContentmContent
nsAutoVoidArray mHasWrittenCellsForRow
nsAutoVoidArray mCurrentNodeIsConverted
nsAutoVoidArray mIsInCiteBlockquote
nsAString * mOutputString
nsHTMLTagmTagStack
PRUint32 mTagStackIndex
PRUint32 mIgnoreAboveIndex
PRInt32mOLStack
PRUint32 mOLStackIndex
PRUint32 mULCount
nsString mLineBreak
nsCOMPtr< nsILineBreakermLineBreaker
const nsString kSpace

Detailed Description

Definition at line 54 of file nsPlainTextSerializer.h.


Constructor & Destructor Documentation

Definition at line 94 of file nsPlainTextSerializer.cpp.

  : kSpace(NS_LITERAL_STRING(" ")) // Init of "constant"
{

  mOutputString = nsnull;
  mInHead = PR_FALSE;
  mAtFirstColumn = PR_TRUE;
  mIndent = 0;
  mCiteQuoteLevel = 0;
  mStructs = PR_TRUE;       // will be read from prefs later
  mHeaderStrategy = 1 /*indent increasingly*/;   // ditto
  mQuotesPreformatted = PR_FALSE;                // ditto
  mDontWrapAnyQuotes = PR_FALSE;                 // ditto
  mHasWrittenCiteBlockquote = PR_FALSE;
  mSpanLevel = 0;
  for (PRInt32 i = 0; i <= 6; i++) {
    mHeaderCounter[i] = 0;
  }

  // Line breaker
  mWrapColumn = 72;     // XXX magic number, we expect someone to reset this
  mCurrentLineWidth = 0;

  // Flow
  mEmptyLines = 1; // The start of the document is an "empty line" in itself,
  mInWhitespace = PR_TRUE;
  mPreFormatted = PR_FALSE;
  mStartedOutput = PR_FALSE;

  // initialize the tag stack to zero:
  mTagStack = new nsHTMLTag[TagStackSize];
  mTagStackIndex = 0;
  mIgnoreAboveIndex = (PRUint32)kNotFound;

  // initialize the OL stack, where numbers for ordered lists are kept:
  mOLStack = new PRInt32[OLStackSize];
  mOLStackIndex = 0;

  mULCount = 0;
}

Definition at line 135 of file nsPlainTextSerializer.cpp.

{
  delete[] mTagStack;
  delete[] mOLStack;
}

Member Function Documentation

This gets called by the parser when you want to add a leaf node to the current container in the content model.

4/1/98 gess

Parameters:
nsIParserNodereference to parser node interface

Implements nsIHTMLContentSink.

Definition at line 101 of file nsPlainTextSerializer.h.

{ return NS_OK; }

This method is called by the parser when it encounters a document type declaration.

XXX Should the parser also part the internal subset?

Parameters:
nsIParserNodereference to parser node interface

Implements nsIHTMLContentSink.

Definition at line 103 of file nsPlainTextSerializer.h.

{ return NS_OK; }

This gets called by the parser to contents to the head container.

Implements nsIHTMLContentSink.

Definition at line 481 of file nsPlainTextSerializer.cpp.

{
  if (eHTMLTag_title == aNode.GetNodeType()) {
    // XXX collect the skipped content
    return NS_OK;
  }
  OpenHead(aNode);
  nsresult rv = AddLeaf(aNode);
  CloseHead();
  return rv;
}

Here is the call graph for this function:

This gets called by the parser when you want to add a leaf node to the current container in the content model.

4/1/98 gess

Parameters:
nsIParserNodereference to parser node interface

Implements nsIHTMLContentSink.

Definition at line 494 of file nsPlainTextSerializer.cpp.

{
  if (mIgnoreAboveIndex != (PRUint32)kNotFound) {
    return NS_OK;
  }

  eHTMLTags type = (eHTMLTags)aNode.GetNodeType();
  const nsAString& text = aNode.GetText();

  if ((type == eHTMLTag_text) ||
      (type == eHTMLTag_whitespace) ||
      (type == eHTMLTag_newline)) {
    // Copy the text out, stripping out CRs
    nsAutoString str;
    PRUint32 length;
    str.SetCapacity(text.Length());
    nsReadingIterator<PRUnichar> srcStart, srcEnd;
    length = nsContentUtils::CopyNewlineNormalizedUnicodeTo(text.BeginReading(srcStart), text.EndReading(srcEnd), str);
    str.SetLength(length);
    return DoAddLeaf(&aNode, type, str);
  }
  else {
    return DoAddLeaf(&aNode, type, text);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

This gets called by the parser when you want to add a leaf node to the current container in the content model.

4/1/98 gess

Parameters:
nsIParserNodereference to parser node interface

Implements nsIHTMLContentSink.

Definition at line 102 of file nsPlainTextSerializer.h.

{ return NS_OK; }
void nsPlainTextSerializer::AddToLine ( const PRUnichar aLineFragment,
PRInt32  aLineFragmentLength 
) [protected]

This function adds a piece of text to the current stored line.

If we are wrapping text and the stored line will become too long, a suitable location to wrap will be found and the line that's complete will be output.

Definition at line 1337 of file nsPlainTextSerializer.cpp.

{
  PRUint32 prefixwidth = (mCiteQuoteLevel > 0 ? mCiteQuoteLevel + 1:0)+mIndent;
  
  if (mLineBreakDue)
    EnsureVerticalSpace(mFloatingLines);

  PRInt32 linelength = mCurrentLine.Length();
  if(0 == linelength) {
    if(0 == aLineFragmentLength) {
      // Nothing at all. Are you kidding me?
      return;
    }

    if(mFlags & nsIDocumentEncoder::OutputFormatFlowed) {
      if(
         (
          '>' == aLineFragment[0] ||
          ' ' == aLineFragment[0] ||
          !nsCRT::strncmp(aLineFragment, NS_LITERAL_STRING("From ").get(), 5)
          )
         && mCiteQuoteLevel == 0  // We space-stuff quoted lines anyway
         )
        {
          // Space stuffing a la RFC 2646 (format=flowed).
          mCurrentLine.Append(PRUnichar(' '));
          
          if(MayWrap()) {
            mCurrentLineWidth += GetUnicharWidth(' ');
#ifdef DEBUG_wrapping
            NS_ASSERTION(GetUnicharStringWidth(mCurrentLine.get(),
                                               mCurrentLine.Length()) ==
                         (PRInt32)mCurrentLineWidth,
                         "mCurrentLineWidth and reality out of sync!");
#endif
          }
        }
    }
    mEmptyLines=-1;
  }
    
  mCurrentLine.Append(aLineFragment, aLineFragmentLength);
  if(MayWrap()) {
    mCurrentLineWidth += GetUnicharStringWidth(aLineFragment,
                                               aLineFragmentLength);
#ifdef DEBUG_wrapping
    NS_ASSERTION(GetUnicharstringWidth(mCurrentLine.get(),
                                       mCurrentLine.Length()) ==
                 (PRInt32)mCurrentLineWidth,
                 "mCurrentLineWidth and reality out of sync!");
#endif
  }

  linelength = mCurrentLine.Length();

  //  Wrap?
  if(MayWrap())
  {
#ifdef DEBUG_wrapping
    NS_ASSERTION(GetUnicharstringWidth(mCurrentLine.get(),
                                  mCurrentLine.Length()) ==
                 (PRInt32)mCurrentLineWidth,
                 "mCurrentLineWidth and reality out of sync!");
#endif
    // Yes, wrap!
    // The "+4" is to avoid wrap lines that only would be a couple
    // of letters too long. We give this bonus only if the
    // wrapcolumn is more than 20.
    PRUint32 bonuswidth = (mWrapColumn > 20) ? 4 : 0;

    // XXX: Should calculate prefixwidth with GetUnicharStringWidth
    while(mCurrentLineWidth+prefixwidth > mWrapColumn+bonuswidth) {
      // Must wrap. Let's find a good place to do that.
      nsresult result = NS_OK;
      
      // We go from the end removing one letter at a time until
      // we have a reasonable width
      PRInt32 goodSpace = mCurrentLine.Length();
      PRUint32 width = mCurrentLineWidth;
      while(goodSpace > 0 && (width+prefixwidth > mWrapColumn)) {
        goodSpace--;
        width -= GetUnicharWidth(mCurrentLine[goodSpace]);
      }

      goodSpace++;
      
      PRBool oNeedMoreText;
      if (nsnull != mLineBreaker) {
        result = mLineBreaker->Prev(mCurrentLine.get(), 
                                    mCurrentLine.Length(), goodSpace,
                                    (PRUint32 *) &goodSpace, &oNeedMoreText);
        if (oNeedMoreText) {
          goodSpace = -1;
        }
        else if (nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace-1))) {
          --goodSpace;    // adjust the position since line breaker returns a position next to space
        }
      }
      // fallback if the line breaker is unavailable or failed
      if (nsnull == mLineBreaker || NS_FAILED(result)) {
        goodSpace = mWrapColumn-prefixwidth;
        while (goodSpace >= 0 &&
               !nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace))) {
          goodSpace--;
        }
      }
      
      nsAutoString restOfLine;
      if (goodSpace < 0) {
        // If we don't found a good place to break, accept long line and
        // try to find another place to break
        goodSpace=(prefixwidth>mWrapColumn+1)?1:mWrapColumn-prefixwidth+1;
        result = NS_OK;
        if (nsnull != mLineBreaker) {
          result = mLineBreaker->Next(mCurrentLine.get(), 
                                      mCurrentLine.Length(), goodSpace,
                                      (PRUint32 *) &goodSpace, &oNeedMoreText);
        }
        // fallback if the line breaker is unavailable or failed
        if (nsnull == mLineBreaker || NS_FAILED(result)) {
          goodSpace=(prefixwidth>mWrapColumn)?1:mWrapColumn-prefixwidth;
          while (goodSpace < linelength &&
                 !nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace))) {
            goodSpace++;
          }
        }
      }
      
      if((goodSpace < linelength) && (goodSpace > 0)) {
        // Found a place to break

        // -1 (trim a char at the break position)
        // only if the line break was a space.
        if (nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace))) {
          mCurrentLine.Right(restOfLine, linelength-goodSpace-1);
        }
        else {
          mCurrentLine.Right(restOfLine, linelength-goodSpace);
        }
        mCurrentLine.Truncate(goodSpace); 
        EndLine(PR_TRUE);
        mCurrentLine.Truncate();
        // Space stuff new line?
        if(mFlags & nsIDocumentEncoder::OutputFormatFlowed) {
          if(
              !restOfLine.IsEmpty()
              &&
              (
                restOfLine[0] == '>' ||
                restOfLine[0] == ' ' ||
                StringBeginsWith(restOfLine, NS_LITERAL_STRING("From "))
              )
              && mCiteQuoteLevel == 0  // We space-stuff quoted lines anyway
            )
          {
            // Space stuffing a la RFC 2646 (format=flowed).
            mCurrentLine.Append(PRUnichar(' '));
            //XXX doesn't seem to work correctly for ' '
          }
        }
        mCurrentLine.Append(restOfLine);
        mCurrentLineWidth = GetUnicharStringWidth(mCurrentLine.get(),
                                                  mCurrentLine.Length());
        linelength = mCurrentLine.Length();
        mEmptyLines = -1;
      } 
      else {
        // Nothing to do. Hopefully we get more data later
        // to use for a place to break line
        break;
      }
    }
  } 
  else {
    // No wrapping.
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

NS_IMETHODIMP nsPlainTextSerializer::AppendCDATASection ( nsIDOMCDATASection aCDATASection,
PRInt32  aStartOffset,
PRInt32  aEndOffset,
nsAString &  aStr 
) [virtual]

Implements nsIContentSerializer.

Definition at line 378 of file nsPlainTextSerializer.cpp.

{
  return AppendText(aCDATASection, aStartOffset, aEndOffset, aStr);
}

Here is the call graph for this function:

NS_IMETHOD nsPlainTextSerializer::AppendComment ( nsIDOMComment aComment,
PRInt32  aStartOffset,
PRInt32  aEndOffset,
nsAString &  aStr 
) [inline, virtual]

Implements nsIContentSerializer.

Definition at line 77 of file nsPlainTextSerializer.h.

                                                                 { return NS_OK; }
NS_IMETHOD nsPlainTextSerializer::AppendDoctype ( nsIDOMDocumentType aDoctype,
nsAString &  aStr 
) [inline, virtual]

Implements nsIContentSerializer.

Definition at line 79 of file nsPlainTextSerializer.h.

                                             { return NS_OK; }
NS_IMETHODIMP nsPlainTextSerializer::AppendDocumentStart ( nsIDOMDocument aDocument,
nsAString &  aStr 
) [virtual]

Append any items in the beginning of the document that won't be serialized by other methods.

XML declaration is the most likely thing this method can produce.

Implements nsIContentSerializer.

Definition at line 460 of file nsPlainTextSerializer.cpp.

{
  return NS_OK;
}
NS_IMETHODIMP nsPlainTextSerializer::AppendElementEnd ( nsIDOMElement aElement,
nsAString &  aStr 
) [virtual]

Implements nsIContentSerializer.

Definition at line 421 of file nsPlainTextSerializer.cpp.

{
  NS_ENSURE_ARG(aElement);

  mContent = do_QueryInterface(aElement);
  if (!mContent) return NS_ERROR_FAILURE;

  nsresult rv;
  PRInt32 id = GetIdForContent(mContent);

  PRBool isContainer = IsContainer(id);

  mOutputString = &aStr;

  rv = NS_OK;
  if (isContainer) {
    rv = DoCloseContainer(id);
  }

  mContent = 0;
  mOutputString = nsnull;

  if (mInHead && id == eHTMLTag_head)
    mInHead = PR_FALSE;    

  return rv;
}

Here is the call graph for this function:

NS_IMETHODIMP nsPlainTextSerializer::AppendElementStart ( nsIDOMElement aElement,
PRBool  aHasChildren,
nsAString &  aStr 
) [virtual]

Implements nsIContentSerializer.

Definition at line 387 of file nsPlainTextSerializer.cpp.

{
  NS_ENSURE_ARG(aElement);

  mContent = do_QueryInterface(aElement);
  if (!mContent) return NS_ERROR_FAILURE;

  nsresult rv;
  PRInt32 id = GetIdForContent(mContent);

  PRBool isContainer = IsContainer(id);

  mOutputString = &aStr;

  if (isContainer) {
    rv = DoOpenContainer(nsnull, id);
  }
  else {
    nsAutoString empty;
    rv = DoAddLeaf(nsnull, id, empty);
  }

  mContent = 0;
  mOutputString = nsnull;

  if (!mInHead && id == eHTMLTag_head)
    mInHead = PR_TRUE;    

  return rv;
} 

Here is the call graph for this function:

NS_IMETHOD nsPlainTextSerializer::AppendProcessingInstruction ( nsIDOMProcessingInstruction aPI,
PRInt32  aStartOffset,
PRInt32  aEndOffset,
nsAString &  aStr 
) [inline, virtual]

Implements nsIContentSerializer.

Definition at line 73 of file nsPlainTextSerializer.h.

                                                           { return NS_OK; }
NS_IMETHODIMP nsPlainTextSerializer::AppendText ( nsIDOMText aText,
PRInt32  aStartOffset,
PRInt32  aEndOffset,
nsAString &  aStr 
) [virtual]

Implements nsIContentSerializer.

Definition at line 295 of file nsPlainTextSerializer.cpp.

{
  if (mIgnoreAboveIndex != (PRUint32)kNotFound) {
    return NS_OK;
  }
    
  NS_ASSERTION(aStartOffset >= 0, "Negative start offset for text fragment!");
  if ( aStartOffset < 0 )
    return NS_ERROR_INVALID_ARG;

  NS_ENSURE_ARG(aText);

  nsresult rv = NS_OK;
  PRInt32 length = 0;
  nsAutoString textstr;

  nsCOMPtr<nsITextContent> content = do_QueryInterface(aText);
  if (!content) return NS_ERROR_FAILURE;
  
  const nsTextFragment* frag = content->Text();

  if (frag) {
    PRInt32 endoffset = (aEndOffset == -1) ? frag->GetLength() : aEndOffset;
    NS_ASSERTION(aStartOffset <= endoffset, "A start offset is beyond the end of the text fragment!");

    length = endoffset - aStartOffset;
    if (length <= 0) {
      return NS_OK;
    }

    if (frag->Is2b()) {
      textstr.Assign(frag->Get2b() + aStartOffset, length);
    }
    else {
      textstr.AssignWithConversion(frag->Get1b()+aStartOffset, length);
    }
  }

  mOutputString = &aStr;

  // We have to split the string across newlines
  // to match parser behavior
  PRInt32 start = 0;
  PRInt32 offset = textstr.FindCharInSet("\n\r");
  while (offset != kNotFound) {

    if(offset>start) {
      // Pass in the line
      rv = DoAddLeaf(nsnull,
                     eHTMLTag_text,
                     Substring(textstr, start, offset-start));
      if (NS_FAILED(rv)) break;
    }

    // Pass in a newline
    rv = DoAddLeaf(nsnull, eHTMLTag_newline, mLineBreak);
    if (NS_FAILED(rv)) break;
    
    start = offset+1;
    offset = textstr.FindCharInSet("\n\r", start);
  }

  // Consume the last bit of the string if there's any left
  if (NS_SUCCEEDED(rv) && start < length) {
    if (start) {
      rv = DoAddLeaf(nsnull,
                     eHTMLTag_text,
                     Substring(textstr, start, length-start));
    }
    else {
      rv = DoAddLeaf(nsnull, eHTMLTag_text, textstr);
    }
  }
  
  mOutputString = nsnull;

  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

NS_IMETHOD nsPlainTextSerializer::BeginContext ( PRInt32  aPosition) [inline, virtual]

This gets called when handling illegal contents, especially in dealing with tables.

This method creates a new context.

04/04/99 harishd

Parameters:
aPosition- The position from where the new context begins.

Implements nsIHTMLContentSink.

Definition at line 126 of file nsPlainTextSerializer.h.

{ return NS_OK; }

This method is used to close the main BODY container.

Implements nsIHTMLContentSink.

Definition at line 553 of file nsPlainTextSerializer.cpp.

{
  return CloseContainer(eHTMLTag_body);
}

Here is the call graph for this function:

This method gets called by the parser when a close container tag has been consumed and needs to be closed.

Parameters:
aTag- The tag to be closed.

Implements nsIHTMLContentSink.

Definition at line 475 of file nsPlainTextSerializer.cpp.

{
  return DoCloseContainer(aTag);
}

Here is the call graph for this function:

Here is the caller graph for this function:

This method is used to close the outer FORM container.

Implements nsIHTMLContentSink.

Definition at line 565 of file nsPlainTextSerializer.cpp.

{
  return CloseContainer(eHTMLTag_form);
}

Here is the call graph for this function:

This method is used to close the FRAMESET container.

Implements nsIHTMLContentSink.

Definition at line 589 of file nsPlainTextSerializer.cpp.

{
  return CloseContainer(eHTMLTag_frameset);
}

Here is the call graph for this function:

This method is used to close the only HEAD container.

Implements nsIHTMLContentSink.

Definition at line 540 of file nsPlainTextSerializer.cpp.

{
  mInHead = PR_FALSE;
  return NS_OK;
}

Here is the caller graph for this function:

This method is used to close the outer HTML container.

Implements nsIHTMLContentSink.

Definition at line 527 of file nsPlainTextSerializer.cpp.

{
  return CloseContainer(eHTMLTag_html);
}

Here is the call graph for this function:

This method is used to close the MAP container.

Implements nsIHTMLContentSink.

Definition at line 577 of file nsPlainTextSerializer.cpp.

{
  return CloseContainer(eHTMLTag_map);
}

Here is the call graph for this function:

This method gets called when the parser concludes the process of building the content model via the content sink.

5/7/98 gess

Implements nsIContentSink.

Definition at line 93 of file nsPlainTextSerializer.h.

{ return NS_OK; }

This method is called when parser has completed the processing for a single token.

Returns:
NS_OK if processing should not be interrupted NS_ERROR_HTMLPARSER_INTERRUPTED if the parsing should be interrupted

Implements nsIHTMLContentSink.

Definition at line 131 of file nsPlainTextSerializer.h.

{ return NS_OK; }

This method is called when parser has completed processing a chunk of tokens.

The processing of the tokens may be interrupted by returning NS_ERROR_HTMLPARSER_INTERRUPTED from DidProcessAToken.

Implements nsIHTMLContentSink.

Definition at line 129 of file nsPlainTextSerializer.h.

{ return NS_OK; }
nsresult nsPlainTextSerializer::DoAddLeaf ( const nsIParserNode aNode,
PRInt32  aTag,
const nsAString &  aText 
) [protected]

aNode may be null when we're working with the DOM, but then mContent is useable instead.

Definition at line 1121 of file nsPlainTextSerializer.cpp.

{
  // If we don't want any output, just return
  if (!DoOutput()) {
    return NS_OK;
  }

  if (aTag != eHTMLTag_whitespace && aTag != eHTMLTag_newline) {
    // Make sure to reset this, since it's no longer true.
    mHasWrittenCiteBlockquote = PR_FALSE;
  }
  
  if (mLineBreakDue)
    EnsureVerticalSpace(mFloatingLines);

  eHTMLTags type = (eHTMLTags)aTag;
  
  if ((mTagStackIndex > 1 &&
       mTagStack[mTagStackIndex-2] == eHTMLTag_select) ||
      (mTagStackIndex > 0 &&
        mTagStack[mTagStackIndex-1] == eHTMLTag_select)) {
    // Don't output the contents of SELECT elements;
    // Might be nice, eventually, to output just the selected element.
    // Read more in bug 31994.
    return NS_OK;
  }
  else if (mTagStackIndex > 0 && mTagStack[mTagStackIndex-1] == eHTMLTag_script) {
    // Don't output the contents of <script> tags;
    return NS_OK;
  }
  else if (type == eHTMLTag_text) {
    /* Check, if we are in a link (symbolized with mURL containing the URL)
       and the text is equal to the URL. In that case we don't want to output
       the URL twice so we scrap the text in mURL. */
    if (!mURL.IsEmpty() && mURL.Equals(aText)) {
      mURL.Truncate();
    }
    Write(aText);
  }
  else if (type == eHTMLTag_entity) {
    nsIParserService* parserService =
      nsContentUtils::GetParserServiceWeakRef();
    if (parserService) {
      nsAutoString str(aText);
      PRInt32 entity;
      parserService->HTMLConvertEntityToUnicode(str, &entity);
      if (entity == -1 && 
          !str.IsEmpty() &&
          str.First() == (PRUnichar) '#') {
        PRInt32 err = 0;
        entity = str.ToInteger(&err, kAutoDetect);  // NCR
      }
      nsAutoString temp;
      temp.Append(PRUnichar(entity));
      Write(temp);
    }
  }
  else if (type == eHTMLTag_br) {
    // Another egregious editor workaround, see bug 38194:
    // ignore the bogus br tags that the editor sticks here and there.
    nsAutoString typeAttr;
    if (NS_FAILED(GetAttributeValue(aNode, nsHTMLAtoms::type, typeAttr))
        || !typeAttr.EqualsLiteral("_moz")) {
      EnsureVerticalSpace(mEmptyLines+1);
    }
  }
  else if (type == eHTMLTag_whitespace) {
    // The only times we want to pass along whitespace from the original
    // html source are if we're forced into preformatted mode via flags,
    // or if we're prettyprinting and we're inside a <pre>.
    // Otherwise, either we're collapsing to minimal text, or we're
    // prettyprinting to mimic the html format, and in neither case
    // does the formatting of the html source help us.
    // One exception: at the very beginning of a selection,
    // we want to preserve whitespace.
    if (mFlags & nsIDocumentEncoder::OutputPreformatted ||
        (mPreFormatted && !mWrapColumn) ||
        IsInPre()) {
      Write(aText);
    }
    else if(!mInWhitespace ||
            (!mStartedOutput
             && mFlags | nsIDocumentEncoder::OutputSelectionOnly)) {
      mInWhitespace = PR_FALSE;
      Write(kSpace);
      mInWhitespace = PR_TRUE;
    }
  }
  else if (type == eHTMLTag_newline) {
    if (mFlags & nsIDocumentEncoder::OutputPreformatted ||
        (mPreFormatted && !mWrapColumn) ||
        IsInPre()) {
      EnsureVerticalSpace(mEmptyLines+1);
    }
    else {
      Write(kSpace);
    }
  }
  else if (type == eHTMLTag_hr &&
           (mFlags & nsIDocumentEncoder::OutputFormatted)) {
    EnsureVerticalSpace(0);

    // Make a line of dashes as wide as the wrap width
    // XXX honoring percentage would be nice
    nsAutoString line;
    PRUint32 width = (mWrapColumn > 0 ? mWrapColumn : 25);
    while (line.Length() < width) {
      line.Append(PRUnichar('-'));
    }
    Write(line);

    EnsureVerticalSpace(0);
  }
  else if (type == eHTMLTag_img) {
    /* Output (in decreasing order of preference)
       alt, title or nothing */
    // See <http://www.w3.org/TR/REC-html40/struct/objects.html#edef-IMG>
    nsAutoString imageDescription;
    if (NS_SUCCEEDED(GetAttributeValue(aNode,
                                       nsHTMLAtoms::alt,
                                       imageDescription))) {
      // If the alt attribute has an empty value (|alt=""|), output nothing
    }
    else if (NS_SUCCEEDED(GetAttributeValue(aNode,
                                            nsHTMLAtoms::title,
                                            imageDescription))
             && !imageDescription.IsEmpty()) {
      imageDescription = NS_LITERAL_STRING(" [") +
                         imageDescription +
                         NS_LITERAL_STRING("] ");
    }
   
    Write(imageDescription);
  }


  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 929 of file nsPlainTextSerializer.cpp.

{
  if (mFlags & nsIDocumentEncoder::OutputRaw) {
    // Raw means raw.  Don't even think about doing anything fancy
    // here like indenting, adding line breaks or any other
    // characters such as list item bullets, quote characters
    // around <q>, etc.  I mean it!  Don't make me smack you!

    return NS_OK;
  }

  if (mTagStackIndex > 0) {
    --mTagStackIndex;
  }

  if (mTagStackIndex >= mIgnoreAboveIndex) {
    if (mTagStackIndex == mIgnoreAboveIndex) {
      // We're dealing with the close tag whose matching
      // open tag had set the mIgnoreAboveIndex value.
      // Reset mIgnoreAboveIndex before discarding this tag.
      mIgnoreAboveIndex = (PRUint32)kNotFound;
    }
    return NS_OK;
  }

  eHTMLTags type = (eHTMLTags)aTag;
  // End current line if we're ending a block level tag
  if((type == eHTMLTag_body) || (type == eHTMLTag_html)) {
    // We want the output to end with a new line,
    // but in preformatted areas like text fields,
    // we can't emit newlines that weren't there.
    // So add the newline only in the case of formatted output.
    if (mFlags & nsIDocumentEncoder::OutputFormatted) {
      EnsureVerticalSpace(0);
    }
    else {
      FlushLine();
    }
    // We won't want to do anything with these in formatted mode either,
    // so just return now:
    return NS_OK;
  }
  else if (type == eHTMLTag_tr) {
    PopBool(mHasWrittenCellsForRow);
    // Should always end a line, but get no more whitespace
    if (mFloatingLines < 0)
      mFloatingLines = 0;
    mLineBreakDue = PR_TRUE;
  } 
  else if ((type == eHTMLTag_li) ||
           (type == eHTMLTag_dt)) {
    // Items that should always end a line, but get no more whitespace
    if (mFloatingLines < 0)
      mFloatingLines = 0;
    mLineBreakDue = PR_TRUE;
  } 
  else if (type == eHTMLTag_pre) {
    mFloatingLines = GetLastBool(mIsInCiteBlockquote) ? 0 : 1;
    mLineBreakDue = PR_TRUE;
  }
  else if (type == eHTMLTag_ul) {
    FlushLine();
    mIndent -= kIndentSizeList;
    if (--mULCount + mOLStackIndex == 0) {
      mFloatingLines = 1;
      mLineBreakDue = PR_TRUE;
    }
  }
  else if (type == eHTMLTag_ol) {
    FlushLine(); // Doing this after decreasing OLStackIndex would be wrong.
    mIndent -= kIndentSizeList;
    mOLStackIndex--;
    if (mULCount + mOLStackIndex == 0) {
      mFloatingLines = 1;
      mLineBreakDue = PR_TRUE;
    }
  }  
  else if (type == eHTMLTag_dl) {
    mFloatingLines = 1;
    mLineBreakDue = PR_TRUE;
  }
  else if (type == eHTMLTag_dd) {
    FlushLine();
    mIndent -= kIndentSizeDD;
  }
  else if (type == eHTMLTag_span) {
    --mSpanLevel;
  }
  else if (type == eHTMLTag_div) {
    if (mFloatingLines < 0)
      mFloatingLines = 0;
    mLineBreakDue = PR_TRUE;
  }
  else if (type == eHTMLTag_blockquote) {
    FlushLine();    // Is this needed?

    // Pop
    PRBool isInCiteBlockquote = PopBool(mIsInCiteBlockquote);

    if (isInCiteBlockquote) {
      mCiteQuoteLevel--;
      mFloatingLines = 0;
      mHasWrittenCiteBlockquote = PR_TRUE;
    }
    else {
      mIndent -= kTabSize;
      mFloatingLines = 1;
    }
    mLineBreakDue = PR_TRUE;
  }
  else if (IsBlockLevel(aTag)
           && type != eHTMLTag_script
           && type != eHTMLTag_doctypeDecl
           && type != eHTMLTag_markupDecl) {
    // All other blocks get 1 vertical space after them
    // in formatted mode, otherwise 0.
    // This is hard. Sometimes 0 is a better number, but
    // how to know?
    if (mFlags & nsIDocumentEncoder::OutputFormatted)
      EnsureVerticalSpace(1);
    else {
      if (mFloatingLines < 0)
        mFloatingLines = 0;
      mLineBreakDue = PR_TRUE;
    }
  }

  if (!(mFlags & nsIDocumentEncoder::OutputFormatted)) {
    return NS_OK;
  }
  // The rest of this routine is formatted output stuff,
  // which we should skip if we're not formatted:

  // Pop the currentConverted stack
  PRBool currentNodeIsConverted = PopBool(mCurrentNodeIsConverted);
  
  if (type == eHTMLTag_h1 || type == eHTMLTag_h2 ||
      type == eHTMLTag_h3 || type == eHTMLTag_h4 ||
      type == eHTMLTag_h5 || type == eHTMLTag_h6) {
    
    if (mHeaderStrategy) {  /*numbered or indent increasingly*/ 
      mIndent -= kIndentSizeHeaders;
    }
    if (mHeaderStrategy == 1 /*indent increasingly*/ ) {
      for (PRInt32 i = HeaderLevel(type); i > 1; i--) {
           // for h(x), run x-1 times
        mIndent -= kIndentIncrementHeaders;
      }
    }
    EnsureVerticalSpace(1);
  }
  else if (type == eHTMLTag_a && !currentNodeIsConverted && !mURL.IsEmpty()) {
    nsAutoString temp; 
    temp.AssignLiteral(" <");
    temp += mURL;
    temp.Append(PRUnichar('>'));
    Write(temp);
    mURL.Truncate();
  }
  else if (type == eHTMLTag_q) {
    Write(NS_LITERAL_STRING("\""));
  }
  else if ((type == eHTMLTag_sup || type == eHTMLTag_sub) 
           && mStructs && !currentNodeIsConverted) {
    Write(kSpace);
  }
  else if (type == eHTMLTag_code && mStructs && !currentNodeIsConverted) {
    Write(NS_LITERAL_STRING("|"));
  }
  else if ((type == eHTMLTag_strong || type == eHTMLTag_b)
           && mStructs && !currentNodeIsConverted) {
    Write(NS_LITERAL_STRING("*"));
  }
  else if ((type == eHTMLTag_em || type == eHTMLTag_i)
           && mStructs && !currentNodeIsConverted) {
    Write(NS_LITERAL_STRING("/"));
  }
  else if (type == eHTMLTag_u && mStructs && !currentNodeIsConverted) {
    Write(NS_LITERAL_STRING("_"));
  }

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

aNode may be null when we're working with the DOM, but then mContent is useable instead.

Definition at line 617 of file nsPlainTextSerializer.cpp.

{
  if (mFlags & nsIDocumentEncoder::OutputRaw) {
    // Raw means raw.  Don't even think about doing anything fancy
    // here like indenting, adding line breaks or any other
    // characters such as list item bullets, quote characters
    // around <q>, etc.  I mean it!  Don't make me smack you!

    return NS_OK;
  }

  eHTMLTags type = (eHTMLTags)aTag;

  if (mTagStackIndex < TagStackSize) {
    mTagStack[mTagStackIndex++] = type;
  }

  if (mIgnoreAboveIndex != (PRUint32)kNotFound) {
    return NS_OK;
  }

  // Reset this so that <blockquote type=cite> doesn't affect the whitespace
  // above random <pre>s below it.
  mHasWrittenCiteBlockquote = mHasWrittenCiteBlockquote && aTag == eHTMLTag_pre;

  PRBool isInCiteBlockquote = PR_FALSE;

  // XXX special-case <blockquote type=cite> so that we don't add additional
  // newlines before the text.
  if (aTag == eHTMLTag_blockquote) {
    nsAutoString value;
    nsresult rv = GetAttributeValue(aNode, nsHTMLAtoms::type, value);
    isInCiteBlockquote = NS_SUCCEEDED(rv) && value.EqualsIgnoreCase("cite");
  }

  if (mLineBreakDue && !isInCiteBlockquote)
    EnsureVerticalSpace(mFloatingLines);

  // Check if this tag's content that should not be output
  if ((type == eHTMLTag_noscript &&
       !(mFlags & nsIDocumentEncoder::OutputNoScriptContent)) ||
      ((type == eHTMLTag_iframe || type == eHTMLTag_noframes) &&
       !(mFlags & nsIDocumentEncoder::OutputNoFramesContent))) {
    // Ignore everything that follows the current tag in 
    // question until a matching end tag is encountered.
    mIgnoreAboveIndex = mTagStackIndex - 1;
    return NS_OK;
  }

  if (type == eHTMLTag_body) {
    // Try to figure out here whether we have a
    // preformatted style attribute.
    //
    // Trigger on the presence of a "-moz-pre-wrap" in the
    // style attribute. That's a very simplistic way to do
    // it, but better than nothing.
    // Also set mWrapColumn to the value given there
    // (which arguably we should only do if told to do so).
    nsAutoString style;
    PRInt32 whitespace;
    if(NS_SUCCEEDED(GetAttributeValue(aNode, nsHTMLAtoms::style, style)) &&
       (kNotFound != (whitespace = style.Find("white-space:")))) {

      if (kNotFound != style.Find("-moz-pre-wrap", PR_TRUE, whitespace)) {
#ifdef DEBUG_preformatted
        printf("Set mPreFormatted based on style moz-pre-wrap\n");
#endif
        mPreFormatted = PR_TRUE;
        PRInt32 widthOffset = style.Find("width:");
        if (widthOffset >= 0) {
          // We have to search for the ch before the semicolon,
          // not for the semicolon itself, because nsString::ToInteger()
          // considers 'c' to be a valid numeric char (even if radix=10)
          // but then gets confused if it sees it next to the number
          // when the radix specified was 10, and returns an error code.
          PRInt32 semiOffset = style.Find("ch", widthOffset+6);
          PRInt32 length = (semiOffset > 0 ? semiOffset - widthOffset - 6
                            : style.Length() - widthOffset);
          nsAutoString widthstr;
          style.Mid(widthstr, widthOffset+6, length);
          PRInt32 err;
          PRInt32 col = widthstr.ToInteger(&err);

          if (NS_SUCCEEDED(err)) {
            mWrapColumn = (PRUint32)col;
#ifdef DEBUG_preformatted
            printf("Set wrap column to %d based on style\n", mWrapColumn);
#endif
          }
        }
      }
      else if (kNotFound != style.Find("pre", PR_TRUE, whitespace)) {
#ifdef DEBUG_preformatted
        printf("Set mPreFormatted based on style pre\n");
#endif
        mPreFormatted = PR_TRUE;
        mWrapColumn = 0;
      }
    } 
    else {
      mPreFormatted = PR_FALSE;
    }

    return NS_OK;
  }

  if (!DoOutput()) {
    return NS_OK;
  }

  if (type == eHTMLTag_p)
    EnsureVerticalSpace(1);
  else if (type == eHTMLTag_pre) {
    if (GetLastBool(mIsInCiteBlockquote))
      EnsureVerticalSpace(0);
    else if (mHasWrittenCiteBlockquote) {
      EnsureVerticalSpace(0);
      mHasWrittenCiteBlockquote = PR_FALSE;
    }
    else
      EnsureVerticalSpace(1);
  }
  else if (type == eHTMLTag_tr) {
    PushBool(mHasWrittenCellsForRow, PR_FALSE);
  }
  else if (type == eHTMLTag_td || type == eHTMLTag_th) {
    // We must make sure that the content of two table cells get a
    // space between them.

    // To make the separation between cells most obvious and
    // importable, we use a TAB.
    if (GetLastBool(mHasWrittenCellsForRow)) {
      // Bypass |Write| so that the TAB isn't compressed away.
      AddToLine(NS_LITERAL_STRING("\t").get(), 1);
      mInWhitespace = PR_TRUE;
    }
    else if (mHasWrittenCellsForRow.Count() == 0) {
      // We don't always see a <tr> (nor a <table>) before the <td> if we're
      // copying part of a table
      PushBool(mHasWrittenCellsForRow, PR_TRUE); // will never be popped
    }
    else {
      SetLastBool(mHasWrittenCellsForRow, PR_TRUE);
    }
  }
  else if (type == eHTMLTag_ul) {
    // Indent here to support nested lists, which aren't included in li :-(
    EnsureVerticalSpace(mULCount + mOLStackIndex == 0 ? 1 : 0);
         // Must end the current line before we change indention
    mIndent += kIndentSizeList;
    mULCount++;
  }
  else if (type == eHTMLTag_ol) {
    EnsureVerticalSpace(mULCount + mOLStackIndex == 0 ? 1 : 0);
    // Must end the current line before we change indention
    if (mOLStackIndex < OLStackSize) {
      nsAutoString startAttr;
      PRInt32 startVal = 1;
      if(NS_SUCCEEDED(GetAttributeValue(aNode, nsHTMLAtoms::start, startAttr))){
        PRInt32 rv = 0;
        startVal = startAttr.ToInteger(&rv);
        if (NS_FAILED(rv))
          startVal = 1;
      }
      mOLStack[mOLStackIndex++] = startVal;
    }
    mIndent += kIndentSizeList;  // see ul
  }
  else if (type == eHTMLTag_li) {
    if (mTagStackIndex > 1 && IsInOL()) {
      if (mOLStackIndex > 0) {
        nsAutoString valueAttr;
        if(NS_SUCCEEDED(GetAttributeValue(aNode, nsHTMLAtoms::value, valueAttr))){
          PRInt32 rv = 0;
          PRInt32 valueAttrVal = valueAttr.ToInteger(&rv);
          if (NS_SUCCEEDED(rv))
            mOLStack[mOLStackIndex-1] = valueAttrVal;
        }
        // This is what nsBulletFrame does for OLs:
        mInIndentString.AppendInt(mOLStack[mOLStackIndex-1]++, 10);
      }
      else {
        mInIndentString.Append(PRUnichar('#'));
      }

      mInIndentString.Append(PRUnichar('.'));

    }
    else {
      static char bulletCharArray[] = "*o+#";
      PRUint32 index = mULCount > 0 ? (mULCount - 1) : 3;
      char bulletChar = bulletCharArray[index % 4];
      mInIndentString.Append(PRUnichar(bulletChar));
    }
    
    mInIndentString.Append(PRUnichar(' '));
  }
  else if (type == eHTMLTag_dl) {
    EnsureVerticalSpace(1);
  }
  else if (type == eHTMLTag_dt) {
    EnsureVerticalSpace(0);
  }
  else if (type == eHTMLTag_dd) {
    EnsureVerticalSpace(0);
    mIndent += kIndentSizeDD;
  }
  else if (type == eHTMLTag_span) {
    ++mSpanLevel;
  }
  else if (type == eHTMLTag_blockquote) {
    // Push
    PushBool(mIsInCiteBlockquote, isInCiteBlockquote);
    if (isInCiteBlockquote) {
      EnsureVerticalSpace(0);
      mCiteQuoteLevel++;
    }
    else {
      EnsureVerticalSpace(1);
      mIndent += kTabSize; // Check for some maximum value?
    }
  }

  // Else make sure we'll separate block level tags,
  // even if we're about to leave, before doing any other formatting.
  else if (IsBlockLevel(aTag)) {
    EnsureVerticalSpace(0);
  }

  if (!(mFlags & nsIDocumentEncoder::OutputFormatted)) {
    return NS_OK;
  }
  // The rest of this routine is formatted output stuff,
  // which we should skip if we're not formatted:

  // Push on stack
  PRBool currentNodeIsConverted = IsCurrentNodeConverted(aNode);
  PushBool(mCurrentNodeIsConverted, currentNodeIsConverted);

  if (type == eHTMLTag_h1 || type == eHTMLTag_h2 ||
      type == eHTMLTag_h3 || type == eHTMLTag_h4 ||
      type == eHTMLTag_h5 || type == eHTMLTag_h6)
  {
    EnsureVerticalSpace(2);
    if (mHeaderStrategy == 2) {  // numbered
      mIndent += kIndentSizeHeaders;
      // Caching
      PRInt32 level = HeaderLevel(type);
      // Increase counter for current level
      mHeaderCounter[level]++;
      // Reset all lower levels
      PRInt32 i;

      for (i = level + 1; i <= 6; i++) {
        mHeaderCounter[i] = 0;
      }

      // Construct numbers
      nsAutoString leadup;
      for (i = 1; i <= level; i++) {
        leadup.AppendInt(mHeaderCounter[i]);
        leadup.Append(PRUnichar('.'));
      }
      leadup.Append(PRUnichar(' '));
      Write(leadup);
    }
    else if (mHeaderStrategy == 1) { // indent increasingly
      mIndent += kIndentSizeHeaders;
      for (PRInt32 i = HeaderLevel(type); i > 1; i--) {
           // for h(x), run x-1 times
        mIndent += kIndentIncrementHeaders;
      }
    }
  }
  else if (type == eHTMLTag_a && !currentNodeIsConverted) {
    nsAutoString url;
    if (NS_SUCCEEDED(GetAttributeValue(aNode, nsHTMLAtoms::href, url))
        && !url.IsEmpty()) {
      mURL = url;
    }
  }
  else if (type == eHTMLTag_q) {
    Write(NS_LITERAL_STRING("\""));
  }
  else if (type == eHTMLTag_sup && mStructs && !currentNodeIsConverted) {
    Write(NS_LITERAL_STRING("^"));
  }
  else if (type == eHTMLTag_sub && mStructs && !currentNodeIsConverted) { 
    Write(NS_LITERAL_STRING("_"));
  }
  else if (type == eHTMLTag_code && mStructs && !currentNodeIsConverted) {
    Write(NS_LITERAL_STRING("|"));
  }
  else if ((type == eHTMLTag_strong || type == eHTMLTag_b)
           && mStructs && !currentNodeIsConverted) {
    Write(NS_LITERAL_STRING("*"));
  }
  else if ((type == eHTMLTag_em || type == eHTMLTag_i)
           && mStructs && !currentNodeIsConverted) {
    Write(NS_LITERAL_STRING("/"));
  }
  else if (type == eHTMLTag_u && mStructs && !currentNodeIsConverted) {
    Write(NS_LITERAL_STRING("_"));
  }

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PRBool nsPlainTextSerializer::DoOutput ( ) [inline, protected]

Definition at line 166 of file nsPlainTextSerializer.h.

  {
    return !mInHead;
  }

Here is the caller graph for this function:

NS_IMETHOD nsPlainTextSerializer::EndContext ( PRInt32  aPosition) [inline, virtual]

This method terminates any new context that got created by BeginContext and switches back to the main context.

04/04/99 harishd

Parameters:
aPosition- Validates the end of a context.

Implements nsIHTMLContentSink.

Definition at line 127 of file nsPlainTextSerializer.h.

{ return NS_OK; }
void nsPlainTextSerializer::EndLine ( PRBool  aSoftlinebreak) [protected]

Outputs the contents of mCurrentLine, and resets line specific variables.

Also adds an indentation and prefix if there is one specified. Strips ending spaces from the line if it isn't preformatted.

Definition at line 1523 of file nsPlainTextSerializer.cpp.

{
  PRUint32 currentlinelength = mCurrentLine.Length();

  if(aSoftlinebreak && 0 == currentlinelength) {
    // No meaning
    return;
  }
  
  // In non-preformatted mode, remove SPACE from the end
  // of the line, unless we got "-- " in a format=flowed
  // output. "-- " is the sig delimiter by convention and
  // shouldn't be touched even in format=flowed
  // (see RFC 2646). We only check for "-- " when it's a hard line
  // break for obvious reasons.
  if(!(mFlags & nsIDocumentEncoder::OutputPreformatted) &&
     (aSoftlinebreak || !mCurrentLine.EqualsLiteral("-- "))) {
    // Remove SPACE:s from the end of the line.
    while(currentlinelength > 0 &&
          mCurrentLine[currentlinelength-1] == ' ') {
      --currentlinelength;
    }
    mCurrentLine.SetLength(currentlinelength);
  }
  
  if(aSoftlinebreak &&
     (mFlags & nsIDocumentEncoder::OutputFormatFlowed) &&
     (mIndent == 0)) {
    // Add the soft part of the soft linebreak (RFC 2646 4.1)
    // We only do this when there is no indentation since format=flowed
    // lines and indentation doesn't work well together.
    mCurrentLine.Append(PRUnichar(' '));
  }

  if(aSoftlinebreak) {
    mEmptyLines=0;
  } 
  else {
    // Hard break
    if(!mCurrentLine.IsEmpty() || !mInIndentString.IsEmpty()) {
      mEmptyLines=-1;
    }

    mEmptyLines++;
  }

  if(mAtFirstColumn) {
    // If we don't have anything "real" to output we have to
    // make sure the indent doesn't end in a space since that
    // would trick a format=flowed-aware receiver.
    PRBool stripTrailingSpaces = mCurrentLine.IsEmpty();
    OutputQuotesAndIndent(stripTrailingSpaces);
  }

  mCurrentLine.Append(mLineBreak);
  Output(mCurrentLine);
  mCurrentLine.Truncate();
  mCurrentLineWidth = 0;
  mAtFirstColumn=PR_TRUE;
  mInWhitespace=PR_TRUE;
  mLineBreakDue = PR_FALSE;
  mFloatingLines = -1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Adds as many newline as necessary to get |noOfRows| empty lines.

noOfRows = -1 : Being in the middle of some line of text noOfRows = 0 : Being at the start of a line noOfRows = n>0 : Having n empty lines before the current line.

Definition at line 1269 of file nsPlainTextSerializer.cpp.

{
  // If we have something in the indent we probably want to output
  // it and it's not included in the count for empty lines so we don't
  // realize that we should start a new line.
  if(noOfRows >= 0 && !mInIndentString.IsEmpty()) {
    EndLine(PR_FALSE);
  }

  while(mEmptyLines < noOfRows) {
    EndLine(PR_FALSE);
  }
  mLineBreakDue = PR_FALSE;
  mFloatingLines = -1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

NS_IMETHODIMP nsPlainTextSerializer::Flush ( nsAString &  aStr) [virtual]

Implements nsIContentSerializer.

Definition at line 451 of file nsPlainTextSerializer.cpp.

{
  mOutputString = &aStr;
  FlushLine();
  mOutputString = nsnull;
  return NS_OK;
}

Here is the call graph for this function:

This empties the current line cache without adding a NEWLINE.

Should not be used if line wrapping is of importance since this function destroys the cache information.

It will also write indentation and quotes if we believe us to be at the start of the line.

Definition at line 1294 of file nsPlainTextSerializer.cpp.

{
  if(!mCurrentLine.IsEmpty()) {
    if(mAtFirstColumn) {
      OutputQuotesAndIndent(); // XXX: Should we always do this? Bug?
    }

    Output(mCurrentLine);
    mAtFirstColumn = mAtFirstColumn && mCurrentLine.IsEmpty();
    mCurrentLine.Truncate();
    mCurrentLineWidth = 0;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

Flush content so that the content model is in sync with the state of the sink.

Parameters:
aTypethe type of flush to perform

Implements nsIContentSink.

Definition at line 104 of file nsPlainTextSerializer.h.

{ }
nsresult nsPlainTextSerializer::GetAttributeValue ( const nsIParserNode aNode,
nsIAtom aName,
nsString aValueRet 
) [protected]

Gets the value of an attribute in a string.

If the function returns NS_ERROR_NOT_AVAILABLE, there was none such attribute specified.

Definition at line 1852 of file nsPlainTextSerializer.cpp.

{
  if (mContent) {
    if (NS_CONTENT_ATTR_NOT_THERE != mContent->GetAttr(kNameSpaceID_None,
                                                       aName, aValueRet)) {
      return NS_OK;
    }
  }
  else if (aNode) {
    nsAutoString name; 
    aName->ToString(name);

    PRInt32 count = aNode->GetAttributeCount();
    for (PRInt32 i=0;i<count;i++) {
      const nsAString& key = aNode->GetKeyAt(i);
      if (key.Equals(name, nsCaseInsensitiveStringComparator())) {
        aValueRet = aNode->GetValueAt(i);
        return NS_OK;
      }
    }
  }

  return NS_ERROR_NOT_AVAILABLE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PRInt32 nsPlainTextSerializer::GetIdForContent ( nsIContent aContent) [static, protected]

Definition at line 1896 of file nsPlainTextSerializer.cpp.

{
  if (!aContent->IsContentOfType(nsIContent::eHTML)) {
    return eHTMLTag_unknown;
  }

  nsIParserService* parserService = nsContentUtils::GetParserServiceWeakRef();

  return parserService ? parserService->HTMLAtomTagToId(aContent->Tag()) :
                         eHTMLTag_unknown;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 240 of file nsPlainTextSerializer.cpp.

{
  PRUint32 size = aStack.Count();
  if (size == 0) {
    return PR_FALSE;
  }
  return (aStack.ElementAt(size-1) != NS_REINTERPRET_CAST(void*, PR_FALSE));
}

Here is the caller graph for this function:

virtual nsISupports* nsPlainTextSerializer::GetTarget ( ) [inline, virtual]

Returns the target object (often a document object) into which the content built by this content sink is being added, if any (IOW, may return null).

Implements nsIContentSink.

Definition at line 106 of file nsPlainTextSerializer.h.

{ return nsnull; }
NS_DECL_ISUPPORTS NS_IMETHOD nsPlainTextSerializer::Init ( PRUint32  flags,
PRUint32  aWrapColumn,
const char *  aCharSet,
PRBool  aIsCopying 
) [virtual]

Implements nsIContentSerializer.

Here is the caller graph for this function:

NS_IMETHODIMP nsPlainTextSerializer::Initialize ( nsAString *  aOutString,
PRUint32  aFlags,
PRUint32  aWrapCol 
) [virtual]

Implements nsIHTMLToTextSink.

Definition at line 280 of file nsPlainTextSerializer.cpp.

{
  nsresult rv = Init(aFlags, aWrapCol, nsnull, PR_FALSE);
  NS_ENSURE_SUCCESS(rv, rv);

  // XXX This is wrong. It violates XPCOM string ownership rules.
  // We're only getting away with this because instances of this
  // class are restricted to single function scope.
  mOutputString = aOutString;

  return NS_OK;
}

Here is the call graph for this function:

Returns true if the id represents an element of block type.

Can be used to determine if a new paragraph should be started.

Definition at line 1913 of file nsPlainTextSerializer.cpp.

{
  PRBool isBlock = PR_FALSE;

  nsIParserService* parserService = nsContentUtils::GetParserServiceWeakRef();
  if (parserService) {
    parserService->IsBlock(aId, isBlock);
  }

  return isBlock;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Returns true if the id represents a container.

Definition at line 1929 of file nsPlainTextSerializer.cpp.

{
  PRBool isContainer = PR_FALSE;

  nsIParserService* parserService = nsContentUtils::GetParserServiceWeakRef();
  if (parserService) {
    parserService->IsContainer(aId, isContainer);
  }

  return isContainer;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Returns true, if the element was inserted by Moz' TXT->HTML converter.

In this case, we should ignore it.

Definition at line 1884 of file nsPlainTextSerializer.cpp.

{
  nsAutoString value;
  nsresult rv = GetAttributeValue(aNode, nsHTMLAtoms::kClass, value);
  return (NS_SUCCEEDED(rv) &&
          (value.EqualsIgnoreCase("moz-txt", 7) ||
           value.EqualsIgnoreCase("\"moz-txt", 8)));
}

Here is the call graph for this function:

Here is the caller graph for this function:

NS_IMETHODIMP nsPlainTextSerializer::IsEnabled ( PRInt32  aTag,
PRBool aReturn 
) [virtual]

01/09/2003 harishd

Parameters:
aTag- Check if this tag is enabled or not.

Implements nsIHTMLContentSink.

Definition at line 595 of file nsPlainTextSerializer.cpp.

{
  nsHTMLTag theHTMLTag = nsHTMLTag(aTag);

  if (theHTMLTag == eHTMLTag_script) {
    *aReturn = !(mFlags & nsIDocumentEncoder::OutputNoScriptContent);
  }
  else if (theHTMLTag == eHTMLTag_frameset) {
    *aReturn = !(mFlags & nsIDocumentEncoder::OutputNoFramesContent);
  }
  else {
    *aReturn = PR_FALSE;
  }

  return NS_OK;
}

This method is required only to indentify LI's inside OL.

Returns TRUE if we are inside an OL tag and FALSE otherwise.

Definition at line 1971 of file nsPlainTextSerializer.cpp.

{
  PRInt32 i = mTagStackIndex;
  while(--i >= 0) {
    if(mTagStack[i] == eHTMLTag_ol)
      return PR_TRUE;
    if (mTagStack[i] == eHTMLTag_ul) {
      // If a UL is reached first, LI belongs the UL nested in OL.
      return PR_FALSE;
    }
  }
  // We may reach here for orphan LI's.
  return PR_FALSE;
}

Here is the caller graph for this function:

Returns true if we currently are inside a.

. The check is done
 by traversing the tag stack looking for 

 until we hit a block
 level tag which is assumed to override any 

:s below it in
 the stack. To do this correctly to a 100% would require access
 to style which we don't support in this converter.

Definition at line 1949 of file nsPlainTextSerializer.cpp.

{
  PRInt32 i = mTagStackIndex;
  while(i > 0) {
    if(mTagStack[i-1] == eHTMLTag_pre)
      return PR_TRUE;
    if(IsBlockLevel(mTagStack[i-1])) {
      // We assume that every other block overrides a <pre>
      return PR_FALSE;
    }
    --i;
  }

  // Not a <pre> in the whole stack
  return PR_FALSE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PRBool nsPlainTextSerializer::MayWrap ( ) [inline, protected]

Definition at line 159 of file nsPlainTextSerializer.h.

Here is the caller graph for this function:

This gets called by the parser to notify observers of the tag.

Parameters:
aErrorResultthe error code

Implements nsIHTMLContentSink.

Definition at line 123 of file nsPlainTextSerializer.h.

{ return NS_OK; }
nsPlainTextSerializer::NS_IMETHOD_ ( PRBool  ) [inline, virtual]

Call this method to determnine if a FORM is on the sink's stack.

Returns:
PR_TRUE if found else PR_FALSE

Implements nsIHTMLContentSink.

Definition at line 124 of file nsPlainTextSerializer.h.

{ return PR_FALSE; }

This method is used to open the main BODY container.

4/1/98 gess

Parameters:
nsIParserNodereference to parser node interface

Implements nsIHTMLContentSink.

Definition at line 547 of file nsPlainTextSerializer.cpp.

{
  return OpenContainer(aNode);
}

Here is the call graph for this function:

This method is used to open a generic container in the sink.

4/1/98 gess

Parameters:
nsIParserNodereference to parser node interface

Implements nsIHTMLContentSink.

Definition at line 467 of file nsPlainTextSerializer.cpp.

{
  PRInt32 type = aNode.GetNodeType();

  return DoOpenContainer(&aNode, type);
}

Here is the call graph for this function:

Here is the caller graph for this function:

This method is used to open a new FORM container.

4/1/98 gess

Parameters:
nsIParserNodereference to parser node interface

Implements nsIHTMLContentSink.

Definition at line 559 of file nsPlainTextSerializer.cpp.

{
  return OpenContainer(aNode);
}

Here is the call graph for this function:

This method is used to open the FRAMESET container.

4/1/98 gess

Parameters:
nsIParserNodereference to parser node interface

Implements nsIHTMLContentSink.

Definition at line 583 of file nsPlainTextSerializer.cpp.

{
  return OpenContainer(aNode);
}

Here is the call graph for this function:

This method is used to open the only HEAD container.

4/1/98 gess

Parameters:
nsIParserNodereference to parser node interface

Implements nsIHTMLContentSink.

Definition at line 533 of file nsPlainTextSerializer.cpp.

{
  mInHead = PR_TRUE;
  return NS_OK;
}

Here is the caller graph for this function:

This method is used to open the outer HTML container.

4/1/98 gess

Parameters:
nsIParserNodereference to parser node interface

Implements nsIHTMLContentSink.

Definition at line 521 of file nsPlainTextSerializer.cpp.

{
  return OpenContainer(aNode);
}

Here is the call graph for this function:

This method is used to open a new MAP container.

4/1/98 gess

Parameters:
nsIParserNodereference to parser node interface

Implements nsIHTMLContentSink.

Definition at line 571 of file nsPlainTextSerializer.cpp.

{
  return OpenContainer(aNode);
}

Here is the call graph for this function:

void nsPlainTextSerializer::Output ( nsString aString) [protected]

Prints the text to output to our current output device (the string mOutputString).

The only logic here is to replace non breaking spaces with a normal space since most (all?) receivers of the result won't understand the nbsp and even be confused by it.

Definition at line 1315 of file nsPlainTextSerializer.cpp.

{
  if (!aString.IsEmpty()) {
    mStartedOutput = PR_TRUE;
  }

  // First, replace all nbsp characters with spaces,
  // which the unicode encoder won't do for us.
  static PRUnichar nbsp = 160;
  static PRUnichar space = ' ';
  aString.ReplaceChar(nbsp, space);

  mOutputString->Append(aString);
}

Here is the caller graph for this function:

void nsPlainTextSerializer::OutputQuotesAndIndent ( PRBool  stripTrailingSpaces = PR_FALSE) [protected]

Outputs the calculated and stored indent and text in the indentation.

That is quote chars and numbers for numbered lists and such. It will also reset any stored text to put in the indentation after using it.

Definition at line 1594 of file nsPlainTextSerializer.cpp.

{
  nsAutoString stringToOutput;
  
  // Put the mail quote "> " chars in, if appropriate:
  if (mCiteQuoteLevel > 0) {
    nsAutoString quotes;
    for(int i=0; i < mCiteQuoteLevel; i++) {
      quotes.Append(PRUnichar('>'));
    }
    if (!mCurrentLine.IsEmpty()) {
      /* Better don't output a space here, if the line is empty,
         in case a recieving f=f-aware UA thinks, this were a flowed line,
         which it isn't - it's just empty.
         (Flowed lines may be joined with the following one,
         so the empty line may be lost completely.) */
      quotes.Append(PRUnichar(' '));
    }
    stringToOutput = quotes;
    mAtFirstColumn = PR_FALSE;
  }
  
  // Indent if necessary
  PRInt32 indentwidth = mIndent - mInIndentString.Length();
  if (indentwidth > 0
      && (!mCurrentLine.IsEmpty() || !mInIndentString.IsEmpty())
      // Don't make empty lines look flowed
      ) {
    nsAutoString spaces;
    for (int i=0; i < indentwidth; ++i)
      spaces.Append(PRUnichar(' '));
    stringToOutput += spaces;
    mAtFirstColumn = PR_FALSE;
  }
  
  if(!mInIndentString.IsEmpty()) {
    stringToOutput += mInIndentString;
    mAtFirstColumn = PR_FALSE;
    mInIndentString.Truncate();
  }

  if(stripTrailingSpaces) {
    PRInt32 lineLength = stringToOutput.Length();
    while(lineLength > 0 &&
          ' ' == stringToOutput[lineLength-1]) {
      --lineLength;
    }
    stringToOutput.SetLength(lineLength);
  }

  if(!stringToOutput.IsEmpty()) {
    Output(stringToOutput);
  }
    
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 268 of file nsPlainTextSerializer.cpp.

{
  PRBool returnValue = PR_FALSE;
  PRUint32 size = aStack.Count();
  if (size > 0) {
    returnValue = (aStack.ElementAt(size-1) != NS_REINTERPRET_CAST(void*, PR_FALSE));
    aStack.RemoveElementAt(size-1);
  }
  return returnValue;
}

Here is the caller graph for this function:

void nsPlainTextSerializer::PushBool ( nsVoidArray aStack,
PRBool  aValue 
) [protected]

Definition at line 262 of file nsPlainTextSerializer.cpp.

{
    aStack.AppendElement(NS_REINTERPRET_CAST(void*, aValue));
}

Here is the caller graph for this function:

NS_IMETHOD nsPlainTextSerializer::SetDocumentCharset ( nsACString &  aCharset) [inline, virtual]

Set the document character set.

This should be passed on to the document itself.

Implements nsIContentSink.

Definition at line 105 of file nsPlainTextSerializer.h.

{ return NS_OK; }
void nsPlainTextSerializer::SetLastBool ( nsVoidArray aStack,
PRBool  aValue 
) [protected]

Definition at line 250 of file nsPlainTextSerializer.cpp.

{
  PRUint32 size = aStack.Count();
  if (size > 0) {
    aStack.ReplaceElementAt(NS_REINTERPRET_CAST(void*, aValue), size-1);
  }
  else {
    NS_ERROR("There is no \"Last\" value");
  }
}

Here is the caller graph for this function:

NS_IMETHOD nsPlainTextSerializer::SetParser ( nsIParser aParser) [inline, virtual]

This method gets called by the parser so that the content sink can retain a reference to the parser.

The expectation is that the content sink will drop the reference when it gets the DidBuildModel notification i.e. when parsing is done.

Implements nsIContentSink.

Definition at line 96 of file nsPlainTextSerializer.h.

{ return NS_OK; }
NS_IMETHOD nsPlainTextSerializer::SetTitle ( const nsString aValue) [inline, virtual]

This method gets called by the parser when it encounters a title tag and wants to set the document title in the sink.

4/1/98 gess

Parameters:
nsStringreference to new title value

Implements nsIHTMLContentSink.

Definition at line 109 of file nsPlainTextSerializer.h.

{ return NS_OK; }

This method gets called when the parser begins the process of building the content model via the content sink.

5/7/98 gess

Implements nsIContentSink.

Definition at line 92 of file nsPlainTextSerializer.h.

{ return NS_OK; }

This method gets called when the parser gets i/o blocked, and wants to notify the sink that it may be a while before more data is available.

5/7/98 gess

Implements nsIContentSink.

Definition at line 94 of file nsPlainTextSerializer.h.

{ return NS_OK; }

This method is called when parser is about to process a single token.

Implements nsIHTMLContentSink.

Definition at line 130 of file nsPlainTextSerializer.h.

{ return NS_OK; }

This method is called when parser is about to begin synchronously processing a chunk of tokens.

Implements nsIHTMLContentSink.

Definition at line 128 of file nsPlainTextSerializer.h.

{ return NS_OK; }

This method gets called when the parser i/o gets unblocked, and we're about to start dumping content again to the sink.

5/7/98 gess

Implements nsIContentSink.

Definition at line 95 of file nsPlainTextSerializer.h.

{ return NS_OK; }
void nsPlainTextSerializer::Write ( const nsAString &  aString) [protected]

Write a string.

This is the highlevel function to use to get text output. By using AddToLine, Output, EndLine and other functions it handles quotation, line wrapping, indentation, whitespace compression and other things.

Definition at line 1656 of file nsPlainTextSerializer.cpp.

{
#ifdef DEBUG_wrapping
  printf("Write(%s): wrap col = %d\n",
         NS_ConvertUCS2toUTF8(aString).get(), mWrapColumn);
#endif

  PRInt32 bol = 0;
  PRInt32 newline;
  
  PRInt32 totLen = aString.Length();

  // If the string is empty, do nothing:
  if (totLen <= 0) return;

  // We have two major codepaths here. One that does preformatted text and one
  // that does normal formatted text. The one for preformatted text calls
  // Output directly while the other code path goes through AddToLine.
  if ((mPreFormatted && !mWrapColumn) || IsInPre()
      || ((((!mQuotesPreformatted && mSpanLevel > 0) || mDontWrapAnyQuotes))
          && mEmptyLines >= 0 && aString.First() == PRUnichar('>'))) {
    // No intelligent wrapping.

    // This mustn't be mixed with intelligent wrapping without clearing
    // the mCurrentLine buffer before!!!
    NS_WARN_IF_FALSE(mCurrentLine.IsEmpty(),
                 "Mixed wrapping data and nonwrapping data on the same line");
    if (!mCurrentLine.IsEmpty()) {
      FlushLine();
    }

    // Put the mail quote "> " chars in, if appropriate.
    // Have to put it in before every line.
    while(bol<totLen) {
      PRBool outputQuotes = mAtFirstColumn;
      PRBool atFirstColumn = mAtFirstColumn;
      PRBool outputLineBreak = PR_FALSE;

      // Find one of '\n' or '\r' using iterators since nsAString
      // doesn't have the old FindCharInSet function.
      nsAString::const_iterator iter;           aString.BeginReading(iter);
      nsAString::const_iterator done_searching; aString.EndReading(done_searching);
      iter.advance(bol); 
      PRInt32 new_newline = bol;
      newline = kNotFound;
      while(iter != done_searching) {
        if('\n' == *iter || '\r' == *iter) {
          newline = new_newline;
          break;
        }
        ++new_newline;
        ++iter;
      }

      // Done searching
      if(newline == kNotFound) {
        // No new lines.
        nsAutoString stringpart(Substring(aString, bol, totLen - bol));
        if(!stringpart.IsEmpty()) {
          PRUnichar lastchar = stringpart[stringpart.Length()-1];
          if((lastchar == '\t') || (lastchar == ' ') ||
             (lastchar == '\r') ||(lastchar == '\n')) {
            mInWhitespace = PR_TRUE;
          } 
          else {
            mInWhitespace = PR_FALSE;
          }
        }
        mCurrentLine.Assign(stringpart);
        mEmptyLines=-1;
        atFirstColumn = mAtFirstColumn && (totLen-bol)==0;
        bol = totLen;
      } 
      else {
        // There is a newline
        nsAutoString stringpart(Substring(aString, bol, newline-bol));
        mInWhitespace = PR_TRUE;
        mCurrentLine.Assign(stringpart);
        outputLineBreak = PR_TRUE;
        mEmptyLines=0;
        atFirstColumn = PR_TRUE;
        bol = newline+1;
        if('\r' == *iter && bol < totLen && '\n' == *++iter) {
          // There was a CRLF in the input. This used to be illegal and
          // stripped by the parser. Apparently not anymore. Let's skip
          // over the LF.
          bol++;
        }
      }

      if(outputQuotes) {
        // Note: this call messes with mAtFirstColumn
        OutputQuotesAndIndent();
      }

      Output(mCurrentLine);
      if (outputLineBreak) {
        Output(mLineBreak);
      }
      mAtFirstColumn = atFirstColumn;
    }

    // Reset mCurrentLine.
    mCurrentLine.Truncate();

#ifdef DEBUG_wrapping
    printf("No wrapping: newline is %d, totLen is %d\n",
           newline, totLen);
#endif
    return;
  }

  // XXX Copy necessary to use nsString methods and gain
  // access to underlying buffer
  nsAutoString str(aString);

  // Intelligent handling of text
  // If needed, strip out all "end of lines"
  // and multiple whitespace between words
  PRInt32 nextpos;
  nsAutoString tempstr;
  const PRUnichar * offsetIntoBuffer = nsnull;
  
  while (bol < totLen) {    // Loop over lines
    // Find a place where we may have to do whitespace compression
    nextpos = str.FindCharInSet(" \t\n\r", bol);
#ifdef DEBUG_wrapping
    nsAutoString remaining;
    str.Right(remaining, totLen - bol);
    foo = ToNewCString(remaining);
    //    printf("Next line: bol = %d, newlinepos = %d, totLen = %d, string = '%s'\n",
    //           bol, nextpos, totLen, foo);
    nsMemory::Free(foo);
#endif

    if(nextpos == kNotFound) {
      // The rest of the string
      offsetIntoBuffer = str.get() + bol;
      AddToLine(offsetIntoBuffer, totLen-bol);
      bol=totLen;
      mInWhitespace=PR_FALSE;
    } 
    else {
      // There's still whitespace left in the string
      if (nextpos != 0 && (nextpos + 1) < totLen) {
        offsetIntoBuffer = str.get() + nextpos;
        // skip '\n' if it is between CJ chars
        if (offsetIntoBuffer[0] == '\n' && IS_CJ_CHAR(offsetIntoBuffer[-1]) && IS_CJ_CHAR(offsetIntoBuffer[1])) {
          offsetIntoBuffer = str.get() + bol;
          AddToLine(offsetIntoBuffer, nextpos-bol);
          bol = nextpos + 1;
          continue;
        }
      }
      // If we're already in whitespace and not preformatted, just skip it:
      if (mInWhitespace && (nextpos == bol) && !mPreFormatted &&
          !(mFlags & nsIDocumentEncoder::OutputPreformatted)) {
        // Skip whitespace
        bol++;
        continue;
      }

      if(nextpos == bol) {
        // Note that we are in whitespace.
        mInWhitespace = PR_TRUE;
        offsetIntoBuffer = str.get() + nextpos;
        AddToLine(offsetIntoBuffer, 1);
        bol++;
        continue;
      }
      
      mInWhitespace = PR_TRUE;
      
      offsetIntoBuffer = str.get() + bol;
      if(mPreFormatted || (mFlags & nsIDocumentEncoder::OutputPreformatted)) {
        // Preserve the real whitespace character
        nextpos++;
        AddToLine(offsetIntoBuffer, nextpos-bol);
        bol = nextpos;
      } 
      else {
        // Replace the whitespace with a space
        AddToLine(offsetIntoBuffer, nextpos-bol);
        AddToLine(kSpace.get(),1);
        bol = nextpos + 1; // Let's eat the whitespace
      }
    }
  } // Continue looping over the string
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 277 of file nsPlainTextSerializer.h.

Definition at line 181 of file nsPlainTextSerializer.h.

Definition at line 205 of file nsPlainTextSerializer.h.

Definition at line 247 of file nsPlainTextSerializer.h.

Definition at line 178 of file nsPlainTextSerializer.h.

Definition at line 215 of file nsPlainTextSerializer.h.

Definition at line 253 of file nsPlainTextSerializer.h.

Definition at line 192 of file nsPlainTextSerializer.h.

Definition at line 222 of file nsPlainTextSerializer.h.

Definition at line 206 of file nsPlainTextSerializer.h.

Definition at line 207 of file nsPlainTextSerializer.h.

nsAutoVoidArray nsPlainTextSerializer::mHasWrittenCellsForRow [protected]

Definition at line 250 of file nsPlainTextSerializer.h.

Definition at line 199 of file nsPlainTextSerializer.h.

Definition at line 241 of file nsPlainTextSerializer.h.

Definition at line 236 of file nsPlainTextSerializer.h.

Definition at line 264 of file nsPlainTextSerializer.h.

Definition at line 201 of file nsPlainTextSerializer.h.

Definition at line 180 of file nsPlainTextSerializer.h.

Definition at line 204 of file nsPlainTextSerializer.h.

Definition at line 226 of file nsPlainTextSerializer.h.

nsAutoVoidArray nsPlainTextSerializer::mIsInCiteBlockquote [protected]

Definition at line 254 of file nsPlainTextSerializer.h.

Definition at line 272 of file nsPlainTextSerializer.h.

Definition at line 233 of file nsPlainTextSerializer.h.

Definition at line 273 of file nsPlainTextSerializer.h.

Definition at line 267 of file nsPlainTextSerializer.h.

Definition at line 268 of file nsPlainTextSerializer.h.

nsAString* nsPlainTextSerializer::mOutputString [protected]

Definition at line 257 of file nsPlainTextSerializer.h.

Definition at line 227 of file nsPlainTextSerializer.h.

Definition at line 191 of file nsPlainTextSerializer.h.

Definition at line 219 of file nsPlainTextSerializer.h.

Definition at line 228 of file nsPlainTextSerializer.h.

Definition at line 194 of file nsPlainTextSerializer.h.

Definition at line 260 of file nsPlainTextSerializer.h.

Definition at line 261 of file nsPlainTextSerializer.h.

Definition at line 270 of file nsPlainTextSerializer.h.

Definition at line 235 of file nsPlainTextSerializer.h.

Definition at line 212 of file nsPlainTextSerializer.h.


The documentation for this class was generated from the following files: