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