tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nsXHTMLContentSerializer.h (5297B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 /*
      8 * nsIContentSerializer implementation that can be used with an
      9 * nsIDocumentEncoder to convert an XHTML (not HTML!) DOM to an XHTML
     10 * string that could be parsed into more or less the original DOM.
     11 */
     12 
     13 #ifndef nsXHTMLContentSerializer_h__
     14 #define nsXHTMLContentSerializer_h__
     15 
     16 #include "nsString.h"
     17 #include "nsTArray.h"
     18 #include "nsXMLContentSerializer.h"
     19 
     20 class nsIContent;
     21 class nsAtom;
     22 
     23 namespace mozilla {
     24 class Encoding;
     25 }
     26 
     27 class nsXHTMLContentSerializer : public nsXMLContentSerializer {
     28 public:
     29  nsXHTMLContentSerializer();
     30  virtual ~nsXHTMLContentSerializer();
     31 
     32  NS_IMETHOD Init(uint32_t flags, uint32_t aWrapColumn,
     33                  const mozilla::Encoding* aEncoding, bool aIsCopying,
     34                  bool aRewriteEncodingDeclaration,
     35                  bool* aNeedsPreformatScanning, nsAString& aOutput) override;
     36 
     37  NS_IMETHOD AppendText(mozilla::dom::Text* aText, int32_t aStartOffset,
     38                        int32_t aEndOffset) override;
     39 
     40  NS_IMETHOD AppendDocumentStart(mozilla::dom::Document* aDocument) override;
     41 
     42 protected:
     43  virtual bool CheckElementStart(mozilla::dom::Element* aElement,
     44                                 bool& aForceFormat, nsAString& aStr,
     45                                 nsresult& aResult) override;
     46 
     47  [[nodiscard]] virtual bool AfterElementStart(nsIContent* aContent,
     48                                               nsIContent* aOriginalElement,
     49                                               nsAString& aStr) override;
     50 
     51  virtual bool CheckElementEnd(mozilla::dom::Element* aContent,
     52                               mozilla::dom::Element* aOriginalElement,
     53                               bool& aForceFormat, nsAString& aStr) override;
     54 
     55  virtual void AfterElementEnd(nsIContent* aContent, nsAString& aStr) override;
     56 
     57  virtual bool LineBreakBeforeOpen(int32_t aNamespaceID,
     58                                   nsAtom* aName) override;
     59  virtual bool LineBreakAfterOpen(int32_t aNamespaceID, nsAtom* aName) override;
     60  virtual bool LineBreakBeforeClose(int32_t aNamespaceID,
     61                                    nsAtom* aName) override;
     62  virtual bool LineBreakAfterClose(int32_t aNamespaceID,
     63                                   nsAtom* aName) override;
     64 
     65  bool HasLongLines(const nsString& text, int32_t& aLastNewlineOffset);
     66 
     67  // functions to check if we enter in or leave from a preformated content
     68  virtual void MaybeEnterInPreContent(nsIContent* aNode) override;
     69  virtual void MaybeLeaveFromPreContent(nsIContent* aNode) override;
     70 
     71  [[nodiscard]] virtual bool SerializeAttributes(
     72      mozilla::dom::Element* aContent, mozilla::dom::Element* aOriginalElement,
     73      nsAString& aTagPrefix, const nsAString& aTagNamespaceURI,
     74      nsAtom* aTagName, nsAString& aStr, uint32_t aSkipAttr,
     75      bool aAddNSAttr) override;
     76 
     77  bool IsFirstChildOfOL(nsIContent* aElement);
     78 
     79  [[nodiscard]] bool SerializeLIValueAttribute(nsIContent* aElement,
     80                                               nsAString& aStr);
     81  bool IsShorthandAttr(const nsAtom* aAttrName, const nsAtom* aElementName);
     82 
     83  [[nodiscard]] virtual bool AppendAndTranslateEntities(
     84      const nsAString& aStr, nsAString& aOutputStr) override;
     85 
     86 private:
     87  bool IsElementPreformatted(nsIContent* aNode);
     88 
     89 protected:
     90  /*
     91   * isHTMLParser should be set to true by the HTML parser which inherits from
     92   * this class. It avoids to redefine methods just for few changes.
     93   */
     94  bool mIsHTMLSerializer;
     95 
     96  bool mIsCopying;  // Set to true only while copying
     97 
     98  /*
     99   * mDisableEntityEncoding is higher than 0 while the serializer is serializing
    100   * the content of a element whose content is considerd CDATA by the
    101   * serializer (such elements are 'script', 'style', 'noscript' and
    102   * possibly others in XHTML) This doesn't have anything to do with if the
    103   * element is defined as CDATA in the DTD, it simply means we'll
    104   * output the content of the element without doing any entity encoding
    105   * what so ever.
    106   */
    107  int32_t mDisableEntityEncoding;
    108 
    109  // This is to ensure that we only do meta tag fixups when dealing with
    110  // whole documents.
    111  bool mRewriteEncodingDeclaration;
    112 
    113  // To keep track of First LI child of OL in selected range
    114  bool mIsFirstChildOfOL;
    115 
    116  // To keep track of startvalue of OL and first list item for nested lists
    117  struct olState {
    118    olState(int32_t aStart, bool aIsFirst)
    119        : startVal(aStart), isFirstListItem(aIsFirst) {}
    120 
    121    olState(const olState& aOlState) {
    122      startVal = aOlState.startVal;
    123      isFirstListItem = aOlState.isFirstListItem;
    124    }
    125 
    126    // the value of the start attribute in the OL
    127    int32_t startVal;
    128 
    129    // is true only before the serialization of the first li of an ol
    130    // should be false for other li in the list
    131    bool isFirstListItem;
    132  };
    133 
    134  // Stack to store one olState struct per <OL>.
    135  AutoTArray<olState, 8> mOLStateStack;
    136 
    137  bool HasNoChildren(nsIContent* aContent);
    138 };
    139 
    140 nsresult NS_NewXHTMLContentSerializer(nsIContentSerializer** aSerializer);
    141 
    142 #endif