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