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 "mozilla/Attributes.h" 17 #include "nsXMLContentSerializer.h" 18 #include "nsIEntityConverter.h" 19 #include "nsString.h" 20 #include "nsTArray.h" 21 22 class nsIContent; 23 class nsIAtom; 24 25 class nsXHTMLContentSerializer : public nsXMLContentSerializer { 26 public: 27 nsXHTMLContentSerializer(); 28 virtual ~nsXHTMLContentSerializer(); 29 30 NS_IMETHOD Init(uint32_t flags, uint32_t aWrapColumn, 31 const char* aCharSet, bool aIsCopying, 32 bool aRewriteEncodingDeclaration) override; 33 34 NS_IMETHOD AppendText(nsIContent* aText, 35 int32_t aStartOffset, 36 int32_t aEndOffset, 37 nsAString& aStr) override; 38 39 NS_IMETHOD AppendDocumentStart(nsIDocument *aDocument, 40 nsAString& aStr) override; 41 42 protected: 43 44 45 virtual bool CheckElementStart(nsIContent * aContent, 46 bool & aForceFormat, 47 nsAString& aStr, 48 nsresult& aResult) override; 49 50 MOZ_MUST_USE 51 virtual bool AfterElementStart(nsIContent* aContent, 52 nsIContent* aOriginalElement, 53 nsAString& aStr) override; 54 55 virtual bool CheckElementEnd(mozilla::dom::Element* aContent, 56 bool& aForceFormat, 57 nsAString& aStr) override; 58 59 virtual void AfterElementEnd(nsIContent * aContent, 60 nsAString& aStr) override; 61 62 virtual bool LineBreakBeforeOpen(int32_t aNamespaceID, nsIAtom* aName) override; 63 virtual bool LineBreakAfterOpen(int32_t aNamespaceID, nsIAtom* aName) override; 64 virtual bool LineBreakBeforeClose(int32_t aNamespaceID, nsIAtom* aName) override; 65 virtual bool LineBreakAfterClose(int32_t aNamespaceID, nsIAtom* aName) override; 66 67 bool HasLongLines(const nsString& text, int32_t& aLastNewlineOffset); 68 69 // functions to check if we enter in or leave from a preformated content 70 virtual void MaybeEnterInPreContent(nsIContent* aNode) override; 71 virtual void MaybeLeaveFromPreContent(nsIContent* aNode) override; 72 73 MOZ_MUST_USE 74 virtual bool SerializeAttributes(nsIContent* aContent, 75 nsIContent *aOriginalElement, 76 nsAString& aTagPrefix, 77 const nsAString& aTagNamespaceURI, 78 nsIAtom* aTagName, 79 nsAString& aStr, 80 uint32_t aSkipAttr, 81 bool aAddNSAttr) override; 82 83 bool IsFirstChildOfOL(nsIContent* aElement); 84 85 MOZ_MUST_USE 86 bool SerializeLIValueAttribute(nsIContent* aElement, 87 nsAString& aStr); 88 bool IsShorthandAttr(const nsIAtom* aAttrName, 89 const nsIAtom* aElementName); 90 91 MOZ_MUST_USE 92 virtual bool AppendAndTranslateEntities(const nsAString& aStr, 93 nsAString& aOutputStr) override; 94 95 nsresult EscapeURI(nsIContent* aContent, 96 const nsAString& aURI, 97 nsAString& aEscapedURI); 98 99 private: 100 bool IsElementPreformatted(nsIContent* aNode); 101 102 protected: 103 nsCOMPtr<nsIEntityConverter> mEntityConverter; 104 105 /* 106 * isHTMLParser should be set to true by the HTML parser which inherits from 107 * this class. It avoids to redefine methods just for few changes. 108 */ 109 bool mIsHTMLSerializer; 110 111 bool mDoHeader; 112 bool mIsCopying; // Set to true only while copying 113 114 /* 115 * mDisableEntityEncoding is higher than 0 while the serializer is serializing 116 * the content of a element whose content is considerd CDATA by the 117 * serializer (such elements are 'script', 'style', 'noscript' and 118 * possibly others in XHTML) This doesn't have anything to do with if the 119 * element is defined as CDATA in the DTD, it simply means we'll 120 * output the content of the element without doing any entity encoding 121 * what so ever. 122 */ 123 int32_t mDisableEntityEncoding; 124 125 // This is to ensure that we only do meta tag fixups when dealing with 126 // whole documents. 127 bool mRewriteEncodingDeclaration; 128 129 // To keep track of First LI child of OL in selected range 130 bool mIsFirstChildOfOL; 131 132 // To keep track of startvalue of OL and first list item for nested lists 133 struct olState { olStateolState134 olState(int32_t aStart, bool aIsFirst) 135 : startVal(aStart), 136 isFirstListItem(aIsFirst) 137 { 138 } 139 olStateolState140 olState(const olState & aOlState) 141 { 142 startVal = aOlState.startVal; 143 isFirstListItem = aOlState.isFirstListItem; 144 } 145 146 // the value of the start attribute in the OL 147 int32_t startVal; 148 149 // is true only before the serialization of the first li of an ol 150 // should be false for other li in the list 151 bool isFirstListItem; 152 }; 153 154 // Stack to store one olState struct per <OL>. 155 AutoTArray<olState, 8> mOLStateStack; 156 157 bool HasNoChildren(nsIContent* aContent); 158 }; 159 160 nsresult 161 NS_NewXHTMLContentSerializer(nsIContentSerializer** aSerializer); 162 163 #endif 164