Back to index

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

#include <mozSanitizingSerializer.h>

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

List of all members.

Public Member Functions

 mozSanitizingHTMLSerializer ()
virtual ~mozSanitizingHTMLSerializer ()
NS_DECL_ISUPPORTS NS_IMETHOD Init (PRUint32 flags, PRUint32 dummy, 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 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 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 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, const nsAString &allowedTags)

Static Public Member Functions

static PRBool PR_CALLBACK ReleaseProperties (nsHashKey *key, void *data, void *closure)

Protected Member Functions

nsresult ParsePrefs (const nsAString &aPref)
 aPref is a long string, which holds an exhaustive list of allowed tags and attributes.
nsresult ParseTagPref (const nsCAutoString &tagpref)
 Parses e.g.
PRBool IsAllowedTag (nsHTMLTag aTag)
PRBool IsAllowedAttribute (nsHTMLTag aTag, const nsAString &anAttributeName)
nsresult SanitizeAttrValue (nsHTMLTag aTag, const nsAString &attr_name, nsString &value)
 Ensures basic sanity of attribute value.
nsresult SanitizeTextNode (nsString &value)
 Similar to SanitizeAttrValue.
PRBool IsContainer (PRInt32 aId)
 Returns true, if the id represents a container.
nsresult GetParserService (nsIParserService **aParserService)
nsresult DoOpenContainer (PRInt32 aTag)
nsresult DoCloseContainer (PRInt32 aTag)
nsresult DoAddLeaf (PRInt32 aTag, const nsAString &aText)
void Write (const nsAString &aString)

Static Protected Member Functions

static PRInt32 GetIdForContent (nsIContent *aContent)

Protected Attributes

PRInt32 mFlags
nsHashtable mAllowedTags
nsCOMPtr< nsIContentmContent
nsAString * mOutputString
nsIParserNodemParserNode
nsCOMPtr< nsIParserServicemParserService
nsCOMPtr< nsIParsermParser

Detailed Description

Definition at line 54 of file mozSanitizingSerializer.h.


Constructor & Destructor Documentation

Definition at line 91 of file mozSanitizingSerializer.cpp.

  : mAllowedTags(30) // Just some initial buffer size
{
  mOutputString = nsnull;
}

Definition at line 97 of file mozSanitizingSerializer.cpp.

{
#ifdef DEBUG_BenB
  printf("Output:\n%s\n", NS_LossyConvertUCS2toASCII(*mOutputString).get());
#endif
  mAllowedTags.Enumerate(ReleaseProperties);
}

Here is the call graph for this function:


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 105 of file mozSanitizingSerializer.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 383 of file mozSanitizingSerializer.cpp.

{
  return NS_OK;
}

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

Implements nsIHTMLContentSink.

Definition at line 313 of file mozSanitizingSerializer.cpp.

{
  nsresult rv = NS_OK;
  eHTMLTags type = (eHTMLTags)aNode.GetNodeType();
  if (eHTMLTag_whitespace == type || 
      eHTMLTag_newline == type    ||
      eHTMLTag_text == type       ||
      eHTMLTag_entity == type) {
    rv = AddLeaf(aNode);
  }
  else if (eHTMLTag_title == type) {
    NS_ASSERTION(mParser, "Only CNavDTD treats title this way.");

    nsString skippedContent;
    PRInt32 lineNo;

    nsCOMPtr<nsIDTD> dtd;
    mParser->GetDTD(getter_AddRefs(dtd));
    NS_ENSURE_TRUE(dtd, NS_ERROR_UNEXPECTED);

    dtd->CollectSkippedContent(type, skippedContent, lineNo);
    SetTitle(skippedContent);
  }
  else {
    rv = OpenContainer(aNode);
    NS_ENSURE_SUCCESS(rv, rv);
    rv = CloseContainer(type);
  }
  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 345 of file mozSanitizingSerializer.cpp.

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

  mParserNode = NS_CONST_CAST(nsIParserNode*, &aNode);
  return DoAddLeaf(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 106 of file mozSanitizingSerializer.h.

                                                    { return NS_OK; }
NS_IMETHOD mozSanitizingHTMLSerializer::AppendCDATASection ( nsIDOMCDATASection aCDATASection,
PRInt32  aStartOffset,
PRInt32  aEndOffset,
nsAString &  aStr 
) [inline, virtual]

Implements nsIContentSerializer.

Definition at line 72 of file mozSanitizingSerializer.h.

                      { return NS_OK; }
NS_IMETHOD mozSanitizingHTMLSerializer::AppendComment ( nsIDOMComment aComment,
PRInt32  aStartOffset,
PRInt32  aEndOffset,
nsAString &  aStr 
) [inline, virtual]

Implements nsIContentSerializer.

Definition at line 81 of file mozSanitizingSerializer.h.

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

Implements nsIContentSerializer.

Definition at line 84 of file mozSanitizingSerializer.h.

                      { return NS_OK; }
NS_IMETHODIMP mozSanitizingHTMLSerializer::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 164 of file mozSanitizingSerializer.cpp.

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

Implements nsIContentSerializer.

Definition at line 270 of file mozSanitizingSerializer.cpp.

{
  NS_ENSURE_ARG(aElement);

  mContent = do_QueryInterface(aElement);
  NS_ENSURE_TRUE(mContent, NS_ERROR_FAILURE);

  mOutputString = &aStr;

  nsresult rv = NS_OK;
  PRInt32 id = GetIdForContent(mContent);

  PRBool isContainer = IsContainer(id);

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

  mContent = 0;
  mOutputString = nsnull;

  return rv;
}

Here is the call graph for this function:

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

Implements nsIContentSerializer.

Definition at line 240 of file mozSanitizingSerializer.cpp.

{
  NS_ENSURE_ARG(aElement);

  mContent = do_QueryInterface(aElement);
  NS_ENSURE_TRUE(mContent, NS_ERROR_FAILURE);

  mOutputString = &aStr;

  PRInt32 id = GetIdForContent(mContent);

  PRBool isContainer = IsContainer(id);

  nsresult rv;
  if (isContainer) {
    rv = DoOpenContainer(id);
  }
  else {
    rv = DoAddLeaf(id, EmptyString());
  }

  mContent = 0;
  mOutputString = nsnull;

  return rv;
} 

Here is the call graph for this function:

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

Implements nsIContentSerializer.

Definition at line 76 of file mozSanitizingSerializer.h.

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

Implements nsIContentSerializer.

Definition at line 224 of file mozSanitizingSerializer.cpp.

{
  nsresult rv = NS_OK;

  mOutputString = &aStr;

  nsAutoString linebuffer;
  rv = DoAddLeaf(eHTMLTag_text, linebuffer);

  return rv;
}

Here is the call graph for this function:

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 130 of file mozSanitizingSerializer.h.

{ return NS_OK; }

This method is used to close the main BODY container.

Implements nsIHTMLContentSink.

Definition at line 419 of file mozSanitizingSerializer.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 305 of file mozSanitizingSerializer.cpp.

{
  // XXX Why do we need this?
  // mParserNode = NS_CONST_CAST(nsIParserNode*, &aNode);
  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 431 of file mozSanitizingSerializer.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 455 of file mozSanitizingSerializer.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 407 of file mozSanitizingSerializer.cpp.

{
  return CloseContainer(eHTMLTag_head);
}

Here is the call graph for this function:

This method is used to close the outer HTML container.

Implements nsIHTMLContentSink.

Definition at line 361 of file mozSanitizingSerializer.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 443 of file mozSanitizingSerializer.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 96 of file mozSanitizingSerializer.h.

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 135 of file mozSanitizingSerializer.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 133 of file mozSanitizingSerializer.h.

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

Definition at line 534 of file mozSanitizingSerializer.cpp.

{
  eHTMLTags type = (eHTMLTags)aTag;

  nsresult rv = NS_OK;

  if (type == eHTMLTag_whitespace ||
      type == eHTMLTag_newline)
  {
    Write(aText); // sure to be safe?
  }
  else if (type == eHTMLTag_text)
  {
    nsAutoString text(aText);
    if(NS_SUCCEEDED(SanitizeTextNode(text)))
      Write(text);
    else
      Write(NS_LITERAL_STRING(TEXT_REMOVED)); // Does not happen (yet)
    NS_ENSURE_SUCCESS(rv, rv);
  }
  else if (type == eHTMLTag_entity)
  {
    Write(NS_LITERAL_STRING("&"));
    Write(aText); // sure to be safe?
    // using + operator here might give an infinitive loop, see above.
    // not adding ";", because Gecko delivers that as part of |aText| (freaky)
  }
  else if (type == eHTMLTag_script ||
           type == eHTMLTag_style ||
           type == eHTMLTag_server)
  {
    // These special tags require some extra care. The parser gives them
    // to us as leaves, but they're really containers. Their content is
    // contained in the "skipped content" of the parser. This code is
    // adapted from nsHTMLContentSink.cpp
    nsString skippedContent;
    PRInt32 lineNo;

    NS_ASSERTION(mParser, "We are receiving containers as leaves with "
                          "no skipped content.");

    nsCOMPtr<nsIDTD> dtd;
    mParser->GetDTD(getter_AddRefs(dtd));
    NS_ENSURE_TRUE(dtd, NS_ERROR_UNEXPECTED);

    // Note: we want to collect the skipped content no matter what. We
    // may end up throwing it away anyway, but the DTD doesn't care
    // about that.
    dtd->CollectSkippedContent(type, skippedContent, lineNo);

    DoOpenContainer(type);
    if (IsAllowedTag(type))
    {
      Write(skippedContent);
    }
    DoCloseContainer(type);
  }
  else
  {
    DoOpenContainer(type);
  }

  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 512 of file mozSanitizingSerializer.cpp.

{
  eHTMLTags type = (eHTMLTags)aTag;

  if (IsAllowedTag(type)) {
    nsIParserService* parserService =
      nsContentUtils::GetParserServiceWeakRef();
    if (!parserService)
      return NS_ERROR_OUT_OF_MEMORY;
    const PRUnichar* tag_name = parserService->HTMLIdToStringTag(aTag);
    NS_ENSURE_TRUE(tag_name, NS_ERROR_INVALID_POINTER);

    Write(NS_LITERAL_STRING("</") + nsDependentString(tag_name)
          + NS_LITERAL_STRING(">"));
  }
  else
    Write(NS_LITERAL_STRING(" "));

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 464 of file mozSanitizingSerializer.cpp.

{
  eHTMLTags type = (eHTMLTags)aTag;

  if (IsAllowedTag(type))
  {
    nsIParserService* parserService =
      nsContentUtils::GetParserServiceWeakRef();
    if (!parserService)
      return NS_ERROR_OUT_OF_MEMORY;
    const PRUnichar* tag_name = parserService->HTMLIdToStringTag(aTag);
    NS_ENSURE_TRUE(tag_name, NS_ERROR_INVALID_POINTER);

    Write(NS_LITERAL_STRING("<") + nsDependentString(tag_name));

    // Attributes
    if (mParserNode)
    {
      PRInt32 count = mParserNode->GetAttributeCount();
      for (PRInt32 i = 0; i < count; i++)
      {
        const nsAString& key = mParserNode->GetKeyAt(i);
        if(IsAllowedAttribute(type, key))
        {
          // Ensure basic sanity of value
          nsAutoString value(mParserNode->GetValueAt(i));
                    // SanitizeAttrValue() modifies |value|
          if (NS_SUCCEEDED(SanitizeAttrValue(type, key, value)))
          {
            // Write out
            Write(NS_LITERAL_STRING(" "));
            Write(key); // I get an infinive loop with | + key + | !!!
            Write(NS_LITERAL_STRING("=\"") + value + NS_LITERAL_STRING("\""));
          }
        }
      }
    }

    Write(NS_LITERAL_STRING(">"));
  }
  else
    Write(NS_LITERAL_STRING(" "));

  return NS_OK;

}

Here is the call graph for this function:

Here is the caller graph for this function:

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 131 of file mozSanitizingSerializer.h.

{ return NS_OK; }
NS_IMETHODIMP mozSanitizingHTMLSerializer::Flush ( nsAString &  aStr) [virtual]

Implements nsIContentSerializer.

Definition at line 154 of file mozSanitizingSerializer.cpp.

{
#ifdef DEBUG_BenB
  printf("Flush: -%s-", NS_LossyConvertUCS2toASCII(aStr).get());
#endif
  Write(aStr);
  return NS_OK;
}

Here is the call 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 109 of file mozSanitizingSerializer.h.

{ }

Definition at line 211 of file mozSanitizingSerializer.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:

virtual nsISupports* mozSanitizingHTMLSerializer::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 111 of file mozSanitizingSerializer.h.

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

Implements nsIContentSerializer.

Here is the caller graph for this function:

NS_IMETHODIMP mozSanitizingHTMLSerializer::Initialize ( nsAString *  aOutString,
PRUint32  aFlags,
const nsAString &  allowedTags 
) [virtual]

Implements mozISanitizingHTMLSerializer.

Definition at line 134 of file mozSanitizingSerializer.cpp.

{
  nsresult rv = Init(aFlags, 0, 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.
  // (Comment copied from nsPlaintextSerializer)
  mOutputString = aOutString;

  ParsePrefs(allowedTags);

  return NS_OK;
}

Here is the call graph for this function:

PRBool mozSanitizingHTMLSerializer::IsAllowedAttribute ( nsHTMLTag  aTag,
const nsAString &  anAttributeName 
) [protected]

Definition at line 689 of file mozSanitizingSerializer.cpp.

{
#ifdef DEBUG_BenB
  printf("IsAllowedAttribute %d, -%s-\n",
         aTag,
         NS_LossyConvertUCS2toASCII(anAttributeName).get());
#endif
  nsresult rv;

  nsPRUint32Key tag_key(aTag);
  nsIProperties* attr_bag = (nsIProperties*)mAllowedTags.Get(&tag_key);
  NS_ENSURE_TRUE(attr_bag, PR_FALSE);

  PRBool allowed;
  nsAutoString attr(anAttributeName);
  ToLowerCase(attr);
  rv = attr_bag->Has(NS_LossyConvertUCS2toASCII(attr).get(),
                     &allowed);
  if (NS_FAILED(rv))
    return PR_FALSE;

#ifdef DEBUG_BenB
  printf(" Allowed: %s\n", allowed?"yes":"no");
#endif
  return allowed;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 673 of file mozSanitizingSerializer.cpp.

{

  nsPRUint32Key tag_key(aTag);
#ifdef DEBUG_BenB
  printf("IsAllowedTag %d: %s\n",
         aTag,
         mAllowedTags.Exists(&tag_key)?"yes":"no");
#endif
  return mAllowedTags.Exists(&tag_key);
}

Here is the caller graph for this function:

Returns true, if the id represents a container.

Definition at line 189 of file mozSanitizingSerializer.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:

01/09/2003 harishd

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

Implements nsIHTMLContentSink.

Definition at line 178 of file mozSanitizingSerializer.cpp.

{
  *aReturn = PR_FALSE;
  return NS_OK;
}

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

Parameters:
aErrorResultthe error code

Implements nsIHTMLContentSink.

Definition at line 128 of file mozSanitizingSerializer.h.

{ return NS_OK; }

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 129 of file mozSanitizingSerializer.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 413 of file mozSanitizingSerializer.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 296 of file mozSanitizingSerializer.cpp.

{
  PRInt32 type = aNode.GetNodeType();

  mParserNode = NS_CONST_CAST(nsIParserNode *, &aNode);
  return DoOpenContainer(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 425 of file mozSanitizingSerializer.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 449 of file mozSanitizingSerializer.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 401 of file mozSanitizingSerializer.cpp.

{
  return OpenContainer(aNode);
}

Here is the call 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 355 of file mozSanitizingSerializer.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 437 of file mozSanitizingSerializer.cpp.

{
  return OpenContainer(aNode);
}

Here is the call graph for this function:

nsresult mozSanitizingHTMLSerializer::ParsePrefs ( const nsAString &  aPref) [protected]

aPref is a long string, which holds an exhaustive list of allowed tags and attributes.

All other tags and attributes will be removed.

aPref has the format "html head body ul ol li a(href,name,title) img(src,alt,title) #text" i.e.

  • tags are separated by whitespace
  • the attribute list follows the tag directly in brackets
  • the attributes are separated by commas.

There is no way to express further restrictions, like "no text inside the <head> element". This is so to considerably reduce the complexity of the pref and this implementation.

Update: Akk told me that I might be able to use DTD classes. Later(TM)...

Definition at line 736 of file mozSanitizingSerializer.cpp.

{
  char* pref = ToNewCString(aPref);
  char* tags_lasts;
  for (char* iTag = PL_strtok_r(pref, " ", &tags_lasts);
       iTag;
       iTag = PL_strtok_r(NULL, " ", &tags_lasts))
  {
    ParseTagPref(nsCAutoString(iTag));
  }
  delete[] pref;

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Parses e.g.

"a(href,title)" (but not several tags at once).

Definition at line 756 of file mozSanitizingSerializer.cpp.

{
  nsIParserService* parserService = nsContentUtils::GetParserServiceWeakRef();
  if (!parserService)
    return NS_ERROR_OUT_OF_MEMORY;

  // Parsing tag
  PRInt32 bracket = tagpref.FindChar('(');
  if (bracket == 0)
  {
    printf(" malformed pref: %s\n", tagpref.get());
    return NS_ERROR_CANNOT_CONVERT_DATA;
  }

  nsAutoString tag;
  CopyUTF8toUTF16(StringHead(tagpref, bracket), tag);

  // Create key
  PRInt32 tag_id = parserService->HTMLStringTagToId(tag);
  if (tag_id == eHTMLTag_userdefined)
  {
    printf(" unknown tag <%s>, won't add.\n",
           NS_ConvertUTF16toUTF8(tag).get());
    return NS_ERROR_CANNOT_CONVERT_DATA;
  }
  nsPRUint32Key tag_key(tag_id);

  if (mAllowedTags.Exists(&tag_key))
  {
    printf(" duplicate tag: %s\n", NS_ConvertUTF16toUTF8(tag).get());
    return NS_ERROR_CANNOT_CONVERT_DATA;
  }
  if (bracket == kNotFound)
    /* There are no attributes in the pref. So, allow none; only the tag
       itself */
  {
    mAllowedTags.Put(&tag_key, 0);
  }
  else
  {
    // Attributes

    // where is the macro for non-fatal errors in opt builds?
    if(tagpref[tagpref.Length() - 1] != ')' ||
       tagpref.Length() < PRUint32(bracket) + 3)
    {
      printf(" malformed pref: %s\n", tagpref.get());
      return NS_ERROR_CANNOT_CONVERT_DATA;
    }
    nsCOMPtr<nsIProperties> attr_bag =
                                 do_CreateInstance(NS_PROPERTIES_CONTRACTID);
    NS_ENSURE_TRUE(attr_bag, NS_ERROR_INVALID_POINTER);
    nsCAutoString attrList;
    attrList.Append(Substring(tagpref,
                              bracket + 1,
                              tagpref.Length() - 2 - bracket));
    char* attrs_lasts;
    for (char* iAttr = PL_strtok_r(attrList.BeginWriting(),
                                   ",", &attrs_lasts);
         iAttr;
         iAttr = PL_strtok_r(NULL, ",", &attrs_lasts))
    {
      attr_bag->Set(iAttr, 0);
    }

    nsIProperties* attr_bag_raw = attr_bag;
    NS_ADDREF(attr_bag_raw);
    mAllowedTags.Put(&tag_key, attr_bag_raw);
  }

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PRBool PR_CALLBACK mozSanitizingHTMLSerializer::ReleaseProperties ( nsHashKey *  key,
void data,
void closure 
) [static]

Definition at line 107 of file mozSanitizingSerializer.cpp.

{
  nsIProperties* prop = (nsIProperties*)data;
  NS_IF_RELEASE(prop);
  return PR_TRUE;
}

Here is the caller graph for this function:

nsresult mozSanitizingHTMLSerializer::SanitizeAttrValue ( nsHTMLTag  aTag,
const nsAString &  anAttrName,
nsString aValue 
) [protected]

Ensures basic sanity of attribute value.

This function also (tries to :-( ) makes sure, that no unwanted / dangerous URLs appear in the document (like javascript: and data:).

Pass the value as |aValue| arg. It will be modified in-place.

If the value is not allowed at all, we return with NS_ERROR_ILLEGAL_VALUE. In that case, do not use the |aValue|, but output nothing.

Definition at line 623 of file mozSanitizingSerializer.cpp.

{
  /* First, cut the attribute to 1000 chars.
     Attributes with values longer than 1000 chars seem bogus,
     considering that we don't support any JS. The longest attributes
     I can think of are URLs, and URLs with 1000 chars are likely to be
     bogus, too. */
  aValue = Substring(aValue, 0, 1000);
  //aValue.Truncate(1000); //-- this cuts half of the document !!?!!

  aValue.Adopt(escape(aValue));

  /* Check some known bad stuff. Add more!
     I don't care too much, if it happens to trigger in some innocent cases
     (like <img alt="Statistical data: Mortage rates and newspapers">) -
     security first. */
  if (aValue.Find("javascript:") != kNotFound ||
      aValue.Find("data:") != kNotFound ||
      aValue.Find("base64") != kNotFound)
    return NS_ERROR_ILLEGAL_VALUE;

  // Check img src scheme
  if (aTag == eHTMLTag_img && 
      anAttrName.LowerCaseEqualsLiteral("src"))
  {
    nsresult rv;
    nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
    NS_ENSURE_SUCCESS(rv, rv);
    nsCAutoString scheme;
    rv = ioService->ExtractScheme(NS_LossyConvertUCS2toASCII(aValue), scheme);
    NS_ENSURE_SUCCESS(rv, rv);

    if (!scheme.Equals("cid", nsCaseInsensitiveCStringComparator()))
      return NS_ERROR_ILLEGAL_VALUE;
  }

#ifdef DEBUG_BenB
  printf("attribute value for %s: -%s-\n",
         NS_LossyConvertUCS2toASCII(anAttrName).get(),
         NS_LossyConvertUCS2toASCII(aValue).get());
#endif

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Similar to SanitizeAttrValue.

Definition at line 605 of file mozSanitizingSerializer.cpp.

{
  aText.Adopt(escape(aText));
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

NS_IMETHODIMP mozSanitizingHTMLSerializer::SetDocumentCharset ( nsACString &  aCharset) [virtual]

Set the document character set.

This should be passed on to the document itself.

Implements nsIContentSink.

Definition at line 389 of file mozSanitizingSerializer.cpp.

{
  // No idea, if this works - it isn't invoked by |TestOutput|.
  Write(NS_LITERAL_STRING("\n<meta http-equiv=\"Context-Type\" content=\"text/html; charset=")
        /* Danger: breaking the line within the string literal, like
           "foo"\n"bar", breaks win32! */
        + nsAdoptingString(escape(NS_ConvertASCIItoUCS2(aCharset)))
        + NS_LITERAL_STRING("\">\n"));
  return NS_OK;
}

Here is the call graph for this function:

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 100 of file mozSanitizingSerializer.h.

{ mParser = aParser; return NS_OK; }

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 367 of file mozSanitizingSerializer.cpp.

{
  if (IsAllowedTag(eHTMLTag_title))
  {
    // See bug 195020 for a good reason to output the tags.
    // It will make sure we have a closing tag, and a
    // missing </title> tag won't result in everything
    // being eaten up as the title.
    Write(NS_LITERAL_STRING("<title>"));
    Write(nsAdoptingString(escape(aValue)));
    Write(NS_LITERAL_STRING("</title>"));
  }
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

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 95 of file mozSanitizingSerializer.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 98 of file mozSanitizingSerializer.h.

{ return NS_OK; }

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

Implements nsIHTMLContentSink.

Definition at line 134 of file mozSanitizingSerializer.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 132 of file mozSanitizingSerializer.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 99 of file mozSanitizingSerializer.h.

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

Definition at line 171 of file mozSanitizingSerializer.cpp.

{
  mOutputString->Append(aString);
}

Here is the caller graph for this function:


Member Data Documentation

Definition at line 159 of file mozSanitizingSerializer.h.

Definition at line 161 of file mozSanitizingSerializer.h.

Definition at line 158 of file mozSanitizingSerializer.h.

Definition at line 162 of file mozSanitizingSerializer.h.

Definition at line 165 of file mozSanitizingSerializer.h.

Definition at line 163 of file mozSanitizingSerializer.h.

Definition at line 164 of file mozSanitizingSerializer.h.


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