content/base/src/nsXHTMLContentSerializer.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/src/nsXHTMLContentSerializer.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,156 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 + * nsIContentSerializer implementation that can be used with an
    1.11 + * nsIDocumentEncoder to convert an XHTML (not HTML!) DOM to an XHTML
    1.12 + * string that could be parsed into more or less the original DOM.
    1.13 + */
    1.14 +
    1.15 +#ifndef nsXHTMLContentSerializer_h__
    1.16 +#define nsXHTMLContentSerializer_h__
    1.17 +
    1.18 +#include "mozilla/Attributes.h"
    1.19 +#include "nsXMLContentSerializer.h"
    1.20 +#include "nsIEntityConverter.h"
    1.21 +#include "nsString.h"
    1.22 +#include "nsTArray.h"
    1.23 +
    1.24 +class nsIContent;
    1.25 +class nsIAtom;
    1.26 +
    1.27 +class nsXHTMLContentSerializer : public nsXMLContentSerializer {
    1.28 + public:
    1.29 +  nsXHTMLContentSerializer();
    1.30 +  virtual ~nsXHTMLContentSerializer();
    1.31 +
    1.32 +  NS_IMETHOD Init(uint32_t flags, uint32_t aWrapColumn,
    1.33 +                  const char* aCharSet, bool aIsCopying,
    1.34 +                  bool aRewriteEncodingDeclaration) MOZ_OVERRIDE;
    1.35 +
    1.36 +  NS_IMETHOD AppendText(nsIContent* aText,
    1.37 +                        int32_t aStartOffset,
    1.38 +                        int32_t aEndOffset,
    1.39 +                        nsAString& aStr) MOZ_OVERRIDE;
    1.40 +
    1.41 +  NS_IMETHOD AppendDocumentStart(nsIDocument *aDocument,
    1.42 +                                 nsAString& aStr) MOZ_OVERRIDE;
    1.43 +
    1.44 + protected:
    1.45 +
    1.46 +
    1.47 +  virtual bool CheckElementStart(nsIContent * aContent,
    1.48 +                          bool & aForceFormat,
    1.49 +                          nsAString& aStr) MOZ_OVERRIDE;
    1.50 +
    1.51 +  virtual void AppendEndOfElementStart(nsIContent *aOriginalElement,
    1.52 +                               nsIAtom * aName,
    1.53 +                               int32_t aNamespaceID,
    1.54 +                               nsAString& aStr) MOZ_OVERRIDE;
    1.55 +
    1.56 +  virtual void AfterElementStart(nsIContent * aContent,
    1.57 +                         nsIContent *aOriginalElement,
    1.58 +                         nsAString& aStr) MOZ_OVERRIDE;
    1.59 +
    1.60 +  virtual bool CheckElementEnd(nsIContent * aContent,
    1.61 +                          bool & aForceFormat,
    1.62 +                          nsAString& aStr) MOZ_OVERRIDE;
    1.63 +
    1.64 +  virtual void AfterElementEnd(nsIContent * aContent,
    1.65 +                               nsAString& aStr) MOZ_OVERRIDE;
    1.66 +
    1.67 +  virtual bool LineBreakBeforeOpen(int32_t aNamespaceID, nsIAtom* aName) MOZ_OVERRIDE;
    1.68 +  virtual bool LineBreakAfterOpen(int32_t aNamespaceID, nsIAtom* aName) MOZ_OVERRIDE;
    1.69 +  virtual bool LineBreakBeforeClose(int32_t aNamespaceID, nsIAtom* aName) MOZ_OVERRIDE;
    1.70 +  virtual bool LineBreakAfterClose(int32_t aNamespaceID, nsIAtom* aName) MOZ_OVERRIDE;
    1.71 +
    1.72 +  bool HasLongLines(const nsString& text, int32_t& aLastNewlineOffset);
    1.73 +
    1.74 +  // functions to check if we enter in or leave from a preformated content
    1.75 +  virtual void MaybeEnterInPreContent(nsIContent* aNode) MOZ_OVERRIDE;
    1.76 +  virtual void MaybeLeaveFromPreContent(nsIContent* aNode) MOZ_OVERRIDE;
    1.77 +
    1.78 +  virtual void SerializeAttributes(nsIContent* aContent,
    1.79 +                           nsIContent *aOriginalElement,
    1.80 +                           nsAString& aTagPrefix,
    1.81 +                           const nsAString& aTagNamespaceURI,
    1.82 +                           nsIAtom* aTagName,
    1.83 +                           nsAString& aStr,
    1.84 +                           uint32_t aSkipAttr,
    1.85 +                           bool aAddNSAttr) MOZ_OVERRIDE;
    1.86 +
    1.87 +  bool IsFirstChildOfOL(nsIContent* aElement);
    1.88 +
    1.89 +  void SerializeLIValueAttribute(nsIContent* aElement,
    1.90 +                                 nsAString& aStr);
    1.91 +  bool IsShorthandAttr(const nsIAtom* aAttrName,
    1.92 +                         const nsIAtom* aElementName);
    1.93 +  virtual void AppendAndTranslateEntities(const nsAString& aStr,
    1.94 +                                          nsAString& aOutputStr) MOZ_OVERRIDE;
    1.95 +  nsresult EscapeURI(nsIContent* aContent,
    1.96 +                     const nsAString& aURI,
    1.97 +                     nsAString& aEscapedURI);
    1.98 +
    1.99 +  nsCOMPtr<nsIEntityConverter> mEntityConverter;
   1.100 +
   1.101 +  /*
   1.102 +   * isHTMLParser should be set to true by the HTML parser which inherits from
   1.103 +   * this class. It avoids to redefine methods just for few changes.
   1.104 +   */
   1.105 +  bool          mIsHTMLSerializer;
   1.106 +
   1.107 +  bool          mDoHeader;
   1.108 +  bool          mIsCopying; // Set to true only while copying
   1.109 +
   1.110 +  /*
   1.111 +   * mDisableEntityEncoding is higher than 0 while the serializer is serializing
   1.112 +   * the content of a element whose content is considerd CDATA by the
   1.113 +   * serializer (such elements are 'script', 'style', 'noscript' and
   1.114 +   * possibly others in XHTML) This doesn't have anything to do with if the
   1.115 +   * element is defined as CDATA in the DTD, it simply means we'll
   1.116 +   * output the content of the element without doing any entity encoding
   1.117 +   * what so ever.
   1.118 +   */
   1.119 +  int32_t mDisableEntityEncoding;
   1.120 +
   1.121 +  // This is to ensure that we only do meta tag fixups when dealing with
   1.122 +  // whole documents.
   1.123 +  bool          mRewriteEncodingDeclaration;
   1.124 +
   1.125 +  // To keep track of First LI child of OL in selected range 
   1.126 +  bool          mIsFirstChildOfOL;
   1.127 +
   1.128 +  // To keep track of startvalue of OL and first list item for nested lists
   1.129 +  struct olState {
   1.130 +    olState(int32_t aStart, bool aIsFirst)
   1.131 +      : startVal(aStart),
   1.132 +        isFirstListItem(aIsFirst)
   1.133 +    {
   1.134 +    }
   1.135 +
   1.136 +    olState(const olState & aOlState)
   1.137 +    {
   1.138 +      startVal = aOlState.startVal;
   1.139 +      isFirstListItem = aOlState.isFirstListItem;
   1.140 +    }
   1.141 +
   1.142 +    // the value of the start attribute in the OL
   1.143 +    int32_t startVal;
   1.144 +
   1.145 +    // is true only before the serialization of the first li of an ol
   1.146 +    // should be false for other li in the list
   1.147 +    bool isFirstListItem;
   1.148 +  };
   1.149 +
   1.150 +  // Stack to store one olState struct per <OL>.
   1.151 +  nsAutoTArray<olState, 8> mOLStateStack;
   1.152 +
   1.153 +  bool HasNoChildren(nsIContent* aContent);
   1.154 +};
   1.155 +
   1.156 +nsresult
   1.157 +NS_NewXHTMLContentSerializer(nsIContentSerializer** aSerializer);
   1.158 +
   1.159 +#endif

mercurial